1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2014 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.jface.text.BadLocationException;
\r
21 import org.eclipse.jface.text.IDocument;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.custom.VerifyKeyListener;
\r
24 import org.eclipse.swt.events.FocusAdapter;
\r
25 import org.eclipse.swt.events.FocusEvent;
\r
26 import org.eclipse.swt.events.FocusListener;
\r
27 import org.eclipse.swt.events.KeyListener;
\r
28 import org.eclipse.swt.events.ModifyListener;
\r
29 import org.eclipse.swt.graphics.Point;
\r
30 import org.eclipse.swt.widgets.Button;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.Event;
\r
33 import org.eclipse.swt.widgets.Label;
\r
34 import org.eclipse.swt.widgets.Listener;
\r
35 import org.eclipse.swt.widgets.Table;
\r
36 import org.simantics.databoard.Bindings;
\r
37 import org.simantics.db.ReadGraph;
\r
38 import org.simantics.db.Resource;
\r
39 import org.simantics.db.WriteGraph;
\r
40 import org.simantics.db.common.CommentMetadata;
\r
41 import org.simantics.db.common.request.ReadRequest;
\r
42 import org.simantics.db.common.request.WriteRequest;
\r
43 import org.simantics.db.common.utils.ListUtils;
\r
44 import org.simantics.db.exception.DatabaseException;
\r
45 import org.simantics.db.layer0.util.RemoverUtil;
\r
46 import org.simantics.db.request.Read;
\r
47 import org.simantics.layer0.Layer0;
\r
48 import org.simantics.sysdyn.SysdynResource;
\r
49 import org.simantics.sysdyn.manager.SysdynModel;
\r
50 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
51 import org.simantics.sysdyn.representation.Stock;
\r
52 import org.simantics.sysdyn.representation.Valve;
\r
53 import org.simantics.ui.SimanticsUI;
\r
54 import org.simantics.utils.datastructures.Pair;
\r
56 public class StockExpression implements IExpression {
\r
58 private final ExpressionWidgetInput input;
\r
59 private ExpressionField integralExpression, initialExpression;
\r
60 private ExpressionField lastSelectedText;
\r
62 public StockExpression(ExpressionWidgetInput input) {
\r
67 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
68 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
69 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
70 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);
\r
73 Label label = new Label(parent, SWT.NONE);
\r
74 label.setText("Integral");
\r
76 Composite integralComposite = new Composite(parent, SWT.NONE);
\r
77 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);
\r
78 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);
\r
80 integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);
\r
81 integralExpression.setExpression(integralEquation);
\r
82 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);
\r
83 integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
86 public void focusLost(FocusEvent e) {
\r
87 lastSelectedText = integralExpression;
\r
91 Button defaultButton = new Button(integralComposite, SWT.PUSH);
\r
92 defaultButton.setText("Use default");
\r
93 defaultButton.addListener(SWT.Selection, new Listener() {
\r
96 public void handleEvent(Event event) {
\r
97 switch (event.type) {
\r
99 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));
\r
100 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
103 public void perform(WriteGraph graph)
\r
104 throws DatabaseException {
\r
105 // Delete the possible integral expression from the database to note
\r
106 // that we are in the "default" mode.
\r
107 graph.startUndoContext();
\r
108 SysdynResource sr = SysdynResource.getInstance(graph);
\r
109 graph.deny(input.expression, sr.StockExpression_useCustomIntegral);
\r
110 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
112 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
113 graph.addMetadata(cm.add("Use default"));
\r
123 label = new Label(parent, SWT.NONE);
\r
124 label.setText("Initial\nValue");
\r
126 initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);
\r
127 initialExpression.setExpression(initialEquation);
\r
128 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);
\r
129 initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
132 public void focusLost(FocusEvent e) {
\r
133 lastSelectedText = initialExpression;
\r
137 lastSelectedText = initialExpression;
\r
141 public void focus() {
\r
142 this.lastSelectedText.focus();
\r
146 public List<ExpressionField> getExpressionFields() {
\r
147 return Arrays.asList(this.integralExpression, this.initialExpression);
\r
151 public void readData(final Resource expression, final Map<String, Object> data) {
\r
152 Pair<String, String> equations = null;
\r
153 if (expression != null && data.get("initialEquation") == null) {
\r
155 equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {
\r
158 public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {
\r
159 SysdynResource sr = SysdynResource.getInstance(graph);
\r
160 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
161 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);
\r
162 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);
\r
163 initialEquation = (initialEquation != null) ? initialEquation : "";
\r
164 return new Pair<String, String>(integralEquation, initialEquation);
\r
166 return new Pair<String, String>(null, "");
\r
171 } catch (DatabaseException e1) {
\r
172 e1.printStackTrace();
\r
174 data.put("initialEquation", equations.second);
\r
177 if (equations.first == null) {
\r
179 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
182 public void perform(WriteGraph graph) throws DatabaseException {
\r
183 SysdynResource sr = SysdynResource.getInstance(graph);
\r
184 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
185 graph.startUndoContext();
\r
186 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
187 graph.addMetadata(cm.add("Set expression"));
\r
191 } catch (DatabaseException e) {
\r
192 // TODO Auto-generated catch block
\r
193 e.printStackTrace();
\r
196 data.put("integral", getDefaultIntegral(expression));
\r
199 final String integral = equations.first;
\r
200 SimanticsUI.getSession().syncRequest(new ReadRequest() {
\r
203 public void run(ReadGraph graph) throws DatabaseException {
\r
204 SysdynResource sr = SysdynResource.getInstance(graph);
\r
205 if (graph.hasStatement(input.expression, sr.StockExpression_useCustomIntegral)) {
\r
206 data.put("integral", integral);
\r
208 data.put("integral", getDefaultIntegral(graph, expression));
\r
213 } catch (DatabaseException e) {
\r
214 data.put("integral", equations.first);
\r
215 e.printStackTrace();
\r
222 public void replaceSelection(String var) {
\r
223 if(lastSelectedText != null) {
\r
224 IDocument doc = lastSelectedText.getDocument();
\r
226 Point selection = lastSelectedText.getSelection();
\r
227 doc.replace(selection.x, selection.y, var);
\r
228 lastSelectedText.setSelection(selection.x + var.length());
\r
229 } catch (BadLocationException e) {
\r
230 e.printStackTrace();
\r
236 public void save(final Resource expression, final Map<String, Object> data) {
\r
237 final String currentInitial = this.initialExpression.getExpression();
\r
238 final String currentIntegral = this.integralExpression.getExpression();
\r
239 if (currentIntegral == null) {
\r
240 this.integralExpression.setExpression(getDefaultIntegral(expression));
\r
242 if(!currentInitial.equals(data.get("initialEquation"))
\r
243 || currentIntegral == null
\r
244 || !currentIntegral.equals(data.get("integral"))) {
\r
245 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
248 public void perform(WriteGraph g)
\r
249 throws DatabaseException {
\r
250 SysdynResource sr = SysdynResource.getInstance(g);
\r
251 Layer0 l0 = Layer0.getInstance(g);
\r
253 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
254 Resource variable = g.getPossibleObject(expression, l0.PartOf);
\r
255 Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);
\r
256 Resource temp = g.newResource();
\r
257 ListUtils.replace(g, expressionList, expression, temp);
\r
259 for(Resource predicate : g.getPredicates(expression)) {
\r
260 g.deny(expression, predicate);
\r
262 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
264 ListUtils.replace(g, expressionList, temp, expression);
\r
266 RemoverUtil.remove(g, temp);
\r
268 g.claim(expression, l0.PartOf, variable);
\r
270 g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);
\r
272 if (!currentIntegral.equals(g.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation, Bindings.STRING))) {
\r
273 // If the value is not same as default, enable the custom tag
\r
274 g.claim(expression, sr.StockExpression_useCustomIntegral, expression);
\r
276 g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);
\r
278 g.startUndoContext();
\r
279 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
\r
280 g.addMetadata(cm.add("Save"));
\r
286 this.initialExpression.setExpression(currentInitial);
\r
287 this.integralExpression.setExpression(currentIntegral);
\r
291 public void updateData(Map<String, Object> data) {
\r
292 if(this.initialExpression != null && this.initialExpression.getExpression() != null)
\r
293 data.put("initialEquation", this.initialExpression.getExpression());
\r
294 if(this.integralExpression != null && this.integralExpression.getExpression() != null)
\r
295 data.put("integral", this.integralExpression.getExpression());
\r
299 private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {
\r
300 SysdynResource sr = SysdynResource.getInstance(graph);
\r
301 Layer0 l0 = Layer0.getInstance(graph);
\r
303 // find the variable
\r
304 Resource variable = graph.getPossibleObject(expression, l0.PartOf);
\r
305 if(variable == null)
\r
308 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
309 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
310 model.update(graph);
\r
312 Stock stock = (Stock)model.getElement(variable);
\r
314 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);
\r
318 StringBuilder builder = new StringBuilder();
\r
319 builder.append("");
\r
320 for(Valve in : stock.getIncomingValves()) {
\r
321 builder.append(" + " + in.getName() + range);
\r
323 for(Valve out : stock.getOutgoingValves()) {
\r
324 builder.append(" - " + out.getName() + range);
\r
326 if (builder.indexOf(" + ") == 0)
\r
327 builder.delete(0, 3);
\r
329 return builder.toString().trim();
\r
333 private static String getDefaultIntegral(final Resource expression) {
\r
335 String integral = "";
\r
336 if(expression == null)
\r
339 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
342 public String perform(ReadGraph graph) throws DatabaseException {
\r
343 return getDefaultIntegral(graph, expression);
\r
347 } catch (DatabaseException e) {
\r
348 e.printStackTrace();
\r
354 public void addKeyListener(KeyListener listener) {
\r
355 this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
356 this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
361 public void addModifyListener(ModifyListener listener) {
\r
362 this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
363 this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
368 public void addFocusListener(FocusListener listener) {
\r
369 this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
370 this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
374 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
375 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
376 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r