]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ebf4d4205981fdeb94b1d3d7813a8790aff72cd8
[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.properties.widgets.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.custom.VerifyKeyListener;\r
24 import org.eclipse.swt.events.FocusListener;\r
25 import org.eclipse.swt.events.KeyListener;\r
26 import org.eclipse.swt.events.ModifyListener;\r
27 import org.eclipse.swt.graphics.Point;\r
28 import org.eclipse.swt.widgets.Composite;\r
29 import org.eclipse.swt.widgets.Label;\r
30 import org.simantics.db.ReadGraph;\r
31 import org.simantics.db.Resource;\r
32 import org.simantics.db.WriteGraph;\r
33 import org.simantics.db.common.request.WriteRequest;\r
34 import org.simantics.db.common.utils.OrderedSetUtils;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.db.request.Read;\r
37 import org.simantics.layer0.Layer0;\r
38 import org.simantics.sysdyn.SysdynResource;\r
39 import org.simantics.ui.SimanticsUI;\r
40 \r
41 public class BasicExpression implements IExpression {\r
42 \r
43     private ExpressionField expression;\r
44     protected Resource expressionType;\r
45 \r
46     @Override\r
47     public void createExpressionFields(Composite parent, Map<String, Object> data) {\r
48         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
49         String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
50 \r
51         Label l = new Label(parent, SWT.NONE);\r
52         l.setText("=");\r
53 \r
54         expression = new ExpressionField(parent, SWT.BORDER);\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 focus() {\r
62         this.expression.focus();\r
63 \r
64     }\r
65 \r
66     @Override\r
67     public List<ExpressionField> getExpressionFields() {\r
68         return Arrays.asList(this.expression);\r
69     }\r
70 \r
71     @Override\r
72     public void readData(final Resource expression, Map<String, Object> data) {\r
73         String equation = null;\r
74         if (expression != null && data.get("equation") == null) {\r
75             try {\r
76                 equation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
77 \r
78                     @Override\r
79                     public String perform(ReadGraph graph) throws DatabaseException {\r
80                         SysdynResource sr = SysdynResource.getInstance(graph);\r
81                         if (expression != null) {\r
82                             String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation);\r
83                             if(equation != null)\r
84                                 return equation;\r
85                         }\r
86                         \r
87                         return "";\r
88                         \r
89                     }\r
90 \r
91                 });\r
92             } catch (DatabaseException e1) {\r
93                 e1.printStackTrace();\r
94             }\r
95             data.put("equation", equation);\r
96         }\r
97     }\r
98 \r
99     @Override\r
100     public void replaceSelection(String var) {\r
101         if(expression != null) {\r
102             IDocument doc = expression.getDocument();\r
103             try {\r
104                 Point selection = expression.getSelection();\r
105                 doc.replace(selection.x, selection.y, var);\r
106                 expression.setSelection(selection.x + var.length());\r
107             } catch (BadLocationException e) {\r
108                 e.printStackTrace();\r
109             }\r
110         }\r
111     }\r
112 \r
113     @Override\r
114     public void save(final Resource expression, Map<String, Object> data) {\r
115         final String currentText = this.expression.getExpression();\r
116         final String oldEquation = (String)data.get("equation");\r
117         if(oldEquation == null || \r
118                 (currentText != null && expressionType != null)) {\r
119             data.put("equation", currentText);\r
120             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
121                 @Override\r
122                 public void perform(WriteGraph g)\r
123                 throws DatabaseException {\r
124                     SysdynResource sr = SysdynResource.getInstance(g);\r
125                     if(ExpressionUtils.isParameter(currentText)) {\r
126                         if(!expressionType.equals(sr.ConstantExpression))\r
127                                 expressionType = sr.ParameterExpression;\r
128                     } else {\r
129                         expressionType = sr.NormalExpression;\r
130                     }\r
131                     if (oldEquation != null \r
132                             && expression != null \r
133                             && g.isInstanceOf(expression, expressionType) \r
134                             && currentText.equals(oldEquation)) {\r
135                         return;\r
136                     }\r
137                     \r
138                     Layer0 l0 = Layer0.getInstance(g);\r
139                     \r
140                     g.deny(expression, l0.InstanceOf);\r
141                     g.deny(expression, sr.HasEquation);\r
142                     \r
143                     g.claim(expression, l0.InstanceOf, null, expressionType);\r
144                     g.claimLiteral(expression, sr.HasEquation, currentText);\r
145                 }\r
146 \r
147             });\r
148         }\r
149     }\r
150 \r
151     @Override\r
152     public void updateData(Map<String, Object> data) {\r
153         if(this.expression != null && this.expression.getExpression() != null)\r
154             data.put("equation", this.expression.getExpression());\r
155     }\r
156 \r
157     @Override\r
158     public void addKeyListener(KeyListener listener) {\r
159         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
160 \r
161     }\r
162 \r
163     @Override\r
164     public void addModifyListener(ModifyListener listener) {\r
165         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
166 \r
167     }\r
168 \r
169     @Override\r
170     public void addFocusListener(FocusListener listener) {\r
171         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
172     }\r
173 \r
174         @Override\r
175         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
176                 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
177         }\r
178 }\r