/******************************************************************************* * 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.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.WeakHashMap; 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.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.ui.selection.WorkbenchSelectionElement; /** * A node type that corresponds to some entity type. * @author Hannu Niemistö */ public class EntityNodeType implements NodeType { public List entityTypes; private static final WeakHashMap, EntityNodeType> nodeTypeCache = new WeakHashMap, EntityNodeType>(); private EntityNodeType(List entityTypes) { this.entityTypes = entityTypes; } public static EntityNodeType create(Resource entityType) { List entityTypes = Collections.singletonList(entityType); synchronized(nodeTypeCache) { EntityNodeType result = nodeTypeCache.get(entityTypes); if(result == null) { result = new EntityNodeType(entityTypes); nodeTypeCache.put(entityTypes, result); } return result; } } public static EntityNodeType create(Collection entityTypes) { return createInternal(new ArrayList(entityTypes)); } private static EntityNodeType createInternal(ArrayList entityTypes) { if(entityTypes.isEmpty()) throw new IllegalArgumentException(); else if(entityTypes.size() == 1) return create(entityTypes.get(0)); else { Collections.sort(entityTypes); synchronized(nodeTypeCache) { EntityNodeType result = nodeTypeCache.get(entityTypes); if(result == null) { result = new EntityNodeType(entityTypes); nodeTypeCache.put(entityTypes, result); } return result; } } } public static NodeType getNodeTypeFor(ReadGraph graph, Resource resource) throws DatabaseException { ArrayList types = new ArrayList(graph.getPrincipalTypes(resource)); if(types.isEmpty()) return null; else return createInternal(types); } public NodeType getNodeTypeOf(ReadGraph graph, Resource resource) throws DatabaseException { ArrayList types = new ArrayList(); loop: for(Resource t : graph.getPrincipalTypes(resource)) { for(Resource et : entityTypes) if(!graph.isInheritedFrom(t, et)) continue loop; types.add(t); } if(types.isEmpty()) return null; return createInternal(types); } @Override public NodeContext createNodeContext(ReadGraph graph, Object content) throws DatabaseException { if(content instanceof Resource) { Resource resource = (Resource)content; NodeType nodeType = getNodeTypeOf(graph, resource); if(nodeType != null) return NodeContextBuilder.buildWithData(KEY_SEQUENCE, new Object[] {content, nodeType}); } return null; } @Override public Class getContentType() { return Resource.class; } @Override public int hashCode() { return entityTypes.hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; EntityNodeType other = (EntityNodeType) obj; return entityTypes.equals(other.entityTypes); } @Override public boolean inherits(ReadGraph graph, NodeType superType) throws DatabaseException { if(this == superType) return true; if (superType.getClass() != EntityNodeType.class) return false; loop: for(Resource st : ((EntityNodeType)superType).entityTypes) { for(Resource t : entityTypes) if(graph.isInheritedFrom(t, st)) continue loop; return false; } return true; } @Override public Collection getSuper(ReadGraph g) throws DatabaseException { if(entityTypes.size() == 1) { Layer0 l0 = Layer0.getInstance(g); Collection supertypes = g.getObjects(entityTypes.get(0), l0.Inherits); ArrayList supernodetypes = new ArrayList(supertypes.size()); for(Resource supertype : supertypes) supernodetypes.add(create(supertype)); return supernodetypes; } else { ArrayList supernodetypes = new ArrayList(entityTypes.size()); for(Resource t : entityTypes) supernodetypes.add(create(t)); return supernodetypes; } } @Override public String toString(ReadGraph graph) throws DatabaseException { StringBuilder b = new StringBuilder(); b.append('['); boolean first = true; for(Resource t : entityTypes) { if(first) first = false; else b.append(", "); b.append(NameUtils.getSafeName(graph, t)); } b.append(']'); return b.toString(); } @Override public WorkbenchSelectionElement getWorkbenchSelectionElement(NodeContext context) { // TODO Auto-generated method stub return null; } }