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