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.Collection;
\r
16 import java.util.List;
\r
17 import java.util.Map;
\r
19 import org.eclipse.jface.layout.GridDataFactory;
\r
20 import org.eclipse.jface.layout.GridLayoutFactory;
\r
21 import org.eclipse.jface.text.BadLocationException;
\r
22 import org.eclipse.jface.text.IDocument;
\r
23 import org.eclipse.swt.SWT;
\r
24 import org.eclipse.swt.custom.VerifyKeyListener;
\r
25 import org.eclipse.swt.events.FocusListener;
\r
26 import org.eclipse.swt.events.KeyListener;
\r
27 import org.eclipse.swt.events.ModifyListener;
\r
28 import org.eclipse.swt.graphics.Point;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.Label;
\r
31 import org.simantics.databoard.Bindings;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.VirtualGraph;
\r
35 import org.simantics.db.WriteGraph;
\r
36 import org.simantics.db.common.request.WriteRequest;
\r
37 import org.simantics.db.common.utils.OrderedSetUtils;
\r
38 import org.simantics.db.exception.DatabaseException;
\r
39 import org.simantics.db.request.Read;
\r
40 import org.simantics.layer0.Layer0;
\r
41 import org.simantics.layer0.utils.direct.GraphUtils;
\r
42 import org.simantics.sysdyn.SysdynResource;
\r
43 import org.simantics.sysdyn.ui.utils.ExpressionUtils;
\r
44 import org.simantics.ui.SimanticsUI;
\r
47 * Basic expression that is used with parameter, auxiliary and constant
\r
48 * @author Teemu Lempinen
\r
51 public class BasicExpression implements IExpression {
\r
53 private ExpressionField expression;
\r
54 protected Resource expressionType;
\r
57 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
58 // Create the single field
\r
59 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
60 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
62 Label l = new Label(parent, SWT.NONE);
\r
65 expression = new ExpressionField(parent, SWT.BORDER);
\r
66 expression.setExpression(equation);
\r
67 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
72 public void focus() {
\r
73 this.expression.focus();
\r
78 public List<ExpressionField> getExpressionFields() {
\r
79 return Arrays.asList(this.expression);
\r
83 public void readData(final Resource expression, Map<String, Object> data) {
\r
84 String equation = null;
\r
85 if (expression != null && data.get("equation") == null) {
\r
87 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
90 public String perform(ReadGraph graph) throws DatabaseException {
\r
91 SysdynResource sr = SysdynResource.getInstance(graph);
\r
92 if (expression != null) {
\r
93 String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);
\r
94 if(equation != null)
\r
103 } catch (DatabaseException e1) {
\r
104 e1.printStackTrace();
\r
106 data.put("equation", equation);
\r
111 public void replaceSelection(String var) {
\r
112 if(expression != null) {
\r
113 IDocument doc = expression.getDocument();
\r
115 Point selection = expression.getSelection();
\r
116 doc.replace(selection.x, selection.y, var);
\r
117 expression.setSelection(selection.x + var.length());
\r
118 } catch (BadLocationException e) {
\r
119 e.printStackTrace();
\r
125 public void save(final Resource expression, Map<String, Object> data) {
\r
126 final String currentText = this.expression.getExpression();
\r
127 final String oldEquation = (String)data.get("equation");
\r
128 if(oldEquation == null ||
\r
129 (currentText != null && expressionType != null)) {
\r
130 data.put("equation", currentText);
\r
131 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
133 public void perform(WriteGraph g)
\r
134 throws DatabaseException {
\r
135 SysdynResource sr = SysdynResource.getInstance(g);
\r
136 Layer0 l0 = Layer0.getInstance(g);
\r
138 // Force change to parameter, if the equation is a parameter
\r
139 if(ExpressionUtils.isParameter(currentText)) {
\r
140 if(!expressionType.equals(sr.ConstantExpression))
\r
141 expressionType = sr.ParameterExpression;
\r
143 expressionType = sr.NormalExpression;
\r
146 // If nothing has changed, do nothing
\r
147 if (oldEquation != null
\r
148 && expression != null
\r
149 && g.isInstanceOf(expression, expressionType)
\r
150 && currentText.equals(oldEquation)) {
\r
154 // If the current expression type is different than the target expression type, create a new expression
\r
155 if(!g.isInstanceOf(expression, expressionType)) {
\r
156 Collection<Resource> ownerLists = OrderedSetUtils.getOwnerLists(g, expression, l0.OrderedSet);
\r
157 if(ownerLists.size() != 1)
\r
159 Resource ownerList = ownerLists.iterator().next();
\r
160 final Resource newExpression = GraphUtils.create2(g, expressionType,
\r
161 sr.HasEquation, currentText);
\r
162 String arrayRange = g.getPossibleRelatedValue(expression, sr.HasArrayRange, Bindings.STRING);
\r
163 if(arrayRange != null)
\r
164 g.claimLiteral(newExpression, sr.HasArrayRange, arrayRange);
\r
166 final Resource variable = g.getSingleObject(ownerList, sr.HasExpressions_Inverse);
\r
167 OrderedSetUtils.replace(g, ownerList, expression, newExpression);
\r
168 g.deny(expression, l0.PartOf);
\r
169 g.claim(newExpression, l0.PartOf, variable);
\r
172 VirtualGraph runtime = g.getService(VirtualGraph.class);
\r
173 g.syncRequest(new WriteRequest(runtime) {
\r
175 public void perform(WriteGraph graph) throws DatabaseException {
\r
176 SysdynResource sr = SysdynResource.getInstance(graph);
\r
177 if(variable != null) {
\r
178 if(graph.hasStatement(variable, sr.HasActiveExpression))
\r
179 graph.deny(variable, sr.HasActiveExpression);
\r
180 graph.claim(variable, sr.HasActiveExpression, newExpression);
\r
186 // Claim value for the expression
\r
187 g.claimLiteral(expression, sr.HasEquation, currentText);
\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
202 public void addKeyListener(KeyListener listener) {
\r
203 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
208 public void addModifyListener(ModifyListener listener) {
\r
209 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
214 public void addFocusListener(FocusListener listener) {
\r
215 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
219 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
220 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r