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.FocusAdapter;
\r
13 import org.eclipse.swt.events.FocusEvent;
\r
14 import org.eclipse.swt.events.FocusListener;
\r
15 import org.eclipse.swt.events.KeyListener;
\r
16 import org.eclipse.swt.events.ModifyListener;
\r
17 import org.eclipse.swt.graphics.Point;
\r
18 import org.eclipse.swt.widgets.Composite;
\r
19 import org.eclipse.swt.widgets.Label;
\r
20 import org.simantics.db.Builtins;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.request.WriteRequest;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
26 import org.simantics.db.request.Read;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
28 import org.simantics.ui.SimanticsUI;
\r
30 public class BasicExpression implements IExpression {
\r
32 private ExpressionField expression;
\r
33 protected Resource expressionType;
\r
34 private Point caretPosition; // Bug or something in source viewer's text widget. Cursor goes to 0, 0 on focus lost
\r
37 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
38 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
39 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
41 Label l = new Label(parent, SWT.NONE);
\r
44 expression = new ExpressionField(parent, SWT.BORDER);
\r
45 expression.setExpression(equation);
\r
46 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
48 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
51 public void focusLost(FocusEvent e) {
\r
52 caretPosition = expression.getSelection();
\r
58 public void focus() {
\r
59 if(this.expression != null && caretPosition != null) {
\r
60 this.expression.focus();
\r
61 expression.getSourceViewer().getTextWidget().setSelection(caretPosition.x, caretPosition.x + caretPosition.y);
\r
67 public List<ExpressionField> getExpressionFields() {
\r
68 return Arrays.asList(this.expression);
\r
72 public void readData(final Resource variable, Map<String, Object> data) {
\r
73 String equation = null;
\r
74 if (variable != null && data.get("equation") == null && expressionType != 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 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
82 if (expression != null && graph.isInstanceOf(expression, expressionType)) {
\r
83 return graph.getRelatedValue(expression, sr.HasEquation);
\r
90 } catch (DatabaseException e1) {
\r
91 e1.printStackTrace();
\r
93 data.put("equation", equation);
\r
98 public void replaceSelection(String var) {
\r
99 if(expression != null) {
\r
100 IDocument doc = expression.getDocument();
\r
102 Point selection = expression.getSelection();
\r
103 doc.replace(selection.x, selection.y, var);
\r
104 expression.setSelection(selection.x + var.length());
\r
105 } catch (BadLocationException e) {
\r
106 e.printStackTrace();
\r
112 public void save(final Resource variable, Map<String, Object> data) {
\r
113 final String currentText = expression.getExpression();
\r
114 if(currentText != null && expressionType != null) {
\r
115 data.put("equation", currentText);
\r
116 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
118 public void perform(WriteGraph g)
\r
119 throws DatabaseException {
\r
120 SysdynResource sr = SysdynResource.getInstance(g);
\r
121 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
122 Builtins b = g.getBuiltins();
\r
123 if(expression != null) {
\r
124 g.deny(variable, sr.HasExpression);
\r
126 expression = g.newResource();
\r
127 g.claim(expression, b.InstanceOf, null, expressionType);
\r
128 g.claim(variable, sr.HasExpression, expression);
\r
129 g.claimValue(expression, sr.HasEquation, currentText);
\r
134 expression.setExpression(currentText);
\r
138 public void updateData(Map<String, Object> data) {
\r
139 if(this.expression != null && this.expression.getExpression() != null)
\r
140 data.put("equation", this.expression.getExpression());
\r
144 public void addKeyListener(KeyListener listener) {
\r
145 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
150 public void addModifyListener(ModifyListener listener) {
\r
151 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
156 public void addFocusListener(FocusListener listener) {
\r
157 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r