]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
131705c93f7891492a38868b08b52648ca60a473
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 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 \r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.jface.layout.GridDataFactory;\r
18 import org.eclipse.jface.layout.GridLayoutFactory;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.widgets.Composite;\r
21 import org.eclipse.swt.widgets.Label;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.variable.Variable;\r
26 import org.simantics.db.request.Read;\r
27 import org.simantics.modelica.fmi.FMUJNIException;\r
28 import org.simantics.simulation.experiment.IExperiment;\r
29 import org.simantics.simulation.project.IExperimentManager;\r
30 import org.simantics.sysdyn.SysdynResource;\r
31 import org.simantics.sysdyn.adapter.HistoryVariable;\r
32 import org.simantics.sysdyn.manager.SysdynGameExperiment;\r
33 import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
34 import org.simantics.ui.SimanticsUI;\r
35 \r
36 public class ParameterExpression extends BasicExpression {\r
37 \r
38         Variable variable;\r
39 \r
40         public ParameterExpression(Variable variable) {\r
41                 try {\r
42                         this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
43 \r
44                                 @Override\r
45                                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
46                                         return SysdynResource.getInstance(graph).ParameterExpression;\r
47                                 }\r
48                         });\r
49                 } catch (DatabaseException e) {\r
50                         e.printStackTrace();\r
51                 }               \r
52 \r
53                 this.variable = variable;\r
54         }\r
55 \r
56 \r
57         @Override\r
58         public void createExpressionFields(Composite parent, Map<String, Object> data) {\r
59                 // Create the single field\r
60                 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
61                 String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
62 \r
63                 Label l = new Label(parent, SWT.NONE);\r
64                 l.setText("=");\r
65 \r
66                 expression = new ExpressionField(parent, SWT.BORDER);\r
67                 expression.setExpression(equation);\r
68                 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
69 \r
70         }\r
71 \r
72         @Override\r
73         public void readData(final Resource expression, Map<String, Object> data) {\r
74                 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
75                 IExperiment experiment = manager.getActiveExperiment();\r
76                 if(experiment == null || !(experiment instanceof SysdynGameExperiment)) {\r
77                         super.readData(expression, data);\r
78                 } else {\r
79                         Double value;\r
80                         try {\r
81                                 value = SimanticsUI.getSession().syncRequest(new Read<Double>() {\r
82 \r
83                                         @Override\r
84                                         public Double perform(ReadGraph graph) throws DatabaseException {\r
85                                                 try {\r
86                                                         return ((HistoryVariable)variable).getParameterValue(graph);\r
87                                                 } catch (DatabaseException e) {\r
88                                                         throw new DatabaseException(e.getMessage());\r
89                                                 } catch (FMUJNIException e) {\r
90                                                         System.err.println("ParameterExpression.java: " + e.getMessage());\r
91                                                         return null;\r
92                                                 }\r
93                                         }\r
94                                 });\r
95                                 if(value != null)\r
96                                         data.put("equation", value.toString());\r
97                                 else\r
98                                         super.readData(expression, data);\r
99 \r
100                         } catch (DatabaseException e) {\r
101                                 super.readData(expression, data);\r
102                         }\r
103                 }\r
104         }\r
105 \r
106         @Override\r
107         public void save(final Resource expression, Map<String, Object> data) {\r
108 \r
109                 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
110                 IExperiment experiment = manager.getActiveExperiment();\r
111                 if(experiment != null && experiment instanceof SysdynGameExperiment && variable instanceof HistoryVariable) {\r
112                         final String currentText = this.expression.getExpression();\r
113                         final String oldEquation = (String)data.get("equation");\r
114                         if(oldEquation == null || \r
115                                         (currentText != null && expressionType != null)) {\r
116                                 if(ExpressionUtils.isParameter(currentText)) {\r
117                                         Boolean success = false;\r
118                                         try {\r
119                                                 success = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
120 \r
121                                                         @Override\r
122                                                         public Boolean perform(ReadGraph graph) throws DatabaseException {\r
123                                                                 try {\r
124                                                                         ((HistoryVariable)variable).setParameterValue(graph, Double.parseDouble(currentText));\r
125                                                                         return true;\r
126                                                                 } catch (Exception e) {\r
127                                                                         return false;\r
128                                                                 }\r
129 \r
130                                                         }\r
131                                                 });\r
132                                         } catch (DatabaseException e) {\r
133                                         }\r
134 \r
135                                         if(success) {\r
136                                                 data.put("equation", currentText);\r
137                                                 return;\r
138                                         } else {\r
139                                                 super.save(expression, data);\r
140                                         }\r
141                                 }\r
142                         }\r
143                 }\r
144 \r
145                 // If setting a parameter value was not succesful, use the normal save-method\r
146                 super.save(expression, data);\r
147         }\r
148 \r
149         public ParameterExpression() {\r
150                 try {\r
151                         this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
152 \r
153                                 @Override\r
154                                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
155                                         return SysdynResource.getInstance(graph).ParameterExpression;\r
156                                 }\r
157                         });\r
158                 } catch (DatabaseException e) {\r
159                         e.printStackTrace();\r
160                 }\r
161         }\r
162 \r
163 \r
164 }\r