]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1ed9a96596b4dd7f39aeed3ce62ce6e93c0be2d4
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.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.swt.SWT;\r
10 import org.eclipse.swt.events.FocusAdapter;\r
11 import org.eclipse.swt.events.FocusEvent;\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         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 focusGained(FocusEvent e){\r
53                 \r
54             }\r
55 \r
56             @Override\r
57             public void focusLost(FocusEvent e) {\r
58                 lastSelection = expression.getSelection();      \r
59             }\r
60         });\r
61         \r
62     }\r
63 \r
64     @Override\r
65     public void readData(final Resource variable, Map<String, Object> data) {\r
66         String equation = null;        \r
67         if (variable != null && data.get("equation") == null) {\r
68             try {\r
69                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
70 \r
71                     @Override\r
72                     public String perform(ReadGraph graph) throws DatabaseException {\r
73                         SysdynResource sr = SysdynResource.getInstance(graph);\r
74                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
75                         if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
76                             return graph.getRelatedValue(expression, sr.HasEquation);\r
77                         } else {\r
78                             return "";\r
79                         }\r
80                     }\r
81 \r
82                 });\r
83             } catch (DatabaseException e1) {\r
84                 e1.printStackTrace();\r
85             }\r
86             data.put("equation", equation);\r
87         }         \r
88     }\r
89 \r
90     @Override\r
91     public void writeData(final Resource variable, Map<String, Object> data) {\r
92         final String currentText = expression.getText();\r
93         if(currentText != null) {\r
94             data.put("equation", currentText);\r
95             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
96                 @Override\r
97                 public void perform(WriteGraph g)\r
98                 throws DatabaseException {\r
99                     SysdynResource sr = SysdynResource.getInstance(g);\r
100                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
101                     Builtins b = g.getBuiltins();\r
102                     if(expression != null) {\r
103                         g.deny(variable, sr.HasExpression);\r
104                     }\r
105                     expression = g.newResource();\r
106                     g.claim(expression, b.InstanceOf, sr.NormalExpression);\r
107                     g.claim(variable, sr.HasExpression, expression);\r
108                     g.claimValue(expression, sr.HasEquation, currentText);\r
109                 }\r
110 \r
111             });\r
112         }        \r
113     }\r
114 \r
115     @Override\r
116     public void focus() {\r
117         if(this.expression != null) this.expression.forceFocus();\r
118     }\r
119 \r
120     @Override\r
121     public void replaceSelection(String var) {\r
122         if(this.expression != null) {\r
123             String oldExpression = expression.getText();\r
124             if(lastSelection == null)\r
125                 lastSelection = new Point(0,0);\r
126             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
127             expression.setText(newExpression);\r
128             expression.setSelection(lastSelection.x + var.length());\r
129         }\r
130     }\r
131 \r
132     @Override\r
133     public void updateData(Map<String, Object> data) {\r
134         if(this.expression != null && this.expression.getText() != null)\r
135             data.put("equation", this.expression.getText());\r
136     }\r
137 \r
138     @Override\r
139     public List<Text> getExpressionFields() {\r
140         return Arrays.asList(this.expression);\r
141     }\r
142 }\r