]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
fe23484823eba18101e6047f9f23a1bafa9b5c43
[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 ParameterExpressionViewFactor 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 \r
47         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
48         equationLabel = new Label(parent, SWT.NONE);\r
49         equationLabel.setFont(FONT);\r
50         equationLabel.setText("=");\r
51         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
52 \r
53 \r
54         expression = new ExpressionField(parent, SWT.BORDER);\r
55         expression.setFont(FONT);\r
56         expression.setExpression(equation);\r
57         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
58     }\r
59 \r
60     @Override\r
61     public void focus() {\r
62         if(this.expression != null) this.expression.focus();\r
63     }\r
64 \r
65     @Override\r
66     public void readData(final Resource variable, Map<String, Object> data) {\r
67         String equation = null;\r
68         if (variable != null && data.get("equation") == null) {\r
69             try {\r
70                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
71 \r
72                     @Override\r
73                     public String perform(ReadGraph graph) throws DatabaseException {\r
74                         SysdynResource sr = SysdynResource.getInstance(graph);\r
75                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
76                         if (expression != null && graph.isInstanceOf(expression, sr.ParameterExpression)) {\r
77                             return graph.getRelatedValue(expression, sr.HasEquation);\r
78                         } else {\r
79                             return "";\r
80                         }\r
81                     }\r
82 \r
83                 });\r
84             } catch (DatabaseException e1) {\r
85                 e1.printStackTrace();\r
86             }\r
87             data.put("equation", equation);\r
88         }\r
89     }\r
90 \r
91     @Override\r
92     public void replaceSelection(String var) {\r
93         if(expression != null) {\r
94             IDocument doc = expression.getDocument();\r
95             try {\r
96                 Point selection = expression.getSelection();\r
97                 doc.replace(selection.x, selection.y, var);\r
98                 expression.setSelection(selection.x + var.length());\r
99             } catch (BadLocationException e) {\r
100                 e.printStackTrace();\r
101             }\r
102         }\r
103     }\r
104 \r
105     @Override\r
106     public void writeData(final Resource variable, Map<String, Object> data) {\r
107         final String currentText = expression.getExpression();\r
108         if(currentText != null) {\r
109             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
110 \r
111                 @Override\r
112                 public void perform(WriteGraph g)\r
113                 throws DatabaseException {\r
114                     SysdynResource sr = SysdynResource.getInstance(g);\r
115                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
116                     Builtins b = g.getBuiltins();\r
117                     if(expression != null) {\r
118                         g.deny(variable, sr.HasExpression);\r
119                     }\r
120                     expression = g.newResource();\r
121                     g.claim(expression, b.InstanceOf, null, sr.ParameterExpression);\r
122                     g.claim(variable, sr.HasExpression, expression);\r
123                     g.claimValue(expression, sr.HasEquation, currentText);\r
124                 }\r
125 \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
141 }\r
142 \r