+/*******************************************************************************\r
+ * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.model.nodetypes;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.osgi.framework.Bundle;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.ui.selection.WorkbenchSelectionElement;\r
+import org.simantics.viewpoint.ontology.ViewpointResource;\r
+\r
+public class SpecialNodeType implements NodeType {\r
+ public Resource resource;\r
+ Class<?> contentType;\r
+ \r
+ public SpecialNodeType(Resource resource, Class<?> contentType) {\r
+ this.resource = resource;\r
+ this.contentType = contentType;\r
+ }\r
+ \r
+ public static SpecialNodeType create(ReadGraph g, Resource r) throws DatabaseException {\r
+ ViewpointResource vr = ViewpointResource.getInstance(g);\r
+ String contentTypeName = g.getPossibleRelatedValue(r, vr.HasContentType);\r
+ Class<?> contentType;\r
+ if("Resource".equals(contentTypeName))\r
+ contentType = Resource.class;\r
+ else if("Statement".equals(contentTypeName))\r
+ contentType = Statement.class;\r
+ else if("Variable".equals(contentTypeName))\r
+ contentType = Variable.class;\r
+ else {\r
+ contentType = Object.class;\r
+ if(contentTypeName != null)\r
+ try {\r
+ String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle);\r
+ Bundle bundle = null;\r
+ if (bundleId != null) {\r
+ bundle = Platform.getBundle(bundleId);\r
+ if (bundle == null)\r
+ System.err.println("Referenced bundle '" + bundleId + "' not found in platform.");\r
+ }\r
+ if (bundle != null)\r
+ contentType = bundle.loadClass(contentTypeName);\r
+ else\r
+ contentType = Class.forName(contentTypeName);\r
+ } catch (ClassNotFoundException e) {\r
+ System.err.println("Unknown content type " + contentTypeName);\r
+ }\r
+ else\r
+ System.err.println("Content type is NULL.");\r
+ }\r
+ return new SpecialNodeType(r, contentType);\r
+ }\r
+\r
+ @Override\r
+ public NodeContext createNodeContext(ReadGraph graph, Object content)\r
+ throws DatabaseException {\r
+ if(contentType.isInstance(content))\r
+ return NodeContextBuilder.buildWithData(KEY_SEQUENCE,\r
+ new Object[] {content, this}\r
+ );\r
+ else\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public Class<?> getContentType() {\r
+ return contentType;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return resource.hashCode();\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ SpecialNodeType other = (SpecialNodeType) obj;\r
+ return resource.equals(other.resource);\r
+ }\r
+\r
+ @Override\r
+ public boolean inherits(ReadGraph graph, NodeType superType) {\r
+ // Special node type does not support inheritance\r
+ return equals(superType);\r
+ }\r
+\r
+ @Override\r
+ public Collection<NodeType> getSuper(ReadGraph g) {\r
+ return Collections.emptyList();\r
+ }\r
+ \r
+ @Override\r
+ public String toString(ReadGraph graph) throws DatabaseException {\r
+ return "(" + NameUtils.getSafeName(graph, resource) + ")";\r
+ }\r
+\r
+ @Override\r
+ public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) {\r
+ return null;\r
+ }\r
+\r
+}\r