]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/EntityNodeType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / nodetypes / EntityNodeType.java
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 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.browsing.ui.model.nodetypes;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 import java.util.WeakHashMap;\r
19 \r
20 import org.simantics.browsing.ui.NodeContext;\r
21 import org.simantics.browsing.ui.common.NodeContextBuilder;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.common.utils.NameUtils;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
28 \r
29 /**\r
30  * A node type that corresponds to some entity type.\r
31  * @author Hannu Niemistö\r
32  */\r
33 public class EntityNodeType implements NodeType {\r
34     public List<Resource> entityTypes;\r
35     private static final WeakHashMap<List<Resource>, EntityNodeType> nodeTypeCache =\r
36         new WeakHashMap<List<Resource>, EntityNodeType>();\r
37 \r
38     private EntityNodeType(List<Resource> entityTypes) {\r
39         this.entityTypes = entityTypes;\r
40     }\r
41     \r
42     public static EntityNodeType create(Resource entityType) {\r
43         List<Resource> entityTypes = Collections.singletonList(entityType);\r
44         synchronized(nodeTypeCache) {\r
45             EntityNodeType result = nodeTypeCache.get(entityTypes);\r
46             if(result == null) {\r
47                 result = new EntityNodeType(entityTypes);\r
48                 nodeTypeCache.put(entityTypes, result);\r
49             }\r
50             return result;\r
51         }\r
52     }\r
53     \r
54     public static EntityNodeType create(Collection<Resource> entityTypes) {\r
55         return createInternal(new ArrayList<Resource>(entityTypes));\r
56     }\r
57     \r
58     private static EntityNodeType createInternal(ArrayList<Resource> entityTypes) {\r
59         if(entityTypes.isEmpty())\r
60             throw new IllegalArgumentException();\r
61         else if(entityTypes.size() == 1)\r
62             return create(entityTypes.get(0));\r
63         else {\r
64             Collections.sort(entityTypes);\r
65             synchronized(nodeTypeCache) {\r
66                 EntityNodeType result = nodeTypeCache.get(entityTypes);\r
67                 if(result == null) {\r
68                     result = new EntityNodeType(entityTypes);\r
69                     nodeTypeCache.put(entityTypes, result);\r
70                 }\r
71                 return result;\r
72             }\r
73         }\r
74     }\r
75     \r
76     public static NodeType getNodeTypeFor(ReadGraph graph, Resource resource) throws DatabaseException {\r
77         ArrayList<Resource> types = new ArrayList<Resource>(graph.getPrincipalTypes(resource));\r
78         if(types.isEmpty())\r
79             return null;\r
80         else\r
81             return createInternal(types);\r
82     }\r
83     \r
84     public NodeType getNodeTypeOf(ReadGraph graph, Resource resource) throws DatabaseException {\r
85         ArrayList<Resource> types = new ArrayList<Resource>();\r
86         loop:\r
87         for(Resource t : graph.getPrincipalTypes(resource)) {\r
88             for(Resource et : entityTypes)\r
89                 if(!graph.isInheritedFrom(t, et))\r
90                     continue loop;\r
91             types.add(t);\r
92         }\r
93         \r
94         if(types.isEmpty())\r
95             return null;\r
96         \r
97         return createInternal(types);\r
98     }\r
99     \r
100     @Override\r
101     public NodeContext createNodeContext(ReadGraph graph, Object content)\r
102             throws DatabaseException {\r
103         if(content instanceof Resource) {\r
104             Resource resource = (Resource)content;\r
105             NodeType nodeType = getNodeTypeOf(graph, resource);            \r
106             if(nodeType != null)\r
107                 return NodeContextBuilder.buildWithData(KEY_SEQUENCE,\r
108                         new Object[] {content, nodeType});            \r
109         }\r
110         return null;\r
111     }\r
112 \r
113     @Override\r
114     public Class<?> getContentType() {\r
115         return Resource.class;\r
116     }\r
117     \r
118     @Override\r
119     public int hashCode() {\r
120         return entityTypes.hashCode();\r
121     }\r
122 \r
123     @Override\r
124     public boolean equals(Object obj) {\r
125         if (this == obj)\r
126             return true;\r
127         if (obj == null)\r
128             return false;\r
129         if (getClass() != obj.getClass())\r
130             return false;\r
131         EntityNodeType other = (EntityNodeType) obj;\r
132         return entityTypes.equals(other.entityTypes);\r
133     }\r
134 \r
135     @Override\r
136     public boolean inherits(ReadGraph graph, NodeType superType) throws DatabaseException {\r
137         if(this == superType)\r
138             return true;\r
139         if (superType.getClass() != EntityNodeType.class)\r
140             return false;\r
141         loop:\r
142         for(Resource st : ((EntityNodeType)superType).entityTypes) {\r
143             for(Resource t : entityTypes)\r
144                 if(graph.isInheritedFrom(t, st))\r
145                     continue loop;\r
146             return false;\r
147         }\r
148         return true;\r
149     }\r
150 \r
151     @Override\r
152     public Collection<NodeType> getSuper(ReadGraph g) throws DatabaseException {\r
153         if(entityTypes.size() == 1) {\r
154             Layer0 l0 = Layer0.getInstance(g);\r
155             Collection<Resource> supertypes = g.getObjects(entityTypes.get(0), l0.Inherits);\r
156             ArrayList<NodeType> supernodetypes = new ArrayList<NodeType>(supertypes.size());\r
157             for(Resource supertype : supertypes)\r
158                 supernodetypes.add(create(supertype));\r
159             return supernodetypes;\r
160         }\r
161         else {\r
162             ArrayList<NodeType> supernodetypes = new ArrayList<NodeType>(entityTypes.size());\r
163             for(Resource t : entityTypes)\r
164                 supernodetypes.add(create(t));\r
165             return supernodetypes;\r
166         }\r
167     }\r
168 \r
169     @Override\r
170     public String toString(ReadGraph graph) throws DatabaseException {\r
171         StringBuilder b = new StringBuilder();\r
172         b.append('[');\r
173         boolean first = true;\r
174         for(Resource t : entityTypes) {\r
175             if(first)\r
176                 first = false;\r
177             else\r
178                 b.append(", ");\r
179             b.append(NameUtils.getSafeName(graph, t));\r
180         }\r
181         b.append(']');\r
182         return b.toString();\r
183     }\r
184 \r
185     @Override\r
186     public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {\r
187         // TODO Auto-generated method stub\r
188         return null;\r
189     }\r
190 \r
191 }\r