1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.util.Arrays;
\r
4 import java.util.List;
\r
5 import java.util.Map;
\r
7 import org.eclipse.jface.layout.GridDataFactory;
\r
8 import org.eclipse.jface.layout.GridLayoutFactory;
\r
9 import org.eclipse.jface.text.BadLocationException;
\r
10 import org.eclipse.jface.text.IDocument;
\r
11 import org.eclipse.swt.SWT;
\r
12 import org.eclipse.swt.events.FocusListener;
\r
13 import org.eclipse.swt.events.KeyListener;
\r
14 import org.eclipse.swt.events.ModifyListener;
\r
15 import org.eclipse.swt.graphics.Point;
\r
16 import org.eclipse.swt.widgets.Composite;
\r
17 import org.eclipse.swt.widgets.Label;
\r
18 import org.simantics.db.Builtins;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.request.Read;
\r
25 import org.simantics.sysdyn.SysdynResource;
\r
26 import org.simantics.ui.SimanticsUI;
\r
28 public class BasicExpression implements IExpression {
\r
30 private ExpressionField expression;
\r
31 protected Resource expressionType;
\r
34 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
35 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
36 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
38 Label l = new Label(parent, SWT.NONE);
\r
41 expression = new ExpressionField(parent, SWT.BORDER);
\r
42 expression.setExpression(equation);
\r
43 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
48 public void focus() {
\r
49 this.expression.focus();
\r
54 public List<ExpressionField> getExpressionFields() {
\r
55 return Arrays.asList(this.expression);
\r
59 public void readData(final Resource variable, Map<String, Object> data) {
\r
60 String equation = null;
\r
61 if (variable != null && data.get("equation") == null) {
\r
63 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
66 public String perform(ReadGraph graph) throws DatabaseException {
\r
67 SysdynResource sr = SysdynResource.getInstance(graph);
\r
68 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
69 if (expression != null) {
\r
70 String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);
\r
71 if(equation != null)
\r
80 } catch (DatabaseException e1) {
\r
81 e1.printStackTrace();
\r
83 data.put("equation", equation);
\r
88 public void replaceSelection(String var) {
\r
89 if(expression != null) {
\r
90 IDocument doc = expression.getDocument();
\r
92 Point selection = expression.getSelection();
\r
93 doc.replace(selection.x, selection.y, var);
\r
94 expression.setSelection(selection.x + var.length());
\r
95 } catch (BadLocationException e) {
\r
96 e.printStackTrace();
\r
102 public void save(final Resource variable, Map<String, Object> data) {
\r
103 final String currentText = expression.getExpression();
\r
104 final String oldEquation = (String)data.get("equation");
\r
105 if(oldEquation == null ||
\r
106 (currentText != null && expressionType != null)) {
\r
107 data.put("equation", currentText);
\r
108 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
110 public void perform(WriteGraph g)
\r
111 throws DatabaseException {
\r
112 SysdynResource sr = SysdynResource.getInstance(g);
\r
113 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
114 if(ExpressionUtils.isParameter(currentText)) {
\r
115 if(!expressionType.equals(sr.ConstantExpression))
\r
116 expressionType = sr.ParameterExpression;
\r
118 expressionType = sr.NormalExpression;
\r
120 if (oldEquation != null
\r
121 && expression != null
\r
122 && g.isInstanceOf(expression, expressionType)
\r
123 && currentText.equals(oldEquation)) {
\r
126 Builtins b = g.getBuiltins();
\r
127 if(expression != null && !g.isInstanceOf(expression, expressionType)) {
\r
128 g.deny(variable, sr.HasExpression);
\r
131 if(expression == null) {
\r
132 expression = g.newResource();
\r
133 g.claim(expression, b.InstanceOf, null, expressionType);
\r
134 g.claim(variable, sr.HasExpression, expression);
\r
136 g.claimLiteral(expression, sr.HasEquation, currentText);
\r
144 public void updateData(Map<String, Object> data) {
\r
145 if(this.expression != null && this.expression.getExpression() != null)
\r
146 data.put("equation", this.expression.getExpression());
\r
150 public void addKeyListener(KeyListener listener) {
\r
151 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
156 public void addModifyListener(ModifyListener listener) {
\r
157 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
162 public void addFocusListener(FocusListener listener) {
\r
163 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r