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