]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9a74a19c53dec0551b2f0b551b282eab0c15e4be
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.equation.expressions;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.text.BadLocationException;\r
21 import org.eclipse.jface.text.IDocument;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.graphics.Point;\r
24 import org.eclipse.swt.widgets.Composite;\r
25 import org.eclipse.swt.widgets.Label;\r
26 import org.simantics.db.Builtins;\r
27 import org.simantics.db.ReadGraph;\r
28 import org.simantics.db.Resource;\r
29 import org.simantics.db.WriteGraph;\r
30 import org.simantics.db.common.request.WriteRequest;\r
31 import org.simantics.db.exception.DatabaseException;\r
32 import org.simantics.db.request.Read;\r
33 import org.simantics.sysdyn.SysdynResource;\r
34 import org.simantics.ui.SimanticsUI;\r
35 \r
36 public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor {\r
37 \r
38     private Label equationLabel;\r
39     private ExpressionField expression;\r
40 \r
41     @Override\r
42     public void createView(Composite parent, Map<String, Object> data) {\r
43 \r
44         final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
45 \r
46         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
47         equationLabel = new Label(parent, SWT.NONE);\r
48         equationLabel.setFont(FONT);\r
49         equationLabel.setText("=");\r
50         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
51 \r
52 \r
53         expression = new ExpressionField(parent, SWT.BORDER);\r
54         expression.setFont(FONT);\r
55         expression.setExpression(equation);\r
56         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
57 \r
58     }\r
59 \r
60     @Override\r
61     public void readData(final Resource variable, Map<String, Object> data) {\r
62         String equation = null;\r
63         if (variable != null && data.get("equation") == null) {\r
64             try {\r
65                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
66 \r
67                     @Override\r
68                     public String perform(ReadGraph graph) throws DatabaseException {\r
69                         SysdynResource sr = SysdynResource.getInstance(graph);\r
70                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
71                         if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
72                             return graph.getRelatedValue(expression, sr.HasEquation);\r
73                         } else {\r
74                             return "";\r
75                         }\r
76                     }\r
77 \r
78                 });\r
79             } catch (DatabaseException e1) {\r
80                 e1.printStackTrace();\r
81             }\r
82             data.put("equation", equation);\r
83         }\r
84     }\r
85 \r
86     @Override\r
87     public void writeData(final Resource variable, Map<String, Object> data) {\r
88         final String currentText = expression.getExpression();\r
89         if(currentText != null) {\r
90             data.put("equation", currentText);\r
91             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
92                 @Override\r
93                 public void perform(WriteGraph g)\r
94                 throws DatabaseException {\r
95                     SysdynResource sr = SysdynResource.getInstance(g);\r
96                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
97                     Builtins b = g.getBuiltins();\r
98                     if(expression != null) {\r
99                         g.deny(variable, sr.HasExpression);\r
100                     }\r
101                     expression = g.newResource();\r
102                     g.claim(expression, b.InstanceOf, null, sr.NormalExpression);\r
103                     g.claim(variable, sr.HasExpression, expression);\r
104                     g.claimValue(expression, sr.HasEquation, currentText);\r
105                 }\r
106 \r
107             });\r
108         }\r
109     }\r
110 \r
111     @Override\r
112     public void focus() {\r
113         if(this.expression != null) this.expression.focus();\r
114     }\r
115 \r
116     @Override\r
117     public void replaceSelection(String var) {\r
118         if(expression != null) {\r
119             IDocument doc = expression.getDocument();\r
120             try {\r
121                 Point selection = expression.getSelection();\r
122                 doc.replace(selection.x, selection.y, var);\r
123                 expression.setSelection(selection.x + var.length());\r
124             } catch (BadLocationException e) {\r
125                 e.printStackTrace();\r
126             }\r
127         }\r
128     }\r
129 \r
130     @Override\r
131     public void updateData(Map<String, Object> data) {\r
132         if(this.expression != null && this.expression.getExpression() != null)\r
133             data.put("equation", this.expression.getExpression());\r
134     }\r
135 \r
136     @Override\r
137     public List<ExpressionField> getExpressionFields() {\r
138         return Arrays.asList(this.expression);\r
139     }\r
140 }\r