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