1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
14 import java.awt.geom.Point2D;
\r
15 import java.util.ArrayList;
\r
16 import java.util.List;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.jface.viewers.ArrayContentProvider;
\r
21 import org.eclipse.jface.viewers.CellEditor;
\r
22 import org.eclipse.jface.viewers.ICellModifier;
\r
23 import org.eclipse.jface.viewers.ITableLabelProvider;
\r
24 import org.eclipse.jface.viewers.LabelProvider;
\r
25 import org.eclipse.jface.viewers.TableViewer;
\r
26 import org.eclipse.jface.viewers.TextCellEditor;
\r
27 import org.eclipse.jface.viewers.Viewer;
\r
28 import org.eclipse.jface.viewers.ViewerComparator;
\r
29 import org.eclipse.swt.SWT;
\r
30 import org.eclipse.swt.graphics.Image;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.Event;
\r
33 import org.eclipse.swt.widgets.Item;
\r
34 import org.eclipse.swt.widgets.Listener;
\r
35 import org.eclipse.swt.widgets.Table;
\r
36 import org.eclipse.swt.widgets.TableColumn;
\r
38 public class LookupInputOutputTable extends Composite {
\r
40 public static final String INPUT = "Input";
\r
41 public static final String OUTPUT = "Output";
\r
42 public static final String[] PROPS = { INPUT, OUTPUT };
\r
44 private Table table;
\r
45 private TableViewer tableViewer;
\r
46 private List<InputOutput> tableRows;
\r
48 public LookupInputOutputTable(Composite parent, int style) {
\r
49 super(parent, style);
\r
51 GridLayoutFactory.fillDefaults().applyTo(this);
\r
52 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
\r
53 table = new Table(this, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
\r
54 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
\r
55 table.setHeaderVisible (true);
\r
56 table.setLinesVisible(true);
\r
57 table.getVerticalBar().setVisible(true);
\r
58 TableColumn column1 = new TableColumn (table, SWT.LEFT);
\r
59 column1.setText (INPUT);
\r
60 column1.setWidth (85);
\r
62 TableColumn column2 = new TableColumn (table, SWT.LEFT);
\r
63 column2.setText (OUTPUT);
\r
64 column2.setWidth (85);
\r
66 // Create the viewer and connect it to the view
\r
67 tableViewer = new TableViewer (table);
\r
69 tableViewer.setContentProvider (new ArrayContentProvider());
\r
70 tableViewer.setLabelProvider (new InputOutputLabelProvider());
\r
71 tableViewer.setCellModifier(new InputOutputCellModifier());
\r
73 tableRows = new ArrayList<InputOutput>();
\r
74 tableViewer.setInput(tableRows);
\r
76 CellEditor[] editors = new CellEditor[2];
\r
77 editors[0] = new TextCellEditor(table);
\r
78 editors[1] = new TextCellEditor(table);
\r
79 tableViewer.setCellEditors(editors);
\r
80 tableViewer.setColumnProperties(PROPS);
\r
84 private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider {
\r
85 public Image getColumnImage (Object element, int columnIndex) {
\r
88 public String getColumnText (Object element, int columnIndex) {
\r
89 if(element instanceof InputOutput) {
\r
90 InputOutput io = (InputOutput)element;
\r
91 switch (columnIndex) {
\r
92 case 0: return (String)io.getInput(String.class);
\r
93 case 1: return (String)io.getOutput(String.class);
\r
100 public void addLocation(Point2D location) {
\r
101 tableRows.add(new InputOutput(location.getX(), location.getY()));
\r
102 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
105 public void run() {
\r
112 public void removeItem(int index) {
\r
113 tableRows.remove(index);
\r
114 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
117 public void run() {
\r
123 public void clearTable() {
\r
124 this.tableRows.clear();
\r
125 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
128 public void run() {
\r
135 public class InputOutput {
\r
136 private double input, output;
\r
138 public InputOutput(double input, double output) {
\r
139 this.input = input;
\r
140 this.output = output;
\r
145 * @param clazz String.class or Double.class
\r
146 * @return input as string or double or null if asked for something else
\r
148 @SuppressWarnings("rawtypes")
\r
149 public Object getInput(Class clazz) {
\r
150 if(clazz == String.class) {
\r
152 } else if (clazz == Double.class) {
\r
158 public Double setInput(String input) {
\r
160 this.input = Double.parseDouble(input);
\r
162 } catch (NumberFormatException e) {
\r
167 public void setInput(double input) {
\r
168 this.input = input;
\r
173 * @param clazz String.class or Double.class
\r
174 * @return output as string or double or null if asked for something else
\r
176 @SuppressWarnings("rawtypes")
\r
177 public Object getOutput(Class clazz) {
\r
178 if(clazz == String.class) {
\r
179 return "" + output;
\r
180 } else if (clazz == Double.class) {
\r
186 public void setOutput(String output) {
\r
188 this.output = Double.parseDouble(output);
\r
189 } catch (NumberFormatException e) {
\r
194 public void setOutput(double output) {
\r
195 this.output = output;
\r
200 class InputOutputComparator extends ViewerComparator {
\r
202 public int compare(Viewer viewer, Object e1, Object e2) {
\r
203 if ((e1 instanceof InputOutput) &&
\r
204 (e2 instanceof InputOutput)) {
\r
205 InputOutput io1 = (InputOutput)e1;
\r
206 InputOutput io2 = (InputOutput)e2;
\r
207 Double d1 = (Double)io1.getInput((Double.class));
\r
208 Double d2 = (Double)io2.getInput((Double.class));
\r
209 return d1.compareTo(d2);
\r
215 public TableViewer getTableViewer() {
\r
216 return this.tableViewer;
\r
219 public void refresh() {
\r
220 if(!tableViewer.getTable().isDisposed()) {
\r
221 tableViewer.setComparator(new InputOutputComparator());
\r
222 tableViewer.refresh();
\r
226 class InputOutputCellModifier implements ICellModifier {
\r
228 public InputOutputCellModifier() {}
\r
230 public boolean canModify(Object element, String property) {
\r
234 public Object getValue(Object element, String property) {
\r
235 InputOutput io = (InputOutput)element;
\r
236 if (LookupInputOutputTable.INPUT.equals(property))
\r
237 return (String)io.getInput(String.class);
\r
238 else if (LookupInputOutputTable.OUTPUT.equals(property))
\r
239 return (String)io.getOutput(String.class);
\r
244 public void modify(Object element, String property, Object value) {
\r
245 if (element instanceof Item) element = ((Item) element).getData();
\r
247 InputOutput io = (InputOutput)element;
\r
249 if (LookupInputOutputTable.INPUT.equals(property)) {
\r
250 io.setInput((String)value);
\r
251 } else if (LookupInputOutputTable.OUTPUT.equals(property)) {
\r
252 io.setOutput((String)value);
\r
259 private void tableModified() {
\r
260 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
263 public void run() {
\r
264 for (Listener listener : getListeners(SWT.Modify)) {
\r
265 Event e = new Event();
\r
266 listener.handleEvent(e);
\r