1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.nodetypes;
14 import java.util.Collection;
15 import java.util.Collections;
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;
30 public class SpecialNodeType implements NodeType {
31 public Resource resource;
34 public SpecialNodeType(Resource resource, Class<?> contentType) {
35 this.resource = resource;
36 this.contentType = contentType;
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);
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;
50 contentType = Object.class;
51 if(contentTypeName != null)
53 String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);
55 if (bundleId != null) {
56 bundle = Platform.getBundle(bundleId);
58 System.err.println("Referenced bundle '" + bundleId + "' not found in platform.");
61 contentType = bundle.loadClass(contentTypeName);
63 contentType = Class.forName(contentTypeName);
64 } catch (ClassNotFoundException e) {
65 System.err.println("Unknown content type " + contentTypeName);
68 System.err.println("Content type is NULL.");
70 return new SpecialNodeType(r, contentType);
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}
85 public Class<?> getContentType() {
90 public int hashCode() {
91 return resource.hashCode();
95 public boolean equals(Object obj) {
100 if (getClass() != obj.getClass())
102 SpecialNodeType other = (SpecialNodeType) obj;
103 return resource.equals(other.resource);
107 public boolean inherits(ReadGraph graph, NodeType superType) {
108 // Special node type does not support inheritance
109 return equals(superType);
113 public Collection<NodeType> getSuper(ReadGraph g) {
114 return Collections.emptyList();
118 public String toString(ReadGraph graph) throws DatabaseException {
119 return "(" + NameUtils.getSafeName(graph, resource) + ")";
123 public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {