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