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