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