/*******************************************************************************
* 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.List;
import net.java.dev.genesis.annotation.ViewHandler;
import net.java.dev.genesis.ui.swt.SWTBinder;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
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.WidgetFactory;
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.wga.common.beans.csconfig.v1.ElementMapping;
import de.innovationgate.wga.common.beans.csconfig.v1.EncoderMapping;
import de.innovationgate.wga.common.beans.csconfig.v1.MediaKey;
import de.innovationgate.wga.model.BeanListTableModelListener;
import de.innovationgate.wga.model.ElementMappingsModel;
import de.innovationgate.wga.model.EncoderMappingsModel;
import de.innovationgate.wga.model.MediaKeysModel;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.WGADesignConfigurationModel;
@ViewHandler
public class MappingsConfigurationPage extends GenesisBoundFormPage {
public static final String ID = "de.innovationgate.eclipse.wgadesigner.pages.MappingsConfigurationPage";
public static final String PAGE_TITLE = "Mappings";
private WGADesignConfigurationModel _model;
private Table _tblEncoderMappings;
private TableColumn _encoderNameColumn;
private TableColumn _encoderImplClassColumn;
private EncoderMappingsModel _encoderMappingsModel;
private TableViewer _tblViewerEncoderMappings;
private Button _btnRemoveEncoderMapping;
private Button _btnAddEncoderMapping;
private Table _tblElementMappings;
private TableColumn _elementNameColumn;
private TableColumn _elementImplClassColumn;
private ElementMappingsModel _elementMappingsModel;
private TableViewer _tblViewerElementMappings;
private Button _btnAddElementMapping;
private Button _btnRemoveElementMapping;
private Table _tblMediaKeys;
private TableColumn _mediaKeyColumn;
private TableColumn _mediaMimeTypeColumn;
private TableColumn _mediaBinaryColumn;
private TableColumn _mediaHTTPLoginColumn;
private MediaKeysModel _mediaKeysModel;
private TableViewer _tblViewerMediaKeys;
private Button _btnAddMediaKey;
private Button _btnRemoveMediaKey;
public MappingsConfigurationPage(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());
WidgetFactory factory = new WidgetFactory(toolkit);
form.setText(PAGE_TITLE);
ColumnLayout layout = new ColumnLayout();
layout.maxNumColumns = 2;
form.getBody().setLayout(layout);
GridLayout sectionLayout = new GridLayout();
sectionLayout.numColumns = 2;
GridData fillHSpan = new GridData(GridData.FILL_HORIZONTAL);
fillHSpan.horizontalSpan = 4;
GridData fillH = new GridData(GridData.FILL_HORIZONTAL);
GridData prefSize = new GridData();
prefSize.widthHint = 50;
int tableWidth = 450;
GridData tblLayoutData = new GridData(GridData.FILL_BOTH);
tblLayoutData.verticalSpan = 2;
tblLayoutData.minimumHeight = 200;
GridData btnLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, true, false);
// mediakeys section
Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Media Keys");
Composite sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
sectionClient.setLayout(sectionLayout);
// create Table media keys
_tblMediaKeys = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblMediaKeys.setHeaderVisible(true);
_tblMediaKeys.setLayoutData(GridDataFactory.copyData(tblLayoutData));
registerField("mediaKeys", _tblMediaKeys);
_mediaKeyColumn = new TableColumn(_tblMediaKeys, SWT.NONE);
_mediaKeyColumn.setText("Key");
_mediaKeyColumn.setWidth((int)(tableWidth * 0.25));
_mediaMimeTypeColumn = new TableColumn(_tblMediaKeys, SWT.NONE);
_mediaMimeTypeColumn.setText("Mime type");
_mediaMimeTypeColumn.setWidth((int)(tableWidth * 0.25));
_mediaBinaryColumn = new TableColumn(_tblMediaKeys, SWT.NONE);
_mediaBinaryColumn.setText("Binary output");
_mediaBinaryColumn.setWidth((int)(tableWidth * 0.25));
_mediaHTTPLoginColumn = new TableColumn(_tblMediaKeys, SWT.NONE);
_mediaHTTPLoginColumn.setText("Use HTTP login");
_mediaHTTPLoginColumn.setWidth((int)(tableWidth * 0.25));
_mediaKeysModel = new MediaKeysModel(_model.getMediaKeys());
_mediaKeysModel.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();
}
});
_tblViewerMediaKeys = new TableViewer(_tblMediaKeys);
CellEditor[] editors = new CellEditor[4];
editors[0] = new TextCellEditor(_tblMediaKeys);
editors[1] = new TextCellEditor(_tblMediaKeys);
editors[2] = new CheckboxCellEditor(_tblMediaKeys, SWT.CENTER);
editors[3] = new CheckboxCellEditor(_tblMediaKeys, SWT.CENTER);
_tblViewerMediaKeys.setCellEditors(editors);
BeanListTableModelCellModifier modifier = new BeanListTableModelCellModifier(_tblViewerMediaKeys, _mediaKeysModel);
modifier.setEditMode(2, BeanListTableModelCellModifier.EDIT_MODE_ON_SINGLE_CLICK);
modifier.setEditMode(3, BeanListTableModelCellModifier.EDIT_MODE_ON_SINGLE_CLICK);
_tblViewerMediaKeys.setCellModifier(modifier);
_tblViewerMediaKeys.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerMediaKeys.setLabelProvider(new BeanListTableModelLabelProvider(_mediaKeysModel));
_tblViewerMediaKeys.setInput(_mediaKeysModel);
_btnAddMediaKey = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddMediaKey.setLayoutData(btnLayout);
_btnAddMediaKey.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddMediaKey();
}
});
_btnRemoveMediaKey = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveMediaKey.setLayoutData(btnLayout);
_btnRemoveMediaKey.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveMediaKey();
}
});
// encoder section
section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.COMPACT);
section.setText("Encoder Mappings");
sectionClient = toolkit.createComposite(section);
sectionClient.setLayout(sectionLayout);
section.setClient(sectionClient);
// create Table encoder mappings
_tblEncoderMappings = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblEncoderMappings.setHeaderVisible(true);
_tblEncoderMappings.setLayoutData(GridDataFactory.copyData(tblLayoutData));
registerField("encoderMappings", _tblEncoderMappings);
_encoderNameColumn = new TableColumn(_tblEncoderMappings, SWT.NONE);
_encoderNameColumn.setText("Encoder name");
_encoderNameColumn.setWidth((int)(tableWidth * 0.5));
_encoderImplClassColumn = new TableColumn(_tblEncoderMappings, SWT.NONE);
_encoderImplClassColumn.setText("Implementation class");
_encoderImplClassColumn.setWidth((int)(tableWidth * 0.5));
_encoderMappingsModel = new EncoderMappingsModel(_model.getEncoderMappings());
_encoderMappingsModel.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();
}
});
_tblViewerEncoderMappings = new TableViewer(_tblEncoderMappings);
// text editors for both columns
editors = new CellEditor[2];
editors[0] = new TextCellEditor(_tblEncoderMappings);
editors[1] = new TextCellEditor(_tblEncoderMappings);
_tblViewerEncoderMappings.setCellEditors(editors);
_tblViewerEncoderMappings.setCellModifier(new BeanListTableModelCellModifier(_tblViewerEncoderMappings, _encoderMappingsModel));
_tblViewerEncoderMappings.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerEncoderMappings.setLabelProvider(new BeanListTableModelLabelProvider(_encoderMappingsModel));
_tblViewerEncoderMappings.setInput(_encoderMappingsModel);
_btnAddEncoderMapping = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddEncoderMapping.setLayoutData(btnLayout);
_btnAddEncoderMapping.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddEncoderMapping();
}
});
_btnRemoveEncoderMapping = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveEncoderMapping.setLayoutData(btnLayout);
_btnRemoveEncoderMapping.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveEncoderMapping();
}
});
// element section
section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.COMPACT);
section.setText("Element Mappings");
sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
sectionLayout.numColumns = 2;
sectionClient.setLayout(sectionLayout);
// create Table element mappings
_tblElementMappings = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblElementMappings.setHeaderVisible(true);
_tblElementMappings.setLayoutData(GridDataFactory.copyData(tblLayoutData));
registerField("elementMappings", _tblElementMappings);
_elementNameColumn = new TableColumn(_tblElementMappings, SWT.NONE);
_elementNameColumn.setText("Element name");
_elementNameColumn.setWidth((int)(tableWidth * 0.5));
_elementImplClassColumn = new TableColumn(_tblElementMappings, SWT.NONE);
_elementImplClassColumn.setText("Implementation class");
_elementImplClassColumn.setWidth((int)(tableWidth * 0.5));
_elementMappingsModel = new ElementMappingsModel(_model.getElementMappings());
_elementMappingsModel.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();
}
});
_tblViewerElementMappings = new TableViewer(_tblElementMappings);
// text editors for both columns
editors = new CellEditor[2];
editors[0] = new TextCellEditor(_tblElementMappings);
editors[1] = new TextCellEditor(_tblElementMappings);
_tblViewerElementMappings.setCellEditors(editors);
_tblViewerElementMappings.setCellModifier(new BeanListTableModelCellModifier(_tblViewerElementMappings, _elementMappingsModel));
_tblViewerElementMappings.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerElementMappings.setLabelProvider(new BeanListTableModelLabelProvider(_elementMappingsModel));
_tblViewerElementMappings.setInput(_elementMappingsModel);
_btnAddElementMapping = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddElementMapping.setLayoutData(btnLayout);
_btnAddElementMapping.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddElementMapping();
}
});
_btnRemoveElementMapping = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveElementMapping.setLayoutData(btnLayout);
_btnRemoveElementMapping.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveElementMapping();
}
});
bind(form, SWTBinder.BINDING_STRATEGY_AS_YOU_TYPE);
}
private void handleAddEncoderMapping() {
EncoderMapping mapping = new EncoderMapping();
mapping.setName("<name>");
mapping.setImplementationClass("<implementation class>");
_encoderMappingsModel.add(mapping);
}
private void handleRemoveEncoderMapping() {
if (_tblEncoderMappings.getSelectionCount() > 0) {
_encoderMappingsModel.remove((EncoderMapping) _tblEncoderMappings.getSelection()[0].getData());
}
}
private void handleAddElementMapping() {
ElementMapping mapping = new ElementMapping();
mapping.setName("<name>");
mapping.setImplementationClass("<implementation class>");
_elementMappingsModel.add(mapping);
}
private void handleRemoveElementMapping() {
if (_tblElementMappings.getSelectionCount() > 0) {
_elementMappingsModel.remove((ElementMapping) _tblElementMappings.getSelection()[0].getData());
}
}
private void handleAddMediaKey() {
MediaKey key = new MediaKey();
key.setKey("<key>");
key.setMimeType("<mime type>");
key.setBinary(false);
key.setHttpLogin(false);
_mediaKeysModel.add(key);
}
private void handleRemoveMediaKey() {
if (_tblMediaKeys.getSelectionCount() > 0) {
_mediaKeysModel.remove((MediaKey) _tblMediaKeys.getSelection()[0].getData());
}
}
@Override
public void refresh() throws IOException {
super.refresh();
if (_model != null) {
if (_elementMappingsModel != null && _model.getElementMappings() != null) {
_elementMappingsModel.refresh(_model.getElementMappings());
}
if (_encoderMappingsModel != null && _model.getEncoderMappings() != null) {
_encoderMappingsModel.refresh(_model.getEncoderMappings());
}
if (_mediaKeysModel != null && _model.getMediaKeys() != null) {
_mediaKeysModel.refresh(_model.getMediaKeys());
}
}
}
}