]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
a3c24fec7540b4589c377afe72af5a2d849224f4
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.expressions;\r
2 \r
3 import java.util.Map;\r
4 \r
5 import org.eclipse.jface.layout.GridDataFactory;\r
6 import org.eclipse.jface.layout.GridLayoutFactory;\r
7 import org.eclipse.swt.SWT;\r
8 import org.eclipse.swt.events.FocusAdapter;\r
9 import org.eclipse.swt.events.FocusEvent;\r
10 import org.eclipse.swt.graphics.Point;\r
11 import org.eclipse.swt.widgets.Composite;\r
12 import org.eclipse.swt.widgets.Label;\r
13 import org.eclipse.swt.widgets.Text;\r
14 import org.simantics.db.Builtins;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.WriteRequest;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.request.Read;\r
21 import org.simantics.sysdyn.SysdynResource;\r
22 import org.simantics.ui.SimanticsUI;\r
23 \r
24 public class ConstantExpressionViewFactor implements IExpressionViewFactor {\r
25 \r
26     private Label equationLabel;\r
27     private Text expression;\r
28     private Point lastSelection = new Point(0,0);\r
29 \r
30     @Override\r
31     public void createView(Composite parent, Map<String, Object> data) {\r
32 \r
33         final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
34 \r
35 \r
36         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
37         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
38         equationLabel = new Label(parent, SWT.NONE);\r
39         equationLabel.setFont(FONT);\r
40         equationLabel.setText("=");\r
41         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
42 \r
43 \r
44         expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
45         expression.setFont(FONT);\r
46         expression.setText(equation);\r
47         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
48 \r
49         expression.addFocusListener(new FocusAdapter() {\r
50 \r
51             @Override\r
52             public void focusLost(FocusEvent e) {\r
53                 lastSelection = expression.getSelection();\r
54             }\r
55         });\r
56 \r
57         expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
58 \r
59 \r
60     }\r
61 \r
62     @Override\r
63     public void focus() {\r
64         if(this.expression != null) this.expression.forceFocus();\r
65     }\r
66 \r
67     @Override\r
68     public void readData(final Resource variable, Map<String, Object> data) {\r
69         String equation = null;        \r
70         if (variable != null && data.get("equation") == null) {\r
71             try {\r
72                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
73 \r
74                     @Override\r
75                     public String perform(ReadGraph graph) throws DatabaseException {\r
76                         SysdynResource sr = SysdynResource.getInstance(graph);\r
77                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
78                         if (expression != null && graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
79                             return graph.getRelatedValue(expression, sr.HasEquation);\r
80                         } else {\r
81                             return "";\r
82                         }\r
83                     }\r
84 \r
85                 });\r
86             } catch (DatabaseException e1) {\r
87                 e1.printStackTrace();\r
88             }\r
89             data.put("equation", equation);\r
90         } \r
91     }\r
92 \r
93     @Override\r
94     public void replaceSelection(String var) {\r
95         if(this.expression != null) {\r
96             String oldExpression = expression.getText();\r
97             if(lastSelection == null)\r
98                 lastSelection = new Point(0,0);\r
99             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
100             expression.setText(newExpression);\r
101             expression.setSelection(lastSelection.x + var.length());\r
102         }\r
103     }\r
104 \r
105     @Override\r
106     public void writeData(final Resource variable, Map<String, Object> data) {\r
107         final String currentText = expression.getText();\r
108         if(currentText != null) {\r
109             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
110 \r
111                 @Override\r
112                 public void perform(WriteGraph g)\r
113                 throws DatabaseException {\r
114                     SysdynResource sr = SysdynResource.getInstance(g);\r
115                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
116                     Builtins b = g.getBuiltins();\r
117                     if(expression != null) {\r
118                         g.deny(variable, sr.HasExpression);\r
119                     }\r
120                     expression = g.newResource();\r
121                     g.claim(expression, b.InstanceOf, sr.ConstantExpression);\r
122                     g.claim(variable, sr.HasExpression, expression);\r
123                     g.claimValue(expression, sr.HasEquation, currentText);\r
124                 }\r
125 \r
126             });\r
127         }        \r
128     }\r
129 \r
130     @Override\r
131     public void updateData(Map<String, Object> data) {\r
132         if(this.expression != null && this.expression.getText() != null)\r
133             data.put("equation", this.expression.getText());        \r
134     }\r
135 \r
136 }\r