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