1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.awt.geom.Point2D;
\r
4 import java.util.ArrayList;
\r
5 import java.util.List;
\r
7 import org.eclipse.jface.layout.GridDataFactory;
\r
8 import org.eclipse.jface.layout.GridLayoutFactory;
\r
9 import org.eclipse.jface.viewers.ArrayContentProvider;
\r
10 import org.eclipse.jface.viewers.CellEditor;
\r
11 import org.eclipse.jface.viewers.ICellModifier;
\r
12 import org.eclipse.jface.viewers.ITableLabelProvider;
\r
13 import org.eclipse.jface.viewers.LabelProvider;
\r
14 import org.eclipse.jface.viewers.TableViewer;
\r
15 import org.eclipse.jface.viewers.TextCellEditor;
\r
16 import org.eclipse.jface.viewers.Viewer;
\r
17 import org.eclipse.jface.viewers.ViewerComparator;
\r
18 import org.eclipse.swt.SWT;
\r
19 import org.eclipse.swt.graphics.Image;
\r
20 import org.eclipse.swt.widgets.Composite;
\r
21 import org.eclipse.swt.widgets.Event;
\r
22 import org.eclipse.swt.widgets.Item;
\r
23 import org.eclipse.swt.widgets.Listener;
\r
24 import org.eclipse.swt.widgets.Table;
\r
25 import org.eclipse.swt.widgets.TableColumn;
\r
27 public class LookupInputOutputTable extends Composite {
\r
29 public static final String INPUT = "Input";
\r
30 public static final String OUTPUT = "Output";
\r
31 public static final String[] PROPS = { INPUT, OUTPUT };
\r
33 private Table table;
\r
34 private TableViewer tableViewer;
\r
35 private List<InputOutput> tableRows;
\r
37 public LookupInputOutputTable(Composite parent, int style) {
\r
38 super(parent, style);
\r
40 GridLayoutFactory.fillDefaults().applyTo(this);
\r
41 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
\r
42 table = new Table(this, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
\r
43 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
\r
44 table.setHeaderVisible (true);
\r
45 table.setLinesVisible(true);
\r
46 table.getVerticalBar().setVisible(true);
\r
47 TableColumn column1 = new TableColumn (table, SWT.LEFT);
\r
48 column1.setText (INPUT);
\r
49 column1.setWidth (85);
\r
51 TableColumn column2 = new TableColumn (table, SWT.LEFT);
\r
52 column2.setText (OUTPUT);
\r
53 column2.setWidth (85);
\r
55 // Create the viewer and connect it to the view
\r
56 tableViewer = new TableViewer (table);
\r
58 tableViewer.setContentProvider (new ArrayContentProvider());
\r
59 tableViewer.setLabelProvider (new InputOutputLabelProvider());
\r
60 tableViewer.setCellModifier(new InputOutputCellModifier());
\r
62 tableRows = new ArrayList<InputOutput>();
\r
63 tableViewer.setInput(tableRows);
\r
65 CellEditor[] editors = new CellEditor[2];
\r
66 editors[0] = new TextCellEditor(table);
\r
67 editors[1] = new TextCellEditor(table);
\r
68 tableViewer.setCellEditors(editors);
\r
69 tableViewer.setColumnProperties(PROPS);
\r
73 private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider {
\r
74 public Image getColumnImage (Object element, int columnIndex) {
\r
77 public String getColumnText (Object element, int columnIndex) {
\r
78 if(element instanceof InputOutput) {
\r
79 InputOutput io = (InputOutput)element;
\r
80 switch (columnIndex) {
\r
81 case 0: return (String)io.getInput(String.class);
\r
82 case 1: return (String)io.getOutput(String.class);
\r
89 public void addLocation(Point2D location) {
\r
90 tableRows.add(new InputOutput(location.getX(), location.getY()));
\r
91 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
101 public void removeItem(int index) {
\r
102 tableRows.remove(index);
\r
103 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
106 public void run() {
\r
112 public void clearTable() {
\r
113 this.tableRows.clear();
\r
114 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
117 public void run() {
\r
124 public class InputOutput {
\r
125 private double input, output;
\r
127 public InputOutput(double input, double output) {
\r
128 this.input = input;
\r
129 this.output = output;
\r
134 * @param clazz String.class or Double.class
\r
135 * @return input as string or double or null if asked for something else
\r
137 @SuppressWarnings("rawtypes")
\r
138 public Object getInput(Class clazz) {
\r
139 if(clazz == String.class) {
\r
141 } else if (clazz == Double.class) {
\r
147 public Double setInput(String input) {
\r
149 this.input = Double.parseDouble(input);
\r
151 } catch (NumberFormatException e) {
\r
156 public void setInput(double input) {
\r
157 this.input = input;
\r
162 * @param clazz String.class or Double.class
\r
163 * @return output as string or double or null if asked for something else
\r
165 @SuppressWarnings("rawtypes")
\r
166 public Object getOutput(Class clazz) {
\r
167 if(clazz == String.class) {
\r
168 return "" + output;
\r
169 } else if (clazz == Double.class) {
\r
175 public void setOutput(String output) {
\r
177 this.output = Double.parseDouble(output);
\r
178 } catch (NumberFormatException e) {
\r
183 public void setOutput(double output) {
\r
184 this.output = output;
\r
189 class InputOutputComparator extends ViewerComparator {
\r
191 public int compare(Viewer viewer, Object e1, Object e2) {
\r
192 if ((e1 instanceof InputOutput) &&
\r
193 (e2 instanceof InputOutput)) {
\r
194 InputOutput io1 = (InputOutput)e1;
\r
195 InputOutput io2 = (InputOutput)e2;
\r
196 Double d1 = (Double)io1.getInput((Double.class));
\r
197 Double d2 = (Double)io2.getInput((Double.class));
\r
198 return d1.compareTo(d2);
\r
204 public TableViewer getTableViewer() {
\r
205 return this.tableViewer;
\r
208 public void refresh() {
\r
209 if(!tableViewer.getTable().isDisposed()) {
\r
210 tableViewer.setComparator(new InputOutputComparator());
\r
211 tableViewer.refresh();
\r
215 class InputOutputCellModifier implements ICellModifier {
\r
217 public InputOutputCellModifier() {}
\r
219 public boolean canModify(Object element, String property) {
\r
223 public Object getValue(Object element, String property) {
\r
224 InputOutput io = (InputOutput)element;
\r
225 if (LookupInputOutputTable.INPUT.equals(property))
\r
226 return (String)io.getInput(String.class);
\r
227 else if (LookupInputOutputTable.OUTPUT.equals(property))
\r
228 return (String)io.getOutput(String.class);
\r
233 public void modify(Object element, String property, Object value) {
\r
234 if (element instanceof Item) element = ((Item) element).getData();
\r
236 InputOutput io = (InputOutput)element;
\r
238 if (LookupInputOutputTable.INPUT.equals(property)) {
\r
239 io.setInput((String)value);
\r
240 } else if (LookupInputOutputTable.OUTPUT.equals(property)) {
\r
241 io.setOutput((String)value);
\r
248 private void tableModified() {
\r
249 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
252 public void run() {
\r
253 for (Listener listener : getListeners(SWT.Modify)) {
\r
254 Event e = new Event();
\r
255 listener.handleEvent(e);
\r