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