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