/*******************************************************************************
* 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.Collections;
import java.util.Comparator;
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.v1.RemoteAction;
import de.innovationgate.wga.model.AccessLevel;
import de.innovationgate.wga.model.BeanListTableModelListener;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.RemoteActionsModel;
import de.innovationgate.wga.model.WGADesignConfigurationModel;
@ViewHandler
public class RemoteActionsConfigurationPage extends GenesisBoundFormPage {
public static final String ID = "de.innovationgate.eclipse.wgadesigner.pages.RemoteActionsConfigurationPage";
public static final String PAGE_TITLE = "Remote Actions";
private WGADesignConfigurationModel _model;
private Table _tblRemoteActions;
private TableColumn _actionModuleColumn;
private TableColumn _minAccessLevelColumn;
private RemoteActionsModel _remoteActionsModel;
private TableViewer _tblViewerRemoteActions;
private Button _btnAddRemoteAction;
private TableColumn _callersColumn;
private Button _btnRemoveRemoteAction;
private HashMap<Integer, AccessLevel> _accessLevelByIndex;
public RemoteActionsConfigurationPage(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("Remote Actions");
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 for remote actions
_tblRemoteActions = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblRemoteActions.setHeaderVisible(true);
GridData tblLayoutData = new GridData(GridData.FILL_BOTH);
tblLayoutData.verticalSpan = 2;
tblLayoutData.minimumHeight = 200;
_tblRemoteActions.setLayoutData(tblLayoutData);
registerField("remoteActions", _tblRemoteActions);
int tableWidth = 450;
_actionModuleColumn = new TableColumn(_tblRemoteActions, SWT.NONE);
_actionModuleColumn.setText("Action module");
_actionModuleColumn.setWidth((int)(tableWidth * 0.3));
_minAccessLevelColumn = new TableColumn(_tblRemoteActions, SWT.NONE);
_minAccessLevelColumn.setText("Minimum access level");
_minAccessLevelColumn.setWidth((int)(tableWidth * 0.3));
_callersColumn = new TableColumn(_tblRemoteActions, SWT.NONE);
_callersColumn.setText("Callers (comma separated)");
_callersColumn.setWidth((int)(tableWidth * 0.3));
_remoteActionsModel = new RemoteActionsModel(_model.getRemoteActions());
_remoteActionsModel.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();
}
});
_tblViewerRemoteActions = new TableViewer(_tblRemoteActions);
List<String> accessLevelValues = new ArrayList<String>();
List<AccessLevel> sortedAccessLevels = new ArrayList<AccessLevel>(WGADesignConfigurationModel.ACCESSLEVELS_REMOTE_ACTIONS.values());
Collections.sort(sortedAccessLevels, new Comparator<AccessLevel>() {
public int compare(AccessLevel o1, AccessLevel o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
Iterator<AccessLevel> it = sortedAccessLevels.iterator();
_accessLevelByIndex = new HashMap<Integer, AccessLevel>();
int i = 0;
while (it.hasNext()) {
AccessLevel level = it.next();
accessLevelValues.add(level.getValue());
_accessLevelByIndex.put(i, level);
i++;
}
CellEditor[] editors = new CellEditor[3];
editors[0] = new TextCellEditor(_tblRemoteActions);
editors[1] = new ComboBoxCellEditor(_tblRemoteActions, accessLevelValues.toArray(new String[0]), SWT.READ_ONLY);
editors[2] = new TextCellEditor(_tblRemoteActions);
_tblViewerRemoteActions.setCellEditors(editors);
BeanListTableModelCellModifier modifier = new BeanListTableModelCellModifier(_tblViewerRemoteActions, _remoteActionsModel);
modifier.setEditMode(1, BeanListTableModelCellModifier.EDIT_MODE_ON_SINGLE_CLICK);
_tblViewerRemoteActions.setCellModifier(modifier);
modifier.setValueMapper(1, new BeanListTableModelValueMapper() {
public Object map(Object element, String property, Object value) {
return _accessLevelByIndex.get((Integer) value).getKey();
}
});
_tblViewerRemoteActions.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerRemoteActions.setLabelProvider(new BeanListTableModelLabelProvider(_remoteActionsModel));
_tblViewerRemoteActions.setInput(_remoteActionsModel);
GridData btnLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, false, false);
_btnAddRemoteAction = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddRemoteAction.setLayoutData(btnLayout);
_btnAddRemoteAction.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddRemoteAction();
}
});
_btnRemoveRemoteAction = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveRemoteAction.setLayoutData(btnLayout);
_btnRemoveRemoteAction.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveRemoteAction();
}
});
bind(form, SWTBinder.BINDING_STRATEGY_AS_YOU_TYPE);
}
@SuppressWarnings("unchecked")
private void handleAddRemoteAction() {
RemoteAction action = new RemoteAction();
action.setModuleName("<module name>");
action.setCallerLevel(AccessLevel.LEVEL_MANAGER);
action.setCallers(new ArrayList());
_remoteActionsModel.add(action);
}
private void handleRemoveRemoteAction() {
if (_tblRemoteActions.getSelectionCount() > 0) {
_remoteActionsModel.remove((RemoteAction) _tblRemoteActions.getSelection()[0].getData());
}
}
@Override
public void refresh() throws IOException {
super.refresh();
if (_remoteActionsModel != null && _model != null && _model.getRemoteActions() != null) {
_remoteActionsModel.refresh(_model.getRemoteActions());
}
}
}