]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6c2b6781746611133f7d10d1cbc5dc713990b8d4
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.arrays;\r
13 \r
14 import java.util.ArrayList;\r
15 \r
16 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;\r
17 import org.simantics.browsing.ui.common.modifiers.Enumeration;\r
18 import org.simantics.browsing.ui.common.node.AbstractNode;\r
19 import org.simantics.browsing.ui.common.node.IModifiableNode;\r
20 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.ObjectsWithType;\r
25 import org.simantics.db.common.utils.NameUtils;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.request.Read;\r
28 import org.simantics.layer0.Layer0;\r
29 import org.simantics.layer0.utils.direct.GraphUtils;\r
30 import org.simantics.sysdyn.SysdynResource;\r
31 import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
32 import org.simantics.ui.SimanticsUI;\r
33 \r
34 public class RedeclarationNode  extends AbstractNode<Resource> implements IModifiableNode {\r
35 \r
36         private Resource module;\r
37         \r
38         public RedeclarationNode(ReadGraph graph, final Resource module, Resource enumeration) {\r
39                 super(enumeration);\r
40                 this.module = module;\r
41         }\r
42         \r
43         /**\r
44          * \r
45          * @param graph\r
46          * @return\r
47          */\r
48         public Resource getReplacingEnumeration(ReadGraph graph) {\r
49                 SysdynResource sr = SysdynResource.getInstance(graph);\r
50                 Resource result = null;\r
51                 try {\r
52                         Resource redeclaration = getRedeclaration(graph);\r
53                         if(redeclaration != null) {\r
54                                 result = graph.getSingleObject(redeclaration, sr.Redeclaration_replacingEnumeration);\r
55                         }\r
56                 } catch(DatabaseException e) {\r
57                         e.printStackTrace();\r
58                 }\r
59                 \r
60                 return result;\r
61         }\r
62         \r
63 \r
64         public Resource getModule() {\r
65                 return module;\r
66         }\r
67 \r
68         public void setModule(Resource module) {\r
69                 this.module = module;\r
70         }\r
71 \r
72         public Resource getRedeclaration(ReadGraph graph) {\r
73                 try {\r
74                         SysdynResource sr = SysdynResource.getInstance(graph);\r
75                         for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.Module_redeclaration, sr.Redeclaration))) {\r
76                                 Resource replacedEnumeration = graph.getPossibleObject(redeclaration, sr.Redeclaration_replacedEnumeration);\r
77                                 if(replacedEnumeration != null && replacedEnumeration.equals(getReplacedEnumeration())) {\r
78                                         return redeclaration;\r
79                                 }\r
80                         }\r
81                 } catch (DatabaseException e) {\r
82                         e.printStackTrace();\r
83                 }\r
84                 return null;\r
85         }\r
86 \r
87         public void setRedeclaration(WriteGraph graph, Resource redeclaration) {\r
88                 try {\r
89                 SysdynResource sr = SysdynResource.getInstance(graph);\r
90                 Resource oldRedeclaration = getRedeclaration(graph);\r
91                 if(oldRedeclaration != null || redeclaration == null) {\r
92                         graph.deny(module, sr.Module_redeclaration, oldRedeclaration);\r
93                 }\r
94                 \r
95                 if(redeclaration != null)\r
96                         graph.claim(module, sr.Module_redeclaration, redeclaration);\r
97                 \r
98                 } catch (DatabaseException e) {\r
99                         e.printStackTrace();\r
100                 }\r
101         }\r
102 \r
103         public Resource getReplacedEnumeration() {\r
104                 return this.data;\r
105         }\r
106 \r
107         @Override\r
108         public Modifier getModifier(String columnId) {\r
109                 \r
110                 if(!ColumnKeys.REPLACED_WITH.equals(columnId))\r
111                         return null;\r
112                 \r
113                 ComboBoxModifier<Resource> cbm = null;\r
114                 \r
115                 try {\r
116                         cbm = SimanticsUI.getSession().syncRequest(new Read<ComboBoxModifier<Resource>>() {\r
117                                 \r
118                                 @Override\r
119                                 public ComboBoxModifier<Resource> perform(ReadGraph graph) throws DatabaseException {\r
120                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
121                                         Layer0 l0 = Layer0.getInstance(graph);\r
122                                         \r
123                                         ArrayList<EnumeratedValue<Resource>> values = new ArrayList<EnumeratedValue<Resource>>();\r
124                                         \r
125                                         \r
126                                 Resource configuration = graph.getSingleObject(module, l0.PartOf);\r
127                                 \r
128                                         for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {\r
129                                                 String name = NameUtils.getSafeName(graph, enumeration);\r
130                                                 values.add(new EnumeratedValue<Resource>(name, enumeration));                                   \r
131                                         }\r
132 \r
133                                         if(values.size() == 0)\r
134                                                 return null;\r
135                                         \r
136                                         values.add(0, new EnumeratedValue<Resource>("", null));\r
137                                         Enumeration<Resource> enumeration = new Enumeration<Resource>(values);  \r
138                                         \r
139                                         ComboBoxModifier<Resource> cbm = new ComboBoxModifier<Resource>(graph.getSession(), enumeration, getReplacingEnumeration(graph)) {\r
140                                                 \r
141                                                 @Override\r
142                                                 protected void modifyWithObject(WriteGraph graph, Resource oldEnumObject,\r
143                                                                 Resource enumObject) throws DatabaseException {\r
144                                                         \r
145                                                         if(enumObject == null) {\r
146                                                                 setRedeclaration(graph, null);\r
147                                                         } else if(!enumObject.equals(oldEnumObject)) {\r
148                                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
149                                                                 Resource redeclaration = GraphUtils.create2(graph, \r
150                                                                                 sr.Redeclaration, \r
151                                                                                 sr.Redeclaration_replacedEnumeration, getReplacedEnumeration(),\r
152                                                                                 sr.Redeclaration_replacingEnumeration, enumObject);\r
153                                                                 setRedeclaration(graph, redeclaration);\r
154                                                         }\r
155                                                         \r
156                                                 }\r
157                                         };\r
158                                         \r
159                                         return cbm;\r
160                                         \r
161                                 }\r
162                         });\r
163                 } catch (DatabaseException e) {\r
164                         // TODO Auto-generated catch block\r
165                         e.printStackTrace();\r
166                 }\r
167 \r
168                 return cbm;\r
169         }\r
170         \r
171         \r
172 }\r