1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\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.equation.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.graphics.Point;
\r
24 import org.eclipse.swt.widgets.Composite;
\r
25 import org.eclipse.swt.widgets.Label;
\r
26 import org.simantics.db.Builtins;
\r
27 import org.simantics.db.ReadGraph;
\r
28 import org.simantics.db.Resource;
\r
29 import org.simantics.db.WriteGraph;
\r
30 import org.simantics.db.common.request.WriteRequest;
\r
31 import org.simantics.db.exception.DatabaseException;
\r
32 import org.simantics.db.request.Read;
\r
33 import org.simantics.sysdyn.SysdynResource;
\r
34 import org.simantics.ui.SimanticsUI;
\r
36 public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor {
\r
38 private Label equationLabel;
\r
39 private ExpressionField expression;
\r
42 public void createView(Composite parent, Map<String, Object> data) {
\r
44 final String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
46 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
47 equationLabel = new Label(parent, SWT.NONE);
\r
48 equationLabel.setFont(FONT);
\r
49 equationLabel.setText("=");
\r
50 GridDataFactory.fillDefaults().applyTo(equationLabel);
\r
53 expression = new ExpressionField(parent, SWT.BORDER);
\r
54 expression.setFont(FONT);
\r
55 expression.setExpression(equation);
\r
56 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
61 public void readData(final Resource variable, Map<String, Object> data) {
\r
62 String equation = null;
\r
63 if (variable != null && data.get("equation") == null) {
\r
65 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
68 public String perform(ReadGraph graph) throws DatabaseException {
\r
69 SysdynResource sr = SysdynResource.getInstance(graph);
\r
70 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
71 if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {
\r
72 return graph.getRelatedValue(expression, sr.HasEquation);
\r
79 } catch (DatabaseException e1) {
\r
80 e1.printStackTrace();
\r
82 data.put("equation", equation);
\r
87 public void writeData(final Resource variable, Map<String, Object> data) {
\r
88 final String currentText = expression.getExpression();
\r
89 if(currentText != null) {
\r
90 data.put("equation", currentText);
\r
91 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
93 public void perform(WriteGraph g)
\r
94 throws DatabaseException {
\r
95 SysdynResource sr = SysdynResource.getInstance(g);
\r
96 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
97 Builtins b = g.getBuiltins();
\r
98 if(expression != null) {
\r
99 g.deny(variable, sr.HasExpression);
\r
101 expression = g.newResource();
\r
102 g.claim(expression, b.InstanceOf, null, sr.NormalExpression);
\r
103 g.claim(variable, sr.HasExpression, expression);
\r
104 g.claimValue(expression, sr.HasEquation, currentText);
\r
112 public void focus() {
\r
113 if(this.expression != null) this.expression.focus();
\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 updateData(Map<String, Object> data) {
\r
132 if(this.expression != null && this.expression.getExpression() != null)
\r
133 data.put("equation", this.expression.getExpression());
\r
137 public List<ExpressionField> getExpressionFields() {
\r
138 return Arrays.asList(this.expression);
\r