/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.design.pages;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import net.java.dev.genesis.annotation.ViewHandler;
import net.java.dev.genesis.ui.swt.SWTBinder;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import de.innovationgate.eclipse.utils.ui.GenesisBoundFormPage;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelCellModifier;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelContentProvider;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelLabelProvider;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelValueMapper;
import de.innovationgate.wga.common.beans.csconfig.v2.Shortcut;
import de.innovationgate.wga.model.BeanListTableModelListener;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.ShortcutType;
import de.innovationgate.wga.model.ShortcutsModel;
import de.innovationgate.wga.model.WGADesignConfigurationModel;
@ViewHandler
public class ShortcutsConfigurationPage extends GenesisBoundFormPage {
public static final String ID = "de.innovationgate.eclipse.wgadesigner.pages.ShortcutsConfigurationPage";
public static final String PAGE_TITLE = "Design Shortcuts";
private WGADesignConfigurationModel _model;
private Table _tblShortcuts;
private TableColumn _nameColumn;
private TableColumn _typeColumn;
private ShortcutsModel _shortcutsModel;
private TableViewer _tblViewerShortcuts;
private Button _btnAddShortcut;
private TableColumn _referenceColumn;
private Button _btnRemoveShortcut;
private HashMap<Integer, ShortcutType> _shortcutTypesByIndex;
public ShortcutsConfigurationPage(FormEditor editor) {
super(editor, ID, PAGE_TITLE);
}
public void setModel(Model model) {
_model = (WGADesignConfigurationModel) model;
}
public Model getModel() {
return _model;
}
@Override
protected void createFormContent(IManagedForm managedForm) {
ScrolledForm form = managedForm.getForm();
FormToolkit toolkit = managedForm.getToolkit();
toolkit.decorateFormHeading(form.getForm());
form.setText(PAGE_TITLE);
ColumnLayout layout = new ColumnLayout();
layout.maxNumColumns = 2;
form.getBody().setLayout(layout);
// encoder section
Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Design Shortcuts");
Composite sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
GridLayout sectionLayout = new GridLayout();
GridData fillHSpan = new GridData(GridData.FILL_HORIZONTAL);
fillHSpan.horizontalSpan = 4;
GridData prefSize = new GridData();
prefSize.widthHint = 50;
sectionLayout.numColumns = 2;
sectionClient.setLayout(sectionLayout);
// create Table
_tblShortcuts = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblShortcuts.setHeaderVisible(true);
GridData tblLayoutData = new GridData(GridData.FILL_BOTH);
tblLayoutData.verticalSpan = 2;
tblLayoutData.minimumHeight = 200;
_tblShortcuts.setLayoutData(tblLayoutData);
registerField("shortcuts", _tblShortcuts);
int tableWidth = 700;
_nameColumn = new TableColumn(_tblShortcuts, SWT.NONE);
_nameColumn.setText("Shortcut");
_nameColumn.setWidth((int)(tableWidth * 0.3));
_typeColumn = new TableColumn(_tblShortcuts, SWT.NONE);
_typeColumn.setText("Type");
_typeColumn.setWidth((int)(tableWidth * 0.3));
_referenceColumn = new TableColumn(_tblShortcuts, SWT.NONE);
_referenceColumn.setText("Reference");
_referenceColumn.setWidth((int)(tableWidth * 0.3));
_shortcutsModel = new ShortcutsModel(_model.getShortcuts());
_shortcutsModel.addListener(new BeanListTableModelListener() {
public void add(Object bean) {
_model.fireModelChanged();
}
public void remove(Object bean) {
_model.fireModelChanged();
}
public void update(Object bean) {
_model.fireModelChanged();
}
@SuppressWarnings("unchecked")
public void refresh(List beans) {
_model.fireModelChanged();
}
});
_tblViewerShortcuts = new TableViewer(_tblShortcuts);
List<String> shortcutTypes = new ArrayList<String>();
Iterator<ShortcutType> it = WGADesignConfigurationModel.SHORTCUTTYPES.values().iterator();
_shortcutTypesByIndex = new HashMap<Integer, ShortcutType>();
int i = 0;
while (it.hasNext()) {
ShortcutType type = it.next();
shortcutTypes.add(type.getValue());
_shortcutTypesByIndex.put(i, type);
i++;
}
CellEditor[] editors = new CellEditor[5];
editors[0] = new TextCellEditor(_tblShortcuts);
editors[1] = new ComboBoxCellEditor(_tblShortcuts, shortcutTypes.toArray(new String[0]), SWT.READ_ONLY);
editors[2] = new TextCellEditor(_tblShortcuts);
editors[3] = new TextCellEditor(_tblShortcuts);
editors[4] = new TextCellEditor(_tblShortcuts);
_tblViewerShortcuts.setCellEditors(editors);
BeanListTableModelCellModifier modifier = new BeanListTableModelCellModifier(_tblViewerShortcuts, _shortcutsModel);
modifier.setEditMode(1, BeanListTableModelCellModifier.EDIT_MODE_ON_SINGLE_CLICK);
_tblViewerShortcuts.setCellModifier(modifier);
modifier.setValueMapper(1, new BeanListTableModelValueMapper() {
public Object map(Object element, String property, Object value) {
return _shortcutTypesByIndex.get((Integer) value).getKey();
}
});
_tblViewerShortcuts.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerShortcuts.setLabelProvider(new BeanListTableModelLabelProvider(_shortcutsModel));
_tblViewerShortcuts.setInput(_shortcutsModel);
GridData btnLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, false, false);
_btnAddShortcut = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddShortcut.setLayoutData(btnLayout);
_btnAddShortcut.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddShortcut();
}
});
_btnRemoveShortcut = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveShortcut.setLayoutData(btnLayout);
_btnRemoveShortcut.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveShortcut();
}
});
bind(form, SWTBinder.BINDING_STRATEGY_AS_YOU_TYPE);
}
private void handleAddShortcut() {
Shortcut shortcut = new Shortcut();
shortcut.setShortcut("");
shortcut.setReference("");
shortcut.setType(ShortcutType.TYPE_PLUGIN);
_shortcutsModel.add(shortcut);
}
private void handleRemoveShortcut() {
if (_tblShortcuts.getSelectionCount() > 0) {
_shortcutsModel.remove((Shortcut) _tblShortcuts.getSelection()[0].getData());
}
}
@Override
public void refresh() throws IOException {
super.refresh();
if (_shortcutsModel != null && _model != null && _model.getShortcuts() != null) {
_shortcutsModel.refresh(_model.getShortcuts());
}
}
}