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