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.eclipse.swt.widgets.Text;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.WriteRequest;
\r
36 import org.simantics.db.exception.DatabaseException;
\r
37 import org.simantics.db.request.Read;
\r
38 import org.simantics.layer0.Layer0;
\r
39 import org.simantics.sysdyn.SysdynResource;
\r
40 import org.simantics.ui.SimanticsUI;
\r
42 public class StockExpression implements IExpression {
\r
44 private Text integral;
\r
45 private ExpressionField expression;
\r
48 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
49 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
50 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
51 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
54 Label label = new Label(parent, SWT.NONE);
\r
55 label.setText("Integral");
\r
57 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
58 integral.setEditable(false);
\r
59 integral.setText(integralEquation);
\r
60 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
63 label = new Label(parent, SWT.NONE);
\r
64 label.setText("Initial\nValue");
\r
66 expression = new ExpressionField(parent, SWT.BORDER);
\r
67 expression.setExpression(initialEquation);
\r
69 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
74 public void focus() {
\r
75 this.expression.focus();
\r
79 public List<ExpressionField> getExpressionFields() {
\r
80 return Arrays.asList(this.expression);
\r
84 public void readData(final Resource variable, Map<String, Object> data) {
\r
85 String initialEquation = null;
\r
87 if (variable != null && data.get("initialEquation") == null) {
\r
89 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
92 public String perform(ReadGraph graph) throws DatabaseException {
\r
93 SysdynResource sr = SysdynResource.getInstance(graph);
\r
94 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
95 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
96 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
103 } catch (DatabaseException e1) {
\r
104 e1.printStackTrace();
\r
106 data.put("initialEquation", initialEquation);
\r
109 data.put("integral", getIntegral(variable));
\r
114 public void replaceSelection(String var) {
\r
115 if(expression != null) {
\r
116 IDocument doc = expression.getDocument();
\r
118 Point selection = expression.getSelection();
\r
119 doc.replace(selection.x, selection.y, var);
\r
120 expression.setSelection(selection.x + var.length());
\r
121 } catch (BadLocationException e) {
\r
122 e.printStackTrace();
\r
128 public void save(final Resource variable, Map<String, Object> data) {
\r
129 final String currentText = expression.getExpression();
\r
130 if(currentText != null) {
\r
131 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
134 public void perform(WriteGraph g)
\r
135 throws DatabaseException {
\r
136 SysdynResource sr = SysdynResource.getInstance(g);
\r
137 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
138 Layer0 l0 = Layer0.getInstance(g);
\r
139 if(expression != null && !g.isInstanceOf(expression, sr.StockExpression)) {
\r
140 g.deny(variable, sr.HasExpression);
\r
143 if(expression == null) {
\r
144 expression = g.newResource();
\r
145 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
146 g.claim(variable, sr.HasExpression, expression);
\r
148 g.claimLiteral(expression, sr.HasInitialEquation, currentText);
\r
153 expression.setExpression(currentText);
\r
157 public void updateData(Map<String, Object> data) {
\r
158 if(this.expression != null && this.expression.getExpression() != null)
\r
159 data.put("initialEquation", this.expression.getExpression());
\r
160 if(this.integral != null && this.integral.getText() != null)
\r
161 data.put("integral", this.integral.getText());
\r
165 private String getIntegral(final Resource variable) {
\r
166 String integral = "";
\r
168 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
171 public String perform(ReadGraph graph) throws DatabaseException {
\r
172 SysdynResource sr = SysdynResource.getInstance(graph);
\r
173 Layer0 l0 = Layer0.getInstance(graph);
\r
174 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
175 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
177 StringBuilder builder = new StringBuilder();
\r
178 builder.append("");
\r
179 for (Resource r : heads) {
\r
180 if(graph.isInstanceOf(r, sr.Flow)) {
\r
181 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
183 Object name = graph.getPossibleRelatedValue(tail, l0.HasName);
\r
185 builder.append(" + " + name);
\r
190 for (Resource r : tails) {
\r
191 if(graph.isInstanceOf(r, sr.Flow)) {
\r
192 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
194 Object name = graph.getPossibleRelatedValue(head, l0.HasName);
\r
196 builder.append(" - " + name);
\r
200 if (builder.indexOf(" + ") == 0)
\r
201 builder.delete(0, 3);
\r
202 return builder.toString().trim();
\r
206 } catch (DatabaseException e) {
\r
207 e.printStackTrace();
\r
213 public void addKeyListener(KeyListener listener) {
\r
214 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
219 public void addModifyListener(ModifyListener listener) {
\r
220 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
225 public void addFocusListener(FocusListener listener) {
\r
226 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
230 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
231 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r