]> gerrit.simantics Code Review - simantics/platform.git/blob
ddd6113ff57ca77ee6f0e08ca0a7f54ec4836ebf
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.nodetypes;
13
14 import java.util.Collection;
15 import java.util.Collections;
16
17 import org.eclipse.core.runtime.Platform;
18 import org.osgi.framework.Bundle;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.common.NodeContextBuilder;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Statement;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.ui.selection.WorkbenchSelectionElement;
28 import org.simantics.viewpoint.ontology.ViewpointResource;
29
30 public class SpecialNodeType implements NodeType {
31     public Resource resource;
32     Class<?> contentType;
33     
34     public SpecialNodeType(Resource resource, Class<?> contentType) {
35         this.resource = resource;
36         this.contentType = contentType;
37     }
38     
39     public static SpecialNodeType create(ReadGraph g, Resource r) throws DatabaseException {
40         ViewpointResource vr = ViewpointResource.getInstance(g);
41         String contentTypeName = g.getPossibleRelatedValue(r, vr.HasContentType);
42         Class<?> contentType;
43         if("Resource".equals(contentTypeName))
44             contentType = Resource.class;
45         else if("Statement".equals(contentTypeName))
46             contentType = Statement.class;
47         else if("Variable".equals(contentTypeName))
48             contentType = Variable.class;
49         else {
50             contentType = Object.class;
51             if(contentTypeName != null)
52                 try {
53                     String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);
54                     Bundle bundle = null;
55                     if (bundleId != null) {
56                         bundle = Platform.getBundle(bundleId);
57                         if (bundle == null)
58                             System.err.println("Referenced bundle '" + bundleId + "' not found in platform.");
59                     }
60                     if (bundle != null)
61                         contentType = bundle.loadClass(contentTypeName);
62                     else
63                         contentType = Class.forName(contentTypeName);
64                 } catch (ClassNotFoundException e) {
65                     System.err.println("Unknown content type " + contentTypeName);
66                 }
67             else
68                 System.err.println("Content type is NULL.");
69         }
70         return new SpecialNodeType(r, contentType);
71     }
72
73     @Override
74     public NodeContext createNodeContext(ReadGraph graph, Object content)
75             throws DatabaseException {
76         if(contentType.isInstance(content))
77             return NodeContextBuilder.buildWithData(KEY_SEQUENCE,
78                     new Object[] {content, this}
79             );
80         else
81             return null;
82     }
83
84     @Override
85     public Class<?> getContentType() {
86         return contentType;
87     }
88
89     @Override
90     public int hashCode() {
91         return resource.hashCode();
92     }
93
94     @Override
95     public boolean equals(Object obj) {
96         if (this == obj)
97             return true;
98         if (obj == null)
99             return false;
100         if (getClass() != obj.getClass())
101             return false;
102         SpecialNodeType other = (SpecialNodeType) obj;
103         return resource.equals(other.resource);
104     }
105
106     @Override
107     public boolean inherits(ReadGraph graph, NodeType superType) {
108         // Special node type does not support inheritance
109         return equals(superType);
110     }
111
112     @Override
113     public Collection<NodeType> getSuper(ReadGraph g) {
114         return Collections.emptyList();
115     }
116     
117     @Override
118     public String toString(ReadGraph graph) throws DatabaseException {
119         return "(" + NameUtils.getSafeName(graph, resource) + ")";
120     }
121
122     @Override
123     public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {
124         return null;
125     }
126
127 }