]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
a964c5b6a8c507740a7703985be93762afea124c
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 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.modules;\r
13 \r
14 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
15 import org.simantics.browsing.ui.model.modifiers.ModifierRule;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.request.ObjectsWithType;\r
20 import org.simantics.db.common.request.WriteRequest;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.util.RemoverUtil;\r
23 import org.simantics.db.request.Read;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.layer0.utils.direct.GraphUtils;\r
26 import org.simantics.sysdyn.SysdynResource;\r
27 import org.simantics.sysdyn.representation.Variability;\r
28 import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
29 import org.simantics.ui.SimanticsUI;\r
30 \r
31 public class ParameterModifierRule implements ModifierRule {\r
32 \r
33     @Override\r
34     public boolean isCompatible(Class<?> contentType) {\r
35         return contentType.equals(Object.class);\r
36     }\r
37 \r
38     @Override\r
39     public Modifier getModifier(ReadGraph graph, Object content, String columnKey) throws DatabaseException {\r
40         if(content instanceof ParameterNode && ColumnKeys.VALUE.equals(columnKey)) {\r
41             final ParameterNode node = (ParameterNode) content;\r
42             return new Modifier() {\r
43 \r
44                 @Override\r
45                 public String getValue() {\r
46                     Read<String> request =\r
47                             new Read<String>() {\r
48 \r
49                         @Override\r
50                         public String perform(ReadGraph graph) throws DatabaseException {\r
51                             String parameterExpression =  ModuleParameterOverrideUtils.getParameterExpressionOrOverride(\r
52                                     graph, \r
53                                     node.getParent(), \r
54                                     node.getIndependentVariable());\r
55 \r
56                             return parameterExpression != null ? parameterExpression : "";\r
57                         }\r
58 \r
59                     };\r
60                     try {\r
61                         return SimanticsUI.getSession().syncRequest(request);\r
62                     } catch (DatabaseException e) {\r
63                         e.printStackTrace();\r
64                         return "";\r
65                     }\r
66                 }\r
67 \r
68                 @Override\r
69                 public String isValid(String label) {\r
70                     if(label.isEmpty())\r
71                         return null;\r
72                     \r
73                     if(Variability.CONTINUOUS.equals(Variability.getVariability(node.getIndependentVariable(), label, false, node.getParentConfiguration()))) \r
74                         return "Not valid";\r
75                     else\r
76                         return null;\r
77                 }\r
78 \r
79                 @Override\r
80                 public void modify(final String label) {\r
81                     SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
82                         @Override\r
83                         public void perform(WriteGraph graph) throws DatabaseException {\r
84                             SysdynResource sr = SysdynResource.getInstance(graph);\r
85                             Layer0 L0 = Layer0.getInstance(graph);\r
86 \r
87                             // Remove possible old override\r
88                             for(Resource r : graph.syncRequest(new ObjectsWithType(node.getParent(), L0.ConsistsOf, sr.Module_ParameterOverride))) {\r
89                                 if(node.getVariableResource().equals(graph.getPossibleObject(r, sr.Module_ParameterOverride_overriddenParameter))) {\r
90                                     RemoverUtil.remove(graph, r);\r
91                                     break;\r
92                                 }\r
93                             }\r
94 \r
95                             // Write the new override, if there is one\r
96                             if(label != null && !label.isEmpty()) {\r
97                                 GraphUtils.create2(graph, sr.Module_ParameterOverride,\r
98                                         sr.Module_ParameterOverride_overriddenParameter, node.getVariableResource(),\r
99                                         sr.Module_ParameterOverride_overrideExpression, label,\r
100                                         L0.PartOf, node.getParent()\r
101                                         );\r
102                             }\r
103                         }\r
104                     });\r
105                 }\r
106 \r
107             };\r
108         } else {\r
109             return null;\r
110         }\r
111     }\r
112 \r
113 }\r