]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6828ef1d737f7611814c2cdde125a641f157dc9b
[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 AuxiliaryExpressionViewFactor 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         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
38         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
39         equationLabel = new Label(parent, SWT.NONE);\r
40         equationLabel.setFont(FONT);\r
41         equationLabel.setText("=");\r
42         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
43 \r
44 \r
45         expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
46         expression.setFont(FONT);\r
47         expression.setText(equation);\r
48         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
49 \r
50         expression.addFocusListener(new FocusAdapter() {\r
51 \r
52             @Override\r
53             public void focusLost(FocusEvent e) {\r
54                 lastSelection = expression.getSelection();      \r
55             }\r
56         });\r
57 \r
58         expression.addKeyListener(new KeyAdapter() {          \r
59 \r
60             @Override\r
61             public void keyPressed(KeyEvent e) {\r
62                 if(e.keyCode == SWT.ESC) {\r
63                     expression.setText(equation);\r
64                 }\r
65             }\r
66 \r
67         });\r
68 \r
69         \r
70     }\r
71 \r
72     @Override\r
73     public void readData(final Resource variable, Map<String, Object> data) {\r
74         String equation = null;        \r
75         if (variable != null && data.get("equation") == null) {\r
76             try {\r
77                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
78 \r
79                     @Override\r
80                     public String perform(ReadGraph graph) throws DatabaseException {\r
81                         SysdynResource sr = SysdynResource.getInstance(graph);\r
82                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
83                         if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
84                             return graph.getRelatedValue(expression, sr.HasEquation);\r
85                         } else {\r
86                             return "";\r
87                         }\r
88                     }\r
89 \r
90                 });\r
91             } catch (DatabaseException e1) {\r
92                 e1.printStackTrace();\r
93             }\r
94             data.put("equation", equation);\r
95         }         \r
96     }\r
97 \r
98     @Override\r
99     public void writeData(final Resource variable, Map<String, Object> data) {\r
100         final String currentText = expression.getText();\r
101         if(currentText != null) {\r
102             data.put("equation", currentText);\r
103             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
104                 @Override\r
105                 public void perform(WriteGraph g)\r
106                 throws DatabaseException {\r
107                     SysdynResource sr = SysdynResource.getInstance(g);\r
108                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
109                     Builtins b = g.getBuiltins();\r
110                     if(expression != null) {\r
111                         g.deny(variable, sr.HasExpression);\r
112                     }\r
113                     expression = g.newResource();\r
114                     g.claim(expression, b.InstanceOf, sr.NormalExpression);\r
115                     g.claim(variable, sr.HasExpression, expression);\r
116                     g.claimValue(expression, sr.HasEquation, currentText);\r
117                 }\r
118 \r
119             });\r
120         }        \r
121     }\r
122 \r
123     @Override\r
124     public void focus() {\r
125         if(this.expression != null) this.expression.forceFocus();\r
126     }\r
127 \r
128     @Override\r
129     public void replaceSelection(String var) {\r
130         if(this.expression != null) {\r
131             String oldExpression = expression.getText();\r
132             if(lastSelection == null)\r
133                 lastSelection = new Point(0,0);\r
134             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
135             expression.setText(newExpression);\r
136             expression.setSelection(lastSelection.x + var.length());\r
137         }\r
138     }\r
139 }\r