1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\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.equation.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.graphics.Point;
\r
25 import org.eclipse.swt.widgets.Composite;
\r
26 import org.eclipse.swt.widgets.Label;
\r
27 import org.eclipse.swt.widgets.Text;
\r
28 import org.simantics.db.Builtins;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.WriteGraph;
\r
32 import org.simantics.db.common.request.WriteRequest;
\r
33 import org.simantics.db.exception.DatabaseException;
\r
34 import org.simantics.db.request.Read;
\r
35 import org.simantics.sysdyn.SysdynResource;
\r
36 import org.simantics.ui.SimanticsUI;
\r
38 public class StockExpressionViewFactor implements IExpressionViewFactor {
\r
40 private Label integralLabel;
\r
41 private Text integral;
\r
42 private Label equationLabel;
\r
43 private ExpressionField expression;
\r
46 public void createView(Composite parent, Map<String, Object> data) {
\r
48 final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
49 final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
51 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
53 integralLabel = new Label(parent, SWT.NONE);
\r
54 integralLabel.setFont(FONT);
\r
55 integralLabel.setText("Integral");
\r
56 GridDataFactory.fillDefaults().applyTo(integralLabel);
\r
58 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
59 integral.setEditable(false);
\r
60 integral.setFont(FONT);
\r
61 integral.setText(integralEquation);
\r
62 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
64 equationLabel = new Label(parent, SWT.NONE);
\r
65 equationLabel.setFont(FONT);
\r
66 equationLabel.setText("Initial\nValue");
\r
67 GridDataFactory.fillDefaults().applyTo(equationLabel);
\r
70 expression = new ExpressionField(parent, SWT.BORDER);
\r
71 expression.setFont(FONT);
\r
72 expression.setExpression(initialEquation);
\r
73 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
78 public void readData(final Resource variable, Map<String, Object> data) {
\r
79 String initialEquation = null;
\r
81 if (variable != null && data.get("initialEquation") == null) {
\r
83 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
86 public String perform(ReadGraph graph) throws DatabaseException {
\r
87 SysdynResource sr = SysdynResource.getInstance(graph);
\r
88 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
89 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
90 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
97 } catch (DatabaseException e1) {
\r
98 e1.printStackTrace();
\r
100 data.put("initialEquation", initialEquation);
\r
103 data.put("integral", getIntegral(variable));
\r
107 public void writeData(final Resource variable, Map<String, Object> data) {
\r
108 final String currentText = expression.getExpression();
\r
109 if(currentText != null) {
\r
110 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
113 public void perform(WriteGraph g)
\r
114 throws DatabaseException {
\r
115 SysdynResource sr = SysdynResource.getInstance(g);
\r
116 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
117 Builtins b = g.getBuiltins();
\r
118 if(expression != null) {
\r
119 g.deny(variable, sr.HasExpression);
\r
121 expression = g.newResource();
\r
122 g.claim(expression, b.InstanceOf, null, sr.StockExpression);
\r
123 g.claim(variable, sr.HasExpression, expression);
\r
124 g.claimValue(expression, sr.HasInitialEquation, currentText);
\r
131 private String getIntegral(final Resource variable) {
\r
132 String integral = "";
\r
134 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
137 public String perform(ReadGraph graph) throws DatabaseException {
\r
138 SysdynResource sr = SysdynResource.getInstance(graph);
\r
139 Builtins b = graph.getBuiltins();
\r
140 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
141 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
143 StringBuilder builder = new StringBuilder();
\r
144 builder.append("");
\r
145 for (Resource r : heads) {
\r
146 if(graph.isInstanceOf(r, sr.Flow)) {
\r
147 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
149 Object name = graph.getPossibleRelatedValue(tail, b.HasName);
\r
151 builder.append(" + " + name);
\r
155 for (Resource r : tails) {
\r
156 if(graph.isInstanceOf(r, sr.Flow)) {
\r
157 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
159 Object name = graph.getPossibleRelatedValue(head, b.HasName);
\r
161 builder.append(" - " + name);
\r
165 if (builder.indexOf(" + ") == 0)
\r
166 builder.delete(0, 3);
\r
167 return builder.toString().trim();
\r
171 } catch (DatabaseException e) {
\r
172 e.printStackTrace();
\r
178 public void focus() {
\r
179 if(this.expression != null) this.expression.focus();
\r
183 public void replaceSelection(String var) {
\r
184 if(expression != null) {
\r
185 IDocument doc = expression.getDocument();
\r
187 Point selection = expression.getSelection();
\r
188 doc.replace(selection.x, selection.y, var);
\r
189 expression.setSelection(selection.x + var.length());
\r
190 } catch (BadLocationException e) {
\r
191 e.printStackTrace();
\r
197 public void updateData(Map<String, Object> data) {
\r
198 if(this.expression != null && this.expression.getExpression() != null)
\r
199 data.put("initialEquation", this.expression.getExpression());
\r
200 if(this.integral != null && this.integral.getText() != null)
\r
201 data.put("integral", this.integral.getText());
\r
205 public List<ExpressionField> getExpressionFields() {
\r
206 return Arrays.asList(this.expression);
\r