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.equation.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.ITableLabelProvider;
\r
23 import org.eclipse.jface.viewers.LabelProvider;
\r
24 import org.eclipse.jface.viewers.TableViewer;
\r
25 import org.eclipse.jface.viewers.TextCellEditor;
\r
26 import org.eclipse.jface.viewers.Viewer;
\r
27 import org.eclipse.jface.viewers.ViewerComparator;
\r
28 import org.eclipse.swt.SWT;
\r
29 import org.eclipse.swt.graphics.Image;
\r
30 import org.eclipse.swt.widgets.Composite;
\r
31 import org.eclipse.swt.widgets.Table;
\r
32 import org.eclipse.swt.widgets.TableColumn;
\r
34 public class LookupInputOutputTable extends Composite {
\r
36 public static final String INPUT = "Input";
\r
37 public static final String OUTPUT = "Output";
\r
38 public static final String[] PROPS = { INPUT, OUTPUT };
\r
41 TableViewer tableViewer;
\r
42 List<InputOutput> tableRows;
\r
44 public LookupInputOutputTable(Composite parent, int style) {
\r
45 super(parent, style);
\r
47 GridLayoutFactory.fillDefaults().applyTo(this);
\r
48 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
\r
49 table = new Table(this, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
\r
50 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
\r
51 table.setHeaderVisible (true);
\r
52 table.setLinesVisible(true);
\r
53 table.getVerticalBar().setVisible(true);
\r
54 TableColumn column1 = new TableColumn (table, SWT.LEFT);
\r
55 column1.setText (INPUT);
\r
56 column1.setWidth (85);
\r
58 TableColumn column2 = new TableColumn (table, SWT.LEFT);
\r
59 column2.setText (OUTPUT);
\r
60 column2.setWidth (85);
\r
62 // Create the viewer and connect it to the view
\r
63 tableViewer = new TableViewer (table);
\r
65 tableViewer.setContentProvider (new ArrayContentProvider());
\r
66 tableViewer.setLabelProvider (new InputOutputLabelProvider());
\r
68 tableRows = new ArrayList<InputOutput>();
\r
69 tableViewer.setInput(tableRows);
\r
71 CellEditor[] editors = new CellEditor[2];
\r
72 editors[0] = new TextCellEditor(table);
\r
73 editors[1] = new TextCellEditor(table);
\r
74 tableViewer.setCellEditors(editors);
\r
75 tableViewer.setColumnProperties(PROPS);
\r
79 private class InputOutputLabelProvider extends LabelProvider implements ITableLabelProvider {
\r
80 public Image getColumnImage (Object element, int columnIndex) {
\r
83 public String getColumnText (Object element, int columnIndex) {
\r
84 if(element instanceof InputOutput) {
\r
85 InputOutput io = (InputOutput)element;
\r
86 switch (columnIndex) {
\r
87 case 0: return (String)io.getInput(String.class);
\r
88 case 1: return (String)io.getOutput(String.class);
\r
95 public void addLocation(Point2D location) {
\r
96 tableRows.add(new InputOutput(location.getX(), location.getY()));
\r
97 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
100 public void run() {
\r
107 public void removeLocation(Point2D location) {
\r
108 for(InputOutput io : tableRows) {
\r
109 if((Double)io.getInput(Double.class) == location.getX()) {
\r
110 tableRows.remove(io);
\r
111 tableViewer.getTable().getDisplay().asyncExec(new Runnable() {
\r
114 public void run() {
\r
125 class InputOutput {
\r
126 private double input, output;
\r
128 public InputOutput(double input, double output) {
\r
129 this.input = input;
\r
130 this.output = output;
\r
135 * @param clazz String.class or Double.class
\r
136 * @return input as string or double or null if asked for something else
\r
138 @SuppressWarnings("rawtypes")
\r
139 public Object getInput(Class clazz) {
\r
140 if(clazz == String.class) {
\r
142 } else if (clazz == Double.class) {
\r
148 public Double setInput(String input) {
\r
150 this.input = Double.parseDouble(input);
\r
152 } catch (NumberFormatException e) {
\r
157 public void setInput(double input) {
\r
158 this.input = input;
\r
163 * @param clazz String.class or Double.class
\r
164 * @return output as string or double or null if asked for something else
\r
166 @SuppressWarnings("rawtypes")
\r
167 public Object getOutput(Class clazz) {
\r
168 if(clazz == String.class) {
\r
169 return "" + output;
\r
170 } else if (clazz == Double.class) {
\r
176 public void setOutput(String output) {
\r
178 this.output = Double.parseDouble(output);
\r
179 } catch (NumberFormatException e) {
\r
184 public void setOutput(double output) {
\r
185 this.output = output;
\r
190 class InputOutputComparator extends ViewerComparator {
\r
192 public int compare(Viewer viewer, Object e1, Object e2) {
\r
193 if ((e1 instanceof InputOutput) &&
\r
194 (e2 instanceof InputOutput)) {
\r
195 InputOutput io1 = (InputOutput)e1;
\r
196 InputOutput io2 = (InputOutput)e2;
\r
197 Double d1 = (Double)io1.getInput((Double.class));
\r
198 Double d2 = (Double)io2.getInput((Double.class));
\r
199 return d1.compareTo(d2);
\r
205 public TableViewer getTableViewer() {
\r
206 return this.tableViewer;
\r
209 public void refresh() {
\r
210 tableViewer.setComparator(new InputOutputComparator());
\r
211 tableViewer.refresh();
\r