1 /*******************************************************************************
\r
2 * Copyright (c) 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.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.ReadGraph;
\r
27 import org.simantics.db.Resource;
\r
28 import org.simantics.db.WriteGraph;
\r
29 import org.simantics.db.common.request.WriteRequest;
\r
30 import org.simantics.db.exception.DatabaseException;
\r
31 import org.simantics.db.request.Read;
\r
32 import org.simantics.layer0.Layer0;
\r
33 import org.simantics.sysdyn.SysdynResource;
\r
34 import org.simantics.ui.SimanticsUI;
\r
36 public class ConstantExpressionViewFactor 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
60 public void focus() {
\r
61 if(this.expression != null) this.expression.focus();
\r
65 public void readData(final Resource variable, Map<String, Object> data) {
\r
66 String equation = null;
\r
67 if (variable != null && data.get("equation") == null) {
\r
69 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
72 public String perform(ReadGraph graph) throws DatabaseException {
\r
73 SysdynResource sr = SysdynResource.getInstance(graph);
\r
74 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
75 if (expression != null && graph.isInstanceOf(expression, sr.ConstantExpression)) {
\r
76 return graph.getRelatedValue(expression, sr.HasEquation);
\r
83 } catch (DatabaseException e1) {
\r
84 e1.printStackTrace();
\r
86 data.put("equation", equation);
\r
91 public void replaceSelection(String var) {
\r
92 if(expression != null) {
\r
93 IDocument doc = expression.getDocument();
\r
95 Point selection = expression.getSelection();
\r
96 doc.replace(selection.x, selection.y, var);
\r
97 expression.setSelection(selection.x + var.length());
\r
98 } catch (BadLocationException e) {
\r
99 e.printStackTrace();
\r
105 public void writeData(final Resource variable, Map<String, Object> data) {
\r
106 final String currentText = expression.getExpression();
\r
107 if(currentText != null) {
\r
108 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
111 public void perform(WriteGraph g)
\r
112 throws DatabaseException {
\r
113 SysdynResource sr = SysdynResource.getInstance(g);
\r
114 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
115 Layer0 l0 = Layer0.getInstance(g);
\r
116 if(expression != null) {
\r
117 g.deny(variable, sr.HasExpression);
\r
119 expression = g.newResource();
\r
120 g.claim(expression, l0.InstanceOf, null, sr.ConstantExpression);
\r
121 g.claim(variable, sr.HasExpression, expression);
\r
122 g.claimLiteral(expression, sr.HasEquation, currentText);
\r
130 public void updateData(Map<String, Object> data) {
\r
131 if(this.expression != null && this.expression.getExpression() != null)
\r
132 data.put("equation", this.expression.getExpression());
\r
136 public List<ExpressionField> getExpressionFields() {
\r
137 return Arrays.asList(this.expression);
\r