]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
654874929ee8eb936886f88017fa2cc1e24fe7ca
[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 \r
57     @Override\r
58     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
59         // Create the single field\r
60         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
61         String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
62 \r
63         Label l = new Label(parent, SWT.NONE);\r
64         l.setText("=");\r
65 \r
66         expression = new ExpressionField(parent, SWT.BORDER, null, false);\r
67         expression.setExpression(equation);\r
68         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
69 \r
70     }\r
71 \r
72     @Override\r
73     public void focus() {\r
74         this.expression.focus();\r
75 \r
76     }\r
77 \r
78     @Override\r
79     public List<ExpressionField> getExpressionFields() {\r
80         return Arrays.asList(this.expression);\r
81     }\r
82 \r
83     @Override\r
84     public void readData(final Resource expression, Map<String, Object> data) {\r
85         String equation = null;\r
86         if (expression != null && data.get("equation") == null) {\r
87             try {\r
88                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
89 \r
90                     @Override\r
91                     public String perform(ReadGraph graph) throws DatabaseException {\r
92                         SysdynResource sr = SysdynResource.getInstance(graph);\r
93                         if (expression != null) {\r
94                             String equation = graph.getPossibleRelatedValue(expression, sr.Expression_equation);\r
95                             if(equation != null)\r
96                                 return equation;\r
97                         }\r
98                         \r
99                         return "";\r
100                         \r
101                     }\r
102 \r
103                 });\r
104             } catch (DatabaseException e1) {\r
105                 e1.printStackTrace();\r
106             }\r
107             data.put("equation", equation);\r
108         }\r
109     }\r
110 \r
111     @Override\r
112     public void replaceSelection(String var) {\r
113         if(expression != null) {\r
114             IDocument doc = expression.getDocument();\r
115             try {\r
116                 Point selection = expression.getSelection();\r
117                 doc.replace(selection.x, selection.y, var);\r
118                 expression.setSelection(selection.x + var.length());\r
119             } catch (BadLocationException e) {\r
120                 e.printStackTrace();\r
121             }\r
122         }\r
123     }\r
124 \r
125     @Override\r
126     public void save(final Resource expression, Map<String, Object> data) {\r
127         final String currentText = this.expression.getExpression();\r
128         final String oldEquation = (String)data.get("equation");\r
129         if(oldEquation == null || \r
130                 (currentText != null && expressionType != null)) {\r
131             data.put("equation", currentText);\r
132             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
133                 @Override\r
134                 public void perform(WriteGraph g)\r
135                 throws DatabaseException {\r
136                     SysdynResource sr = SysdynResource.getInstance(g);\r
137                     Layer0 l0 = Layer0.getInstance(g);\r
138 \r
139                     // Force change to parameter, if the equation is a parameter\r
140                     if(ExpressionUtils.isParameter(currentText)) {\r
141                         if(!expressionType.equals(sr.ConstantExpression))\r
142                                 expressionType = sr.ParameterExpression;\r
143                     } else {\r
144                         expressionType = sr.NormalExpression;\r
145                     }\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                     // If the current expression type is different than the target expression type, create a new expression\r
156                     if(!g.isInstanceOf(expression, expressionType)) {\r
157 \r
158                         final Resource newExpression = GraphUtils.create2(g, expressionType, \r
159                                         sr.Expression_equation, currentText);\r
160                         String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);\r
161                         if(arrayRange != null)\r
162                                 g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);\r
163                         \r
164                         final Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
165                         if(variable == null)\r
166                             return;\r
167                         Resource ownerList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
168                         if(ownerList == null)\r
169                             return;\r
170                         \r
171                         ListUtils.replace(g, ownerList, expression, newExpression);\r
172                         \r
173                         g.deny(expression, l0.PartOf);\r
174                         \r
175                         g.claim(newExpression, l0.PartOf, variable);\r
176                         \r
177                         \r
178                         VirtualGraphSupport support = g.getService(VirtualGraphSupport.class);\r
179                                                 g.syncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {\r
180                                                         @Override\r
181                                                         public void perform(WriteGraph graph) throws DatabaseException {\r
182                                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
183                                                                 if(variable != null) {\r
184                                                                         if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
185                                                                                 graph.deny(variable, sr.IndependentVariable_activeExpression);\r
186                                                                         graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);\r
187                                                                 }\r
188                                                         }\r
189                                                 }\r
190                                                 );\r
191                     } else {\r
192                         // Claim value for the expression\r
193                         g.claimLiteral(expression, sr.Expression_equation, currentText);\r
194                     }\r
195                 }\r
196 \r
197             });\r
198         }\r
199     }\r
200 \r
201     @Override\r
202     public void updateData(Map<String, Object> data) {\r
203         if(this.expression != null && this.expression.getExpression() != null)\r
204             data.put("equation", this.expression.getExpression());\r
205     }\r
206 \r
207     @Override\r
208     public void addKeyListener(KeyListener listener) {\r
209         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
210 \r
211     }\r
212 \r
213     @Override\r
214     public void addModifyListener(ModifyListener listener) {\r
215         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
216 \r
217     }\r
218 \r
219     @Override\r
220     public void addFocusListener(FocusListener listener) {\r
221         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
222     }\r
223 \r
224         @Override\r
225         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
226                 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
227         }\r
228 }\r