1 package org.simantics.sysdyn.ui.equation.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.swt.SWT;
\r
11 import org.eclipse.swt.events.FocusAdapter;
\r
12 import org.eclipse.swt.events.FocusEvent;
\r
13 import org.eclipse.swt.graphics.Point;
\r
14 import org.eclipse.swt.widgets.Composite;
\r
15 import org.eclipse.swt.widgets.Label;
\r
16 import org.eclipse.swt.widgets.Text;
\r
17 import org.simantics.db.Builtins;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.request.Read;
\r
24 import org.simantics.sysdyn.SysdynResource;
\r
25 import org.simantics.ui.SimanticsUI;
\r
27 public class StockExpressionViewFactor implements IExpressionViewFactor {
\r
29 private Label integralLabel;
\r
30 private Text integral;
\r
31 private Label equationLabel;
\r
32 private Text expression;
\r
33 private Point lastSelection = new Point(0,0);
\r
36 public void createView(Composite parent, Map<String, Object> data) {
\r
38 final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
39 final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
41 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
\r
42 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
44 integralLabel = new Label(parent, SWT.NONE);
\r
45 integralLabel.setFont(FONT);
\r
46 integralLabel.setText("Integral");
\r
47 GridDataFactory.fillDefaults().applyTo(integralLabel);
\r
49 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
50 integral.setEditable(false);
\r
51 integral.setFont(FONT);
\r
52 integral.setText(integralEquation);
\r
53 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
55 equationLabel = new Label(parent, SWT.NONE);
\r
56 equationLabel.setFont(FONT);
\r
57 equationLabel.setText("Initial\nValue");
\r
58 GridDataFactory.fillDefaults().applyTo(equationLabel);
\r
61 expression = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
62 expression.setFont(FONT);
\r
63 expression.setText(initialEquation);
\r
64 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
66 expression.addFocusListener(new FocusAdapter() {
\r
69 public void focusLost(FocusEvent e) {
\r
70 lastSelection = expression.getSelection();
\r
77 public void readData(final Resource variable, Map<String, Object> data) {
\r
78 String initialEquation = null;
\r
80 if (variable != null && data.get("initialEquation") == null) {
\r
82 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
85 public String perform(ReadGraph graph) throws DatabaseException {
\r
86 SysdynResource sr = SysdynResource.getInstance(graph);
\r
87 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
88 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
89 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
96 } catch (DatabaseException e1) {
\r
97 e1.printStackTrace();
\r
99 data.put("initialEquation", initialEquation);
\r
102 data.put("integral", getIntegral(variable));
\r
106 public void writeData(final Resource variable, Map<String, Object> data) {
\r
107 final String currentText = expression.getText();
\r
108 if(currentText != null) {
\r
109 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
112 public void perform(WriteGraph g)
\r
113 throws DatabaseException {
\r
114 SysdynResource sr = SysdynResource.getInstance(g);
\r
115 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
116 Builtins b = g.getBuiltins();
\r
117 if(expression != null) {
\r
118 g.deny(variable, sr.HasExpression);
\r
120 expression = g.newResource();
\r
121 g.claim(expression, b.InstanceOf, sr.StockExpression);
\r
122 g.claim(variable, sr.HasExpression, expression);
\r
123 g.claimValue(expression, sr.HasInitialEquation, currentText);
\r
130 private String getIntegral(final Resource variable) {
\r
131 String integral = "";
\r
133 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
136 public String perform(ReadGraph graph) throws DatabaseException {
\r
137 SysdynResource sr = SysdynResource.getInstance(graph);
\r
138 Builtins b = graph.getBuiltins();
\r
139 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
140 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
142 StringBuilder builder = new StringBuilder();
\r
143 builder.append("");
\r
144 for (Resource r : heads) {
\r
145 if(graph.isInstanceOf(r, sr.Flow)) {
\r
146 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
148 Object name = graph.getPossibleRelatedValue(tail, b.HasName);
\r
150 builder.append(" + " + name);
\r
154 for (Resource r : tails) {
\r
155 if(graph.isInstanceOf(r, sr.Flow)) {
\r
156 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
158 Object name = graph.getPossibleRelatedValue(head, b.HasName);
\r
160 builder.append(" - " + name);
\r
164 if (builder.indexOf(" + ") == 0)
\r
165 builder.delete(0, 3);
\r
166 return builder.toString().trim();
\r
170 } catch (DatabaseException e) {
\r
171 e.printStackTrace();
\r
177 public void focus() {
\r
178 if(this.expression != null) this.expression.forceFocus();
\r
182 public void replaceSelection(String var) {
\r
183 if(this.expression != null) {
\r
184 String oldExpression = expression.getText();
\r
185 if(lastSelection == null)
\r
186 lastSelection = new Point(0,0);
\r
187 String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);
\r
188 expression.setText(newExpression);
\r
189 expression.setSelection(lastSelection.x + var.length());
\r
195 public void updateData(Map<String, Object> data) {
\r
196 if(this.expression != null && this.expression.getText() != null)
\r
197 data.put("initialEquation", this.expression.getText());
\r
198 if(this.integral != null && this.integral.getText() != null)
\r
199 data.put("integral", this.integral.getText());
\r
203 public List<Text> getExpressionFields() {
\r
204 return Arrays.asList(this.expression);
\r