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.FocusListener;
\r
25 import org.eclipse.swt.events.KeyListener;
\r
26 import org.eclipse.swt.events.ModifyListener;
\r
27 import org.eclipse.swt.graphics.Point;
\r
28 import org.eclipse.swt.widgets.Composite;
\r
29 import org.eclipse.swt.widgets.Label;
\r
30 import org.simantics.databoard.Bindings;
\r
31 import org.simantics.db.ReadGraph;
\r
32 import org.simantics.db.Resource;
\r
33 import org.simantics.db.VirtualGraph;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.WriteRequest;
\r
36 import org.simantics.db.common.utils.OrderedSetUtils;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.request.Read;
\r
39 import org.simantics.layer0.Layer0;
\r
40 import org.simantics.layer0.utils.direct.GraphUtils;
\r
41 import org.simantics.sysdyn.SysdynResource;
\r
42 import org.simantics.sysdyn.ui.utils.ExpressionUtils;
\r
43 import org.simantics.ui.SimanticsUI;
\r
45 public class BasicExpression implements IExpression {
\r
47 private ExpressionField expression;
\r
48 protected Resource expressionType;
\r
51 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
52 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
53 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
55 Label l = new Label(parent, SWT.NONE);
\r
58 expression = new ExpressionField(parent, SWT.BORDER);
\r
59 expression.setExpression(equation);
\r
60 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
65 public void focus() {
\r
66 this.expression.focus();
\r
71 public List<ExpressionField> getExpressionFields() {
\r
72 return Arrays.asList(this.expression);
\r
76 public void readData(final Resource expression, Map<String, Object> data) {
\r
77 String equation = null;
\r
78 if (expression != null && data.get("equation") == null) {
\r
80 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
83 public String perform(ReadGraph graph) throws DatabaseException {
\r
84 SysdynResource sr = SysdynResource.getInstance(graph);
\r
85 if (expression != null) {
\r
86 String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);
\r
87 if(equation != null)
\r
96 } catch (DatabaseException e1) {
\r
97 e1.printStackTrace();
\r
99 data.put("equation", equation);
\r
104 public void replaceSelection(String var) {
\r
105 if(expression != null) {
\r
106 IDocument doc = expression.getDocument();
\r
108 Point selection = expression.getSelection();
\r
109 doc.replace(selection.x, selection.y, var);
\r
110 expression.setSelection(selection.x + var.length());
\r
111 } catch (BadLocationException e) {
\r
112 e.printStackTrace();
\r
118 public void save(final Resource expression, Map<String, Object> data) {
\r
119 final String currentText = this.expression.getExpression();
\r
120 final String oldEquation = (String)data.get("equation");
\r
121 if(oldEquation == null ||
\r
122 (currentText != null && expressionType != null)) {
\r
123 data.put("equation", currentText);
\r
124 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
126 public void perform(WriteGraph g)
\r
127 throws DatabaseException {
\r
128 SysdynResource sr = SysdynResource.getInstance(g);
\r
129 Layer0 l0 = Layer0.getInstance(g);
\r
131 if(ExpressionUtils.isParameter(currentText)) {
\r
132 if(!expressionType.equals(sr.ConstantExpression))
\r
133 expressionType = sr.ParameterExpression;
\r
135 expressionType = sr.NormalExpression;
\r
137 if (oldEquation != null
\r
138 && expression != null
\r
139 && g.isInstanceOf(expression, expressionType)
\r
140 && currentText.equals(oldEquation)) {
\r
144 if(!g.isInstanceOf(expression, expressionType)) {
\r
145 Resource ownerList = OrderedSetUtils.getSingleOwnerList(g, expression);
\r
146 final Resource newExpression = GraphUtils.create2(g, expressionType,
\r
147 sr.HasEquation, currentText);
\r
148 String arrayRange = g.getPossibleRelatedValue(expression, sr.HasArrayRange, Bindings.STRING);
\r
149 if(arrayRange != null)
\r
150 g.claimLiteral(newExpression, sr.HasArrayRange, arrayRange);
\r
152 final Resource variable = g.getSingleObject(ownerList, sr.HasExpressions_Inverse);
\r
153 OrderedSetUtils.replace(g, ownerList, expression, newExpression);
\r
154 g.deny(expression, l0.PartOf);
\r
155 g.claim(newExpression, l0.PartOf, variable);
\r
158 VirtualGraph runtime = g.getService(VirtualGraph.class);
\r
159 g.syncRequest(new WriteRequest(runtime) {
\r
161 public void perform(WriteGraph graph) throws DatabaseException {
\r
162 SysdynResource sr = SysdynResource.getInstance(graph);
\r
163 if(variable != null) {
\r
164 if(graph.hasStatement(variable, sr.HasActiveExpression))
\r
165 graph.deny(variable, sr.HasActiveExpression);
\r
166 graph.claim(variable, sr.HasActiveExpression, newExpression);
\r
172 g.claimLiteral(expression, sr.HasEquation, currentText);
\r
181 public void updateData(Map<String, Object> data) {
\r
182 if(this.expression != null && this.expression.getExpression() != null)
\r
183 data.put("equation", this.expression.getExpression());
\r
187 public void addKeyListener(KeyListener listener) {
\r
188 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
193 public void addModifyListener(ModifyListener listener) {
\r
194 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
199 public void addFocusListener(FocusListener listener) {
\r
200 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
204 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
205 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r