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