]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ce8cdb81b000de98800b1ba6ed8a5b038f87d4af
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.text.BadLocationException;\r
21 import org.eclipse.jface.text.IDocument;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.custom.VerifyKeyListener;\r
24 import org.eclipse.swt.events.FocusAdapter;\r
25 import org.eclipse.swt.events.FocusEvent;\r
26 import org.eclipse.swt.events.FocusListener;\r
27 import org.eclipse.swt.events.KeyListener;\r
28 import org.eclipse.swt.events.ModifyListener;\r
29 import org.eclipse.swt.events.SelectionAdapter;\r
30 import org.eclipse.swt.events.SelectionEvent;\r
31 import org.eclipse.swt.graphics.Point;\r
32 import org.eclipse.swt.widgets.Composite;\r
33 import org.eclipse.swt.widgets.Label;\r
34 import org.eclipse.swt.widgets.Spinner;\r
35 import org.eclipse.swt.widgets.Table;\r
36 import org.simantics.databoard.Bindings;\r
37 import org.simantics.db.ReadGraph;\r
38 import org.simantics.db.Resource;\r
39 import org.simantics.db.VirtualGraph;\r
40 import org.simantics.db.WriteGraph;\r
41 import org.simantics.db.common.request.WriteRequest;\r
42 import org.simantics.db.common.utils.ListUtils;\r
43 import org.simantics.db.exception.DatabaseException;\r
44 import org.simantics.db.request.Read;\r
45 import org.simantics.layer0.Layer0;\r
46 import org.simantics.layer0.utils.direct.GraphUtils;\r
47 import org.simantics.sysdyn.SysdynResource;\r
48 import org.simantics.ui.SimanticsUI;\r
49 \r
50 /**\r
51  * IExpression for displaying fields of DelayExpression\r
52  * @author Teemu Lempinen\r
53  * @author Tuomas Miettinen\r
54  *\r
55  */\r
56 public class DelayExpression implements IExpression {\r
57 \r
58     private ExpressionField equation, delayTime, initialValue;\r
59     private ExpressionField lastSelectedText = equation;\r
60     private Spinner order;\r
61     private Resource expression;\r
62 \r
63     /**\r
64      * Creates a new DelayExpression\r
65      * @param expression\r
66      */\r
67     public DelayExpression(Resource expression) {\r
68         this.expression = expression;\r
69     }\r
70 \r
71     /**\r
72      * Displays the fields for delayExpression\r
73      */\r
74     @Override\r
75     public void createExpressionFields(Composite parent, final Map<String, Object> data, Table allowedVariables) {\r
76         // Get possible existing data\r
77         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
78         String eq = data.get("equation") != null ? (String)data.get("equation") : "";\r
79         String dt = data.get("delayTime") != null ? (String)data.get("delayTime") : "";\r
80         String iv = data.get("initialValue") != null ? (String)data.get("initialValue") : "";\r
81         int o = data.get("order") != null ? (Integer)data.get("order") : 3;\r
82 \r
83         Label l = new Label(parent, SWT.NONE);\r
84         l.setText("expression");\r
85 \r
86         // Equation that is delayed\r
87         equation = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);\r
88         equation.setExpression(eq);\r
89         GridDataFactory.fillDefaults().grab(true, true).applyTo(equation);\r
90         equation.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
91 \r
92             @Override\r
93             public void focusLost(FocusEvent e) {\r
94                 lastSelectedText = equation;\r
95             }\r
96         });\r
97 \r
98         l = new Label(parent, SWT.NONE);\r
99         l.setText("delay time");\r
100 \r
101         // How much the equation is delayed\r
102         delayTime = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);\r
103         delayTime.setExpression(dt);\r
104         GridDataFactory.fillDefaults().grab(true, true).applyTo(delayTime);\r
105 \r
106         delayTime.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
107 \r
108             @Override\r
109             public void focusLost(FocusEvent e) {\r
110                 lastSelectedText = delayTime;\r
111             }\r
112         });\r
113 \r
114         l = new Label(parent, SWT.NONE);\r
115         l.setText("initial value");\r
116 \r
117         // What is the initial value of the delay (empty == same as equation)\r
118         initialValue = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);\r
119         initialValue.setExpression(iv);\r
120         GridDataFactory.fillDefaults().grab(true, true).applyTo(initialValue);\r
121 \r
122         initialValue.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
123 \r
124             @Override\r
125             public void focusLost(FocusEvent e) {\r
126                 lastSelectedText = initialValue;\r
127             }\r
128         });\r
129 \r
130 \r
131         l = new Label(parent, SWT.NONE);\r
132         l.setText("order");\r
133 \r
134         // The order of the delay (default == 3)\r
135         order = new Spinner(parent, SWT.BORDER);\r
136         order.setMinimum(1);\r
137         order.setSelection(o);\r
138         order.addSelectionListener(new SelectionAdapter() {\r
139             @Override\r
140             public void widgetSelected(SelectionEvent e) {\r
141                 save(expression, data);\r
142             }\r
143         });\r
144         GridDataFactory.fillDefaults().applyTo(order);\r
145     }\r
146 \r
147     @Override\r
148     public void focus() {\r
149         lastSelectedText.setFocus();\r
150     }\r
151 \r
152     @Override\r
153     public List<ExpressionField> getExpressionFields() {\r
154         return Arrays.asList(equation, delayTime, initialValue);\r
155     }\r
156 \r
157     @Override\r
158     public void readData(final Resource expression, Map<String, Object> data) {\r
159         class Auxiliary {\r
160             String equation, delayTime, initialValue;\r
161             Integer order;\r
162         }\r
163 \r
164         Auxiliary results = null;\r
165 \r
166         try {\r
167             results = SimanticsUI.getSession().syncRequest(new Read<Auxiliary>() {\r
168 \r
169                 @Override\r
170                 public Auxiliary perform(ReadGraph graph) throws DatabaseException {\r
171                     Auxiliary results = new Auxiliary();\r
172                     SysdynResource sr = SysdynResource.getInstance(graph);\r
173                     if (expression != null && graph.isInstanceOf(expression, sr.DelayExpression)) {\r
174                         results.equation = graph.getPossibleRelatedValue(expression, sr.DelayExpression_expression);\r
175                         results.delayTime = graph.getPossibleRelatedValue(expression, sr.DelayExpression_delayTime);\r
176                         results.initialValue = graph.getPossibleRelatedValue(expression, sr.DelayExpression_initialValue);\r
177                         results.order = graph.getPossibleRelatedValue(expression, sr.DelayExpression_order);\r
178                     } else {\r
179                         results.equation = "";\r
180                         results.delayTime = "";\r
181                         results.order = 1;\r
182                     }\r
183                     return results;\r
184                 }\r
185             });\r
186         } catch (DatabaseException e1) {\r
187             e1.printStackTrace();\r
188         }\r
189         if(results.equation != null)\r
190             data.put("equation", results.equation);\r
191         if(results.delayTime != null)\r
192             data.put("delayTime", results.delayTime);\r
193         if(results.initialValue != null)\r
194             data.put("initialValue", results.initialValue);\r
195         if(results.order != null)\r
196             data.put("order", results.order);\r
197     }\r
198 \r
199     @Override\r
200     public void replaceSelection(String var) {\r
201         if(lastSelectedText != null) {\r
202             IDocument doc = lastSelectedText.getDocument();\r
203             try {\r
204                 Point selection = lastSelectedText.getSelection();\r
205                 doc.replace(selection.x, selection.y, var);\r
206                 lastSelectedText.setSelection(selection.x + var.length());\r
207             } catch (BadLocationException e) {\r
208                 e.printStackTrace();\r
209             }\r
210         }    \r
211     }\r
212 \r
213     @Override\r
214     public void save(Resource expr, Map<String, Object> data) {\r
215         this.expression = expr;\r
216         final String currentEquation = this.equation.getExpression();\r
217         final String currentDelayTime =  this.delayTime.getExpression();\r
218         final String currentInitialValue = this.initialValue.getExpression();\r
219         final Integer currentOrder = this.order.getSelection();\r
220 \r
221         String oldEquation = (String)data.get("equation");\r
222         String oldDelayTime = (String)data.get("delayTime");\r
223         String oldInitialValue = (String)data.get("initialValue");\r
224         Integer oldOrder = (Integer)data.get("order");\r
225 \r
226         if((oldEquation == null || oldDelayTime == null || oldOrder == null || oldInitialValue == null) ||\r
227                 !oldEquation.equals(currentEquation) || !oldDelayTime.equals(currentDelayTime) ||\r
228                 !oldOrder.equals(currentOrder) || !oldInitialValue.equals(currentInitialValue)) {\r
229             // old value was null or value has changed -> Do save\r
230 \r
231             data.putAll(data);\r
232             data.put("equation", currentEquation);\r
233             data.put("delayTime", currentDelayTime);\r
234             data.put("initialValue", currentInitialValue);\r
235             data.put("order", currentOrder);\r
236 \r
237             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
238                 @Override\r
239                 public void perform(WriteGraph g)\r
240                         throws DatabaseException {\r
241                     SysdynResource sr = SysdynResource.getInstance(g);\r
242                     Layer0 l0 = Layer0.getInstance(g);\r
243 \r
244                     if(!g.isInstanceOf(expression, sr.DelayExpression)) {\r
245                         // Create a new DelayExpression, if the old expression was something else\r
246 //                        Resource ownerList = OrderedSetUtils.getSingleOwnerList(g, expression);\r
247                         final Resource newExpression = GraphUtils.create2(g, sr.DelayExpression);\r
248                         String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);\r
249                         if(arrayRange != null)\r
250                             g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);\r
251 \r
252 //                        final Resource variable = g.getSingleObject(ownerList, sr.Variable_expressions_Inverse);\r
253                         final Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
254                         Resource ownerList = g.getPossibleObject(expression, sr.Variable_expressionList);\r
255 //                        OrderedSetUtils.replace(g, ownerList, expression, newExpression);\r
256                         ListUtils.replace(g, ownerList, expression, newExpression);\r
257                         g.deny(expression, l0.PartOf);\r
258                         g.claim(newExpression, l0.PartOf, variable);\r
259 \r
260                         VirtualGraph runtime = g.getService(VirtualGraph.class);\r
261                         g.syncRequest(new WriteRequest(runtime) {\r
262                             @Override\r
263                             public void perform(WriteGraph graph) throws DatabaseException {\r
264                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
265                                 if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
266                                     graph.deny(variable, sr.IndependentVariable_activeExpression);\r
267                                 graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);\r
268                             }\r
269                         }\r
270                                 );\r
271                         expression = newExpression;\r
272                     }\r
273                     \r
274                     // Claim values\r
275                     g.claimLiteral(expression, sr.DelayExpression_expression, currentEquation);\r
276                     g.claimLiteral(expression, sr.DelayExpression_delayTime, currentDelayTime);\r
277                     g.claimLiteral(expression, sr.DelayExpression_initialValue, currentInitialValue);\r
278                     g.claimLiteral(expression, sr.DelayExpression_order, currentOrder);\r
279                 }\r
280             });\r
281         }\r
282 \r
283     }\r
284 \r
285     @Override\r
286     public void updateData(Map<String, Object> data) {\r
287         if(this.equation != null && this.equation.getExpression() != null)\r
288             data.put("equation", this.equation.getExpression());\r
289         if(this.delayTime != null && this.delayTime.getExpression() != null)\r
290             data.put("delayTime", this.delayTime.getExpression());   \r
291         if(this.initialValue != null && this.initialValue.getExpression() != null)\r
292             data.put("initialValue", this.initialValue.getExpression());   \r
293         if(this.order != null)\r
294             data.put("order", this.order.getSelection());   \r
295     }\r
296 \r
297     @Override\r
298     public void addKeyListener(KeyListener listener) {\r
299         this.equation.getSourceViewer().getTextWidget().addKeyListener(listener);\r
300         this.delayTime.getSourceViewer().getTextWidget().addKeyListener(listener);\r
301         this.initialValue.getSourceViewer().getTextWidget().addKeyListener(listener);\r
302     }\r
303 \r
304     @Override\r
305     public void addModifyListener(ModifyListener listener) {\r
306         this.equation.getSourceViewer().getTextWidget().addModifyListener(listener);\r
307         this.delayTime.getSourceViewer().getTextWidget().addModifyListener(listener);\r
308         this.initialValue.getSourceViewer().getTextWidget().addModifyListener(listener);\r
309     }\r
310 \r
311     @Override\r
312     public void addFocusListener(FocusListener listener) {\r
313         this.equation.getSourceViewer().getTextWidget().addFocusListener(listener);\r
314         this.delayTime.getSourceViewer().getTextWidget().addFocusListener(listener);\r
315         this.initialValue.getSourceViewer().getTextWidget().addFocusListener(listener);\r
316     }\r
317 \r
318     @Override\r
319     public void addVerifyKeyListener(VerifyKeyListener listener) {\r
320         this.equation.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
321         this.delayTime.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
322         this.initialValue.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
323     }\r
324 \r
325 }\r