1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
16 import java.util.Map;
\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.custom.VerifyKeyListener;
\r
24 import org.eclipse.swt.events.FocusAdapter;
\r
25 import org.eclipse.swt.events.FocusEvent;
\r
26 import org.eclipse.swt.events.FocusListener;
\r
27 import org.eclipse.swt.events.KeyListener;
\r
28 import org.eclipse.swt.events.ModifyListener;
\r
29 import org.eclipse.swt.events.SelectionAdapter;
\r
30 import org.eclipse.swt.events.SelectionEvent;
\r
31 import org.eclipse.swt.graphics.Point;
\r
32 import org.eclipse.swt.widgets.Composite;
\r
33 import org.eclipse.swt.widgets.Label;
\r
34 import org.eclipse.swt.widgets.Spinner;
\r
35 import org.simantics.databoard.Bindings;
\r
36 import org.simantics.db.ReadGraph;
\r
37 import org.simantics.db.Resource;
\r
38 import org.simantics.db.VirtualGraph;
\r
39 import org.simantics.db.WriteGraph;
\r
40 import org.simantics.db.common.request.WriteRequest;
\r
41 import org.simantics.db.common.utils.OrderedSetUtils;
\r
42 import org.simantics.db.exception.DatabaseException;
\r
43 import org.simantics.db.request.Read;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.layer0.utils.direct.GraphUtils;
\r
46 import org.simantics.sysdyn.SysdynResource;
\r
47 import org.simantics.ui.SimanticsUI;
\r
50 * IExpression for displaying fields of DelayExpression
\r
51 * @author Teemu Lempinen
\r
54 public class DelayExpression implements IExpression {
\r
56 private ExpressionField equation, delayTime, initialValue;
\r
57 private ExpressionField lastSelectedText = equation;
\r
58 private Spinner order;
\r
59 private Resource expression;
\r
62 * Creates a new DelayExpression
\r
65 public DelayExpression(Resource expression) {
\r
66 this.expression = expression;
\r
70 * Displays the fields for delayExpression
\r
73 public void createExpressionFields(Composite parent, final Map<String, Object> data) {
\r
74 // Get possible existing data
\r
75 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
76 String eq = data.get("equation") != null ? (String)data.get("equation") : "";
\r
77 String dt = data.get("delayTime") != null ? (String)data.get("delayTime") : "";
\r
78 String iv = data.get("initialValue") != null ? (String)data.get("initialValue") : "";
\r
79 int o = data.get("order") != null ? (Integer)data.get("order") : 3;
\r
81 Label l = new Label(parent, SWT.NONE);
\r
82 l.setText("expression");
\r
84 // Equation that is delayed
\r
85 equation = new ExpressionField(parent, SWT.BORDER);
\r
86 equation.setExpression(eq);
\r
87 GridDataFactory.fillDefaults().grab(true, true).applyTo(equation);
\r
88 equation.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
91 public void focusLost(FocusEvent e) {
\r
92 lastSelectedText = equation;
\r
96 l = new Label(parent, SWT.NONE);
\r
97 l.setText("delay time");
\r
99 // How much the equation is delayed
\r
100 delayTime = new ExpressionField(parent, SWT.BORDER);
\r
101 delayTime.setExpression(dt);
\r
102 GridDataFactory.fillDefaults().grab(true, true).applyTo(delayTime);
\r
104 delayTime.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
107 public void focusLost(FocusEvent e) {
\r
108 lastSelectedText = delayTime;
\r
112 l = new Label(parent, SWT.NONE);
\r
113 l.setText("initial value");
\r
115 // What is the initial value of the delay (empty == same as equation)
\r
116 initialValue = new ExpressionField(parent, SWT.BORDER);
\r
117 initialValue.setExpression(iv);
\r
118 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialValue);
\r
120 initialValue.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
123 public void focusLost(FocusEvent e) {
\r
124 lastSelectedText = initialValue;
\r
129 l = new Label(parent, SWT.NONE);
\r
130 l.setText("order");
\r
132 // The order of the delay (default == 3)
\r
133 order = new Spinner(parent, SWT.BORDER);
\r
134 order.setMinimum(1);
\r
135 order.setSelection(o);
\r
136 order.addSelectionListener(new SelectionAdapter() {
\r
138 public void widgetSelected(SelectionEvent e) {
\r
139 save(expression, data);
\r
142 GridDataFactory.fillDefaults().applyTo(order);
\r
146 public void focus() {
\r
147 equation.setFocus();
\r
151 public List<ExpressionField> getExpressionFields() {
\r
152 return Arrays.asList(equation, delayTime);
\r
156 public void readData(final Resource expression, Map<String, Object> data) {
\r
158 String equation, delayTime, initialValue;
\r
162 Auxiliary results = null;
\r
165 results = SimanticsUI.getSession().syncRequest(new Read<Auxiliary>() {
\r
168 public Auxiliary perform(ReadGraph graph) throws DatabaseException {
\r
169 Auxiliary results = new Auxiliary();
\r
170 SysdynResource sr = SysdynResource.getInstance(graph);
\r
171 if (expression != null && graph.isInstanceOf(expression, sr.DelayExpression)) {
\r
172 results.equation = graph.getPossibleRelatedValue(expression, sr.DelayExpression_expression);
\r
173 results.delayTime = graph.getPossibleRelatedValue(expression, sr.DelayExpression_delayTime);
\r
174 results.initialValue = graph.getPossibleRelatedValue(expression, sr.DelayExpression_initialValue);
\r
175 results.order = graph.getPossibleRelatedValue(expression, sr.DelayExpression_order);
\r
177 results.equation = "";
\r
178 results.delayTime = "";
\r
184 } catch (DatabaseException e1) {
\r
185 e1.printStackTrace();
\r
187 if(results.equation != null)
\r
188 data.put("equation", results.equation);
\r
189 if(results.delayTime != null)
\r
190 data.put("delayTime", results.delayTime);
\r
191 if(results.initialValue != null)
\r
192 data.put("initialValue", results.initialValue);
\r
193 if(results.order != null)
\r
194 data.put("order", results.order);
\r
198 public void replaceSelection(String var) {
\r
199 if(lastSelectedText != null) {
\r
200 IDocument doc = lastSelectedText.getDocument();
\r
202 Point selection = lastSelectedText.getSelection();
\r
203 doc.replace(selection.x, selection.y, var);
\r
204 lastSelectedText.setSelection(selection.x + var.length());
\r
205 } catch (BadLocationException e) {
\r
206 e.printStackTrace();
\r
212 public void save(Resource expr, Map<String, Object> data) {
\r
213 this.expression = expr;
\r
214 final String currentEquation = this.equation.getExpression();
\r
215 final String currentDelayTime = this.delayTime.getExpression();
\r
216 final String currentInitialValue = this.initialValue.getExpression();
\r
217 final Integer currentOrder = this.order.getSelection();
\r
219 String oldEquation = (String)data.get("equation");
\r
220 String oldDelayTime = (String)data.get("delayTime");
\r
221 String oldInitialValue = (String)data.get("initialValue");
\r
222 Integer oldOrder = (Integer)data.get("order");
\r
224 if((oldEquation == null || oldDelayTime == null || oldOrder == null || oldInitialValue == null) ||
\r
225 !oldEquation.equals(currentEquation) || !oldDelayTime.equals(currentDelayTime) ||
\r
226 !oldOrder.equals(currentOrder) || !oldInitialValue.equals(currentInitialValue)) {
\r
227 // old value was null or value has changed -> Do save
\r
230 data.put("equation", currentEquation);
\r
231 data.put("delayTime", currentDelayTime);
\r
232 data.put("initialValue", currentInitialValue);
\r
233 data.put("order", currentOrder);
\r
235 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
237 public void perform(WriteGraph g)
\r
238 throws DatabaseException {
\r
239 SysdynResource sr = SysdynResource.getInstance(g);
\r
240 Layer0 l0 = Layer0.getInstance(g);
\r
242 if(!g.isInstanceOf(expression, sr.DelayExpression)) {
\r
243 // Create a new DelayExpression, if the old expression was something else
\r
244 Resource ownerList = OrderedSetUtils.getSingleOwnerList(g, expression);
\r
245 final Resource newExpression = GraphUtils.create2(g, sr.DelayExpression);
\r
246 String arrayRange = g.getPossibleRelatedValue(expression, sr.HasArrayRange, Bindings.STRING);
\r
247 if(arrayRange != null)
\r
248 g.claimLiteral(newExpression, sr.HasArrayRange, arrayRange);
\r
250 final Resource variable = g.getSingleObject(ownerList, sr.HasExpressions_Inverse);
\r
251 OrderedSetUtils.replace(g, ownerList, expression, newExpression);
\r
252 g.deny(expression, l0.PartOf);
\r
253 g.claim(newExpression, l0.PartOf, variable);
\r
255 VirtualGraph runtime = g.getService(VirtualGraph.class);
\r
256 g.syncRequest(new WriteRequest(runtime) {
\r
258 public void perform(WriteGraph graph) throws DatabaseException {
\r
259 SysdynResource sr = SysdynResource.getInstance(graph);
\r
260 if(graph.hasStatement(variable, sr.HasActiveExpression))
\r
261 graph.deny(variable, sr.HasActiveExpression);
\r
262 graph.claim(variable, sr.HasActiveExpression, newExpression);
\r
266 expression = newExpression;
\r
270 g.claimLiteral(expression, sr.DelayExpression_expression, currentEquation);
\r
271 g.claimLiteral(expression, sr.DelayExpression_delayTime, currentDelayTime);
\r
272 g.claimLiteral(expression, sr.DelayExpression_initialValue, currentInitialValue);
\r
273 g.claimLiteral(expression, sr.DelayExpression_order, currentOrder);
\r
281 public void updateData(Map<String, Object> data) {
\r
282 if(this.equation != null && this.equation.getExpression() != null)
\r
283 data.put("equation", this.equation.getExpression());
\r
284 if(this.delayTime != null && this.delayTime.getExpression() != null)
\r
285 data.put("delayTime", this.delayTime.getExpression());
\r
286 if(this.initialValue != null && this.initialValue.getExpression() != null)
\r
287 data.put("initialValue", this.initialValue.getExpression());
\r
288 if(this.order != null)
\r
289 data.put("order", this.order.getSelection());
\r
293 public void addKeyListener(KeyListener listener) {
\r
294 this.equation.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
295 this.delayTime.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
296 this.initialValue.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
300 public void addModifyListener(ModifyListener listener) {
\r
301 this.equation.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
302 this.delayTime.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
303 this.initialValue.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
307 public void addFocusListener(FocusListener listener) {
\r
308 this.equation.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
309 this.delayTime.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
310 this.initialValue.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
314 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
315 this.equation.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
316 this.delayTime.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
317 this.initialValue.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r