]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
b97c6c21b09e7021a9b74db706d9e30a219d64a4
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.equation.expressions;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.text.BadLocationException;\r
21 import org.eclipse.jface.text.IDocument;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.events.FocusAdapter;\r
24 import org.eclipse.swt.events.FocusEvent;\r
25 import org.eclipse.swt.events.SelectionAdapter;\r
26 import org.eclipse.swt.events.SelectionEvent;\r
27 import org.eclipse.swt.graphics.Point;\r
28 import org.eclipse.swt.widgets.Button;\r
29 import org.eclipse.swt.widgets.Composite;\r
30 import org.eclipse.swt.widgets.Label;\r
31 import org.simantics.db.ReadGraph;\r
32 import org.simantics.db.Resource;\r
33 import org.simantics.db.WriteGraph;\r
34 import org.simantics.db.common.request.WriteRequest;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.db.request.Read;\r
37 import org.simantics.layer0.Layer0;\r
38 import org.simantics.sysdyn.SysdynResource;\r
39 import org.simantics.ui.SimanticsUI;\r
40 \r
41 public class WithLookupExpressionViewFactor implements IExpressionViewFactor {\r
42 \r
43 \r
44     private Label expressionLabel;\r
45     private ExpressionField expression;\r
46     private Label lookupLabel;\r
47     private ExpressionField lookup;\r
48     private Button asGraph;\r
49     private ExpressionField lastSelectedText = expression;\r
50     private Resource variable;\r
51     private LookupChartInfo chartInfo;\r
52 \r
53     public WithLookupExpressionViewFactor(Resource variable) {\r
54         super();\r
55         this.variable = variable;\r
56     }\r
57     \r
58     @Override\r
59     public void createView(Composite parent, final Map<String, Object> data) {\r
60         String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
61         String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";\r
62 \r
63         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
64 \r
65         expressionLabel = new Label(parent, SWT.NONE);\r
66         expressionLabel.setFont(FONT);\r
67         expressionLabel.setText("With\nLookup");\r
68         GridDataFactory.fillDefaults().applyTo(expressionLabel);\r
69 \r
70         expression = new ExpressionField(parent, SWT.BORDER);\r
71         expression.setFont(FONT);\r
72         expression.setExpression(equation);\r
73         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
74 \r
75         expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
76 \r
77             @Override\r
78             public void focusLost(FocusEvent e) {\r
79                 lastSelectedText = expression;\r
80             }\r
81         });\r
82 \r
83         lookupLabel = new Label(parent, SWT.NONE);\r
84         lookupLabel.setFont(FONT);\r
85         lookupLabel.setText("Lookup\ntable");\r
86         GridDataFactory.fillDefaults().applyTo(lookupLabel);\r
87 \r
88         lookup = new ExpressionField(parent, SWT.BORDER);\r
89         lookup.setFont(FONT);\r
90         lookup.setExpression(lookupTable);\r
91         GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);\r
92 \r
93         lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
94 \r
95             @Override\r
96             public void focusLost(FocusEvent e) {\r
97                 lastSelectedText = lookup;\r
98             }\r
99         });\r
100 \r
101         asGraph = new Button(parent, SWT.None);\r
102         asGraph.setText("As graph");\r
103         asGraph.setFont(FONT);\r
104         asGraph.addSelectionListener(new SelectionAdapter() {\r
105             public void widgetSelected(SelectionEvent e) {\r
106                 if(e.widget == asGraph) {\r
107                     if(chartInfo == null) chartInfo = new LookupChartInfo(expression.getExpression(), lookup.getExpression(), variable, data);\r
108                     LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), chartInfo);\r
109                     LookupChartInfo newInfo = pud.open(false);\r
110                     if(pud.getReturnCode() == org.eclipse.jface.window.Window.OK) {\r
111                         chartInfo = newInfo;\r
112                         lookup.setExpression(chartInfo.lookupTable);\r
113                     }\r
114                 }\r
115             }\r
116         });\r
117 \r
118     }\r
119 \r
120     @Override\r
121     public void readData(final Resource variable, Map<String, Object> data) {\r
122         String[] results = null;\r
123         if (variable != null && data.get("equation") == null) {\r
124             try {\r
125                 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {\r
126 \r
127                     @Override\r
128                     public String[] perform(ReadGraph graph) throws DatabaseException {\r
129                         String[] results = new String[2];\r
130                         SysdynResource sr = SysdynResource.getInstance(graph);\r
131                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
132                         if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
133                             results[0] = graph.getRelatedValue(expression, sr.HasEquation);\r
134                             results[1] = graph.getRelatedValue(expression, sr.HasLookup);\r
135                         } else {\r
136                             results[0] = "";\r
137                             results[1] = "";\r
138                         }\r
139                         return results;\r
140                     }\r
141 \r
142                 });\r
143             } catch (DatabaseException e1) {\r
144                 e1.printStackTrace();\r
145             }\r
146             data.put("equation", results[0]);\r
147             data.put("lookup", results[1]);\r
148         }\r
149     }\r
150 \r
151     @Override\r
152     public void writeData(final Resource variable, Map<String, Object> data) {\r
153         final String currentExpression = expression.getExpression();\r
154         final String currentLookupTable = lookup.getExpression();\r
155 \r
156         if(currentExpression != null && currentLookupTable != null) {\r
157             data.putAll(data);\r
158             data.put("equation", currentExpression);\r
159             data.put("lookup", currentLookupTable);\r
160             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
161                 @Override\r
162                 public void perform(WriteGraph g)\r
163                 throws DatabaseException {\r
164                     SysdynResource sr = SysdynResource.getInstance(g);\r
165                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
166                     Layer0 l0 = Layer0.getInstance(g);\r
167                     if(expression != null) {\r
168                         g.deny(variable, sr.HasExpression);\r
169                     }\r
170                     expression = g.newResource();\r
171                     g.claim(expression, l0.InstanceOf, null, sr.WithLookupExpression);\r
172                     g.claim(variable, sr.HasExpression, expression);\r
173                     g.claimLiteral(expression, sr.HasEquation, currentExpression);\r
174                     g.claimLiteral(expression, sr.HasLookup, currentLookupTable);\r
175                     g.claimLiteral(expression, sr.HasMinX, chartInfo.minX);\r
176                     g.claimLiteral(expression, sr.HasMaxX, chartInfo.maxX);\r
177                     g.claimLiteral(expression, sr.HasMinY, chartInfo.minY);\r
178                     g.claimLiteral(expression, sr.HasMaxY, chartInfo.maxY);\r
179                 }\r
180             });\r
181         }\r
182     }\r
183 \r
184     @Override\r
185     public void focus() {\r
186         if(this.lastSelectedText != null) this.lastSelectedText.focus();\r
187     }\r
188 \r
189     @Override\r
190     public void replaceSelection(String var) {\r
191         if(lastSelectedText != null) {\r
192             IDocument doc = lastSelectedText.getDocument();\r
193             try {\r
194                 Point selection = lastSelectedText.getSelection();\r
195                 doc.replace(selection.x, selection.y, var);\r
196                 lastSelectedText.setSelection(selection.x + var.length());\r
197             } catch (BadLocationException e) {\r
198                 e.printStackTrace();\r
199             }\r
200         }\r
201     }\r
202 \r
203     @Override\r
204     public void updateData(Map<String, Object> data) {\r
205         if(this.expression != null && this.expression.getExpression() != null)\r
206             data.put("equation", this.expression.getExpression());\r
207         if(this.lookup != null && this.lookup.getExpression() != null)\r
208             data.put("lookup", this.lookup.getExpression());\r
209         if(this.chartInfo != null) {\r
210             data.put("minX", chartInfo.minX);\r
211             data.put("maxX", chartInfo.maxX);\r
212             data.put("minY", chartInfo.minY);\r
213             data.put("maxY", chartInfo.maxY);\r
214         }\r
215     }\r
216 \r
217     @Override\r
218     public List<ExpressionField> getExpressionFields() {\r
219         return Arrays.asList(this.expression, this.lookup);\r
220     }\r
221 }