]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/SpecialNodeType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / nodetypes / SpecialNodeType.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.Collection;\r
15 import java.util.Collections;\r
16 \r
17 import org.eclipse.core.runtime.Platform;\r
18 import org.osgi.framework.Bundle;\r
19 import org.simantics.browsing.ui.NodeContext;\r
20 import org.simantics.browsing.ui.common.NodeContextBuilder;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.Statement;\r
24 import org.simantics.db.common.utils.NameUtils;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.layer0.variable.Variable;\r
27 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
28 import org.simantics.viewpoint.ontology.ViewpointResource;\r
29 \r
30 public class SpecialNodeType implements NodeType {\r
31     public Resource resource;\r
32     Class<?> contentType;\r
33     \r
34     public SpecialNodeType(Resource resource, Class<?> contentType) {\r
35         this.resource = resource;\r
36         this.contentType = contentType;\r
37     }\r
38     \r
39     public static SpecialNodeType create(ReadGraph g, Resource r) throws DatabaseException {\r
40         ViewpointResource vr = ViewpointResource.getInstance(g);\r
41         String contentTypeName = g.getPossibleRelatedValue(r, vr.HasContentType);\r
42         Class<?> contentType;\r
43         if("Resource".equals(contentTypeName))\r
44             contentType = Resource.class;\r
45         else if("Statement".equals(contentTypeName))\r
46             contentType = Statement.class;\r
47         else if("Variable".equals(contentTypeName))\r
48             contentType = Variable.class;\r
49         else {\r
50             contentType = Object.class;\r
51             if(contentTypeName != null)\r
52                 try {\r
53                     String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);\r
54                     Bundle bundle = null;\r
55                     if (bundleId != null) {\r
56                         bundle = Platform.getBundle(bundleId);\r
57                         if (bundle == null)\r
58                             System.err.println("Referenced bundle '" + bundleId + "' not found in platform.");\r
59                     }\r
60                     if (bundle != null)\r
61                         contentType = bundle.loadClass(contentTypeName);\r
62                     else\r
63                         contentType = Class.forName(contentTypeName);\r
64                 } catch (ClassNotFoundException e) {\r
65                     System.err.println("Unknown content type " + contentTypeName);\r
66                 }\r
67             else\r
68                 System.err.println("Content type is NULL.");\r
69         }\r
70         return new SpecialNodeType(r, contentType);\r
71     }\r
72 \r
73     @Override\r
74     public NodeContext createNodeContext(ReadGraph graph, Object content)\r
75             throws DatabaseException {\r
76         if(contentType.isInstance(content))\r
77             return NodeContextBuilder.buildWithData(KEY_SEQUENCE,\r
78                     new Object[] {content, this}\r
79             );\r
80         else\r
81             return null;\r
82     }\r
83 \r
84     @Override\r
85     public Class<?> getContentType() {\r
86         return contentType;\r
87     }\r
88 \r
89     @Override\r
90     public int hashCode() {\r
91         return resource.hashCode();\r
92     }\r
93 \r
94     @Override\r
95     public boolean equals(Object obj) {\r
96         if (this == obj)\r
97             return true;\r
98         if (obj == null)\r
99             return false;\r
100         if (getClass() != obj.getClass())\r
101             return false;\r
102         SpecialNodeType other = (SpecialNodeType) obj;\r
103         return resource.equals(other.resource);\r
104     }\r
105 \r
106     @Override\r
107     public boolean inherits(ReadGraph graph, NodeType superType) {\r
108         // Special node type does not support inheritance\r
109         return equals(superType);\r
110     }\r
111 \r
112     @Override\r
113     public Collection<NodeType> getSuper(ReadGraph g) {\r
114         return Collections.emptyList();\r
115     }\r
116     \r
117     @Override\r
118     public String toString(ReadGraph graph) throws DatabaseException {\r
119         return "(" + NameUtils.getSafeName(graph, resource) + ")";\r
120     }\r
121 \r
122     @Override\r
123     public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {\r
124         return null;\r
125     }\r
126 \r
127 }\r