1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.util.Arrays;
\r
4 import java.util.Collection;
\r
5 import java.util.List;
\r
6 import java.util.Map;
\r
8 import org.eclipse.jface.layout.GridDataFactory;
\r
9 import org.eclipse.jface.layout.GridLayoutFactory;
\r
10 import org.eclipse.jface.text.BadLocationException;
\r
11 import org.eclipse.jface.text.IDocument;
\r
12 import org.eclipse.swt.SWT;
\r
13 import org.eclipse.swt.events.FocusListener;
\r
14 import org.eclipse.swt.events.KeyListener;
\r
15 import org.eclipse.swt.events.ModifyListener;
\r
16 import org.eclipse.swt.graphics.Point;
\r
17 import org.eclipse.swt.widgets.Composite;
\r
18 import org.eclipse.swt.widgets.Label;
\r
19 import org.eclipse.swt.widgets.Text;
\r
20 import org.simantics.db.Builtins;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.request.WriteRequest;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
26 import org.simantics.db.request.Read;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
28 import org.simantics.ui.SimanticsUI;
\r
30 public class StockExpression implements IExpression {
\r
32 private Text integral;
\r
33 private ExpressionField expression;
\r
36 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
37 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
38 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
39 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
42 Label label = new Label(parent, SWT.NONE);
\r
43 label.setText("Integral");
\r
45 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
46 integral.setEditable(false);
\r
47 integral.setText(integralEquation);
\r
48 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
51 label = new Label(parent, SWT.NONE);
\r
52 label.setText("Initial\nValue");
\r
54 expression = new ExpressionField(parent, SWT.BORDER);
\r
55 expression.setExpression(initialEquation);
\r
57 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
62 public void focus() {
\r
63 this.expression.focus();
\r
67 public List<ExpressionField> getExpressionFields() {
\r
68 return Arrays.asList(this.expression);
\r
72 public void readData(final Resource variable, Map<String, Object> data) {
\r
73 String initialEquation = null;
\r
75 if (variable != null && data.get("initialEquation") == null) {
\r
77 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
80 public String perform(ReadGraph graph) throws DatabaseException {
\r
81 SysdynResource sr = SysdynResource.getInstance(graph);
\r
82 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
83 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
84 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
91 } catch (DatabaseException e1) {
\r
92 e1.printStackTrace();
\r
94 data.put("initialEquation", initialEquation);
\r
97 data.put("integral", getIntegral(variable));
\r
102 public void replaceSelection(String var) {
\r
103 if(expression != null) {
\r
104 IDocument doc = expression.getDocument();
\r
106 Point selection = expression.getSelection();
\r
107 doc.replace(selection.x, selection.y, var);
\r
108 expression.setSelection(selection.x + var.length());
\r
109 } catch (BadLocationException e) {
\r
110 e.printStackTrace();
\r
116 public void save(final Resource variable, Map<String, Object> data) {
\r
117 final String currentText = expression.getExpression();
\r
118 if(currentText != null) {
\r
119 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
122 public void perform(WriteGraph g)
\r
123 throws DatabaseException {
\r
124 SysdynResource sr = SysdynResource.getInstance(g);
\r
125 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
126 Builtins b = g.getBuiltins();
\r
127 if(expression != null && !g.isInstanceOf(expression, sr.StockExpression)) {
\r
128 g.deny(variable, sr.HasExpression);
\r
131 if(expression == null) {
\r
132 expression = g.newResource();
\r
133 g.claim(expression, b.InstanceOf, null, sr.StockExpression);
\r
134 g.claim(variable, sr.HasExpression, expression);
\r
136 g.claimValue(expression, sr.HasInitialEquation, currentText);
\r
141 expression.setExpression(currentText);
\r
145 public void updateData(Map<String, Object> data) {
\r
146 if(this.expression != null && this.expression.getExpression() != null)
\r
147 data.put("initialEquation", this.expression.getExpression());
\r
148 if(this.integral != null && this.integral.getText() != null)
\r
149 data.put("integral", this.integral.getText());
\r
153 private String getIntegral(final Resource variable) {
\r
154 String integral = "";
\r
156 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
159 public String perform(ReadGraph graph) throws DatabaseException {
\r
160 SysdynResource sr = SysdynResource.getInstance(graph);
\r
161 Builtins b = graph.getBuiltins();
\r
162 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
163 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
165 StringBuilder builder = new StringBuilder();
\r
166 builder.append("");
\r
167 for (Resource r : heads) {
\r
168 if(graph.isInstanceOf(r, sr.Flow)) {
\r
169 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
171 Object name = graph.getPossibleRelatedValue(tail, b.HasName);
\r
173 builder.append(" + " + name);
\r
178 for (Resource r : tails) {
\r
179 if(graph.isInstanceOf(r, sr.Flow)) {
\r
180 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
182 Object name = graph.getPossibleRelatedValue(head, b.HasName);
\r
184 builder.append(" - " + name);
\r
188 if (builder.indexOf(" + ") == 0)
\r
189 builder.delete(0, 3);
\r
190 return builder.toString().trim();
\r
194 } catch (DatabaseException e) {
\r
195 e.printStackTrace();
\r
201 public void addKeyListener(KeyListener listener) {
\r
202 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
207 public void addModifyListener(ModifyListener listener) {
\r
208 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
213 public void addFocusListener(FocusListener listener) {
\r
214 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r