]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
b7373e0adc303989001528c2cb18c2471e770e38
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.List;\r
5 import java.util.Map;\r
6 \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
29 \r
30 public class BasicExpression implements IExpression {\r
31     \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
35     \r
36     @Override\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
40         \r
41         Label l = new Label(parent, SWT.NONE);\r
42         l.setText("=");\r
43         \r
44         expression = new ExpressionField(parent, SWT.BORDER);\r
45         expression.setExpression(equation);\r
46         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
47         \r
48         expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
49 \r
50             @Override\r
51             public void focusLost(FocusEvent e) {\r
52                 caretPosition = expression.getSelection();\r
53             }\r
54         });\r
55     }\r
56 \r
57     @Override\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
62         }\r
63         \r
64     }\r
65 \r
66     @Override\r
67     public List<ExpressionField> getExpressionFields() {\r
68         return Arrays.asList(this.expression);\r
69     }\r
70 \r
71     @Override\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
75             try {\r
76                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
77 \r
78                     @Override\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
84                         } else {\r
85                             return "";\r
86                         }\r
87                     }\r
88 \r
89                 });\r
90             } catch (DatabaseException e1) {\r
91                 e1.printStackTrace();\r
92             }\r
93             data.put("equation", equation);\r
94         }\r
95     }\r
96 \r
97     @Override\r
98     public void replaceSelection(String var) {\r
99         if(expression != null) {\r
100             IDocument doc = expression.getDocument();\r
101             try {\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
107             }\r
108         }\r
109     }\r
110 \r
111     @Override\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
117                 @Override\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
125                     }\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
130                 }\r
131 \r
132             });\r
133         }\r
134         expression.setExpression(currentText);\r
135     }\r
136 \r
137     @Override\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
141     }\r
142 \r
143     @Override\r
144     public void addKeyListener(KeyListener listener) {\r
145         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
146         \r
147     }\r
148 \r
149     @Override\r
150     public void addModifyListener(ModifyListener listener) {\r
151         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
152         \r
153     }\r
154     \r
155     @Override\r
156     public void addFocusListener(FocusListener listener) {\r
157         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
158     }\r
159 }\r