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