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