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 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
\r
43 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
45 expressionLabel = new Label(parent, SWT.NONE);
\r
46 expressionLabel.setFont(FONT);
\r
47 expressionLabel.setText("With\nLookup");
\r
48 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
50 expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
51 expression.setFont(FONT);
\r
52 expression.setText(equation);
\r
53 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
56 expression.addFocusListener(new FocusAdapter() {
\r
59 public void focusLost(FocusEvent e) {
\r
60 lastSelection = expression.getSelection();
\r
61 lastSelectedText = expression;
\r
65 lookupLabel = new Label(parent, SWT.NONE);
\r
66 lookupLabel.setFont(FONT);
\r
67 lookupLabel.setText("Lookup\ntable");
\r
68 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
71 lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
72 lookup.setFont(FONT);
\r
73 lookup.setText(lookupTable);
\r
74 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
76 lookup.addFocusListener(new FocusAdapter() {
\r
79 public void focusLost(FocusEvent e) {
\r
80 lastSelection = lookup.getSelection();
\r
81 lastSelectedText = lookup;
\r
88 public void readData(final Resource variable, Map<String, Object> data) {
\r
89 String[] results = null;
\r
90 if (variable != null && data.get("equation") == null) {
\r
92 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
95 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
96 String[] results = new String[2];
\r
97 SysdynResource sr = SysdynResource.getInstance(graph);
\r
98 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
99 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
100 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
101 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
110 } catch (DatabaseException e1) {
\r
111 e1.printStackTrace();
\r
113 data.put("equation", results[0]);
\r
114 data.put("lookup", results[1]);
\r
119 public void writeData(final Resource variable, Map<String, Object> data) {
\r
120 final String currentExpression = expression.getText();
\r
121 final String currentLookupTable = lookup.getText();
\r
123 if(currentExpression != null && currentLookupTable != null) {
\r
124 data.put("equation", currentExpression);
\r
125 data.put("lookup", currentLookupTable);
\r
126 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
128 public void perform(WriteGraph g)
\r
129 throws DatabaseException {
\r
130 SysdynResource sr = SysdynResource.getInstance(g);
\r
131 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
132 Builtins b = g.getBuiltins();
\r
133 if(expression != null) {
\r
134 g.deny(variable, sr.HasExpression);
\r
136 expression = g.newResource();
\r
137 g.claim(expression, b.InstanceOf, sr.WithLookupExpression);
\r
138 g.claim(variable, sr.HasExpression, expression);
\r
139 g.claimValue(expression, sr.HasEquation, currentExpression);
\r
140 g.claimValue(expression, sr.HasLookup, currentLookupTable);
\r
148 public void focus() {
\r
149 if(this.lastSelectedText != null) this.lastSelectedText.forceFocus();
\r
153 public void replaceSelection(String var) {
\r
154 if(this.lastSelectedText != null) {
\r
155 String oldExpression = lastSelectedText.getText();
\r
156 if(lastSelection == null)
\r
157 lastSelection = new Point(0,0);
\r
158 String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);
\r
159 lastSelectedText.setText(newExpression);
\r
160 lastSelectedText.setSelection(lastSelection.x + var.length());
\r
165 public void updateData(Map<String, Object> data) {
\r
166 if(this.expression != null && this.expression.getText() != null)
\r
167 data.put("equation", this.expression.getText());
\r
168 if(this.lookup != null && this.lookup.getText() != null)
\r
169 data.put("lookup", this.lookup.getText());
\r
173 public List<Text> getExpressionFields() {
\r
174 return Arrays.asList(this.expression, this.lookup);
\r