]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
f66a0c2ff96ead7f3096846867273397c966c979
[simantics/sysdyn.git] /
1 /*******************************************************************************
2  * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
13
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IUndoManager;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.VerifyKeyListener;
25 import org.eclipse.swt.events.FocusListener;
26 import org.eclipse.swt.events.KeyListener;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Table;
32 import org.simantics.Simantics;
33 import org.simantics.databoard.Bindings;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.common.CommentMetadata;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.common.utils.ListUtils;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.request.Read;
42 import org.simantics.db.service.VirtualGraphSupport;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.layer0.utils.direct.GraphUtils;
45 import org.simantics.sysdyn.SysdynResource;
46 import org.simantics.sysdyn.ui.utils.ExpressionUtils;
47
48 /**
49  * Basic expression that is used with parameter, auxiliary and constant
50  * @author Teemu Lempinen
51  * @author Tuomas Miettinen
52  *
53  */
54 public class BasicExpression implements IExpression {
55
56     protected ExpressionField expression;
57     protected Resource expressionType;
58     protected ExpressionWidgetInput input;
59     
60     public BasicExpression(ExpressionWidgetInput input) {
61         this.input = input;
62     }
63     
64     @Override
65     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
66         // Create the single field
67         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
68         String equation = data.get("equation") != null ? (String)data.get("equation") : "";
69
70         Label l = new Label(parent, SWT.NONE);
71         l.setText("=");
72
73         expression = new ExpressionField(parent, SWT.BORDER, null, false, input);
74         expression.setExpression(equation);
75         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
76
77     }
78
79     @Override
80     public void focus() {
81         this.expression.focus();
82
83     }
84
85     @Override
86     public List<ExpressionField> getExpressionFields() {
87         return Arrays.asList(this.expression);
88     }
89
90     @Override
91     public void readData(final Resource expression, Map<String, Object> data) {
92         String equation = null;
93         if (expression != null && data.get("equation") == null) {
94             try {
95                 equation = Simantics.getSession().syncRequest(new Read<String>() {
96
97                     @Override
98                     public String perform(ReadGraph graph) throws DatabaseException {
99                         SysdynResource sr = SysdynResource.getInstance(graph);
100                         if (expression != null) {
101                             String equation = graph.getPossibleRelatedValue(expression, sr.Expression_equation);
102                             if(equation != null)
103                                 return equation;
104                         }
105                         
106                         return "";
107                         
108                     }
109
110                 });
111             } catch (DatabaseException e1) {
112                 e1.printStackTrace();
113             }
114             data.put("equation", equation);
115         }
116     }
117
118     @Override
119     public void replaceSelection(String var) {
120         if(expression != null) {
121             IDocument doc = expression.getDocument();
122             try {
123                 Point selection = expression.getSelection();
124                 doc.replace(selection.x, selection.y, var);
125                 expression.setSelection(selection.x + var.length());
126             } catch (BadLocationException e) {
127                 e.printStackTrace();
128             }
129         }
130     }
131
132     @Override
133     public void save(final Resource expression, Map<String, Object> data) {
134         final String currentText = this.expression.getExpression();
135         final String oldEquation = (String)data.get("equation");
136
137         if(oldEquation == null || 
138                 (currentText != null && expressionType != null)) {
139             data.put("equation", currentText);
140             Simantics.getSession().asyncRequest(new WriteRequest() {
141                 @Override
142                 public void perform(WriteGraph g)
143                 throws DatabaseException {
144                     SysdynResource sr = SysdynResource.getInstance(g);
145                     Layer0 l0 = Layer0.getInstance(g);
146
147                     // If nothing has changed, do nothing
148                     if (oldEquation != null 
149                             && expression != null 
150                             && g.isInstanceOf(expression, expressionType) 
151                             && currentText.equals(oldEquation)) {
152                         return;
153                     }
154                     
155                     // Force change to parameter, if the equation is a parameter
156                     if(ExpressionUtils.isParameter(currentText)) {
157                         if(!expressionType.equals(sr.ConstantExpression))
158                                 expressionType = sr.ParameterExpression;
159                     } else {
160                         expressionType = sr.NormalExpression;
161                     }
162                     
163                     g.markUndoPoint();
164                                 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
165                                 g.addMetadata(cm.add("Set equation"));
166
167                     // If the current expression type is different than the target expression type, create a new expression
168                     if(!g.isInstanceOf(expression, expressionType)) {
169
170                         final Resource newExpression = GraphUtils.create2(g, expressionType, 
171                                         sr.Expression_equation, currentText);
172                         String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);
173                         if(arrayRange != null)
174                                 g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);
175                         
176                         final Resource variable = g.getPossibleObject(expression, l0.PartOf);
177                         if(variable == null)
178                             return;
179                         Resource ownerList = g.getPossibleObject(variable, sr.Variable_expressionList);
180                         if(ownerList == null)
181                             return;
182                         
183                         ListUtils.replace(g, ownerList, expression, newExpression);
184                         
185                         g.deny(expression, l0.PartOf);
186                         
187                         g.claim(newExpression, l0.PartOf, variable);
188                         
189                         
190                         VirtualGraphSupport support = g.getService(VirtualGraphSupport.class);
191                                                 g.syncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) {
192                                                         @Override
193                                                         public void perform(WriteGraph graph) throws DatabaseException {
194                                                                 SysdynResource sr = SysdynResource.getInstance(graph);
195                                                                 if(variable != null) {
196                                                                         if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))
197                                                                                 graph.deny(variable, sr.IndependentVariable_activeExpression);
198                                                                         graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);
199                                                                 }
200                                                         }
201                                                 }
202                                                 );
203                     } else {
204                         // Claim value for the expression
205                         g.claimLiteral(expression, sr.Expression_equation, currentText);
206                     }
207                 }
208
209             });
210         }
211     }
212
213     @Override
214     public void updateData(Map<String, Object> data) {
215         if(this.expression != null && this.expression.getExpression() != null)
216             data.put("equation", this.expression.getExpression());
217     }
218
219     @Override
220     public void addKeyListener(KeyListener listener) {
221         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
222
223     }
224
225     @Override
226     public void addModifyListener(ModifyListener listener) {
227         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
228
229     }
230
231     @Override
232     public void addFocusListener(FocusListener listener) {
233         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
234     }
235
236         @Override
237         public void addVerifyKeyListener(VerifyKeyListener listener) {
238                 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
239         }
240
241     @Override
242     public IUndoManager getUndoManager() {
243         // TODO Auto-generated method stub
244         return null;
245     }
246 }