1 package org.simantics.sysdyn.ui.equation.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.ITableLabelProvider;
\r
12 import org.eclipse.jface.viewers.LabelProvider;
\r
13 import org.eclipse.jface.viewers.TableViewer;
\r
14 import org.eclipse.jface.viewers.TextCellEditor;
\r
15 import org.eclipse.jface.viewers.Viewer;
\r
16 import org.eclipse.jface.viewers.ViewerComparator;
\r
17 import org.eclipse.swt.SWT;
\r
18 import org.eclipse.swt.graphics.Image;
\r
19 import org.eclipse.swt.widgets.Composite;
\r
20 import org.eclipse.swt.widgets.Table;
\r
21 import org.eclipse.swt.widgets.TableColumn;
\r
23 public class LookupInputOutputTable extends Composite {
\r
25 public static final String INPUT = "Input";
\r
26 public static final String OUTPUT = "Output";
\r
27 public static final String[] PROPS = { INPUT, OUTPUT };
\r
30 TableViewer tableViewer;
\r
31 List<InputOutput> tableRows;
\r
33 public LookupInputOutputTable(Composite parent, int style) {
\r
34 super(parent, style);
\r
36 GridLayoutFactory.fillDefaults().applyTo(this);
\r
37 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
\r
38 table = new Table(this, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
\r
39 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
\r
40 table.setHeaderVisible (true);
\r
41 table.setLinesVisible(true);
\r
42 table.getVerticalBar().setVisible(true);
\r
43 TableColumn column1 = new TableColumn (table, SWT.LEFT);
\r
44 column1.setText (INPUT);
\r
45 column1.setWidth (85);
\r
47 TableColumn column2 = new TableColumn (table, SWT.LEFT);
\r
48 column2.setText (OUTPUT);
\r
49 column2.setWidth (85);
\r
51 // Create the viewer and connect it to the view
\r
52 tableViewer = new TableViewer (table);
\r
54 tableViewer.setContentProvider (new ArrayContentProvider());
\r
55 tableViewer.setLabelProvider (new InputOutputLabelProvider());
\r
57 tableRows = new ArrayList<InputOutput>();
\r
58 tableViewer.setInput(tableRows);
\r
60 CellEditor[] editors = new CellEditor[2];
\r
61 editors[0] = new TextCellEditor(table);
\r
62 editors[1] = new TextCellEditor(table);
\r
63 tableViewer.setCellEditors(editors);
\r
64 tableViewer.setColumnProperties(PROPS);
\r
68 private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider {
\r
69 public Image getColumnImage (Object element, int columnIndex) {
\r
72 public String getColumnText (Object element, int columnIndex) {
\r
73 if(element instanceof InputOutput) {
\r
74 InputOutput io = (InputOutput)element;
\r
75 switch (columnIndex) {
\r
76 case 0: return (String)io.getInput(String.class);
\r
77 case 1: return (String)io.getOutput(String.class);
\r
84 public void addLocation(Point2D location) {
\r
85 tableRows.add(new InputOutput(location.getX(), location.getY()));
\r
86 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
96 public void removeLocation(Point2D location) {
\r
97 for(InputOutput io : tableRows) {
\r
98 if((Double)io.getInput(Double.class) == location.getX()) {
\r
99 tableRows.remove(io);
\r
100 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
103 public void run() {
\r
114 class InputOutput {
\r
115 private double input, output;
\r
117 public InputOutput(double input, double output) {
\r
118 this.input = input;
\r
119 this.output = output;
\r
124 * @param clazz String.class or Double.class
\r
125 * @return input as string or double or null if asked for something else
\r
127 @SuppressWarnings("rawtypes")
\r
128 public Object getInput(Class clazz) {
\r
129 if(clazz == String.class) {
\r
131 } else if (clazz == Double.class) {
\r
137 public Double setInput(String input) {
\r
139 this.input = Double.parseDouble(input);
\r
141 } catch (NumberFormatException e) {
\r
146 public void setInput(double input) {
\r
147 this.input = input;
\r
152 * @param clazz String.class or Double.class
\r
153 * @return output as string or double or null if asked for something else
\r
155 @SuppressWarnings("rawtypes")
\r
156 public Object getOutput(Class clazz) {
\r
157 if(clazz == String.class) {
\r
158 return "" + output;
\r
159 } else if (clazz == Double.class) {
\r
165 public void setOutput(String output) {
\r
167 this.output = Double.parseDouble(output);
\r
168 } catch (NumberFormatException e) {
\r
173 public void setOutput(double output) {
\r
174 this.output = output;
\r
179 class InputOutputComparator extends ViewerComparator {
\r
181 public int compare(Viewer viewer, Object e1, Object e2) {
\r
182 if ((e1 instanceof InputOutput) &&
\r
183 (e2 instanceof InputOutput)) {
\r
184 InputOutput io1 = (InputOutput)e1;
\r
185 InputOutput io2 = (InputOutput)e2;
\r
186 Double d1 = (Double)io1.getInput((Double.class));
\r
187 Double d2 = (Double)io2.getInput((Double.class));
\r
188 return d1.compareTo(d2);
\r
194 public TableViewer getTableViewer() {
\r
195 return this.tableViewer;
\r
198 public void refresh() {
\r
199 tableViewer.setComparator(new InputOutputComparator());
\r
200 tableViewer.refresh();
\r