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