]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/SpecialNodeType.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / nodetypes / SpecialNodeType.java
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 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SpecialNodeType implements NodeType {
33     
34     private static final Logger LOGGER = LoggerFactory.getLogger(SpecialNodeType.class);
35     
36     public Resource resource;
37     Class<?> contentType;
38     
39     public SpecialNodeType(Resource resource, Class<?> contentType) {
40         this.resource = resource;
41         this.contentType = contentType;
42     }
43     
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);
47         Class<?> contentType;
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;
54         else {
55             contentType = Object.class;
56             if(contentTypeName != null)
57                 try {
58                     String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);
59                     Bundle bundle = null;
60                     if (bundleId != null) {
61                         bundle = Platform.getBundle(bundleId);
62                         if (bundle == null)
63                             LOGGER.warn("Referenced bundle '" + bundleId + "' not found in platform.");
64                     }
65                     if (bundle != null)
66                         contentType = bundle.loadClass(contentTypeName);
67                     else
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);
73                 }
74             else
75                 LOGGER.warn("Content type is NULL.");
76         }
77         return new SpecialNodeType(r, contentType);
78     }
79
80     @Override
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}
86             );
87         else
88             return null;
89     }
90
91     @Override
92     public Class<?> getContentType() {
93         return contentType;
94     }
95
96     @Override
97     public int hashCode() {
98         return resource.hashCode();
99     }
100
101     @Override
102     public boolean equals(Object obj) {
103         if (this == obj)
104             return true;
105         if (obj == null)
106             return false;
107         if (getClass() != obj.getClass())
108             return false;
109         SpecialNodeType other = (SpecialNodeType) obj;
110         return resource.equals(other.resource);
111     }
112
113     @Override
114     public boolean inherits(ReadGraph graph, NodeType superType) {
115         // Special node type does not support inheritance
116         return equals(superType);
117     }
118
119     @Override
120     public Collection<NodeType> getSuper(ReadGraph g) {
121         return Collections.emptyList();
122     }
123     
124     @Override
125     public String toString(ReadGraph graph) throws DatabaseException {
126         return "(" + NameUtils.getSafeName(graph, resource) + ")";
127     }
128
129     @Override
130     public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {
131         return null;
132     }
133
134 }