]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e0c6212a2d1fac7d767af861ea90917f21f29c20
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.expressions;\r
2 \r
3 import java.util.Map;\r
4 \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.graphics.Point;\r
11 import org.eclipse.swt.widgets.Composite;\r
12 import org.eclipse.swt.widgets.Label;\r
13 import org.eclipse.swt.widgets.Text;\r
14 import org.simantics.db.Builtins;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.WriteRequest;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.request.Read;\r
21 import org.simantics.sysdyn.SysdynResource;\r
22 import org.simantics.ui.SimanticsUI;\r
23 \r
24 public class WithLookupExpressionViewFactor implements IExpressionViewFactor {\r
25 \r
26     \r
27     Label expressionLabel;\r
28     Text expression;\r
29     Label lookupLabel;\r
30     Text lookup;\r
31     \r
32     private Point lastSelection = new Point(0,0);\r
33     private Text lastSelectedText = expression;\r
34     \r
35     @Override\r
36     public void createView(Composite parent, Map<String, Object> data) {\r
37 \r
38         final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
39         final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
40         \r
41         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
42         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
43 \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
48 \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
53         \r
54         \r
55         expression.addFocusListener(new FocusAdapter() {\r
56 \r
57             @Override\r
58             public void focusLost(FocusEvent e) {\r
59                 lastSelection = expression.getSelection();\r
60                 lastSelectedText = expression;\r
61             }\r
62         });\r
63 \r
64         expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
65 \r
66         lookupLabel = new Label(parent, SWT.NONE);\r
67         lookupLabel.setFont(FONT);\r
68         lookupLabel.setText("Lookup\ntable");\r
69         GridDataFactory.fillDefaults().applyTo(lookupLabel);\r
70 \r
71 \r
72         lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
73         lookup.setFont(FONT);\r
74         lookup.setText(lookupTable);\r
75         GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);\r
76 \r
77         lookup.addFocusListener(new FocusAdapter() {\r
78 \r
79             @Override\r
80             public void focusLost(FocusEvent e) {\r
81                 lastSelection = lookup.getSelection();\r
82                 lastSelectedText = lookup;\r
83             }\r
84         });\r
85 \r
86         lookup.addKeyListener(new ExpressionFieldKeyListener(lookupTable));\r
87         \r
88     }\r
89 \r
90     @Override\r
91     public void readData(final Resource variable, Map<String, Object> data) {\r
92         String[] results = null;        \r
93         if (variable != null && data.get("equation") == null) {\r
94             try {\r
95                 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {\r
96 \r
97                     @Override\r
98                     public String[] perform(ReadGraph graph) throws DatabaseException {\r
99                         String[] results = new String[2];\r
100                         SysdynResource sr = SysdynResource.getInstance(graph);\r
101                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
102                         if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
103                             results[0] = graph.getRelatedValue(expression, sr.HasEquation);\r
104                             results[1] = graph.getRelatedValue(expression, sr.HasLookup);\r
105                         } else {\r
106                             results[0] = "";\r
107                             results[1] = "";\r
108                         }\r
109                         return results;\r
110                     }\r
111 \r
112                 });\r
113             } catch (DatabaseException e1) {\r
114                 e1.printStackTrace();\r
115             }\r
116             data.put("equation", results[0]);\r
117             data.put("lookup", results[1]);\r
118         }         \r
119     }\r
120 \r
121     @Override\r
122     public void writeData(final Resource variable, Map<String, Object> data) {\r
123         final String currentExpression = expression.getText();\r
124         final String currentLookupTable = lookup.getText();\r
125         \r
126         if(currentExpression != null && currentLookupTable != null) {\r
127             data.put("equation", currentExpression);\r
128             data.put("lookup", currentLookupTable);\r
129             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
130                 @Override\r
131                 public void perform(WriteGraph g)\r
132                 throws DatabaseException {\r
133                     SysdynResource sr = SysdynResource.getInstance(g);\r
134                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
135                     Builtins b = g.getBuiltins();\r
136                     if(expression != null) {\r
137                         g.deny(variable, sr.HasExpression);\r
138                     }\r
139                     expression = g.newResource();\r
140                     g.claim(expression, b.InstanceOf, sr.WithLookupExpression);\r
141                     g.claim(variable, sr.HasExpression, expression);\r
142                     g.claimValue(expression, sr.HasEquation, currentExpression);\r
143                     g.claimValue(expression, sr.HasLookup, currentLookupTable);\r
144                 }\r
145 \r
146             });\r
147         }        \r
148     }\r
149 \r
150     @Override\r
151     public void focus() {\r
152         if(this.lastSelectedText != null) this.lastSelectedText.forceFocus();\r
153     }\r
154 \r
155     @Override\r
156     public void replaceSelection(String var) {\r
157         if(this.lastSelectedText != null) {\r
158             String oldExpression = lastSelectedText.getText();\r
159             if(lastSelection == null)\r
160                 lastSelection = new Point(0,0);\r
161             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
162             lastSelectedText.setText(newExpression);\r
163             lastSelectedText.setSelection(lastSelection.x + var.length());\r
164         }\r
165     }\r
166 \r
167     @Override\r
168     public void updateData(Map<String, Object> data) {\r
169         if(this.expression != null && this.expression.getText() != null)\r
170             data.put("equation", this.expression.getText());   \r
171         if(this.lookup != null && this.lookup.getText() != null)\r
172             data.put("lookup", this.lookup.getText());        \r
173     }\r
174 }