]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ModeledActions.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / ModeledActions.java
index ea50134d75ed0d728d6706175066299b8186cb0d..478d8b761046177484db3ecbc02643ada1801feb 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2011, 2016 Association for Decentralized Information Management\r
- * in 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
- *     Semantum Oy - refactoring\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.actions;\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.Comparator;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.eclipse.core.runtime.CoreException;\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.core.runtime.IConfigurationElement;\r
-import org.eclipse.core.runtime.IExecutableExtension;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.ActionContributionItem;\r
-import org.eclipse.jface.action.IContributionItem;\r
-import org.eclipse.jface.action.MenuManager;\r
-import org.eclipse.jface.action.Separator;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.common.NodeContextBuilder;\r
-import org.simantics.browsing.ui.model.InvalidContribution;\r
-import org.simantics.browsing.ui.model.actions.ActionBrowseContext;\r
-import org.simantics.browsing.ui.model.actions.IActionCategory;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.issues.common.IssueUtils;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.project.ontology.ProjectResource;\r
-import org.simantics.ui.contribution.DynamicMenuContribution;\r
-import org.simantics.ui.selection.WorkbenchSelectionElement;\r
-import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-public class ModeledActions extends DynamicMenuContribution implements IExecutableExtension {\r
-    private static final Logger LOGGER = LoggerFactory.getLogger(IssueUtils.class);\r
-\r
-    public static final Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectActionContext);\r
-\r
-    protected Set<String> browseContexts = defaultBrowseContexts;\r
-\r
-    public static final Comparator<Action> ACTION_COMPARATOR = new Comparator<Action>() {\r
-        @Override\r
-        public int compare(Action o1, Action o2) {\r
-            String t1 = o1.getText();\r
-            String t2 = o2.getText();\r
-            if(t1 == null) {\r
-                if(t2 == null)\r
-                    return 0;\r
-                else\r
-                    return -1;\r
-            }           \r
-            else {\r
-                if(t2 == null)\r
-                    return 1;\r
-                else\r
-                    return t1.compareTo(t2);\r
-            }\r
-        }\r
-    };\r
-\r
-    @Override\r
-    public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {\r
-        if(data instanceof String) {\r
-            String str = (String)data;\r
-            String[] parms = str.split(";");\r
-            for(String parm : parms) {\r
-                String[] keyValue = parm.split("=");\r
-                if(keyValue.length == 2) {\r
-                    String key = keyValue[0].trim();\r
-                    if("context".equals(key)) {\r
-                        browseContexts = Collections.singleton(keyValue[1]);\r
-                    }\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    protected Collection<String> getBrowseContexts() {\r
-        return browseContexts;\r
-    }\r
-\r
-    protected Collection<Resource> getBrowseContextResources(ReadGraph graph) throws DatabaseException {\r
-        Collection<String> names = getBrowseContexts(); \r
-        ArrayList<Resource> result = new ArrayList<>(names.size());\r
-        for(String name : names)\r
-            result.add(graph.getResource(name));\r
-        return result;\r
-    }\r
-\r
-    protected List<NodeContext> getContexts(Object[] selection) {\r
-        if (selection.length == 0)\r
-            return Collections.emptyList();\r
-\r
-        ArrayList<NodeContext> result = new ArrayList<>(selection.length);\r
-\r
-        for (Object o : selection) {\r
-            if ((o instanceof IAdaptable)) {\r
-                   NodeContext nodeContext = ((IAdaptable) o).getAdapter(NodeContext.class);\r
-                   if (nodeContext != null)\r
-                       result.add(nodeContext);\r
-            } else if (o instanceof WorkbenchSelectionElement) {\r
-               try {\r
-                                       Resource res = WorkbenchSelectionUtils.getPossibleResource((WorkbenchSelectionElement)o);\r
-                                       if(res != null) {\r
-                                               result.add(NodeContextBuilder.buildWithInput(res));\r
-                                       }\r
-                               } catch (DatabaseException e) {\r
-                                   LOGGER.error("Failed to get node contexts for selection.", e);\r
-                               }\r
-            }\r
-        }\r
-\r
-        return result;\r
-    }\r
-\r
-    protected List<NodeContext> getContexts(ReadGraph graph, Object[] selection) throws DatabaseException {\r
-        return getContexts(selection);\r
-    }\r
-\r
-    protected static IContributionItem[] toContributionItems(Map<IActionCategory, List<Action>> map) {\r
-        if (map.isEmpty())\r
-            return NONE;\r
-\r
-        IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);\r
-        Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);\r
-\r
-        ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();\r
-        boolean first = true;\r
-        for (IActionCategory category : categories) {\r
-            List<Action> actions = map.get(category);\r
-            Collections.sort(actions, ACTION_COMPARATOR);\r
-\r
-            if (category != null && category.isSubmenu()) {\r
-                MenuManager manager = new MenuManager(category.getLabel());\r
-                for (Action action : actions)\r
-                    manager.add(new ActionContributionItem(action));\r
-                items.add(manager);\r
-            }\r
-            else {\r
-                if (first)\r
-                    first = false;\r
-                else\r
-                    items.add(new Separator(category == null ? "" : category.getLabel()));\r
-                for (Action action : actions)\r
-                    items.add(new ActionContributionItem(action));\r
-            }\r
-        }\r
-        return items.toArray(new IContributionItem[items.size()]);\r
-    }\r
-\r
-    @Override\r
-    protected Object[] getSelectedObjects() {\r
-        Object[] sel = super.getSelectedObjects();\r
-        List<NodeContext> contexts = getContexts(sel);\r
-        return contexts.toArray(NodeContext.NONE);\r
-    }\r
-\r
-    @Override\r
-    protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection)\r
-            throws DatabaseException\r
-    {\r
-        List<NodeContext> contexts = Arrays.asList( (NodeContext[]) selection );\r
-        if (contexts.isEmpty())\r
-            return NONE;\r
-\r
-        try {\r
-            NodeContext nodeContext = contexts.get(0);\r
-\r
-            ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, getBrowseContextResources(graph));\r
-            ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);\r
-\r
-            Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);\r
-            Map<IActionCategory, List<Action>> current = result;\r
-\r
-            for (int i = 1; i < contexts.size(); i++) {\r
-\r
-                browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);\r
-\r
-                Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);\r
-\r
-                result = new HashMap<>();\r
-\r
-                for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {\r
-                    List<Action> exist = current.get(entry.getKey());\r
-                    if (exist == null)\r
-                        continue;\r
-\r
-                    ArrayList<Action> l = new ArrayList<Action>();\r
-                    for(Action e : exist) {\r
-                        String id = e.getId();\r
-                        boolean found = false;\r
-                        for(Action a : entry.getValue()) {\r
-                            if(id.equals(a.getId())) {\r
-                                found = true;\r
-                                break;\r
-                            }\r
-                        }\r
-                        if(found) l.add(e);\r
-                    }\r
-                    if(!l.isEmpty()) result.put(entry.getKey(), l);\r
-                }\r
-\r
-                current = result;\r
-\r
-            }\r
-\r
-            return toContributionItems(result);\r
-\r
-        } catch (InvalidContribution e) {\r
-            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
-                    "Invalid contribution encountered in " + getClass().getSimpleName() + ".", e));\r
-        }\r
-        return NONE;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2011, 2016 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
+ *     Semantum Oy - refactoring
+ *******************************************************************************/
+package org.simantics.modeling.ui.actions;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExecutableExtension;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.NodeContextBuilder;
+import org.simantics.browsing.ui.model.InvalidContribution;
+import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
+import org.simantics.browsing.ui.model.actions.IActionCategory;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.issues.common.IssueUtils;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.project.ontology.ProjectResource;
+import org.simantics.ui.contribution.DynamicMenuContribution;
+import org.simantics.ui.selection.WorkbenchSelectionElement;
+import org.simantics.ui.selection.WorkbenchSelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ModeledActions extends DynamicMenuContribution implements IExecutableExtension {
+    private static final Logger LOGGER = LoggerFactory.getLogger(IssueUtils.class);
+
+    public static final Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectActionContext);
+
+    protected Set<String> browseContexts = defaultBrowseContexts;
+
+    public static final Comparator<Action> ACTION_COMPARATOR = new Comparator<Action>() {
+        @Override
+        public int compare(Action o1, Action o2) {
+            String t1 = o1.getText();
+            String t2 = o2.getText();
+            if(t1 == null) {
+                if(t2 == null)
+                    return 0;
+                else
+                    return -1;
+            }           
+            else {
+                if(t2 == null)
+                    return 1;
+                else
+                    return t1.compareTo(t2);
+            }
+        }
+    };
+
+    @Override
+    public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
+        if(data instanceof String) {
+            String str = (String)data;
+            String[] parms = str.split(";");
+            for(String parm : parms) {
+                String[] keyValue = parm.split("=");
+                if(keyValue.length == 2) {
+                    String key = keyValue[0].trim();
+                    if("context".equals(key)) {
+                        browseContexts = Collections.singleton(keyValue[1]);
+                    }
+                }
+            }
+        }
+    }
+
+    protected Collection<String> getBrowseContexts() {
+        return browseContexts;
+    }
+
+    protected Collection<Resource> getBrowseContextResources(ReadGraph graph) throws DatabaseException {
+        Collection<String> names = getBrowseContexts(); 
+        ArrayList<Resource> result = new ArrayList<>(names.size());
+        for(String name : names)
+            result.add(graph.getResource(name));
+        return result;
+    }
+
+    protected List<NodeContext> getContexts(Object[] selection) {
+        if (selection.length == 0)
+            return Collections.emptyList();
+
+        ArrayList<NodeContext> result = new ArrayList<>(selection.length);
+
+        for (Object o : selection) {
+            if ((o instanceof IAdaptable)) {
+                   NodeContext nodeContext = ((IAdaptable) o).getAdapter(NodeContext.class);
+                   if (nodeContext != null) {
+                       result.add(nodeContext);
+                       continue;
+                   }
+            }
+            try {
+               Resource res = WorkbenchSelectionUtils.getPossibleResource(o);
+               if(res != null) {
+                       result.add(NodeContextBuilder.buildWithInput(res));
+               }
+            } catch (DatabaseException e) {
+               LOGGER.error("Failed to get node contexts for selection.", e);
+            }
+        }
+
+        return result;
+    }
+
+    protected List<NodeContext> getContexts(ReadGraph graph, Object[] selection) throws DatabaseException {
+        return getContexts(selection);
+    }
+
+    protected static IContributionItem[] toContributionItems(Map<IActionCategory, List<Action>> map) {
+        if (map.isEmpty())
+            return NONE;
+
+        IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
+        Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
+
+        ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();
+        boolean first = true;
+        for (IActionCategory category : categories) {
+            List<Action> actions = map.get(category);
+            Collections.sort(actions, ACTION_COMPARATOR);
+
+            if (category != null && category.isSubmenu()) {
+                MenuManager manager = new MenuManager(category.getLabel());
+                for (Action action : actions)
+                    manager.add(new ActionContributionItem(action));
+                items.add(manager);
+            }
+            else {
+                if (first)
+                    first = false;
+                else
+                    items.add(new Separator(category == null ? "" : category.getLabel()));
+                for (Action action : actions)
+                    items.add(new ActionContributionItem(action));
+            }
+        }
+        return items.toArray(new IContributionItem[items.size()]);
+    }
+
+    @Override
+    protected Object[] getSelectedObjects() {
+        Object[] sel = super.getSelectedObjects();
+        List<NodeContext> contexts = getContexts(sel);
+        return contexts.toArray(NodeContext.NONE);
+    }
+
+    @Override
+    protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection)
+            throws DatabaseException
+    {
+        List<NodeContext> contexts = Arrays.asList( (NodeContext[]) selection );
+        if (contexts.isEmpty())
+            return NONE;
+
+        try {
+            NodeContext nodeContext = contexts.get(0);
+
+            ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, getBrowseContextResources(graph));
+            ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
+
+            Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
+            Map<IActionCategory, List<Action>> current = result;
+
+            for (int i = 1; i < contexts.size(); i++) {
+
+                browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
+
+                Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
+
+                result = new HashMap<>();
+
+                for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
+                    List<Action> exist = current.get(entry.getKey());
+                    if (exist == null)
+                        continue;
+
+                    ArrayList<Action> l = new ArrayList<Action>();
+                    for(Action e : exist) {
+                        String id = e.getId();
+                        boolean found = false;
+                        for(Action a : entry.getValue()) {
+                            if(id.equals(a.getId())) {
+                                found = true;
+                                break;
+                            }
+                        }
+                        if(found) l.add(e);
+                    }
+                    if(!l.isEmpty()) result.put(entry.getKey(), l);
+                }
+
+                current = result;
+
+            }
+
+            return toContributionItems(result);
+
+        } catch (InvalidContribution e) {
+            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                    "Invalid contribution encountered in " + getClass().getSimpleName() + ".", e));
+        }
+        return NONE;
+    }
+
+}