]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/ActionBrowseContext.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / actions / ActionBrowseContext.java
index e1c67bebcd4727a8a9b5a270c3e7626cec00e501..ac9548fffc57c48bf79cf54719414924e5170a2d 100644 (file)
-/*******************************************************************************\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.actions;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.eclipse.jface.action.Action;\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.model.InvalidContribution;\r
-import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;\r
-import org.simantics.browsing.ui.model.browsecontexts.ResolveActionBrowseContext;\r
-import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;\r
-import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
-import org.simantics.browsing.ui.model.nodetypes.NodeTypeMultiMap;\r
-import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.viewpoint.ontology.ViewpointResource;\r
-\r
-/**\r
- * ActionBrowseContext holds all contributions related to given set of action browse contexts.\r
- * \r
- * @author Hannu Niemistö\r
- */\r
-public class ActionBrowseContext {\r
-    NodeTypeMultiMap<ActionContribution> actionContributions = new NodeTypeMultiMap<ActionContribution>();\r
-    // TODO: remove these two temporary mechanisms\r
-    THashSet<Resource> removableNodeTypes = new THashSet<Resource>(); \r
-    THashSet<Resource> renameableNodeTypes = new THashSet<Resource>(); \r
-    NodeTypeMultiMap<TestContribution> removalContributions = new NodeTypeMultiMap<TestContribution>();\r
-    NodeTypeMultiMap<TestContribution> renamingContributions = new NodeTypeMultiMap<TestContribution>();\r
-\r
-    private final String[] uris; \r
-\r
-    private ActionBrowseContext(String[] uris) {\r
-        if (uris == null)\r
-            throw new NullPointerException("null URIs");\r
-        this.uris = uris;\r
-    }\r
-\r
-    public String[] getURIs() {\r
-        return uris;\r
-    }\r
-\r
-    public static ActionBrowseContext get(ReadGraph graph,NodeContext context,ActionBrowseContext defaultContext) throws DatabaseException {\r
-        ActionBrowseContext mbc = graph.syncRequest(new ResolveActionBrowseContext(context));\r
-        if(mbc != null) return mbc;\r
-        ActionBrowseContext parentContext = (ActionBrowseContext)context.getConstant(BuiltinKeys.ACTION_BROWSE_CONTEXT);\r
-        if(parentContext != null) return parentContext;\r
-        return defaultContext;\r
-    }\r
-    \r
-    public static ActionBrowseContext create(ReadGraph g, Collection<Resource> browseContextResources) throws DatabaseException, InvalidContribution {\r
-        ViewpointResource vr = ViewpointResource.getInstance(g);\r
-        ActionBrowseContext browseContext = new ActionBrowseContext( BrowseContexts.toSortedURIs(g, browseContextResources) );\r
-        for(Resource browseContextResource : findSubcontexts(g, browseContextResources)) {\r
-            for(Resource actionContributionResource : \r
-                g.getObjects(browseContextResource, vr.BrowseContext_HasActionContribution)) {\r
-                try {\r
-                    ActionContribution.load(g, actionContributionResource,\r
-                            browseContext.actionContributions\r
-                            );\r
-                } catch(DatabaseException e) {\r
-                    e.printStackTrace();\r
-                }\r
-            }\r
-            for(Resource canRemove : g.getObjects(browseContextResource, vr.BrowseContext_SupportsRemovalOf))\r
-                browseContext.removableNodeTypes.add(canRemove);\r
-            for(Resource canRemove : g.getObjects(browseContextResource, vr.BrowseContext_SupportsRenamingOf))\r
-                browseContext.renameableNodeTypes.add(canRemove);\r
-\r
-            for (Resource testContribution : g.getObjects(browseContextResource, vr.BrowseContext_HasTestContribution)) {\r
-                try {\r
-                    Set<Resource> types = g.getTypes(testContribution);\r
-                    if (types.contains(vr.RemovalTestContribution))\r
-                        TestContribution.load(g, testContribution, browseContext.removalContributions);\r
-                    if (types.contains(vr.RenamingTestContribution))\r
-                        TestContribution.load(g, testContribution, browseContext.renamingContributions);\r
-                } catch (DatabaseException e) {\r
-                    e.printStackTrace();\r
-                }\r
-            }\r
-        }\r
-        return browseContext;\r
-    }\r
-    \r
-    private static Collection<Resource> findSubcontexts(ReadGraph g,\r
-            Collection<Resource> browseContexts) throws DatabaseException {\r
-        ViewpointResource vr = ViewpointResource.getInstance(g);\r
-        HashSet<Resource> result = new HashSet<Resource>(browseContexts);\r
-        ArrayList<Resource> stack = new ArrayList<Resource>(browseContexts);\r
-        while(!stack.isEmpty()) {\r
-            Resource cur = stack.remove(stack.size()-1);\r
-            for(Resource sc : g.getObjects(cur, vr.BrowseContext_Includes))\r
-                if(result.add(sc))\r
-                    stack.add(sc);\r
-        }\r
-        return result;\r
-    }    \r
-    \r
-    private static NodeType getNodeType(ReadGraph graph, NodeContext parent) throws DatabaseException {\r
-        NodeType nodeType = parent.getConstant(NodeType.TYPE);\r
-        if(nodeType == null) {            \r
-            // TODO remove this code when root of model browser is fixed\r
-            Object input = parent.getConstant(BuiltinKeys.INPUT);\r
-            if(input instanceof Resource)\r
-                nodeType = EntityNodeType.getNodeTypeFor(graph, (Resource)input);\r
-        }\r
-        return nodeType;\r
-    }\r
-    \r
-    public Map<IActionCategory, List<Action>> getActions(ReadGraph graph, NodeContext parent, Collection<NodeContext> all) throws DatabaseException {\r
-        NodeType nodeType = getNodeType(graph, parent);\r
-        if(nodeType == null)\r
-            return Collections.emptyMap();\r
-        THashMap<IActionCategory, List<Action>> map = new THashMap<IActionCategory, List<Action>>();\r
-        for(ActionContribution contribution : actionContributions.get(graph, nodeType)) { \r
-            CategorizedAction action = contribution.getAction(graph, parent, all);\r
-            if(action != null) {\r
-                List<Action> actions = map.get(action.category);\r
-                if(actions == null) {\r
-                    actions = new ArrayList<Action>();\r
-                    map.put(action.category, actions);\r
-                }\r
-                actions.add(action.action);\r
-            }\r
-        }\r
-        return map;\r
-    }\r
-\r
-    @Override\r
-    public int hashCode() {\r
-        return Arrays.hashCode(uris);\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
-        ActionBrowseContext other = (ActionBrowseContext) obj;\r
-        return Arrays.equals(uris, other.uris);\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        return getClass().getSimpleName() + Arrays.toString(uris);\r
-    }\r
-\r
-    /*\r
-     * This is an attempt to improve removal logic with SpecialNodeTypes. However it\r
-     * should not be considered as a final solution.\r
-     */\r
-    public boolean canRemove(ReadGraph graph, NodeContext parent) throws DatabaseException {\r
-        return testContributions(graph, parent, removalContributions, removableNodeTypes);\r
-    }\r
-\r
-    /*\r
-     * This is an attempt to improve renaming logic with SpecialNodeTypes. However it\r
-     * should not be considered as a final solution.\r
-     */\r
-    public boolean canRename(ReadGraph graph, NodeContext parent) throws DatabaseException {\r
-        return testContributions(graph, parent, renamingContributions, renameableNodeTypes);\r
-    }\r
-\r
-    private boolean testContributions(\r
-            ReadGraph graph,\r
-            NodeContext parent,\r
-            NodeTypeMultiMap<TestContribution> contributions,\r
-            Set<Resource> allowedSpecialTypes) throws DatabaseException {\r
-        NodeType nodeType = getNodeType(graph, parent);\r
-        if (nodeType == null)\r
-            // Return true for now if node type is not available\r
-            // to prevent older and more custom solutions such as\r
-            // property view tables from breaking up.\r
-            return true;\r
-\r
-        // TODO: this is a previous temporary solution that should probably be removed\r
-        if (nodeType instanceof SpecialNodeType\r
-                && allowedSpecialTypes.contains(((SpecialNodeType) nodeType).resource))\r
-            return true;\r
-\r
-        for (TestContribution contribution : contributions.get(graph, nodeType)) { \r
-            if (!contribution.test(graph, parent))\r
-                return false;\r
-        }\r
-        return true;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * 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.actions;
+
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.set.hash.THashSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.jface.action.Action;
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.model.InvalidContribution;
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;
+import org.simantics.browsing.ui.model.browsecontexts.ResolveActionBrowseContext;
+import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
+import org.simantics.browsing.ui.model.nodetypes.NodeType;
+import org.simantics.browsing.ui.model.nodetypes.NodeTypeMultiMap;
+import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.viewpoint.ontology.ViewpointResource;
+
+/**
+ * ActionBrowseContext holds all contributions related to given set of action browse contexts.
+ * 
+ * @author Hannu Niemistö
+ */
+public class ActionBrowseContext {
+    NodeTypeMultiMap<ActionContribution> actionContributions = new NodeTypeMultiMap<ActionContribution>();
+    // TODO: remove these two temporary mechanisms
+    THashSet<Resource> removableNodeTypes = new THashSet<Resource>(); 
+    THashSet<Resource> renameableNodeTypes = new THashSet<Resource>(); 
+    NodeTypeMultiMap<TestContribution> removalContributions = new NodeTypeMultiMap<TestContribution>();
+    NodeTypeMultiMap<TestContribution> renamingContributions = new NodeTypeMultiMap<TestContribution>();
+
+    private final String[] uris; 
+
+    private ActionBrowseContext(String[] uris) {
+        if (uris == null)
+            throw new NullPointerException("null URIs");
+        this.uris = uris;
+    }
+
+    public String[] getURIs() {
+        return uris;
+    }
+
+    public static ActionBrowseContext get(ReadGraph graph,NodeContext context,ActionBrowseContext defaultContext) throws DatabaseException {
+        ActionBrowseContext mbc = graph.syncRequest(new ResolveActionBrowseContext(context));
+        if(mbc != null) return mbc;
+        ActionBrowseContext parentContext = (ActionBrowseContext)context.getConstant(BuiltinKeys.ACTION_BROWSE_CONTEXT);
+        if(parentContext != null) return parentContext;
+        return defaultContext;
+    }
+    
+    public static ActionBrowseContext create(ReadGraph g, Collection<Resource> browseContextResources) throws DatabaseException, InvalidContribution {
+        ViewpointResource vr = ViewpointResource.getInstance(g);
+        ActionBrowseContext browseContext = new ActionBrowseContext( BrowseContexts.toSortedURIs(g, browseContextResources) );
+        for(Resource browseContextResource : findSubcontexts(g, browseContextResources)) {
+            for(Resource actionContributionResource : 
+                g.getObjects(browseContextResource, vr.BrowseContext_HasActionContribution)) {
+                try {
+                    ActionContribution.load(g, actionContributionResource,
+                            browseContext.actionContributions
+                            );
+                } catch(DatabaseException e) {
+                    e.printStackTrace();
+                }
+            }
+            for(Resource canRemove : g.getObjects(browseContextResource, vr.BrowseContext_SupportsRemovalOf))
+                browseContext.removableNodeTypes.add(canRemove);
+            for(Resource canRemove : g.getObjects(browseContextResource, vr.BrowseContext_SupportsRenamingOf))
+                browseContext.renameableNodeTypes.add(canRemove);
+
+            for (Resource testContribution : g.getObjects(browseContextResource, vr.BrowseContext_HasTestContribution)) {
+                try {
+                    Set<Resource> types = g.getTypes(testContribution);
+                    if (types.contains(vr.RemovalTestContribution))
+                        TestContribution.load(g, testContribution, browseContext.removalContributions);
+                    if (types.contains(vr.RenamingTestContribution))
+                        TestContribution.load(g, testContribution, browseContext.renamingContributions);
+                } catch (DatabaseException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return browseContext;
+    }
+    
+    private static Collection<Resource> findSubcontexts(ReadGraph g,
+            Collection<Resource> browseContexts) throws DatabaseException {
+        ViewpointResource vr = ViewpointResource.getInstance(g);
+        HashSet<Resource> result = new HashSet<Resource>(browseContexts);
+        ArrayList<Resource> stack = new ArrayList<Resource>(browseContexts);
+        while(!stack.isEmpty()) {
+            Resource cur = stack.remove(stack.size()-1);
+            for(Resource sc : g.getObjects(cur, vr.BrowseContext_Includes))
+                if(result.add(sc))
+                    stack.add(sc);
+        }
+        return result;
+    }    
+    
+    private static NodeType getNodeType(ReadGraph graph, NodeContext parent) throws DatabaseException {
+        NodeType nodeType = parent.getConstant(NodeType.TYPE);
+        if(nodeType == null) {            
+            // TODO remove this code when root of model browser is fixed
+            Object input = parent.getConstant(BuiltinKeys.INPUT);
+            if(input instanceof Resource)
+                nodeType = EntityNodeType.getNodeTypeFor(graph, (Resource)input);
+        }
+        return nodeType;
+    }
+    
+    public Map<IActionCategory, List<Action>> getActions(ReadGraph graph, NodeContext parent, Collection<NodeContext> all) throws DatabaseException {
+        NodeType nodeType = getNodeType(graph, parent);
+        if(nodeType == null)
+            return Collections.emptyMap();
+        THashMap<IActionCategory, List<Action>> map = new THashMap<IActionCategory, List<Action>>();
+        for(ActionContribution contribution : actionContributions.get(graph, nodeType)) { 
+            CategorizedAction action = contribution.getAction(graph, parent, all);
+            if(action != null) {
+                List<Action> actions = map.get(action.category);
+                if(actions == null) {
+                    actions = new ArrayList<Action>();
+                    map.put(action.category, actions);
+                }
+                actions.add(action.action);
+            }
+        }
+        return map;
+    }
+
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(uris);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        ActionBrowseContext other = (ActionBrowseContext) obj;
+        return Arrays.equals(uris, other.uris);
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + Arrays.toString(uris);
+    }
+
+    /*
+     * This is an attempt to improve removal logic with SpecialNodeTypes. However it
+     * should not be considered as a final solution.
+     */
+    public boolean canRemove(ReadGraph graph, NodeContext parent) throws DatabaseException {
+        return testContributions(graph, parent, removalContributions, removableNodeTypes);
+    }
+
+    /*
+     * This is an attempt to improve renaming logic with SpecialNodeTypes. However it
+     * should not be considered as a final solution.
+     */
+    public boolean canRename(ReadGraph graph, NodeContext parent) throws DatabaseException {
+        return testContributions(graph, parent, renamingContributions, renameableNodeTypes);
+    }
+
+    private boolean testContributions(
+            ReadGraph graph,
+            NodeContext parent,
+            NodeTypeMultiMap<TestContribution> contributions,
+            Set<Resource> allowedSpecialTypes) throws DatabaseException {
+        NodeType nodeType = getNodeType(graph, parent);
+        if (nodeType == null)
+            // Return true for now if node type is not available
+            // to prevent older and more custom solutions such as
+            // property view tables from breaking up.
+            return true;
+
+        // TODO: this is a previous temporary solution that should probably be removed
+        if (nodeType instanceof SpecialNodeType
+                && allowedSpecialTypes.contains(((SpecialNodeType) nodeType).resource))
+            return true;
+
+        for (TestContribution contribution : contributions.get(graph, nodeType)) { 
+            if (!contribution.test(graph, parent))
+                return false;
+        }
+        return true;
+    }
+
+}