1 package org.simantics.sysdyn.ui.equation.expressions;
\r
3 import java.util.Arrays;
\r
4 import java.util.List;
\r
5 import java.util.Map;
\r
7 import org.eclipse.jface.layout.GridDataFactory;
\r
8 import org.eclipse.jface.layout.GridLayoutFactory;
\r
9 import org.eclipse.swt.SWT;
\r
10 import org.eclipse.swt.events.FocusAdapter;
\r
11 import org.eclipse.swt.events.FocusEvent;
\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 private Label expressionLabel;
\r
30 private Text expression;
\r
31 private Label lookupLabel;
\r
32 private Text lookup;
\r
33 private Point lastSelection = new Point(0,0);
\r
34 private Text lastSelectedText = expression;
\r
37 public void createView(Composite parent, Map<String, Object> data) {
\r
39 final String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
40 final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
42 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
44 expressionLabel = new Label(parent, SWT.NONE);
\r
45 expressionLabel.setFont(FONT);
\r
46 expressionLabel.setText("With\nLookup");
\r
47 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
49 expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
50 expression.setFont(FONT);
\r
51 expression.setText(equation);
\r
52 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
55 expression.addFocusListener(new FocusAdapter() {
\r
58 public void focusLost(FocusEvent e) {
\r
59 lastSelection = expression.getSelection();
\r
60 lastSelectedText = expression;
\r
64 lookupLabel = new Label(parent, SWT.NONE);
\r
65 lookupLabel.setFont(FONT);
\r
66 lookupLabel.setText("Lookup\ntable");
\r
67 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
70 lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
71 lookup.setFont(FONT);
\r
72 lookup.setText(lookupTable);
\r
73 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
75 lookup.addFocusListener(new FocusAdapter() {
\r
78 public void focusLost(FocusEvent e) {
\r
79 lastSelection = lookup.getSelection();
\r
80 lastSelectedText = lookup;
\r
87 public void readData(final Resource variable, Map<String, Object> data) {
\r
88 String[] results = null;
\r
89 if (variable != null && data.get("equation") == null) {
\r
91 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
94 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
95 String[] results = new String[2];
\r
96 SysdynResource sr = SysdynResource.getInstance(graph);
\r
97 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
98 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
99 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
100 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
109 } catch (DatabaseException e1) {
\r
110 e1.printStackTrace();
\r
112 data.put("equation", results[0]);
\r
113 data.put("lookup", results[1]);
\r
118 public void writeData(final Resource variable, Map<String, Object> data) {
\r
119 final String currentExpression = expression.getText();
\r
120 final String currentLookupTable = lookup.getText();
\r
122 if(currentExpression != null && currentLookupTable != null) {
\r
123 data.put("equation", currentExpression);
\r
124 data.put("lookup", currentLookupTable);
\r
125 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
127 public void perform(WriteGraph g)
\r
128 throws DatabaseException {
\r
129 SysdynResource sr = SysdynResource.getInstance(g);
\r
130 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
131 Builtins b = g.getBuiltins();
\r
132 if(expression != null) {
\r
133 g.deny(variable, sr.HasExpression);
\r
135 expression = g.newResource();
\r
136 g.claim(expression, b.InstanceOf, sr.WithLookupExpression);
\r
137 g.claim(variable, sr.HasExpression, expression);
\r
138 g.claimValue(expression, sr.HasEquation, currentExpression);
\r
139 g.claimValue(expression, sr.HasLookup, currentLookupTable);
\r
147 public void focus() {
\r
148 if(this.lastSelectedText != null) this.lastSelectedText.forceFocus();
\r
152 public void replaceSelection(String var) {
\r
153 if(this.lastSelectedText != null) {
\r
154 String oldExpression = lastSelectedText.getText();
\r
155 if(lastSelection == null)
\r
156 lastSelection = new Point(0,0);
\r
157 String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);
\r
158 lastSelectedText.setText(newExpression);
\r
159 lastSelectedText.setSelection(lastSelection.x + var.length());
\r
164 public void updateData(Map<String, Object> data) {
\r
165 if(this.expression != null && this.expression.getText() != null)
\r
166 data.put("equation", this.expression.getText());
\r
167 if(this.lookup != null && this.lookup.getText() != null)
\r
168 data.put("lookup", this.lookup.getText());
\r
172 public List<Text> getExpressionFields() {
\r
173 return Arrays.asList(this.expression, this.lookup);
\r