]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
7a920ace347b73f75d9c688f87c8ca931c352458
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.expressions;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.Map;\r
5 \r
6 import org.eclipse.jface.layout.GridDataFactory;\r
7 import org.eclipse.jface.layout.GridLayoutFactory;\r
8 import org.eclipse.swt.SWT;\r
9 import org.eclipse.swt.events.FocusAdapter;\r
10 import org.eclipse.swt.events.FocusEvent;\r
11 import org.eclipse.swt.events.KeyAdapter;\r
12 import org.eclipse.swt.events.KeyEvent;\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     Label integralLabel;\r
30     Text integral;\r
31     Label equationLabel;\r
32     Text expression;\r
33     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         expression.addKeyListener(new KeyAdapter() {          \r
75 \r
76             @Override\r
77             public void keyPressed(KeyEvent e) {\r
78                 if(e.keyCode == SWT.ESC) {\r
79                     expression.setText(initialEquation);\r
80                 }                \r
81             }\r
82 \r
83         });\r
84 \r
85     }\r
86 \r
87     @Override\r
88     public void readData(final Resource variable, Map<String, Object> data) {\r
89         String initialEquation = null;\r
90 \r
91         if (variable != null && data.get("initialEquation") == null) {\r
92             try {\r
93                 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
94 \r
95                     @Override\r
96                     public String perform(ReadGraph graph) throws DatabaseException {\r
97                         SysdynResource sr = SysdynResource.getInstance(graph);\r
98                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
99                         if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {\r
100                             return graph.getRelatedValue(expression, sr.HasInitialEquation);\r
101                         } else {\r
102                             return "";\r
103                         }\r
104                     }\r
105 \r
106                 });\r
107             } catch (DatabaseException e1) {\r
108                 e1.printStackTrace();\r
109             }\r
110             data.put("initialEquation", initialEquation);\r
111         } \r
112 \r
113         data.put("integral", getIntegral(variable));\r
114     }\r
115 \r
116     @Override\r
117     public void writeData(final Resource variable, Map<String, Object> data) {\r
118         final String currentText = expression.getText();\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                     Builtins b = g.getBuiltins();\r
128                     if(expression != null) {\r
129                         g.deny(variable, sr.HasExpression);\r
130                     }\r
131                     expression = g.newResource();\r
132                     g.claim(expression, b.InstanceOf, sr.StockExpression);\r
133                     g.claim(variable, sr.HasExpression, expression);\r
134                     g.claimValue(expression, sr.HasInitialEquation, currentText);\r
135                 }\r
136 \r
137             });\r
138         }        \r
139     }\r
140 \r
141     private String getIntegral(final Resource variable) {\r
142         String integral = "";\r
143         try {\r
144             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
145 \r
146                 @Override\r
147                 public String perform(ReadGraph graph) throws DatabaseException {\r
148                     SysdynResource sr = SysdynResource.getInstance(graph);\r
149                     Builtins b = graph.getBuiltins();\r
150                     Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);\r
151                     Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);\r
152 \r
153                     StringBuilder builder = new StringBuilder();\r
154                     builder.append("");\r
155                     for (Resource r : heads) {\r
156                         if(graph.isInstanceOf(r, sr.Flow)) {\r
157                             Resource tail = graph.getPossibleObject(r, sr.HasTail);\r
158                             if(tail != null) {\r
159                                 Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
160                                 if (name != null) \r
161                                     builder.append(" + " + name);\r
162                             }\r
163                         }  \r
164                     }\r
165                     for (Resource r : tails) {\r
166                         if(graph.isInstanceOf(r, sr.Flow)) {\r
167                             Resource head = graph.getPossibleObject(r, sr.HasHead);\r
168                             if(head != null) {\r
169                                 Object name = graph.getPossibleRelatedValue(head, b.HasName);\r
170                                 if (name != null) \r
171                                     builder.append(" - " + name);\r
172                             }\r
173                         }  \r
174                     }\r
175                     if (builder.indexOf(" + ") == 0)\r
176                         builder.delete(0, 3);\r
177                     return builder.toString().trim();\r
178                 }\r
179 \r
180             });\r
181         } catch (DatabaseException e) {\r
182             e.printStackTrace();\r
183         }      \r
184         return integral;\r
185     }\r
186 \r
187     @Override\r
188     public void focus() {\r
189         if(this.expression != null) this.expression.forceFocus();\r
190     }\r
191 \r
192     @Override\r
193     public void replaceSelection(String var) {\r
194         if(this.expression != null) {\r
195             String oldExpression = expression.getText();\r
196             if(lastSelection == null)\r
197                 lastSelection = new Point(0,0);\r
198             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
199             expression.setText(newExpression);      \r
200             expression.setSelection(lastSelection.x + var.length());\r
201         }\r
202 \r
203     }\r
204 \r
205     @Override\r
206     public void updateData(Map<String, Object> data) {\r
207         if(this.expression != null && this.expression.getText() != null)\r
208             data.put("initialEquation", this.expression.getText());   \r
209         if(this.integral != null && this.integral.getText() != null)\r
210             data.put("integral", this.integral.getText());\r
211     }\r
212 }\r