]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6308759b7120cd012459097da565f4ac2e8c45de
[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 WithLookupExpressionViewFactor implements IExpressionViewFactor {\r
27 \r
28     \r
29     Label expressionLabel;\r
30     Text expression;\r
31     Label lookupLabel;\r
32     Text lookup;\r
33     \r
34     private Point lastSelection = new Point(0,0);\r
35     private Text lastSelectedText = expression;\r
36     \r
37     @Override\r
38     public void createView(Composite parent, Map<String, Object> data) {\r
39 \r
40         final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
41         final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
42         \r
43         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
44         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
45 \r
46         expressionLabel = new Label(parent, SWT.NONE);\r
47         expressionLabel.setFont(FONT);\r
48         expressionLabel.setText("With\nLookup");\r
49         GridDataFactory.fillDefaults().applyTo(expressionLabel);\r
50 \r
51         expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
52         expression.setFont(FONT);\r
53         expression.setText(equation);\r
54         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
55         \r
56         \r
57         expression.addFocusListener(new FocusAdapter() {\r
58 \r
59             @Override\r
60             public void focusLost(FocusEvent e) {\r
61                 lastSelection = expression.getSelection();\r
62                 lastSelectedText = expression;\r
63             }\r
64         });\r
65 \r
66         expression.addKeyListener(new KeyAdapter() {          \r
67 \r
68             @Override\r
69             public void keyPressed(KeyEvent e) {\r
70                 if(e.keyCode == SWT.ESC) {\r
71                     expression.setText(equation);\r
72                 }                \r
73             }\r
74 \r
75         });\r
76 \r
77         lookupLabel = new Label(parent, SWT.NONE);\r
78         lookupLabel.setFont(FONT);\r
79         lookupLabel.setText("Lookup\ntable");\r
80         GridDataFactory.fillDefaults().applyTo(lookupLabel);\r
81 \r
82 \r
83         lookup = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
84         lookup.setFont(FONT);\r
85         lookup.setText(lookupTable);\r
86         GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);\r
87 \r
88         lookup.addFocusListener(new FocusAdapter() {\r
89 \r
90             @Override\r
91             public void focusLost(FocusEvent e) {\r
92                 lastSelection = lookup.getSelection();\r
93                 lastSelectedText = lookup;\r
94             }\r
95         });\r
96 \r
97         lookup.addKeyListener(new KeyAdapter() {          \r
98 \r
99             @Override\r
100             public void keyPressed(KeyEvent e) {\r
101                 if(e.keyCode == SWT.ESC) {\r
102                     lookup.setText(lookupTable);\r
103                 }                \r
104             }\r
105 \r
106         });\r
107         \r
108     }\r
109 \r
110     @Override\r
111     public void readData(final Resource variable, Map<String, Object> data) {\r
112         String[] results = null;        \r
113         if (variable != null && data.get("equation") == null) {\r
114             try {\r
115                 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {\r
116 \r
117                     @Override\r
118                     public String[] perform(ReadGraph graph) throws DatabaseException {\r
119                         String[] results = new String[2];\r
120                         SysdynResource sr = SysdynResource.getInstance(graph);\r
121                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
122                         if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
123                             results[0] = graph.getRelatedValue(expression, sr.HasEquation);\r
124                             results[1] = graph.getRelatedValue(expression, sr.HasLookup);\r
125                         } else {\r
126                             results[0] = "";\r
127                             results[1] = "";\r
128                         }\r
129                         return results;\r
130                     }\r
131 \r
132                 });\r
133             } catch (DatabaseException e1) {\r
134                 e1.printStackTrace();\r
135             }\r
136             data.put("equation", results[0]);\r
137             data.put("lookup", results[1]);\r
138         }         \r
139     }\r
140 \r
141     @Override\r
142     public void writeData(final Resource variable, Map<String, Object> data) {\r
143         final String currentExpression = expression.getText();\r
144         final String currentLookupTable = lookup.getText();\r
145         \r
146         if(currentExpression != null && currentLookupTable != null) {\r
147             data.put("equation", currentExpression);\r
148             data.put("lookup", currentLookupTable);\r
149             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
150                 @Override\r
151                 public void perform(WriteGraph g)\r
152                 throws DatabaseException {\r
153                     SysdynResource sr = SysdynResource.getInstance(g);\r
154                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
155                     Builtins b = g.getBuiltins();\r
156                     if(expression != null) {\r
157                         g.deny(variable, sr.HasExpression);\r
158                     }\r
159                     expression = g.newResource();\r
160                     g.claim(expression, b.InstanceOf, sr.WithLookupExpression);\r
161                     g.claim(variable, sr.HasExpression, expression);\r
162                     g.claimValue(expression, sr.HasEquation, currentExpression);\r
163                     g.claimValue(expression, sr.HasLookup, currentLookupTable);\r
164                 }\r
165 \r
166             });\r
167         }        \r
168     }\r
169 \r
170     @Override\r
171     public void focus() {\r
172         if(this.lastSelectedText != null) this.lastSelectedText.forceFocus();\r
173     }\r
174 \r
175     @Override\r
176     public void replaceSelection(String var) {\r
177         if(this.lastSelectedText != null) {\r
178             String oldExpression = lastSelectedText.getText();\r
179             if(lastSelection == null)\r
180                 lastSelection = new Point(0,0);\r
181             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
182             lastSelectedText.setText(newExpression);\r
183             lastSelectedText.setSelection(lastSelection.x + var.length());\r
184         }\r
185     }\r
186 \r
187     @Override\r
188     public void updateData(Map<String, Object> data) {\r
189         if(this.expression != null && this.expression.getText() != null)\r
190             data.put("equation", this.expression.getText());   \r
191         if(this.lookup != null && this.lookup.getText() != null)\r
192             data.put("lookup", this.lookup.getText());        \r
193     }\r
194 }