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.text.BadLocationException;
\r
9 import org.eclipse.jface.text.IDocument;
\r
10 import org.eclipse.swt.SWT;
\r
11 import org.eclipse.swt.events.FocusListener;
\r
12 import org.eclipse.swt.events.KeyListener;
\r
13 import org.eclipse.swt.events.ModifyListener;
\r
14 import org.eclipse.swt.graphics.Point;
\r
15 import org.eclipse.swt.widgets.Composite;
\r
16 import org.eclipse.swt.widgets.Label;
\r
17 import org.simantics.db.Builtins;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.request.Read;
\r
24 import org.simantics.sysdyn.SysdynResource;
\r
25 import org.simantics.sysdyn.ui.properties.EquationTab;
\r
26 import org.simantics.ui.SimanticsUI;
\r
28 public class BasicExpression implements IExpression {
\r
30 ExpressionField expression;
\r
31 Resource expressionType;
\r
34 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
35 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
37 Label l = new Label(parent, SWT.NONE);
\r
38 l.setFont(EquationTab.FONT);
\r
41 expression = new ExpressionField(parent, SWT.BORDER);
\r
42 expression.setExpression(equation);
\r
43 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
47 public void focus() {
\r
48 if(this.expression != null) this.expression.focus();
\r
53 public List<ExpressionField> getExpressionFields() {
\r
54 return Arrays.asList(this.expression);
\r
58 public void readData(final Resource variable, Map<String, Object> data) {
\r
59 String equation = null;
\r
60 if (variable != null && data.get("equation") == null && expressionType != null) {
\r
62 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
65 public String perform(ReadGraph graph) throws DatabaseException {
\r
66 SysdynResource sr = SysdynResource.getInstance(graph);
\r
67 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
68 if (expression != null && graph.isInstanceOf(expression, expressionType)) {
\r
69 return graph.getRelatedValue(expression, sr.HasEquation);
\r
76 } catch (DatabaseException e1) {
\r
77 e1.printStackTrace();
\r
79 data.put("equation", equation);
\r
84 public void replaceSelection(String var) {
\r
85 if(expression != null) {
\r
86 IDocument doc = expression.getDocument();
\r
88 Point selection = expression.getSelection();
\r
89 doc.replace(selection.x, selection.y, var);
\r
90 expression.setSelection(selection.x + var.length());
\r
91 } catch (BadLocationException e) {
\r
92 e.printStackTrace();
\r
98 public void save(final Resource variable, Map<String, Object> data) {
\r
99 final String currentText = expression.getExpression();
\r
100 if(currentText != null && expressionType != null) {
\r
101 data.put("equation", currentText);
\r
102 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
104 public void perform(WriteGraph g)
\r
105 throws DatabaseException {
\r
106 SysdynResource sr = SysdynResource.getInstance(g);
\r
107 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
108 Builtins b = g.getBuiltins();
\r
109 if(expression != null) {
\r
110 g.deny(variable, sr.HasExpression);
\r
112 expression = g.newResource();
\r
113 g.claim(expression, b.InstanceOf, null, expressionType);
\r
114 g.claim(variable, sr.HasExpression, expression);
\r
115 g.claimValue(expression, sr.HasEquation, currentText);
\r
120 expression.setExpression(currentText);
\r
124 public void updateData(Map<String, Object> data) {
\r
125 if(this.expression != null && this.expression.getExpression() != null)
\r
126 data.put("equation", this.expression.getExpression());
\r
130 public void addKeyListener(KeyListener listener) {
\r
131 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
136 public void addModifyListener(ModifyListener listener) {
\r
137 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
142 public void addFocusListener(FocusListener listener) {
\r
143 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r