X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Fnodetypes%2FSpecialNodeType.java;fp=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Fnodetypes%2FSpecialNodeType.java;h=9020abd57f8cd0ba271fd1270807a42ed87d62ff;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/SpecialNodeType.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/SpecialNodeType.java new file mode 100644 index 000000000..9020abd57 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/nodetypes/SpecialNodeType.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (c) 2010, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.model.nodetypes; + +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.core.runtime.Platform; +import org.osgi.framework.Bundle; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.common.NodeContextBuilder; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.ui.selection.WorkbenchSelectionElement; +import org.simantics.viewpoint.ontology.ViewpointResource; + +public class SpecialNodeType implements NodeType { + public Resource resource; + Class contentType; + + public SpecialNodeType(Resource resource, Class contentType) { + this.resource = resource; + this.contentType = contentType; + } + + public static SpecialNodeType create(ReadGraph g, Resource r) throws DatabaseException { + ViewpointResource vr = ViewpointResource.getInstance(g); + String contentTypeName = g.getPossibleRelatedValue(r, vr.HasContentType); + Class contentType; + if("Resource".equals(contentTypeName)) + contentType = Resource.class; + else if("Statement".equals(contentTypeName)) + contentType = Statement.class; + else if("Variable".equals(contentTypeName)) + contentType = Variable.class; + else { + contentType = Object.class; + if(contentTypeName != null) + try { + String bundleId = g.getPossibleRelatedValue(r, vr.HasBundle); + Bundle bundle = null; + if (bundleId != null) { + bundle = Platform.getBundle(bundleId); + if (bundle == null) + System.err.println("Referenced bundle '" + bundleId + "' not found in platform."); + } + if (bundle != null) + contentType = bundle.loadClass(contentTypeName); + else + contentType = Class.forName(contentTypeName); + } catch (ClassNotFoundException e) { + System.err.println("Unknown content type " + contentTypeName); + } + else + System.err.println("Content type is NULL."); + } + return new SpecialNodeType(r, contentType); + } + + @Override + public NodeContext createNodeContext(ReadGraph graph, Object content) + throws DatabaseException { + if(contentType.isInstance(content)) + return NodeContextBuilder.buildWithData(KEY_SEQUENCE, + new Object[] {content, this} + ); + else + return null; + } + + @Override + public Class getContentType() { + return contentType; + } + + @Override + public int hashCode() { + return resource.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SpecialNodeType other = (SpecialNodeType) obj; + return resource.equals(other.resource); + } + + @Override + public boolean inherits(ReadGraph graph, NodeType superType) { + // Special node type does not support inheritance + return equals(superType); + } + + @Override + public Collection getSuper(ReadGraph g) { + return Collections.emptyList(); + } + + @Override + public String toString(ReadGraph graph) throws DatabaseException { + return "(" + NameUtils.getSafeName(graph, resource) + ")"; + } + + @Override + public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) { + return null; + } + +}