]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
136ade16ea380c8d4148074a8dde71189134cef4
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.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.swt.SWT;\r
10 import org.eclipse.swt.events.FocusAdapter;\r
11 import org.eclipse.swt.events.FocusEvent;\r
12 import org.eclipse.swt.graphics.Point;\r
13 import org.eclipse.swt.widgets.Composite;\r
14 import org.eclipse.swt.widgets.Label;\r
15 import org.simantics.db.Builtins;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.request.WriteRequest;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.request.Read;\r
22 import org.simantics.sysdyn.SysdynResource;\r
23 import org.simantics.ui.SimanticsUI;\r
24 \r
25 public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor {\r
26     \r
27     private Label equationLabel;\r
28     private ExpressionField expression;\r
29     private Point lastSelection = new Point(0,0);\r
30     \r
31     @Override\r
32     public void createView(Composite parent, Map<String, Object> data) {\r
33 \r
34         final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
35         \r
36         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
37         equationLabel = new Label(parent, SWT.NONE);\r
38         equationLabel.setFont(FONT);\r
39         equationLabel.setText("=");\r
40         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
41 \r
42 \r
43         expression = new ExpressionField(parent, SWT.BORDER);\r
44         expression.setFont(FONT);\r
45         expression.setExpression(equation);\r
46         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
47 \r
48         expression.addFocusListener(new FocusAdapter() {\r
49 \r
50             @Override\r
51             public void focusGained(FocusEvent e){\r
52                 \r
53             }\r
54 \r
55             @Override\r
56             public void focusLost(FocusEvent e) {\r
57                 lastSelection = expression.getSelection();      \r
58             }\r
59         });\r
60         \r
61     }\r
62 \r
63     @Override\r
64     public void readData(final Resource variable, Map<String, Object> data) {\r
65         String equation = null;        \r
66         if (variable != null && data.get("equation") == null) {\r
67             try {\r
68                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
69 \r
70                     @Override\r
71                     public String perform(ReadGraph graph) throws DatabaseException {\r
72                         SysdynResource sr = SysdynResource.getInstance(graph);\r
73                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
74                         if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
75                             return graph.getRelatedValue(expression, sr.HasEquation);\r
76                         } else {\r
77                             return "";\r
78                         }\r
79                     }\r
80 \r
81                 });\r
82             } catch (DatabaseException e1) {\r
83                 e1.printStackTrace();\r
84             }\r
85             data.put("equation", equation);\r
86         }         \r
87     }\r
88 \r
89     @Override\r
90     public void writeData(final Resource variable, Map<String, Object> data) {\r
91         final String currentText = expression.getExpression();\r
92         if(currentText != null) {\r
93             data.put("equation", currentText);\r
94             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
95                 @Override\r
96                 public void perform(WriteGraph g)\r
97                 throws DatabaseException {\r
98                     SysdynResource sr = SysdynResource.getInstance(g);\r
99                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
100                     Builtins b = g.getBuiltins();\r
101                     if(expression != null) {\r
102                         g.deny(variable, sr.HasExpression);\r
103                     }\r
104                     expression = g.newResource();\r
105                     g.claim(expression, b.InstanceOf, sr.NormalExpression);\r
106                     g.claim(variable, sr.HasExpression, expression);\r
107                     g.claimValue(expression, sr.HasEquation, currentText);\r
108                 }\r
109 \r
110             });\r
111         }        \r
112     }\r
113 \r
114     @Override\r
115     public void focus() {\r
116         if(this.expression != null) this.expression.forceFocus();\r
117     }\r
118 \r
119     @Override\r
120     public void replaceSelection(String var) {\r
121         if(this.expression != null) {\r
122             String oldExpression = expression.getExpression();\r
123             if(lastSelection == null)\r
124                 lastSelection = new Point(0,0);\r
125             String newExpression = oldExpression.substring(0, lastSelection.x) + var + oldExpression.substring(lastSelection.y);\r
126             expression.setExpression(newExpression);\r
127             expression.setSelection(lastSelection.x + var.length());\r
128         }\r
129     }\r
130 \r
131     @Override\r
132     public void updateData(Map<String, Object> data) {\r
133         if(this.expression != null && this.expression.getExpression() != null)\r
134             data.put("equation", this.expression.getExpression());\r
135     }\r
136 \r
137     @Override\r
138     public List<ExpressionField> getExpressionFields() {\r
139         return Arrays.asList(this.expression);\r
140     }\r
141 }\r