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.custom.VerifyKeyListener;
\r
14 import org.eclipse.swt.events.FocusListener;
\r
15 import org.eclipse.swt.events.KeyListener;
\r
16 import org.eclipse.swt.events.ModifyListener;
\r
17 import org.eclipse.swt.graphics.Point;
\r
18 import org.eclipse.swt.widgets.Composite;
\r
19 import org.eclipse.swt.widgets.Label;
\r
20 import org.eclipse.swt.widgets.Text;
\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.layer0.Layer0;
\r
28 import org.simantics.sysdyn.SysdynResource;
\r
29 import org.simantics.ui.SimanticsUI;
\r
31 public class StockExpression implements IExpression {
\r
33 private Text integral;
\r
34 private ExpressionField expression;
\r
37 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
38 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
39 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
40 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
43 Label label = new Label(parent, SWT.NONE);
\r
44 label.setText("Integral");
\r
46 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
47 integral.setEditable(false);
\r
48 integral.setText(integralEquation);
\r
49 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
52 label = new Label(parent, SWT.NONE);
\r
53 label.setText("Initial\nValue");
\r
55 expression = new ExpressionField(parent, SWT.BORDER);
\r
56 expression.setExpression(initialEquation);
\r
58 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
63 public void focus() {
\r
64 this.expression.focus();
\r
68 public List<ExpressionField> getExpressionFields() {
\r
69 return Arrays.asList(this.expression);
\r
73 public void readData(final Resource variable, Map<String, Object> data) {
\r
74 String initialEquation = null;
\r
76 if (variable != null && data.get("initialEquation") == null) {
\r
78 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
81 public String perform(ReadGraph graph) throws DatabaseException {
\r
82 SysdynResource sr = SysdynResource.getInstance(graph);
\r
83 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
84 if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {
\r
85 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
92 } catch (DatabaseException e1) {
\r
93 e1.printStackTrace();
\r
95 data.put("initialEquation", initialEquation);
\r
98 data.put("integral", getIntegral(variable));
\r
103 public void replaceSelection(String var) {
\r
104 if(expression != null) {
\r
105 IDocument doc = expression.getDocument();
\r
107 Point selection = expression.getSelection();
\r
108 doc.replace(selection.x, selection.y, var);
\r
109 expression.setSelection(selection.x + var.length());
\r
110 } catch (BadLocationException e) {
\r
111 e.printStackTrace();
\r
117 public void save(final Resource variable, Map<String, Object> data) {
\r
118 final String currentText = expression.getExpression();
\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 Layer0 l0 = Layer0.getInstance(g);
\r
128 if(expression != null && !g.isInstanceOf(expression, sr.StockExpression)) {
\r
129 g.deny(variable, sr.HasExpression);
\r
132 if(expression == null) {
\r
133 expression = g.newResource();
\r
134 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
135 g.claim(variable, sr.HasExpression, expression);
\r
137 g.claimLiteral(expression, sr.HasInitialEquation, currentText);
\r
142 expression.setExpression(currentText);
\r
146 public void updateData(Map<String, Object> data) {
\r
147 if(this.expression != null && this.expression.getExpression() != null)
\r
148 data.put("initialEquation", this.expression.getExpression());
\r
149 if(this.integral != null && this.integral.getText() != null)
\r
150 data.put("integral", this.integral.getText());
\r
154 private String getIntegral(final Resource variable) {
\r
155 String integral = "";
\r
157 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
160 public String perform(ReadGraph graph) throws DatabaseException {
\r
161 SysdynResource sr = SysdynResource.getInstance(graph);
\r
162 Layer0 l0 = Layer0.getInstance(graph);
\r
163 Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);
\r
164 Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);
\r
166 StringBuilder builder = new StringBuilder();
\r
167 builder.append("");
\r
168 for (Resource r : heads) {
\r
169 if(graph.isInstanceOf(r, sr.Flow)) {
\r
170 Resource tail = graph.getPossibleObject(r, sr.HasTail);
\r
172 Object name = graph.getPossibleRelatedValue(tail, l0.HasName);
\r
174 builder.append(" + " + name);
\r
179 for (Resource r : tails) {
\r
180 if(graph.isInstanceOf(r, sr.Flow)) {
\r
181 Resource head = graph.getPossibleObject(r, sr.HasHead);
\r
183 Object name = graph.getPossibleRelatedValue(head, l0.HasName);
\r
185 builder.append(" - " + name);
\r
189 if (builder.indexOf(" + ") == 0)
\r
190 builder.delete(0, 3);
\r
191 return builder.toString().trim();
\r
195 } catch (DatabaseException e) {
\r
196 e.printStackTrace();
\r
202 public void addKeyListener(KeyListener listener) {
\r
203 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
208 public void addModifyListener(ModifyListener listener) {
\r
209 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
214 public void addFocusListener(FocusListener listener) {
\r
215 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
219 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
220 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r