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