1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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.db.ReadGraph;
\r
37 import org.simantics.db.Resource;
\r
38 import org.simantics.db.WriteGraph;
\r
39 import org.simantics.db.common.request.WriteRequest;
\r
40 import org.simantics.db.common.utils.ListUtils;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.layer0.util.RemoverUtil;
\r
43 import org.simantics.db.request.Read;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.sysdyn.SysdynResource;
\r
46 import org.simantics.sysdyn.manager.SysdynModel;
\r
47 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
48 import org.simantics.sysdyn.representation.Stock;
\r
49 import org.simantics.sysdyn.representation.Valve;
\r
50 import org.simantics.ui.SimanticsUI;
\r
51 import org.simantics.utils.datastructures.Pair;
\r
53 public class StockExpression implements IExpression {
\r
55 private final ExpressionWidgetInput input;
\r
56 private ExpressionField integralExpression, initialExpression;
\r
57 private ExpressionField lastSelectedText;
\r
59 public StockExpression(ExpressionWidgetInput input) {
\r
64 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
65 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
66 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
67 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
70 Label label = new Label(parent, SWT.NONE);
\r
71 label.setText("Integral");
\r
73 Composite integralComposite = new Composite(parent, SWT.NONE);
\r
74 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);
\r
75 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);
\r
77 integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);
\r
78 integralExpression.setExpression(integralEquation);
\r
79 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);
\r
80 integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
83 public void focusLost(FocusEvent e) {
\r
84 lastSelectedText = integralExpression;
\r
88 Button defaultButton = new Button(integralComposite, SWT.PUSH);
\r
89 defaultButton.setText("Use default");
\r
90 defaultButton.addListener(SWT.Selection, new Listener() {
\r
93 public void handleEvent(Event event) {
\r
94 switch (event.type) {
\r
96 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));
\r
97 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
100 public void perform(WriteGraph graph)
\r
101 throws DatabaseException {
\r
102 // Delete the possible integral expression from the database to note
\r
103 // that we are in the "default" mode.
\r
104 SysdynResource sr = SysdynResource.getInstance(graph);
\r
105 graph.deny(input.expression, sr.StockExpression_integralEquation);
\r
114 label = new Label(parent, SWT.NONE);
\r
115 label.setText("Initial\nValue");
\r
117 initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);
\r
118 initialExpression.setExpression(initialEquation);
\r
119 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);
\r
120 initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
123 public void focusLost(FocusEvent e) {
\r
124 lastSelectedText = initialExpression;
\r
128 lastSelectedText = initialExpression;
\r
132 public void focus() {
\r
133 this.lastSelectedText.focus();
\r
137 public List<ExpressionField> getExpressionFields() {
\r
138 return Arrays.asList(this.integralExpression, this.initialExpression);
\r
142 public void readData(final Resource expression, Map<String, Object> data) {
\r
143 Pair<String, String> equations = null;
\r
144 if (expression != null && data.get("initialEquation") == null) {
\r
146 equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {
\r
149 public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {
\r
150 SysdynResource sr = SysdynResource.getInstance(graph);
\r
151 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
152 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);
\r
153 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);
\r
154 initialEquation = (initialEquation != null) ? initialEquation : "";
\r
155 return new Pair<String, String>(integralEquation, initialEquation);
\r
157 return new Pair<String, String>(null, "");
\r
162 } catch (DatabaseException e1) {
\r
163 e1.printStackTrace();
\r
165 data.put("initialEquation", equations.second);
\r
168 if (equations.first == null) {
\r
169 data.put("integral", getDefaultIntegral(expression));
\r
171 data.put("integral", equations.first);
\r
177 public void replaceSelection(String var) {
\r
178 if(lastSelectedText != null) {
\r
179 IDocument doc = lastSelectedText.getDocument();
\r
181 Point selection = lastSelectedText.getSelection();
\r
182 doc.replace(selection.x, selection.y, var);
\r
183 lastSelectedText.setSelection(selection.x + var.length());
\r
184 } catch (BadLocationException e) {
\r
185 e.printStackTrace();
\r
191 public void save(final Resource expression, final Map<String, Object> data) {
\r
192 final String currentInitial = this.initialExpression.getExpression();
\r
193 final String currentIntegral = this.integralExpression.getExpression();
\r
194 if(currentInitial == null
\r
195 || !currentInitial.equals(data.get("initialEquation"))
\r
196 || (currentIntegral != null
\r
197 && !currentIntegral.equals(data.get("integral")))) {
\r
198 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
201 public void perform(WriteGraph g)
\r
202 throws DatabaseException {
\r
203 SysdynResource sr = SysdynResource.getInstance(g);
\r
204 Layer0 l0 = Layer0.getInstance(g);
\r
206 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
207 Resource variable = g.getPossibleObject(expression, l0.PartOf);
\r
208 Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);
\r
209 Resource temp = g.newResource();
\r
210 ListUtils.replace(g, expressionList, expression, temp);
\r
212 for(Resource predicate : g.getPredicates(expression)) {
\r
213 g.deny(expression, predicate);
\r
215 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
217 ListUtils.replace(g, expressionList, temp, expression);
\r
219 RemoverUtil.remove(g, temp);
\r
221 g.claim(expression, l0.PartOf, variable);
\r
223 g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);
\r
225 // If the value is same as default, do not save to database.
\r
226 if (currentIntegral != null && !currentIntegral.equals(data.get("integral"))) {
\r
227 g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);
\r
233 this.initialExpression.setExpression(currentInitial);
\r
234 if (currentIntegral != null)
\r
235 this.integralExpression.setExpression(currentIntegral);
\r
239 public void updateData(Map<String, Object> data) {
\r
240 if(this.initialExpression != null && this.initialExpression.getExpression() != null)
\r
241 data.put("initialEquation", this.initialExpression.getExpression());
\r
242 if(this.integralExpression != null && this.integralExpression.getExpression() != null)
\r
243 data.put("integral", this.integralExpression.getExpression());
\r
246 private String getDefaultIntegral(final Resource expression) {
\r
248 String integral = "";
\r
249 if(expression == null)
\r
252 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
255 public String perform(ReadGraph graph) throws DatabaseException {
\r
256 SysdynResource sr = SysdynResource.getInstance(graph);
\r
257 Layer0 l0 = Layer0.getInstance(graph);
\r
259 // find the variable
\r
260 Resource variable = graph.getPossibleObject(expression, l0.PartOf);
\r
261 if(variable == null)
\r
264 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
265 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
266 model.update(graph);
\r
268 Stock stock = (Stock)model.getElement(variable);
\r
270 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);
\r
274 StringBuilder builder = new StringBuilder();
\r
275 builder.append("");
\r
276 for(Valve in : stock.getIncomingValves()) {
\r
277 builder.append(" + " + in.getName() + range);
\r
279 for(Valve out : stock.getOutgoingValves()) {
\r
280 builder.append(" - " + out.getName() + range);
\r
282 if (builder.indexOf(" + ") == 0)
\r
283 builder.delete(0, 3);
\r
285 return builder.toString().trim();
\r
289 } catch (DatabaseException e) {
\r
290 e.printStackTrace();
\r
296 public void addKeyListener(KeyListener listener) {
\r
297 this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
298 this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
303 public void addModifyListener(ModifyListener listener) {
\r
304 this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
305 this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
310 public void addFocusListener(FocusListener listener) {
\r
311 this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
312 this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
316 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
317 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
318 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r