1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.util.Arrays;
\r
4 import java.util.List;
\r
5 import java.util.Map;
\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
33 public class WithLookupExpression implements IExpression {
\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
42 public WithLookupExpression(Resource variable) {
\r
43 this.variable = variable;
\r
47 public void createExpressionFields(Composite parent, final Map<String, Object> data) {
\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
52 Label l = new Label(parent, SWT.NONE);
\r
53 l.setFont(EquationTab.FONT);
\r
54 l.setText("With\nLookup");
\r
56 expression = new ExpressionField(parent, SWT.BORDER);
\r
57 expression.setExpression(equation);
\r
58 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
60 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
63 public void focusLost(FocusEvent e) {
\r
64 lastSelectedText = expression;
\r
69 l = new Label(parent, SWT.NONE);
\r
70 l.setFont(EquationTab.FONT);
\r
71 l.setText("Lookup\ntable");
\r
73 lookup = new ExpressionField(parent, SWT.BORDER);
\r
74 lookup.setExpression(lookupTable);
\r
75 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
77 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
80 public void focusLost(FocusEvent e) {
\r
81 lastSelectedText = lookup;
\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
104 public void focus() {
\r
105 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
109 public List<ExpressionField> getExpressionFields() {
\r
110 return Arrays.asList(this.expression, this.lookup);
\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
118 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\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
136 } catch (DatabaseException e1) {
\r
137 e1.printStackTrace();
\r
139 data.put("equation", results[0]);
\r
140 data.put("lookup", results[1]);
\r
146 public void replaceSelection(String var) {
\r
147 if(lastSelectedText != null) {
\r
148 IDocument doc = lastSelectedText.getDocument();
\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
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
164 if(currentExpression != null && currentLookupTable != null) {
\r
166 data.put("equation", currentExpression);
\r
167 data.put("lookup", currentLookupTable);
\r
168 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\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
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
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
210 public void addKeyListener(KeyListener listener) {
\r
211 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
212 this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
216 public void addModifyListener(ModifyListener listener) {
\r
217 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
218 this.lookup.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
222 public void addFocusListener(FocusListener listener) {
\r
223 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
224 this.lookup.getSourceViewer().getTextWidget().addFocusListener(listener);
\r