1 package org.simantics.sysdyn.ui.equation.expressions;
\r
3 import java.util.Map;
\r
5 import org.eclipse.jface.layout.GridDataFactory;
\r
6 import org.eclipse.jface.layout.GridLayoutFactory;
\r
7 import org.eclipse.swt.SWT;
\r
8 import org.eclipse.swt.events.FocusAdapter;
\r
9 import org.eclipse.swt.events.FocusEvent;
\r
10 import org.eclipse.swt.events.KeyAdapter;
\r
11 import org.eclipse.swt.events.KeyEvent;
\r
12 import org.eclipse.swt.graphics.Point;
\r
13 import org.eclipse.swt.widgets.Composite;
\r
14 import org.eclipse.swt.widgets.Label;
\r
15 import org.eclipse.swt.widgets.Text;
\r
16 import org.simantics.db.Builtins;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.common.request.WriteRequest;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.request.Read;
\r
23 import org.simantics.sysdyn.SysdynResource;
\r
24 import org.simantics.ui.SimanticsUI;
\r
26 public class WithLookupExpressionViewFactor implements IExpressionViewFactor {
\r
29 Label expressionLabel;
\r
34 private Point lastSelection = new Point(0,0);
\r
35 private Text lastSelectedText = expression;
\r
38 public void createView(Composite parent, Map<String, Object> data) {
\r
40 final String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
41 final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
43 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
\r
44 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
46 expressionLabel = new Label(parent, SWT.NONE);
\r
47 expressionLabel.setFont(FONT);
\r
48 expressionLabel.setText("With\nLookup");
\r
49 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
51 expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
52 expression.setFont(FONT);
\r
53 expression.setText(equation);
\r
54 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
57 expression.addFocusListener(new FocusAdapter() {
\r
60 public void focusLost(FocusEvent e) {
\r
61 lastSelection = expression.getSelection();
\r
62 lastSelectedText = expression;
\r
66 expression.addKeyListener(new KeyAdapter() {
\r
69 public void keyPressed(KeyEvent e) {
\r
70 if(e.keyCode == SWT.ESC) {
\r
71 expression.setText(equation);
\r
77 lookupLabel = new Label(parent, SWT.NONE);
\r
78 lookupLabel.setFont(FONT);
\r
79 lookupLabel.setText("Lookup\ntable");
\r
80 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
83 lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
84 lookup.setFont(FONT);
\r
85 lookup.setText(lookupTable);
\r
86 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
88 lookup.addFocusListener(new FocusAdapter() {
\r
91 public void focusLost(FocusEvent e) {
\r
92 lastSelection = lookup.getSelection();
\r
93 lastSelectedText = lookup;
\r
97 lookup.addKeyListener(new KeyAdapter() {
\r
100 public void keyPressed(KeyEvent e) {
\r
101 if(e.keyCode == SWT.ESC) {
\r
102 lookup.setText(lookupTable);
\r
111 public void readData(final Resource variable, Map<String, Object> data) {
\r
112 String[] results = null;
\r
113 if (variable != null && data.get("equation") == null) {
\r
115 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
118 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
119 String[] results = new String[2];
\r
120 SysdynResource sr = SysdynResource.getInstance(graph);
\r
121 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
122 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
123 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
124 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
133 } catch (DatabaseException e1) {
\r
134 e1.printStackTrace();
\r
136 data.put("equation", results[0]);
\r
137 data.put("lookup", results[1]);
\r
142 public void writeData(final Resource variable, Map<String, Object> data) {
\r
143 final String currentExpression = expression.getText();
\r
144 final String currentLookupTable = lookup.getText();
\r
146 if(currentExpression != null && currentLookupTable != null) {
\r
147 data.put("equation", currentExpression);
\r
148 data.put("lookup", currentLookupTable);
\r
149 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
151 public void perform(WriteGraph g)
\r
152 throws DatabaseException {
\r
153 SysdynResource sr = SysdynResource.getInstance(g);
\r
154 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
155 Builtins b = g.getBuiltins();
\r
156 if(expression != null) {
\r
157 g.deny(variable, sr.HasExpression);
\r
159 expression = g.newResource();
\r
160 g.claim(expression, b.InstanceOf, sr.WithLookupExpression);
\r
161 g.claim(variable, sr.HasExpression, expression);
\r
162 g.claimValue(expression, sr.HasEquation, currentExpression);
\r
163 g.claimValue(expression, sr.HasLookup, currentLookupTable);
\r
171 public void focus() {
\r
172 if(this.lastSelectedText != null) this.lastSelectedText.forceFocus();
\r
176 public void replaceSelection(String var) {
\r
177 if(this.lastSelectedText != null) {
\r
178 String oldExpression = lastSelectedText.getText();
\r
179 if(lastSelection == null)
\r
180 lastSelection = new Point(0,0);
\r
181 String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);
\r
182 lastSelectedText.setText(newExpression);
\r
183 lastSelectedText.setSelection(lastSelection.x + var.length());
\r
188 public void updateData(Map<String, Object> data) {
\r
189 if(this.expression != null && this.expression.getText() != null)
\r
190 data.put("equation", this.expression.getText());
\r
191 if(this.lookup != null && this.lookup.getText() != null)
\r
192 data.put("lookup", this.lookup.getText());
\r