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