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