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.FocusAdapter;
\r
14 import org.eclipse.swt.events.FocusEvent;
\r
15 import org.eclipse.swt.events.FocusListener;
\r
16 import org.eclipse.swt.events.KeyListener;
\r
17 import org.eclipse.swt.events.ModifyListener;
\r
18 import org.eclipse.swt.graphics.Point;
\r
19 import org.eclipse.swt.widgets.Composite;
\r
20 import org.eclipse.swt.widgets.Label;
\r
21 import org.eclipse.swt.widgets.Text;
\r
22 import org.simantics.db.Builtins;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.WriteGraph;
\r
26 import org.simantics.db.common.request.WriteRequest;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.request.Read;
\r
29 import org.simantics.sysdyn.SysdynResource;
\r
30 import org.simantics.ui.SimanticsUI;
\r
32 public class StockExpression implements IExpression {
\r
34 private Text integral;
\r
35 private ExpressionField expression;
\r
36 private Point caretPosition; // Bug or something in source viewer's text widget. Cursor goes to 0, 0 on focus lost
\r
39 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
40 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
41 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
42 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
45 Label label = new Label(parent, SWT.NONE);
\r
46 label.setText("Integral");
\r
48 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
49 integral.setEditable(false);
\r
50 integral.setText(integralEquation);
\r
51 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
54 label = new Label(parent, SWT.NONE);
\r
55 label.setText("Initial\nValue");
\r
57 expression = new ExpressionField(parent, SWT.BORDER);
\r
58 expression.setExpression(initialEquation);
\r
60 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
62 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
65 public void focusLost(FocusEvent e) {
\r
66 caretPosition = expression.getSelection();
\r
73 public void focus() {
\r
74 if(this.expression != null && caretPosition != null) {
\r
75 this.expression.focus();
\r
76 expression.getSourceViewer().getTextWidget().setSelection(caretPosition.x, caretPosition.x + caretPosition.y);
\r
82 public List<ExpressionField> getExpressionFields() {
\r
83 return Arrays.asList(this.expression);
\r
87 public void readData(final Resource variable, Map<String, Object> data) {
\r
88 String initialEquation = null;
\r
90 if (variable != null && data.get("initialEquation") == null) {
\r
92 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
95 public String perform(ReadGraph graph) throws DatabaseException {
\r
96 SysdynResource sr = SysdynResource.getInstance(graph);
\r
97 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
98 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
99 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
106 } catch (DatabaseException e1) {
\r
107 e1.printStackTrace();
\r
109 data.put("initialEquation", initialEquation);
\r
112 data.put("integral", getIntegral(variable));
\r
117 public void replaceSelection(String var) {
\r
118 if(expression != null) {
\r
119 IDocument doc = expression.getDocument();
\r
121 Point selection = expression.getSelection();
\r
122 doc.replace(selection.x, selection.y, var);
\r
123 expression.setSelection(selection.x + var.length());
\r
124 } catch (BadLocationException e) {
\r
125 e.printStackTrace();
\r
131 public void save(final Resource variable, Map<String, Object> data) {
\r
132 final String currentText = expression.getExpression();
\r
133 if(currentText != null) {
\r
134 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
137 public void perform(WriteGraph g)
\r
138 throws DatabaseException {
\r
139 SysdynResource sr = SysdynResource.getInstance(g);
\r
140 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
141 Builtins b = g.getBuiltins();
\r
142 if(expression != null) {
\r
143 g.deny(variable, sr.HasExpression);
\r
145 expression = g.newResource();
\r
146 g.claim(expression, b.InstanceOf, null, sr.StockExpression);
\r
147 g.claim(variable, sr.HasExpression, expression);
\r
148 g.claimValue(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 Builtins b = graph.getBuiltins();
\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, b.HasName);
\r
184 if (name != null) {
\r
185 System.out.println("STOCK += " + name);
\r
186 builder.append(" + " + name);
\r
191 for (Resource r : tails) {
\r
192 if(graph.isInstanceOf(r, sr.Flow)) {
\r
193 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
195 Object name = graph.getPossibleRelatedValue(head, b.HasName);
\r
197 builder.append(" - " + name);
\r
201 if (builder.indexOf(" + ") == 0)
\r
202 builder.delete(0, 3);
\r
203 return builder.toString().trim();
\r
207 } catch (DatabaseException e) {
\r
208 e.printStackTrace();
\r
214 public void addKeyListener(KeyListener listener) {
\r
215 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
220 public void addModifyListener(ModifyListener listener) {
\r
221 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
226 public void addFocusListener(FocusListener listener) {
\r
227 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r