package tool.editors.cdf;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;
import tool.model.ToolEvent;
import tool.model.ToolParameter;
import tool.model.validation.ToolNameValidator;
public class EventDialog extends TitleAreaDialog {
private DataBindingContext m_bindingContext;
public static int show(ToolEvent event){
Shell shell = Display.getDefault().getActiveShell();
EventDialog dialog = new EventDialog(shell, event);
return dialog.open();
}
private ToolEvent event;
private Text text;
private Table table;
private Button button;
private TableViewer tableViewer;
/**
* Create the dialog.
* @param parentShell
*/
private EventDialog(Shell parentShell, ToolEvent event) {
super(parentShell);
setShellStyle(SWT.PRIMARY_MODAL);
this.event = event;
}
/**
* Create contents of the dialog.
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
setTitleImage(ResourceManager.getPluginImage("Tool", "icons/private_event.gif"));
setTitle("Event");
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
GridLayout gl_container = new GridLayout(2, false);
gl_container.horizontalSpacing = 8;
container.setLayout(gl_container);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
Label label = new Label(container, SWT.NONE);
label.setText("Name");
text = new Text(container, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(container, SWT.NONE);
button = new Button(container, SWT.CHECK);
button.setText("Public");
new Label(container, SWT.NONE);
tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn = tableViewerColumn.getColumn();
tableColumn.setWidth(42);
tableColumn.setText("Copy");
TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_1 = tableViewerColumn_1.getColumn();
tableColumn_1.setWidth(100);
tableColumn_1.setText("Name");
TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_2 = tableViewerColumn_2.getColumn();
tableColumn_2.setWidth(100);
tableColumn_2.setText("Type");
TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_3 = tableViewerColumn_3.getColumn();
tableColumn_3.setWidth(100);
tableColumn_3.setText("Parsing");
TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_4 = tableViewerColumn_4.getColumn();
tableColumn_4.setWidth(100);
tableColumn_4.setText("Default");
return area;
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
m_bindingContext = initDataBindings();
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(523, 300);
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue textObserveTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
IObservableValue eventNameObserveValue = BeansObservables.observeValue(event, "toolName");
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setAfterConvertValidator(new ToolNameValidator(text));
bindingContext.bindValue(textObserveTextObserveWidget, eventNameObserveValue, strategy, null);
//
IObservableValue buttonObserveSelectionObserveWidget = SWTObservables.observeSelection(button);
IObservableValue eventPublicObserveValue = BeansObservables.observeValue(event, "public");
bindingContext.bindValue(buttonObserveSelectionObserveWidget, eventPublicObserveValue, null, null);
//
ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
tableViewer.setContentProvider(listContentProvider);
//
IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), ToolParameter.class, new String[]{"name", "type", "parsingType", "defaultValue"});
tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
//
IObservableList eventParametersObserveList = BeansObservables.observeList(Realm.getDefault(), event, "parameters");
tableViewer.setInput(eventParametersObserveList);
//
return bindingContext;
}
}