]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3d7a65e46f5699fd234c9504726a09a5798a6e24
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.common.request.WriteRequest;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.request.Read;\r
32 import org.simantics.layer0.Layer0;\r
33 import org.simantics.sysdyn.SysdynResource;\r
34 import org.simantics.ui.SimanticsUI;\r
35 \r
36 public class ConstantExpressionViewFactor 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     @Override\r
60     public void focus() {\r
61         if(this.expression != null) this.expression.focus();\r
62     }\r
63 \r
64     @Override\r
65     public void readData(final Resource variable, Map<String, Object> data) {\r
66         String equation = null;        \r
67         if (variable != null && data.get("equation") == null) {\r
68             try {\r
69                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
70 \r
71                     @Override\r
72                     public String perform(ReadGraph graph) throws DatabaseException {\r
73                         SysdynResource sr = SysdynResource.getInstance(graph);\r
74                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
75                         if (expression != null && graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
76                             return graph.getRelatedValue(expression, sr.HasEquation);\r
77                         } else {\r
78                             return "";\r
79                         }\r
80                     }\r
81 \r
82                 });\r
83             } catch (DatabaseException e1) {\r
84                 e1.printStackTrace();\r
85             }\r
86             data.put("equation", equation);\r
87         } \r
88     }\r
89 \r
90     @Override\r
91     public void replaceSelection(String var) {\r
92         if(expression != null) {\r
93             IDocument doc = expression.getDocument();\r
94             try {\r
95                 Point selection = expression.getSelection();\r
96                 doc.replace(selection.x, selection.y, var);\r
97                 expression.setSelection(selection.x + var.length());\r
98             } catch (BadLocationException e) {\r
99                 e.printStackTrace();\r
100             }\r
101         }\r
102     }\r
103 \r
104     @Override\r
105     public void writeData(final Resource variable, Map<String, Object> data) {\r
106         final String currentText = expression.getExpression();\r
107         if(currentText != null) {\r
108             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
109 \r
110                 @Override\r
111                 public void perform(WriteGraph g)\r
112                 throws DatabaseException {\r
113                     SysdynResource sr = SysdynResource.getInstance(g);\r
114                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
115                     Layer0 l0 = Layer0.getInstance(g);\r
116                     if(expression != null) {\r
117                         g.deny(variable, sr.HasExpression);\r
118                     }\r
119                     expression = g.newResource();\r
120                     g.claim(expression, l0.InstanceOf, null, sr.ConstantExpression);\r
121                     g.claim(variable, sr.HasExpression, expression);\r
122                     g.claimLiteral(expression, sr.HasEquation, currentText);\r
123                 }\r
124 \r
125             });\r
126         }        \r
127     }\r
128 \r
129     @Override\r
130     public void updateData(Map<String, Object> data) {\r
131         if(this.expression != null && this.expression.getExpression() != null)\r
132             data.put("equation", this.expression.getExpression());        \r
133     }\r
134 \r
135     @Override\r
136     public List<ExpressionField> getExpressionFields() {\r
137         return Arrays.asList(this.expression);\r
138     }\r
139 \r
140 }\r