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.Comparator;
\r
17 import java.util.List;
\r
19 import org.eclipse.jface.layout.GridDataFactory;
\r
20 import org.eclipse.jface.layout.GridLayoutFactory;
\r
21 import org.eclipse.jface.viewers.ArrayContentProvider;
\r
22 import org.eclipse.jface.viewers.CellEditor;
\r
23 import org.eclipse.jface.viewers.ICellModifier;
\r
24 import org.eclipse.jface.viewers.ITableLabelProvider;
\r
25 import org.eclipse.jface.viewers.LabelProvider;
\r
26 import org.eclipse.jface.viewers.TableViewer;
\r
27 import org.eclipse.jface.viewers.TextCellEditor;
\r
28 import org.eclipse.jface.viewers.Viewer;
\r
29 import org.eclipse.jface.viewers.ViewerComparator;
\r
30 import org.eclipse.swt.SWT;
\r
31 import org.eclipse.swt.graphics.Image;
\r
32 import org.eclipse.swt.widgets.Composite;
\r
33 import org.eclipse.swt.widgets.Event;
\r
34 import org.eclipse.swt.widgets.Item;
\r
35 import org.eclipse.swt.widgets.Listener;
\r
36 import org.eclipse.swt.widgets.Table;
\r
37 import org.eclipse.swt.widgets.TableColumn;
\r
39 public class LookupInputOutputTable extends Composite {
\r
41 public static final String INPUT = "Input";
\r
42 public static final String OUTPUT = "Output";
\r
43 public static final String[] PROPS = { INPUT, OUTPUT };
\r
45 private Table table;
\r
46 private TableViewer tableViewer;
\r
47 private List<InputOutput> tableRows;
\r
49 public LookupInputOutputTable(Composite parent, int style) {
\r
50 super(parent, style);
\r
52 GridLayoutFactory.fillDefaults().applyTo(this);
\r
53 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
\r
54 table = new Table(this, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
\r
55 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
\r
56 table.setHeaderVisible (true);
\r
57 table.setLinesVisible(true);
\r
58 table.getVerticalBar().setVisible(true);
\r
59 TableColumn column1 = new TableColumn (table, SWT.LEFT);
\r
60 column1.setText (INPUT);
\r
61 column1.setWidth (85);
\r
63 TableColumn column2 = new TableColumn (table, SWT.LEFT);
\r
64 column2.setText (OUTPUT);
\r
65 column2.setWidth (85);
\r
67 // Create the viewer and connect it to the view
\r
68 tableViewer = new TableViewer (table);
\r
70 tableViewer.setContentProvider (new ArrayContentProvider());
\r
71 tableViewer.setLabelProvider (new InputOutputLabelProvider());
\r
72 tableViewer.setCellModifier(new InputOutputCellModifier());
\r
74 tableRows = new ArrayList<InputOutput>();
\r
75 tableViewer.setInput(tableRows);
\r
77 CellEditor[] editors = new CellEditor[2];
\r
78 editors[0] = new TextCellEditor(table);
\r
79 editors[1] = new TextCellEditor(table);
\r
80 tableViewer.setCellEditors(editors);
\r
81 tableViewer.setColumnProperties(PROPS);
\r
85 private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider {
\r
86 public Image getColumnImage (Object element, int columnIndex) {
\r
89 public String getColumnText (Object element, int columnIndex) {
\r
90 if(element instanceof InputOutput) {
\r
91 InputOutput io = (InputOutput)element;
\r
92 switch (columnIndex) {
\r
93 case 0: return (String)io.getInput(String.class);
\r
94 case 1: return (String)io.getOutput(String.class);
\r
101 public void addLocation(Point2D location) {
\r
102 tableRows.add(new InputOutput(location.getX(), location.getY()));
\r
103 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
106 public void run() {
\r
113 public void removeItem(int index) {
\r
114 tableRows.remove(index);
\r
115 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
118 public void run() {
\r
124 public void clearTable() {
\r
125 this.tableRows.clear();
\r
126 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
129 public void run() {
\r
136 public class InputOutput {
\r
137 private double input, output;
\r
139 public InputOutput(double input, double output) {
\r
140 this.input = input;
\r
141 this.output = output;
\r
146 * @param clazz String.class or Double.class
\r
147 * @return input as string or double or null if asked for something else
\r
149 @SuppressWarnings("rawtypes")
\r
150 public Object getInput(Class clazz) {
\r
151 if(clazz == String.class) {
\r
153 } else if (clazz == Double.class) {
\r
159 public Double setInput(String input) {
\r
161 this.input = Double.parseDouble(input);
\r
163 } catch (NumberFormatException e) {
\r
168 public void setInput(double input) {
\r
169 this.input = input;
\r
174 * @param clazz String.class or Double.class
\r
175 * @return output as string or double or null if asked for something else
\r
177 @SuppressWarnings("rawtypes")
\r
178 public Object getOutput(Class clazz) {
\r
179 if(clazz == String.class) {
\r
180 return "" + output;
\r
181 } else if (clazz == Double.class) {
\r
187 public void setOutput(String output) {
\r
189 this.output = Double.parseDouble(output);
\r
190 } catch (NumberFormatException e) {
\r
195 public void setOutput(double output) {
\r
196 this.output = output;
\r
201 public class InputOutputComparator extends ViewerComparator implements Comparator<InputOutput>{
\r
203 public int compare(Viewer viewer, Object e1, Object e2) {
\r
204 if ((e1 instanceof InputOutput) &&
\r
205 (e2 instanceof InputOutput)) {
\r
206 return compare((InputOutput)e1, (InputOutput)e2);
\r
213 public int compare(InputOutput e1, InputOutput e2) {
\r
214 InputOutput io1 = (InputOutput)e1;
\r
215 InputOutput io2 = (InputOutput)e2;
\r
216 Double d1 = (Double)io1.getInput((Double.class));
\r
217 Double d2 = (Double)io2.getInput((Double.class));
\r
218 return d1.compareTo(d2);
\r
222 public TableViewer getTableViewer() {
\r
223 return this.tableViewer;
\r
226 public void refresh() {
\r
227 if(!tableViewer.getTable().isDisposed()) {
\r
228 tableViewer.setComparator(new InputOutputComparator());
\r
229 tableViewer.refresh();
\r
233 class InputOutputCellModifier implements ICellModifier {
\r
235 public InputOutputCellModifier() {}
\r
237 public boolean canModify(Object element, String property) {
\r
241 public Object getValue(Object element, String property) {
\r
242 InputOutput io = (InputOutput)element;
\r
243 if (LookupInputOutputTable.INPUT.equals(property))
\r
244 return (String)io.getInput(String.class);
\r
245 else if (LookupInputOutputTable.OUTPUT.equals(property))
\r
246 return (String)io.getOutput(String.class);
\r
251 public void modify(Object element, String property, Object value) {
\r
252 if (element instanceof Item) element = ((Item) element).getData();
\r
254 InputOutput io = (InputOutput)element;
\r
256 if (LookupInputOutputTable.INPUT.equals(property)) {
\r
257 io.setInput((String)value);
\r
258 } else if (LookupInputOutputTable.OUTPUT.equals(property)) {
\r
259 io.setOutput((String)value);
\r
266 private void tableModified() {
\r
267 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
270 public void run() {
\r
271 for (Listener listener : getListeners(SWT.Modify)) {
\r
272 Event e = new Event();
\r
273 listener.handleEvent(e);
\r