1 package org.simantics.sysdyn.ui.equation.expressions;
\r
3 import java.util.Collection;
\r
4 import java.util.Map;
\r
6 import org.eclipse.jface.layout.GridDataFactory;
\r
7 import org.eclipse.jface.layout.GridLayoutFactory;
\r
8 import org.eclipse.swt.SWT;
\r
9 import org.eclipse.swt.events.FocusAdapter;
\r
10 import org.eclipse.swt.events.FocusEvent;
\r
11 import org.eclipse.swt.events.KeyAdapter;
\r
12 import org.eclipse.swt.events.KeyEvent;
\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 Label integralLabel;
\r
31 Label equationLabel;
\r
33 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
74 expression.addKeyListener(new KeyAdapter() {
\r
77 public void keyPressed(KeyEvent e) {
\r
78 if(e.keyCode == SWT.ESC) {
\r
79 expression.setText(initialEquation);
\r
88 public void readData(final Resource variable, Map<String, Object> data) {
\r
89 String initialEquation = null;
\r
91 if (variable != null && data.get("initialEquation") == null) {
\r
93 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
96 public String perform(ReadGraph graph) throws DatabaseException {
\r
97 SysdynResource sr = SysdynResource.getInstance(graph);
\r
98 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
99 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
100 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
107 } catch (DatabaseException e1) {
\r
108 e1.printStackTrace();
\r
110 data.put("initialEquation", initialEquation);
\r
113 data.put("integral", getIntegral(variable));
\r
117 public void writeData(final Resource variable, Map<String, Object> data) {
\r
118 final String currentText = expression.getText();
\r
119 if(currentText != null) {
\r
120 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
123 public void perform(WriteGraph g)
\r
124 throws DatabaseException {
\r
125 SysdynResource sr = SysdynResource.getInstance(g);
\r
126 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
127 Builtins b = g.getBuiltins();
\r
128 if(expression != null) {
\r
129 g.deny(variable, sr.HasExpression);
\r
131 expression = g.newResource();
\r
132 g.claim(expression, b.InstanceOf, sr.StockExpression);
\r
133 g.claim(variable, sr.HasExpression, expression);
\r
134 g.claimValue(expression, sr.HasInitialEquation, currentText);
\r
141 private String getIntegral(final Resource variable) {
\r
142 String integral = "";
\r
144 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
147 public String perform(ReadGraph graph) throws DatabaseException {
\r
148 SysdynResource sr = SysdynResource.getInstance(graph);
\r
149 Builtins b = graph.getBuiltins();
\r
150 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
151 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
153 StringBuilder builder = new StringBuilder();
\r
154 builder.append("");
\r
155 for (Resource r : heads) {
\r
156 if(graph.isInstanceOf(r, sr.Flow)) {
\r
157 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
159 Object name = graph.getPossibleRelatedValue(tail, b.HasName);
\r
161 builder.append(" + " + name);
\r
165 for (Resource r : tails) {
\r
166 if(graph.isInstanceOf(r, sr.Flow)) {
\r
167 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
169 Object name = graph.getPossibleRelatedValue(head, b.HasName);
\r
171 builder.append(" - " + name);
\r
175 if (builder.indexOf(" + ") == 0)
\r
176 builder.delete(0, 3);
\r
177 return builder.toString().trim();
\r
181 } catch (DatabaseException e) {
\r
182 e.printStackTrace();
\r
188 public void focus() {
\r
189 if(this.expression != null) this.expression.forceFocus();
\r
193 public void replaceSelection(String var) {
\r
194 if(this.expression != null) {
\r
195 String oldExpression = expression.getText();
\r
196 if(lastSelection == null)
\r
197 lastSelection = new Point(0,0);
\r
198 String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);
\r
199 expression.setText(newExpression);
\r
200 expression.setSelection(lastSelection.x + var.length());
\r