]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0ddbebc54434d93fb4bec9e6bce62734cd18a052
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 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.FocusListener;\r
25 import org.eclipse.swt.events.KeyListener;\r
26 import org.eclipse.swt.events.ModifyListener;\r
27 import org.eclipse.swt.graphics.Point;\r
28 import org.eclipse.swt.widgets.Composite;\r
29 import org.eclipse.swt.widgets.Label;\r
30 import org.eclipse.swt.widgets.Table;\r
31 import org.simantics.databoard.Bindings;\r
32 import org.simantics.db.ReadGraph;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.WriteGraph;\r
35 import org.simantics.db.common.request.WriteRequest;\r
36 import org.simantics.db.common.utils.ListUtils;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.request.Read;\r
39 import org.simantics.db.service.VirtualGraphSupport;\r
40 import org.simantics.layer0.Layer0;\r
41 import org.simantics.layer0.utils.direct.GraphUtils;\r
42 import org.simantics.sysdyn.SysdynResource;\r
43 import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
44 import org.simantics.ui.SimanticsUI;\r
45 \r
46 /**\r
47  * Basic expression that is used with parameter, auxiliary and constant\r
48  * @author Teemu Lempinen\r
49  * @author Tuomas Miettinen\r
50  *\r
51  */\r
52 public class BasicExpression implements IExpression {\r
53 \r
54     protected ExpressionField expression;\r
55     protected Resource expressionType;\r
56     protected ExpressionWidgetInput input;\r
57     \r
58     public BasicExpression(ExpressionWidgetInput input) {\r
59         this.input = input;\r
60     }\r
61     \r
62     @Override\r
63     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
64         // Create the single field\r
65         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
66         String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
67 \r
68         Label l = new Label(parent, SWT.NONE);\r
69         l.setText("=");\r
70 \r
71         expression = new ExpressionField(parent, SWT.BORDER, null, false, input);\r
72         expression.setExpression(equation);\r
73         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
74 \r
75     }\r
76 \r
77     @Override\r
78     public void focus() {\r
79         this.expression.focus();\r
80 \r
81     }\r
82 \r
83     @Override\r
84     public List<ExpressionField> getExpressionFields() {\r
85         return Arrays.asList(this.expression);\r
86     }\r
87 \r
88     @Override\r
89     public void readData(final Resource expression, Map<String, Object> data) {\r
90         String equation = null;\r
91         if (expression != null && data.get("equation") == null) {\r
92             try {\r
93                 equation = 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                         if (expression != null) {\r
99                             String equation = graph.getPossibleRelatedValue(expression, sr.Expression_equation);\r
100                             if(equation != null)\r
101                                 return equation;\r
102                         }\r
103                         \r
104                         return "";\r
105                         \r
106                     }\r
107 \r
108                 });\r
109             } catch (DatabaseException e1) {\r
110                 e1.printStackTrace();\r
111             }\r
112             data.put("equation", equation);\r
113         }\r
114     }\r
115 \r
116     @Override\r
117     public void replaceSelection(String var) {\r
118         if(expression != null) {\r
119             IDocument doc = expression.getDocument();\r
120             try {\r
121                 Point selection = expression.getSelection();\r
122                 doc.replace(selection.x, selection.y, var);\r
123                 expression.setSelection(selection.x + var.length());\r
124             } catch (BadLocationException e) {\r
125                 e.printStackTrace();\r
126             }\r
127         }\r
128     }\r
129 \r
130     @Override\r
131     public void save(final Resource expression, Map<String, Object> data) {\r
132         final String currentText = this.expression.getExpression();\r
133         final String oldEquation = (String)data.get("equation");\r
134         \r
135         if(currentText != null && oldEquation != null && currentText.equals(oldEquation))\r
136             return; // Nothing is changed, no need to save\r
137         \r
138         if(oldEquation == null || \r
139                 (currentText != null && expressionType != null)) {\r
140             data.put("equation", currentText);\r
141             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
142                 @Override\r
143                 public void perform(WriteGraph g)\r
144                 throws DatabaseException {\r
145                     SysdynResource sr = SysdynResource.getInstance(g);\r
146                     Layer0 l0 = Layer0.getInstance(g);\r
147 \r
148                     // Force change to parameter, if the equation is a parameter\r
149                     if(ExpressionUtils.isParameter(currentText)) {\r
150                         if(!expressionType.equals(sr.ConstantExpression))\r
151                                 expressionType = sr.ParameterExpression;\r
152                     } else {\r
153                         expressionType = sr.NormalExpression;\r
154                     }\r
155                     \r
156                     // If nothing has changed, do nothing\r
157                     if (oldEquation != null \r
158                             && expression != null \r
159                             && g.isInstanceOf(expression, expressionType) \r
160                             && currentText.equals(oldEquation)) {\r
161                         return;\r
162                     }\r
163                     \r
164                     // If the current expression type is different than the target expression type, create a new expression\r
165                     if(!g.isInstanceOf(expression, expressionType)) {\r
166 \r
167                         final Resource newExpression = GraphUtils.create2(g, expressionType, \r
168                                         sr.Expression_equation, currentText);\r
169                         String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);\r
170                         if(arrayRange != null)\r
171                                 g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);\r
172                         \r
173                         final Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
174                         if(variable == null)\r
175                             return;\r
176                         Resource ownerList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
177                         if(ownerList == null)\r
178                             return;\r
179                         \r
180                         ListUtils.replace(g, ownerList, expression, newExpression);\r
181                         \r
182                         g.deny(expression, l0.PartOf);\r
183                         \r
184                         g.claim(newExpression, l0.PartOf, variable);\r
185                         \r
186                         \r
187                         VirtualGraphSupport support = g.getService(VirtualGraphSupport.class);\r
188                                                 g.syncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {\r
189                                                         @Override\r
190                                                         public void perform(WriteGraph graph) throws DatabaseException {\r
191                                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
192                                                                 if(variable != null) {\r
193                                                                         if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
194                                                                                 graph.deny(variable, sr.IndependentVariable_activeExpression);\r
195                                                                         graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);\r
196                                                                 }\r
197                                                         }\r
198                                                 }\r
199                                                 );\r
200                     } else {\r
201                         // Claim value for the expression\r
202                         g.claimLiteral(expression, sr.Expression_equation, currentText);\r
203                     }\r
204                 }\r
205 \r
206             });\r
207         }\r
208     }\r
209 \r
210     @Override\r
211     public void updateData(Map<String, Object> data) {\r
212         if(this.expression != null && this.expression.getExpression() != null)\r
213             data.put("equation", this.expression.getExpression());\r
214     }\r
215 \r
216     @Override\r
217     public void addKeyListener(KeyListener listener) {\r
218         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
219 \r
220     }\r
221 \r
222     @Override\r
223     public void addModifyListener(ModifyListener listener) {\r
224         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
225 \r
226     }\r
227 \r
228     @Override\r
229     public void addFocusListener(FocusListener listener) {\r
230         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
231     }\r
232 \r
233         @Override\r
234         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
235                 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
236         }\r
237 }\r