]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
58d265f9a3218b36d8b9b463e6609c9d9d08a20d
[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) {\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) {\r
70                             String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);\r
71                             if(equation != null)\r
72                                 return equation;\r
73                         }\r
74                         \r
75                         return "";\r
76                         \r
77                     }\r
78 \r
79                 });\r
80             } catch (DatabaseException e1) {\r
81                 e1.printStackTrace();\r
82             }\r
83             data.put("equation", equation);\r
84         }\r
85     }\r
86 \r
87     @Override\r
88     public void replaceSelection(String var) {\r
89         if(expression != null) {\r
90             IDocument doc = expression.getDocument();\r
91             try {\r
92                 Point selection = expression.getSelection();\r
93                 doc.replace(selection.x, selection.y, var);\r
94                 expression.setSelection(selection.x + var.length());\r
95             } catch (BadLocationException e) {\r
96                 e.printStackTrace();\r
97             }\r
98         }\r
99     }\r
100 \r
101     @Override\r
102     public void save(final Resource variable, Map<String, Object> data) {\r
103         final String currentText = expression.getExpression();\r
104         final String oldEquation = (String)data.get("equation");\r
105         if(oldEquation == null || \r
106                 (currentText != null && expressionType != null)) {\r
107             data.put("equation", currentText);\r
108             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
109                 @Override\r
110                 public void perform(WriteGraph g)\r
111                 throws DatabaseException {\r
112                     SysdynResource sr = SysdynResource.getInstance(g);\r
113                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
114                     if(ExpressionUtils.isParameter(currentText)) {\r
115                         if(!expressionType.equals(sr.ConstantExpression))\r
116                                 expressionType = sr.ParameterExpression;\r
117                     } else {\r
118                         expressionType = sr.NormalExpression;\r
119                     }\r
120                     if (oldEquation != null \r
121                             && expression != null \r
122                             && g.isInstanceOf(expression, expressionType) \r
123                             && currentText.equals(oldEquation)) {\r
124                         return;\r
125                     }\r
126                     Builtins b = g.getBuiltins();\r
127                     if(expression != null && !g.isInstanceOf(expression, expressionType)) {\r
128                         g.deny(variable, sr.HasExpression);\r
129                         expression = null;\r
130                     }\r
131                     if(expression == null) {\r
132                         expression = g.newResource();\r
133                         g.claim(expression, b.InstanceOf, null, expressionType);\r
134                         g.claim(variable, sr.HasExpression, expression);\r
135                     }\r
136                     g.claimLiteral(expression, sr.HasEquation, currentText);\r
137                 }\r
138 \r
139             });\r
140         }\r
141     }\r
142 \r
143     @Override\r
144     public void updateData(Map<String, Object> data) {\r
145         if(this.expression != null && this.expression.getExpression() != null)\r
146             data.put("equation", this.expression.getExpression());\r
147     }\r
148 \r
149     @Override\r
150     public void addKeyListener(KeyListener listener) {\r
151         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
152 \r
153     }\r
154 \r
155     @Override\r
156     public void addModifyListener(ModifyListener listener) {\r
157         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
158 \r
159     }\r
160 \r
161     @Override\r
162     public void addFocusListener(FocusListener listener) {\r
163         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
164     }\r
165 }\r