]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
398858eeb771f44a1963ad9ab81dd749c4aef3ba
[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.text.BadLocationException;\r
10 import org.eclipse.jface.text.IDocument;\r
11 import org.eclipse.swt.SWT;\r
12 import org.eclipse.swt.events.FocusListener;\r
13 import org.eclipse.swt.events.KeyListener;\r
14 import org.eclipse.swt.events.ModifyListener;\r
15 import org.eclipse.swt.graphics.Point;\r
16 import org.eclipse.swt.widgets.Composite;\r
17 import org.eclipse.swt.widgets.Label;\r
18 import org.eclipse.swt.widgets.Text;\r
19 import org.simantics.db.Builtins;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteGraph;\r
23 import org.simantics.db.common.request.WriteRequest;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.request.Read;\r
26 import org.simantics.sysdyn.SysdynResource;\r
27 import org.simantics.sysdyn.ui.properties.EquationTab;\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         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
38         String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
39 \r
40         \r
41         Label label = new Label(parent, SWT.NONE);\r
42         label.setFont(EquationTab.FONT);\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.setFont(EquationTab.FONT);\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.setFont(EquationTab.FONT);\r
54         label.setText("Initial\nValue");\r
55         \r
56         expression = new ExpressionField(parent, SWT.BORDER);\r
57         expression.setExpression(initialEquation);\r
58         \r
59         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
60         \r
61     }\r
62 \r
63     @Override\r
64     public void focus() {\r
65         if(this.expression != null) this.expression.focus();\r
66         \r
67     }\r
68 \r
69     @Override\r
70     public List<ExpressionField> getExpressionFields() {\r
71         return Arrays.asList(this.expression);\r
72     }\r
73 \r
74     @Override\r
75     public void readData(final Resource variable, Map<String, Object> data) {\r
76         String initialEquation = null;\r
77 \r
78         if (variable != null && data.get("initialEquation") == null) {\r
79             try {\r
80                 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
81 \r
82                     @Override\r
83                     public String perform(ReadGraph graph) throws DatabaseException {\r
84                         SysdynResource sr = SysdynResource.getInstance(graph);\r
85                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
86                         if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {\r
87                             return graph.getRelatedValue(expression, sr.HasInitialEquation);\r
88                         } else {\r
89                             return "";\r
90                         }\r
91                     }\r
92 \r
93                 });\r
94             } catch (DatabaseException e1) {\r
95                 e1.printStackTrace();\r
96             }\r
97             data.put("initialEquation", initialEquation);\r
98         }\r
99 \r
100         data.put("integral", getIntegral(variable));\r
101         \r
102     }\r
103 \r
104     @Override\r
105     public void replaceSelection(String var) {\r
106         if(expression != null) {\r
107             IDocument doc = expression.getDocument();\r
108             try {\r
109                 Point selection = expression.getSelection();\r
110                 doc.replace(selection.x, selection.y, var);\r
111                 expression.setSelection(selection.x + var.length());\r
112             } catch (BadLocationException e) {\r
113                 e.printStackTrace();\r
114             }\r
115         }\r
116     }\r
117 \r
118     @Override\r
119     public void save(final Resource variable, Map<String, Object> data) {\r
120         final String currentText = expression.getExpression();\r
121         if(currentText != null) {\r
122             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
123 \r
124                 @Override\r
125                 public void perform(WriteGraph g)\r
126                 throws DatabaseException {\r
127                     SysdynResource sr = SysdynResource.getInstance(g);\r
128                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
129                     Builtins b = g.getBuiltins();\r
130                     if(expression != null) {\r
131                         g.deny(variable, sr.HasExpression);\r
132                     }\r
133                     expression = g.newResource();\r
134                     g.claim(expression, b.InstanceOf, null, sr.StockExpression);\r
135                     g.claim(variable, sr.HasExpression, expression);\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                     for (Resource r : tails) {\r
178                         if(graph.isInstanceOf(r, sr.Flow)) {\r
179                             Resource head = graph.getPossibleObject(r, sr.HasHead);\r
180                             if(head != null) {\r
181                                 Object name = graph.getPossibleRelatedValue(head, b.HasName);\r
182                                 if (name != null)\r
183                                     builder.append(" - " + name);\r
184                             }\r
185                         }\r
186                     }\r
187                     if (builder.indexOf(" + ") == 0)\r
188                         builder.delete(0, 3);\r
189                     return builder.toString().trim();\r
190                 }\r
191 \r
192             });\r
193         } catch (DatabaseException e) {\r
194             e.printStackTrace();\r
195         }\r
196         return integral;\r
197     }\r
198 \r
199     @Override\r
200     public void addKeyListener(KeyListener listener) {\r
201         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
202         \r
203     }\r
204 \r
205     @Override\r
206     public void addModifyListener(ModifyListener listener) {\r
207         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
208         \r
209     }\r
210     \r
211     @Override\r
212     public void addFocusListener(FocusListener listener) {\r
213         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
214     }\r
215 \r
216 }\r