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;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class SpecialNodeType implements NodeType {
34 private static final Logger LOGGER = LoggerFactory.getLogger(SpecialNodeType.class);
36 public Resource resource;
39 public SpecialNodeType(Resource resource, Class<?> contentType) {
40 this.resource = resource;
41 this.contentType = contentType;
44 public static SpecialNodeType create(ReadGraph g, Resource r) throws DatabaseException {
45 ViewpointResource vr = ViewpointResource.getInstance(g);
46 String contentTypeName = g.getPossibleRelatedValue(r, vr.HasContentType);
48 if("Resource".equals(contentTypeName))
49 contentType = Resource.class;
50 else if("Statement".equals(contentTypeName))
51 contentType = Statement.class;
52 else if("Variable".equals(contentTypeName))
53 contentType = Variable.class;
55 contentType = Object.class;
56 if(contentTypeName != null)
58 String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);
60 if (bundleId != null) {
61 bundle = Platform.getBundle(bundleId);
63 LOGGER.warn("Referenced bundle '" + bundleId + "' not found in platform.");
66 contentType = bundle.loadClass(contentTypeName);
68 contentType = Class.forName(contentTypeName);
69 } catch (ClassNotFoundException e) {
70 LOGGER.error("Unknown content type {} - {}", contentTypeName, e.getMessage());
71 if (LOGGER.isTraceEnabled())
72 LOGGER.trace("Unknown content type {}", contentTypeName, e);
75 LOGGER.warn("Content type is NULL.");
77 return new SpecialNodeType(r, contentType);
81 public NodeContext createNodeContext(ReadGraph graph, Object content)
82 throws DatabaseException {
83 if(contentType.isInstance(content))
84 return NodeContextBuilder.buildWithData(KEY_SEQUENCE,
85 new Object[] {content, this}
92 public Class<?> getContentType() {
97 public int hashCode() {
98 return resource.hashCode();
102 public boolean equals(Object obj) {
107 if (getClass() != obj.getClass())
109 SpecialNodeType other = (SpecialNodeType) obj;
110 return resource.equals(other.resource);
114 public boolean inherits(ReadGraph graph, NodeType superType) {
115 // Special node type does not support inheritance
116 return equals(superType);
120 public Collection<NodeType> getSuper(ReadGraph g) {
121 return Collections.emptyList();
125 public String toString(ReadGraph graph) throws DatabaseException {
126 return "(" + NameUtils.getSafeName(graph, resource) + ")";
130 public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {