]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
f9be16055bdec1e9f8a1bb519e7dda05c7ad4065
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.Collection;\r
5 import java.util.List;\r
6 import java.util.Map;\r
7 \r
8 import org.eclipse.jface.layout.GridDataFactory;\r
9 import org.eclipse.jface.layout.GridLayoutFactory;\r
10 import org.eclipse.jface.text.BadLocationException;\r
11 import org.eclipse.jface.text.IDocument;\r
12 import org.eclipse.swt.SWT;\r
13 import org.eclipse.swt.events.FocusAdapter;\r
14 import org.eclipse.swt.events.FocusEvent;\r
15 import org.eclipse.swt.events.FocusListener;\r
16 import org.eclipse.swt.events.KeyListener;\r
17 import org.eclipse.swt.events.ModifyListener;\r
18 import org.eclipse.swt.graphics.Point;\r
19 import org.eclipse.swt.widgets.Composite;\r
20 import org.eclipse.swt.widgets.Label;\r
21 import org.eclipse.swt.widgets.Text;\r
22 import org.simantics.db.Builtins;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.WriteGraph;\r
26 import org.simantics.db.common.request.WriteRequest;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.request.Read;\r
29 import org.simantics.sysdyn.SysdynResource;\r
30 import org.simantics.ui.SimanticsUI;\r
31 \r
32 public class StockExpression implements IExpression {\r
33 \r
34     private Text integral;\r
35     private ExpressionField expression;\r
36     private Point caretPosition; // Bug or something in source viewer's text widget. Cursor goes to 0, 0 on focus lost\r
37     \r
38     @Override\r
39     public void createExpressionFields(Composite parent, Map<String, Object> data) {\r
40         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
41         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
42         String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
43 \r
44         \r
45         Label label = new Label(parent, SWT.NONE);\r
46         label.setText("Integral");\r
47         \r
48         integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
49         integral.setEditable(false);\r
50         integral.setText(integralEquation);\r
51         GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);\r
52         \r
53         \r
54         label = new Label(parent, SWT.NONE);\r
55         label.setText("Initial\nValue");\r
56         \r
57         expression = new ExpressionField(parent, SWT.BORDER);\r
58         expression.setExpression(initialEquation);\r
59         \r
60         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
61         \r
62         expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
63 \r
64             @Override\r
65             public void focusLost(FocusEvent e) {\r
66                 caretPosition = expression.getSelection();\r
67             }\r
68         });\r
69         \r
70     }\r
71 \r
72     @Override\r
73     public void focus() {\r
74         if(this.expression != null && caretPosition != null) {\r
75             this.expression.focus();\r
76             expression.getSourceViewer().getTextWidget().setSelection(caretPosition.x, caretPosition.x + caretPosition.y);\r
77         }\r
78         \r
79     }\r
80 \r
81     @Override\r
82     public List<ExpressionField> getExpressionFields() {\r
83         return Arrays.asList(this.expression);\r
84     }\r
85 \r
86     @Override\r
87     public void readData(final Resource variable, Map<String, Object> data) {\r
88         String initialEquation = null;\r
89 \r
90         if (variable != null && data.get("initialEquation") == null) {\r
91             try {\r
92                 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
93 \r
94                     @Override\r
95                     public String perform(ReadGraph graph) throws DatabaseException {\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.StockExpression)) {\r
99                             return graph.getRelatedValue(expression, sr.HasInitialEquation);\r
100                         } else {\r
101                             return "";\r
102                         }\r
103                     }\r
104 \r
105                 });\r
106             } catch (DatabaseException e1) {\r
107                 e1.printStackTrace();\r
108             }\r
109             data.put("initialEquation", initialEquation);\r
110         }\r
111 \r
112         data.put("integral", getIntegral(variable));\r
113         \r
114     }\r
115 \r
116     @Override\r
117     public void replaceSelection(String var) {\r
118         if(expression != null) {\r
119             IDocument doc = expression.getDocument();\r
120             try {\r
121                 Point selection = expression.getSelection();\r
122                 doc.replace(selection.x, selection.y, var);\r
123                 expression.setSelection(selection.x + var.length());\r
124             } catch (BadLocationException e) {\r
125                 e.printStackTrace();\r
126             }\r
127         }\r
128     }\r
129 \r
130     @Override\r
131     public void save(final Resource variable, Map<String, Object> data) {\r
132         final String currentText = expression.getExpression();\r
133         if(currentText != null) {\r
134             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
135 \r
136                 @Override\r
137                 public void perform(WriteGraph g)\r
138                 throws DatabaseException {\r
139                     SysdynResource sr = SysdynResource.getInstance(g);\r
140                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
141                     Builtins b = g.getBuiltins();\r
142                     if(expression != null) {\r
143                         g.deny(variable, sr.HasExpression);\r
144                     }\r
145                     expression = g.newResource();\r
146                     g.claim(expression, b.InstanceOf, null, sr.StockExpression);\r
147                     g.claim(variable, sr.HasExpression, expression);\r
148                     g.claimValue(expression, sr.HasInitialEquation, currentText);\r
149                 }\r
150 \r
151             });\r
152         }\r
153         expression.setExpression(currentText);\r
154     }\r
155 \r
156     @Override\r
157     public void updateData(Map<String, Object> data) {\r
158         if(this.expression != null && this.expression.getExpression() != null)\r
159             data.put("initialEquation", this.expression.getExpression());\r
160         if(this.integral != null && this.integral.getText() != null)\r
161             data.put("integral", this.integral.getText());\r
162     }\r
163     \r
164     \r
165     private String getIntegral(final Resource variable) {\r
166         String integral = "";\r
167         try {\r
168             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
169 \r
170                 @Override\r
171                 public String perform(ReadGraph graph) throws DatabaseException {\r
172                     SysdynResource sr = SysdynResource.getInstance(graph);\r
173                     Builtins b = graph.getBuiltins();\r
174                     Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);\r
175                     Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);\r
176 \r
177                     StringBuilder builder = new StringBuilder();\r
178                     builder.append("");\r
179                     for (Resource r : heads) {\r
180                         if(graph.isInstanceOf(r, sr.Flow)) {\r
181                             Resource tail = graph.getPossibleObject(r, sr.HasTail);\r
182                             if(tail != null) {\r
183                                 Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
184                                 if (name != null) \r
185                                     builder.append(" + " + name);\r
186                                 \r
187                             }\r
188                         }\r
189                     }\r
190                     for (Resource r : tails) {\r
191                         if(graph.isInstanceOf(r, sr.Flow)) {\r
192                             Resource head = graph.getPossibleObject(r, sr.HasHead);\r
193                             if(head != null) {\r
194                                 Object name = graph.getPossibleRelatedValue(head, b.HasName);\r
195                                 if (name != null)\r
196                                     builder.append(" - " + name);\r
197                             }\r
198                         }\r
199                     }\r
200                     if (builder.indexOf(" + ") == 0)\r
201                         builder.delete(0, 3);\r
202                     return builder.toString().trim();\r
203                 }\r
204 \r
205             });\r
206         } catch (DatabaseException e) {\r
207             e.printStackTrace();\r
208         }\r
209         return integral;\r
210     }\r
211 \r
212     @Override\r
213     public void addKeyListener(KeyListener listener) {\r
214         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
215         \r
216     }\r
217 \r
218     @Override\r
219     public void addModifyListener(ModifyListener listener) {\r
220         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
221         \r
222     }\r
223     \r
224     @Override\r
225     public void addFocusListener(FocusListener listener) {\r
226         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
227     }\r
228 \r
229 }\r