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