]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ea0bbc744178d10c6c74a8c83abe9fbd0c73f26c
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.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.text.BadLocationException;\r
9 import org.eclipse.jface.text.IDocument;\r
10 import org.eclipse.swt.SWT;\r
11 import org.eclipse.swt.events.FocusAdapter;\r
12 import org.eclipse.swt.events.FocusEvent;\r
13 import org.eclipse.swt.events.FocusListener;\r
14 import org.eclipse.swt.events.KeyListener;\r
15 import org.eclipse.swt.events.ModifyListener;\r
16 import org.eclipse.swt.events.SelectionAdapter;\r
17 import org.eclipse.swt.events.SelectionEvent;\r
18 import org.eclipse.swt.graphics.Point;\r
19 import org.eclipse.swt.widgets.Button;\r
20 import org.eclipse.swt.widgets.Composite;\r
21 import org.eclipse.swt.widgets.Label;\r
22 import org.simantics.db.Builtins;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.WriteGraph;\r
26 import org.simantics.db.common.request.WriteRequest;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.request.Read;\r
29 import org.simantics.sysdyn.SysdynResource;\r
30 import org.simantics.sysdyn.ui.properties.EquationTab;\r
31 import org.simantics.ui.SimanticsUI;\r
32 \r
33 public class WithLookupExpression implements IExpression {\r
34 \r
35     private ExpressionField expression;\r
36     private ExpressionField lookup;\r
37     private Button asGraph;\r
38     private ExpressionField lastSelectedText = expression;\r
39     private Resource variable;\r
40     private LookupChartInfo chartInfo;\r
41 \r
42     public WithLookupExpression(Resource variable) {\r
43         this.variable = variable;\r
44     }\r
45 \r
46     @Override\r
47     public void createExpressionFields(Composite parent, final Map<String, Object> data) {\r
48 \r
49         String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
50         String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
51 \r
52         Label l = new Label(parent, SWT.NONE);\r
53         l.setFont(EquationTab.FONT);\r
54         l.setText("With\nLookup");\r
55 \r
56         expression = new ExpressionField(parent, SWT.BORDER);\r
57         expression.setExpression(equation);\r
58         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
59 \r
60         expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
61 \r
62             @Override\r
63             public void focusLost(FocusEvent e) {\r
64                 lastSelectedText = expression;\r
65             }\r
66         });\r
67 \r
68 \r
69         l = new Label(parent, SWT.NONE);\r
70         l.setFont(EquationTab.FONT);\r
71         l.setText("Lookup\ntable");\r
72 \r
73         lookup = new ExpressionField(parent, SWT.BORDER);\r
74         lookup.setExpression(lookupTable);\r
75         GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);\r
76 \r
77         lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
78 \r
79             @Override\r
80             public void focusLost(FocusEvent e) {\r
81                 lastSelectedText = lookup;\r
82             }\r
83         });\r
84 \r
85         asGraph = new Button(parent, SWT.None);\r
86         asGraph.setText("As graph");\r
87         asGraph.setFont(EquationTab.FONT);\r
88         asGraph.addSelectionListener(new SelectionAdapter() {\r
89             public void widgetSelected(SelectionEvent e) {\r
90                 if(chartInfo == null) chartInfo = new LookupChartInfo(expression.getExpression(), lookup.getExpression(), variable, data);\r
91                 LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), chartInfo);\r
92                 LookupChartInfo newInfo = pud.open(false);\r
93                 if(pud.getReturnCode() == org.eclipse.jface.window.Window.OK) {\r
94                     chartInfo = newInfo;\r
95                     lookup.setExpression(chartInfo.lookupTable);\r
96                     lookup.focus();\r
97                 }\r
98             }\r
99         });\r
100 \r
101     }\r
102 \r
103     @Override\r
104     public void focus() {\r
105         if(this.lastSelectedText != null) this.lastSelectedText.focus();        \r
106     }\r
107 \r
108     @Override\r
109     public List<ExpressionField> getExpressionFields() {\r
110         return Arrays.asList(this.expression, this.lookup);\r
111     }\r
112 \r
113     @Override\r
114     public void readData(final Resource variable, Map<String, Object> data) {\r
115         String[] results = null;\r
116         if (variable != null && data.get("equation") == null) {\r
117             try {\r
118                 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {\r
119 \r
120                     @Override\r
121                     public String[] perform(ReadGraph graph) throws DatabaseException {\r
122                         String[] results = new String[2];\r
123                         SysdynResource sr = SysdynResource.getInstance(graph);\r
124                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
125                         if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
126                             results[0] = graph.getRelatedValue(expression, sr.HasEquation);\r
127                             results[1] = graph.getRelatedValue(expression, sr.HasLookup);\r
128                         } else {\r
129                             results[0] = "";\r
130                             results[1] = "";\r
131                         }\r
132                         return results;\r
133                     }\r
134 \r
135                 });\r
136             } catch (DatabaseException e1) {\r
137                 e1.printStackTrace();\r
138             }\r
139             data.put("equation", results[0]);\r
140             data.put("lookup", results[1]);\r
141         }\r
142 \r
143     }\r
144 \r
145     @Override\r
146     public void replaceSelection(String var) {\r
147         if(lastSelectedText != null) {\r
148             IDocument doc = lastSelectedText.getDocument();\r
149             try {\r
150                 Point selection = lastSelectedText.getSelection();\r
151                 doc.replace(selection.x, selection.y, var);\r
152                 lastSelectedText.setSelection(selection.x + var.length());\r
153             } catch (BadLocationException e) {\r
154                 e.printStackTrace();\r
155             }\r
156         }        \r
157     }\r
158 \r
159     @Override\r
160     public void save(final Resource variable, Map<String, Object> data) {\r
161         final String currentExpression = expression.getExpression();\r
162         final String currentLookupTable = lookup.getExpression();\r
163 \r
164         if(currentExpression != null && currentLookupTable != null) {\r
165             data.putAll(data);\r
166             data.put("equation", currentExpression);\r
167             data.put("lookup", currentLookupTable);\r
168             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
169                 @Override\r
170                 public void perform(WriteGraph g)\r
171                 throws DatabaseException {\r
172                     SysdynResource sr = SysdynResource.getInstance(g);\r
173                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
174                     Builtins b = g.getBuiltins();\r
175                     if(expression != null) {\r
176                         g.deny(variable, sr.HasExpression);\r
177                     }\r
178                     expression = g.newResource();\r
179                     g.claim(expression, b.InstanceOf, null, sr.WithLookupExpression);\r
180                     g.claim(variable, sr.HasExpression, expression);\r
181                     g.claimValue(expression, sr.HasEquation, currentExpression);\r
182                     g.claimValue(expression, sr.HasLookup, currentLookupTable);\r
183                     if(chartInfo != null) {\r
184                         g.claimValue(expression, sr.HasMinX, chartInfo.minX);\r
185                         g.claimValue(expression, sr.HasMaxX, chartInfo.maxX);\r
186                         g.claimValue(expression, sr.HasMinY, chartInfo.minY);\r
187                         g.claimValue(expression, sr.HasMaxY, chartInfo.maxY);\r
188                     }\r
189                 }\r
190             });\r
191         }\r
192 \r
193     }\r
194 \r
195     @Override\r
196     public void updateData(Map<String, Object> data) {\r
197         if(this.expression != null && this.expression.getExpression() != null)\r
198             data.put("equation", this.expression.getExpression());\r
199         if(this.lookup != null && this.lookup.getExpression() != null)\r
200             data.put("lookup", this.lookup.getExpression());\r
201         if(this.chartInfo != null) {\r
202             data.put("minX", chartInfo.minX);\r
203             data.put("maxX", chartInfo.maxX);\r
204             data.put("minY", chartInfo.minY);\r
205             data.put("maxY", chartInfo.maxY);\r
206         }        \r
207     }\r
208 \r
209     @Override\r
210     public void addKeyListener(KeyListener listener) {\r
211         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
212         this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener);\r
213     }\r
214 \r
215     @Override\r
216     public void addModifyListener(ModifyListener listener) {\r
217         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
218         this.lookup.getSourceViewer().getTextWidget().addModifyListener(listener);\r
219     }\r
220 \r
221     @Override\r
222     public void addFocusListener(FocusListener listener) {\r
223         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
224         this.lookup.getSourceViewer().getTextWidget().addFocusListener(listener);\r
225     }\r
226 \r
227 }\r