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.eclipse.swt.widgets.Table;
\r
36 import org.simantics.databoard.Bindings;
\r
37 import org.simantics.db.ReadGraph;
\r
38 import org.simantics.db.Resource;
\r
39 import org.simantics.db.VirtualGraph;
\r
40 import org.simantics.db.WriteGraph;
\r
41 import org.simantics.db.common.request.WriteRequest;
\r
42 import org.simantics.db.common.utils.OrderedSetUtils;
\r
43 import org.simantics.db.exception.DatabaseException;
\r
44 import org.simantics.db.request.Read;
\r
45 import org.simantics.layer0.Layer0;
\r
46 import org.simantics.layer0.utils.direct.GraphUtils;
\r
47 import org.simantics.sysdyn.SysdynResource;
\r
48 import org.simantics.ui.SimanticsUI;
\r
51 * IExpression for displaying fields of DelayExpression
\r
52 * @author Teemu Lempinen
\r
53 * @author Tuomas Miettinen
\r
56 public class DelayExpression implements IExpression {
\r
58 private ExpressionField equation, delayTime, initialValue;
\r
59 private ExpressionField lastSelectedText = equation;
\r
60 private Spinner order;
\r
61 private Resource expression;
\r
64 * Creates a new DelayExpression
\r
67 public DelayExpression(Resource expression) {
\r
68 this.expression = expression;
\r
72 * Displays the fields for delayExpression
\r
75 public void createExpressionFields(Composite parent, final Map<String, Object> data, Table allowedVariables) {
\r
76 // Get possible existing data
\r
77 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
78 String eq = data.get("equation") != null ? (String)data.get("equation") : "";
\r
79 String dt = data.get("delayTime") != null ? (String)data.get("delayTime") : "";
\r
80 String iv = data.get("initialValue") != null ? (String)data.get("initialValue") : "";
\r
81 int o = data.get("order") != null ? (Integer)data.get("order") : 3;
\r
83 Label l = new Label(parent, SWT.NONE);
\r
84 l.setText("expression");
\r
86 // Equation that is delayed
\r
87 equation = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);
\r
88 equation.setExpression(eq);
\r
89 GridDataFactory.fillDefaults().grab(true, true).applyTo(equation);
\r
90 equation.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
93 public void focusLost(FocusEvent e) {
\r
94 lastSelectedText = equation;
\r
98 l = new Label(parent, SWT.NONE);
\r
99 l.setText("delay time");
\r
101 // How much the equation is delayed
\r
102 delayTime = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);
\r
103 delayTime.setExpression(dt);
\r
104 GridDataFactory.fillDefaults().grab(true, true).applyTo(delayTime);
\r
106 delayTime.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
109 public void focusLost(FocusEvent e) {
\r
110 lastSelectedText = delayTime;
\r
114 l = new Label(parent, SWT.NONE);
\r
115 l.setText("initial value");
\r
117 // What is the initial value of the delay (empty == same as equation)
\r
118 initialValue = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);
\r
119 initialValue.setExpression(iv);
\r
120 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialValue);
\r
122 initialValue.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
125 public void focusLost(FocusEvent e) {
\r
126 lastSelectedText = initialValue;
\r
131 l = new Label(parent, SWT.NONE);
\r
132 l.setText("order");
\r
134 // The order of the delay (default == 3)
\r
135 order = new Spinner(parent, SWT.BORDER);
\r
136 order.setMinimum(1);
\r
137 order.setSelection(o);
\r
138 order.addSelectionListener(new SelectionAdapter() {
\r
140 public void widgetSelected(SelectionEvent e) {
\r
141 save(expression, data);
\r
144 GridDataFactory.fillDefaults().applyTo(order);
\r
148 public void focus() {
\r
149 lastSelectedText.setFocus();
\r
153 public List<ExpressionField> getExpressionFields() {
\r
154 return Arrays.asList(equation, delayTime, initialValue);
\r
158 public void readData(final Resource expression, Map<String, Object> data) {
\r
160 String equation, delayTime, initialValue;
\r
164 Auxiliary results = null;
\r
167 results = SimanticsUI.getSession().syncRequest(new Read<Auxiliary>() {
\r
170 public Auxiliary perform(ReadGraph graph) throws DatabaseException {
\r
171 Auxiliary results = new Auxiliary();
\r
172 SysdynResource sr = SysdynResource.getInstance(graph);
\r
173 if (expression != null && graph.isInstanceOf(expression, sr.DelayExpression)) {
\r
174 results.equation = graph.getPossibleRelatedValue(expression, sr.DelayExpression_expression);
\r
175 results.delayTime = graph.getPossibleRelatedValue(expression, sr.DelayExpression_delayTime);
\r
176 results.initialValue = graph.getPossibleRelatedValue(expression, sr.DelayExpression_initialValue);
\r
177 results.order = graph.getPossibleRelatedValue(expression, sr.DelayExpression_order);
\r
179 results.equation = "";
\r
180 results.delayTime = "";
\r
186 } catch (DatabaseException e1) {
\r
187 e1.printStackTrace();
\r
189 if(results.equation != null)
\r
190 data.put("equation", results.equation);
\r
191 if(results.delayTime != null)
\r
192 data.put("delayTime", results.delayTime);
\r
193 if(results.initialValue != null)
\r
194 data.put("initialValue", results.initialValue);
\r
195 if(results.order != null)
\r
196 data.put("order", results.order);
\r
200 public void replaceSelection(String var) {
\r
201 if(lastSelectedText != null) {
\r
202 IDocument doc = lastSelectedText.getDocument();
\r
204 Point selection = lastSelectedText.getSelection();
\r
205 doc.replace(selection.x, selection.y, var);
\r
206 lastSelectedText.setSelection(selection.x + var.length());
\r
207 } catch (BadLocationException e) {
\r
208 e.printStackTrace();
\r
214 public void save(Resource expr, Map<String, Object> data) {
\r
215 this.expression = expr;
\r
216 final String currentEquation = this.equation.getExpression();
\r
217 final String currentDelayTime = this.delayTime.getExpression();
\r
218 final String currentInitialValue = this.initialValue.getExpression();
\r
219 final Integer currentOrder = this.order.getSelection();
\r
221 String oldEquation = (String)data.get("equation");
\r
222 String oldDelayTime = (String)data.get("delayTime");
\r
223 String oldInitialValue = (String)data.get("initialValue");
\r
224 Integer oldOrder = (Integer)data.get("order");
\r
226 if((oldEquation == null || oldDelayTime == null || oldOrder == null || oldInitialValue == null) ||
\r
227 !oldEquation.equals(currentEquation) || !oldDelayTime.equals(currentDelayTime) ||
\r
228 !oldOrder.equals(currentOrder) || !oldInitialValue.equals(currentInitialValue)) {
\r
229 // old value was null or value has changed -> Do save
\r
232 data.put("equation", currentEquation);
\r
233 data.put("delayTime", currentDelayTime);
\r
234 data.put("initialValue", currentInitialValue);
\r
235 data.put("order", currentOrder);
\r
237 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
239 public void perform(WriteGraph g)
\r
240 throws DatabaseException {
\r
241 SysdynResource sr = SysdynResource.getInstance(g);
\r
242 Layer0 l0 = Layer0.getInstance(g);
\r
244 if(!g.isInstanceOf(expression, sr.DelayExpression)) {
\r
245 // Create a new DelayExpression, if the old expression was something else
\r
246 Resource ownerList = OrderedSetUtils.getSingleOwnerList(g, expression);
\r
247 final Resource newExpression = GraphUtils.create2(g, sr.DelayExpression);
\r
248 String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);
\r
249 if(arrayRange != null)
\r
250 g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);
\r
252 final Resource variable = g.getSingleObject(ownerList, sr.Variable_expressions_Inverse);
\r
253 OrderedSetUtils.replace(g, ownerList, expression, newExpression);
\r
254 g.deny(expression, l0.PartOf);
\r
255 g.claim(newExpression, l0.PartOf, variable);
\r
257 VirtualGraph runtime = g.getService(VirtualGraph.class);
\r
258 g.syncRequest(new WriteRequest(runtime) {
\r
260 public void perform(WriteGraph graph) throws DatabaseException {
\r
261 SysdynResource sr = SysdynResource.getInstance(graph);
\r
262 if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))
\r
263 graph.deny(variable, sr.IndependentVariable_activeExpression);
\r
264 graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);
\r
268 expression = newExpression;
\r
272 g.claimLiteral(expression, sr.DelayExpression_expression, currentEquation);
\r
273 g.claimLiteral(expression, sr.DelayExpression_delayTime, currentDelayTime);
\r
274 g.claimLiteral(expression, sr.DelayExpression_initialValue, currentInitialValue);
\r
275 g.claimLiteral(expression, sr.DelayExpression_order, currentOrder);
\r
283 public void updateData(Map<String, Object> data) {
\r
284 if(this.equation != null && this.equation.getExpression() != null)
\r
285 data.put("equation", this.equation.getExpression());
\r
286 if(this.delayTime != null && this.delayTime.getExpression() != null)
\r
287 data.put("delayTime", this.delayTime.getExpression());
\r
288 if(this.initialValue != null && this.initialValue.getExpression() != null)
\r
289 data.put("initialValue", this.initialValue.getExpression());
\r
290 if(this.order != null)
\r
291 data.put("order", this.order.getSelection());
\r
295 public void addKeyListener(KeyListener listener) {
\r
296 this.equation.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
297 this.delayTime.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
298 this.initialValue.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
302 public void addModifyListener(ModifyListener listener) {
\r
303 this.equation.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
304 this.delayTime.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
305 this.initialValue.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
309 public void addFocusListener(FocusListener listener) {
\r
310 this.equation.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
311 this.delayTime.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
312 this.initialValue.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
316 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
317 this.equation.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
318 this.delayTime.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
319 this.initialValue.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r