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.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.FocusListener;
\r
25 import org.eclipse.swt.events.KeyListener;
\r
26 import org.eclipse.swt.events.ModifyListener;
\r
27 import org.eclipse.swt.graphics.Point;
\r
28 import org.eclipse.swt.widgets.Composite;
\r
29 import org.eclipse.swt.widgets.Label;
\r
30 import org.simantics.db.ReadGraph;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.WriteGraph;
\r
33 import org.simantics.db.common.request.WriteRequest;
\r
34 import org.simantics.db.common.utils.OrderedSetUtils;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.db.request.Read;
\r
37 import org.simantics.layer0.Layer0;
\r
38 import org.simantics.sysdyn.SysdynResource;
\r
39 import org.simantics.ui.SimanticsUI;
\r
41 public class BasicExpression implements IExpression {
\r
43 private ExpressionField expression;
\r
44 protected Resource expressionType;
\r
47 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
48 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
49 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
51 Label l = new Label(parent, SWT.NONE);
\r
54 expression = new ExpressionField(parent, SWT.BORDER);
\r
55 expression.setExpression(equation);
\r
56 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
61 public void focus() {
\r
62 this.expression.focus();
\r
67 public List<ExpressionField> getExpressionFields() {
\r
68 return Arrays.asList(this.expression);
\r
72 public void readData(final Resource expression, Map<String, Object> data) {
\r
73 String equation = null;
\r
74 if (expression != null && data.get("equation") == null) {
\r
76 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
79 public String perform(ReadGraph graph) throws DatabaseException {
\r
80 SysdynResource sr = SysdynResource.getInstance(graph);
\r
81 if (expression != null) {
\r
82 String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);
\r
83 if(equation != null)
\r
92 } catch (DatabaseException e1) {
\r
93 e1.printStackTrace();
\r
95 data.put("equation", equation);
\r
100 public void replaceSelection(String var) {
\r
101 if(expression != null) {
\r
102 IDocument doc = expression.getDocument();
\r
104 Point selection = expression.getSelection();
\r
105 doc.replace(selection.x, selection.y, var);
\r
106 expression.setSelection(selection.x + var.length());
\r
107 } catch (BadLocationException e) {
\r
108 e.printStackTrace();
\r
114 public void save(final Resource expression, Map<String, Object> data) {
\r
115 final String currentText = this.expression.getExpression();
\r
116 final String oldEquation = (String)data.get("equation");
\r
117 if(oldEquation == null ||
\r
118 (currentText != null && expressionType != null)) {
\r
119 data.put("equation", currentText);
\r
120 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
122 public void perform(WriteGraph g)
\r
123 throws DatabaseException {
\r
124 SysdynResource sr = SysdynResource.getInstance(g);
\r
125 if(ExpressionUtils.isParameter(currentText)) {
\r
126 if(!expressionType.equals(sr.ConstantExpression))
\r
127 expressionType = sr.ParameterExpression;
\r
129 expressionType = sr.NormalExpression;
\r
131 if (oldEquation != null
\r
132 && expression != null
\r
133 && g.isInstanceOf(expression, expressionType)
\r
134 && currentText.equals(oldEquation)) {
\r
138 Layer0 l0 = Layer0.getInstance(g);
\r
140 g.deny(expression, l0.InstanceOf);
\r
141 g.deny(expression, sr.HasEquation);
\r
143 g.claim(expression, l0.InstanceOf, null, expressionType);
\r
144 g.claimLiteral(expression, sr.HasEquation, currentText);
\r
152 public void updateData(Map<String, Object> data) {
\r
153 if(this.expression != null && this.expression.getExpression() != null)
\r
154 data.put("equation", this.expression.getExpression());
\r
158 public void addKeyListener(KeyListener listener) {
\r
159 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
164 public void addModifyListener(ModifyListener listener) {
\r
165 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
170 public void addFocusListener(FocusListener listener) {
\r
171 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
175 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
176 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r