]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/view/handler/MenuActions.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / MenuActions.java
index 9b125ed446342deb269dd77ec5b2c02517ba9074..0bbfd78f5c939c5a713e5c63e4969480ed670625 100644 (file)
-package org.simantics.event.view.handler;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.IAction;\r
-import org.eclipse.jface.action.IStatusLineManager;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.swt.dnd.Clipboard;\r
-import org.eclipse.swt.dnd.TextTransfer;\r
-import org.eclipse.swt.dnd.Transfer;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.PossibleTypedParent;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.event.Activator;\r
-import org.simantics.event.ontology.EventResource;\r
-import org.simantics.event.util.EventUtils;\r
-import org.simantics.ui.contribution.DynamicMenuContribution;\r
-import org.simantics.ui.workbench.action.PerformDefaultAction;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class MenuActions extends DynamicMenuContribution {\r
-\r
-    /**\r
-     * The name of the virtual graph the menu actions perform their changes into.\r
-     */\r
-    private static final String VG_EXPERIMENTS = "experiments";\r
-\r
-    @Override\r
-    protected Object[] getSelectedObjects() {\r
-        ISelection sel = getSelection();\r
-        List<Resource> resources = ISelectionUtils.getPossibleKeys(sel, SelectionHints.KEY_MAIN, Resource.class);\r
-        return resources.toArray();\r
-    }\r
-\r
-    List<Resource> toResources(Object[] array) {\r
-        Resource[] a = new Resource[array.length];\r
-        for (int i = 0; i < array.length; ++i)\r
-            a[i] = (Resource) array[i];\r
-        return Arrays.asList(a);\r
-    }\r
-\r
-    @Override\r
-    protected IAction[] getActions(ReadGraph graph, Object[] selection) throws DatabaseException {\r
-        if (selection.length == 0)\r
-            return new IAction[0];\r
-\r
-        List<Resource> input = toResources(selection);\r
-        if (input.isEmpty())\r
-            return new IAction[0];\r
-\r
-        boolean logs = false;\r
-        boolean events = false;\r
-\r
-        EventResource EVENT = EventResource.getInstance(graph);\r
-        int hiddenCount = 0; \r
-        int milestoneCount = 0; \r
-        int hasSourceCount = 0;\r
-        for (Resource r : input) {\r
-            logs |= graph.isInstanceOf(r, EVENT.EventLog);\r
-            events |= graph.isInstanceOf(r, EVENT.Event);\r
-            if (graph.hasStatement(r, EVENT.Hidden))\r
-                ++hiddenCount;\r
-            if (graph.hasStatement(r, EVENT.Milestone))\r
-                ++milestoneCount;\r
-            if (graph.hasStatement(r, EVENT.Event_source))\r
-                ++hasSourceCount;\r
-        }\r
-        boolean allHidden = hiddenCount == selection.length;\r
-        boolean allMilestones= milestoneCount == selection.length;\r
-\r
-        Resource event = null;\r
-        String eventSourceName = null;\r
-        if (input.size() == 1) {\r
-            event = input.get(0);\r
-            if (hasSourceCount == 1) {\r
-                eventSourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName, Bindings.STRING);\r
-            }\r
-        }\r
-\r
-        List<IAction> actions = new ArrayList<IAction>();\r
-\r
-        if (eventSourceName != null && !eventSourceName.isEmpty())\r
-            actions.add(toClipboardAction(eventSourceName, eventSourceName));\r
-        if (!allHidden)\r
-            actions.add(hideAction(input));\r
-        if (hiddenCount > 0 || allHidden)\r
-            actions.add(unhideAction(input));\r
-\r
-        if (!logs && !allMilestones)\r
-            actions.add(markMilestoneAction(input));\r
-        if (!logs && (milestoneCount > 0 || allMilestones)) {\r
-            Resource eventLog = graph.syncRequest(new PossibleTypedParent(input.get(0), EVENT.EventLog));\r
-            actions.add(unmarkMilestoneAction(eventLog, input));\r
-        }\r
-\r
-        if (!logs && events && event != null) {\r
-            Resource eventLog = graph.syncRequest(new PossibleTypedParent(event, EVENT.EventLog));\r
-            if (eventLog != null) {\r
-                boolean isBaseline = graph.hasStatement(eventLog, EVENT.EventLog_HasBaselineEvent, event);\r
-                if (isBaseline && allMilestones)\r
-                    actions.add(removeBaseline(Collections.singletonList(eventLog)));\r
-                else\r
-                    actions.add(setBaseline(eventLog, event));\r
-            }\r
-        }\r
-        if (logs && !events) {\r
-            actions.add(removeBaseline(input));\r
-        }\r
-\r
-        if (event != null && hasSourceCount == 1) {\r
-            Resource eventSource = graph.getPossibleObject(event, EVENT.Event_source);\r
-            if (eventSource != null)\r
-                actions.add(performDefaultAction(eventSource, null));\r
-        }\r
-\r
-        return actions.toArray(new IAction[actions.size()]);\r
-    }\r
-\r
-    private IAction toClipboardAction(String label, String text) {\r
-        return new ToClipboardAction(label, text);\r
-    }\r
-\r
-    private IAction markMilestoneAction(List<Resource> input) {\r
-        return tagAction("Mark as Milestone", Activator.MARK_MILESTONE_ICON, EventResource.URIs.Milestone, true, input);\r
-    }\r
-\r
-    private IAction unmarkMilestoneAction(Resource eventLog, List<Resource> input) {\r
-        return new UnmarkMilestone(VG_EXPERIMENTS, eventLog, input);\r
-    }\r
-\r
-    private IAction unhideAction(List<Resource> input) {\r
-        return tagAction("Unhide", Activator.UNHIDE_ICON, EventResource.URIs.Hidden, false, input);\r
-    }\r
-\r
-    private IAction hideAction(List<Resource> input) {\r
-        return contentChangingTagAction("Hide", Activator.HIDE_ICON, EventResource.URIs.Hidden, true, input);\r
-    }\r
-\r
-    private IAction tagAction(String label, ImageDescriptor image, String tagURI, boolean tag, List<Resource> input) {\r
-        return new TagAction(label, image, VG_EXPERIMENTS, tagURI, tag, input);\r
-    }\r
-\r
-    private IAction contentChangingTagAction(String label, ImageDescriptor image, String tagURI, boolean tag, List<Resource> input) {\r
-        return new ContentChangingTagAction(label, image, VG_EXPERIMENTS, tagURI, tag, input);\r
-    }\r
-\r
-    private IAction setBaseline(Resource eventLog, Resource event) {\r
-        return new SetBaseline(VG_EXPERIMENTS, eventLog, event);\r
-    }\r
-\r
-    private IAction removeBaseline(List<Resource> logs) {\r
-        return new DenyAction("Remove Baseline", Activator.REMOVE_BASELINE_ICON, VG_EXPERIMENTS, EventResource.URIs.EventLog_HasBaselineEvent, logs);\r
-    }\r
-\r
-    private IAction performDefaultAction(Resource input, String sourceName) {\r
-        String title = "Show Event Source";\r
-        if (sourceName != null)\r
-            title += " (" + sourceName + ")";\r
-        return new PerformDefaultAction(title, null, input);\r
-    }\r
-\r
-    private static class ContentChangingTagAction extends TagAction {\r
-        public ContentChangingTagAction(String label, ImageDescriptor image, String virtualGraphId, String tagURI, boolean tag, List<Resource> input) {\r
-            super(label, image, virtualGraphId, tagURI, tag, input);\r
-        }\r
-\r
-        @Override\r
-        public void postTagWrite(WriteGraph graph) throws DatabaseException {\r
-            EventUtils.bumpModificationCounter(graph, resources);\r
-        }\r
-    }\r
-\r
-    private static class ToClipboardAction extends Action {\r
-        private String text;\r
-\r
-        public ToClipboardAction(String label, String text) {\r
-            super(label, Activator.CLIPBOARD_ICON);\r
-            this.text = text;\r
-        }\r
-\r
-        @Override\r
-        public void run() {\r
-            Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay());\r
-            clipboard.setContents(\r
-                    new Object[] { text },\r
-                    new Transfer[] { TextTransfer.getInstance() }\r
-                    );\r
-            clipboard.dispose();\r
-\r
-            // Show a message in the status line if possible\r
-            IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
-            if (part != null) {\r
-                IStatusLineManager status = WorkbenchUtils.getStatusLine(part);\r
-                if (status != null) {\r
-                    status.setErrorMessage(null);\r
-                    status.setMessage("Copied '" + text + "' to clipboard");\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    private static class UnmarkMilestone extends TagAction {\r
-        private final Resource eventLog;\r
-        private final List<Resource> events;\r
-        public UnmarkMilestone(String virtualGraphId, Resource eventLog, List<Resource> events) {\r
-            super("Unmark Milestone", Activator.UNMARK_MILESTONE_ICON, virtualGraphId, EventResource.URIs.Milestone, false, events);\r
-            this.eventLog = eventLog;\r
-            this.events = events;\r
-        }\r
-        @Override\r
-        public void postTagWrite(WriteGraph graph) throws DatabaseException {\r
-            EventResource EVENT = EventResource.getInstance(graph);\r
-            Resource baselineEvent = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent);\r
-            if (baselineEvent != null) {\r
-                if (events.contains(baselineEvent)) {\r
-                    graph.deny(eventLog, EVENT.EventLog_HasBaselineEvent);\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    private static class SetBaseline extends ClaimAction {\r
-        public SetBaseline(String virtualGraphId, Resource subject, Resource object) {\r
-            super("Set Baseline", Activator.SET_BASELINE_ICON, virtualGraphId, subject, EventResource.URIs.EventLog_HasBaselineEvent, object);\r
-        }\r
-        @Override\r
-        public void claim(WriteGraph graph) throws DatabaseException {\r
-            super.claim(graph);\r
-            EventResource EVENT = EventResource.getInstance(graph);\r
-            if (!graph.hasStatement(object, EVENT.Milestone)) {\r
-                graph.claim(object, EVENT.Milestone, object);\r
-                CorrectMilestoneLabelsAction.correctMilestoneLabels(graph, graph.getProvider(), Collections.singleton(object));\r
-            }\r
-        }\r
-    }\r
+package org.simantics.event.view.handler;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.PossibleTypedParent;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.event.Activator;
+import org.simantics.event.ontology.EventResource;
+import org.simantics.event.util.EventUtils;
+import org.simantics.ui.contribution.DynamicMenuContribution;
+import org.simantics.ui.workbench.action.PerformDefaultAction;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class MenuActions extends DynamicMenuContribution {
+
+    /**
+     * The name of the virtual graph the menu actions perform their changes into.
+     */
+    private static final String VG_EXPERIMENTS = "experiments";
+
+    @Override
+    protected Object[] getSelectedObjects() {
+        ISelection sel = getSelection();
+        List<Resource> resources = ISelectionUtils.getPossibleKeys(sel, SelectionHints.KEY_MAIN, Resource.class);
+        return resources.toArray();
+    }
+
+    List<Resource> toResources(Object[] array) {
+        Resource[] a = new Resource[array.length];
+        for (int i = 0; i < array.length; ++i)
+            a[i] = (Resource) array[i];
+        return Arrays.asList(a);
+    }
+
+    @Override
+    protected IAction[] getActions(ReadGraph graph, Object[] selection) throws DatabaseException {
+        if (selection.length == 0)
+            return new IAction[0];
+
+        List<Resource> input = toResources(selection);
+        if (input.isEmpty())
+            return new IAction[0];
+
+        boolean logs = false;
+        boolean events = false;
+
+        EventResource EVENT = EventResource.getInstance(graph);
+        int hiddenCount = 0; 
+        int milestoneCount = 0; 
+        int hasSourceCount = 0;
+        for (Resource r : input) {
+            logs |= graph.isInstanceOf(r, EVENT.EventLog);
+            events |= graph.isInstanceOf(r, EVENT.Event);
+            if (graph.hasStatement(r, EVENT.Hidden))
+                ++hiddenCount;
+            if (graph.hasStatement(r, EVENT.Milestone))
+                ++milestoneCount;
+            if (graph.hasStatement(r, EVENT.Event_source))
+                ++hasSourceCount;
+        }
+        boolean allHidden = hiddenCount == selection.length;
+        boolean allMilestones= milestoneCount == selection.length;
+
+        Resource event = null;
+        String eventSourceName = null;
+        if (input.size() == 1) {
+            event = input.get(0);
+            if (hasSourceCount == 1) {
+                eventSourceName = graph.getPossibleRelatedValue(event, EVENT.Event_sourceName, Bindings.STRING);
+            }
+        }
+
+        List<IAction> actions = new ArrayList<IAction>();
+
+        if (eventSourceName != null && !eventSourceName.isEmpty())
+            actions.add(toClipboardAction(eventSourceName, eventSourceName));
+        if (!allHidden)
+            actions.add(hideAction(input));
+        if (hiddenCount > 0 || allHidden)
+            actions.add(unhideAction(input));
+
+        if (!logs && !allMilestones)
+            actions.add(markMilestoneAction(input));
+        if (!logs && (milestoneCount > 0 || allMilestones)) {
+            Resource eventLog = graph.syncRequest(new PossibleTypedParent(input.get(0), EVENT.EventLog));
+            actions.add(unmarkMilestoneAction(eventLog, input));
+        }
+
+        if (!logs && events && event != null) {
+            Resource eventLog = graph.syncRequest(new PossibleTypedParent(event, EVENT.EventLog));
+            if (eventLog != null) {
+                boolean isBaseline = graph.hasStatement(eventLog, EVENT.EventLog_HasBaselineEvent, event);
+                if (isBaseline && allMilestones)
+                    actions.add(removeBaseline(Collections.singletonList(eventLog)));
+                else
+                    actions.add(setBaseline(eventLog, event));
+            }
+        }
+        if (logs && !events) {
+            actions.add(removeBaseline(input));
+        }
+
+        if (event != null && hasSourceCount == 1) {
+            Resource eventSource = graph.getPossibleObject(event, EVENT.Event_source);
+            if (eventSource != null)
+                actions.add(performDefaultAction(eventSource, null));
+        }
+
+        return actions.toArray(new IAction[actions.size()]);
+    }
+
+    private IAction toClipboardAction(String label, String text) {
+        return new ToClipboardAction(label, text);
+    }
+
+    private IAction markMilestoneAction(List<Resource> input) {
+        return tagAction("Mark as Milestone", Activator.MARK_MILESTONE_ICON, EventResource.URIs.Milestone, true, input);
+    }
+
+    private IAction unmarkMilestoneAction(Resource eventLog, List<Resource> input) {
+        return new UnmarkMilestone(VG_EXPERIMENTS, eventLog, input);
+    }
+
+    private IAction unhideAction(List<Resource> input) {
+        return tagAction("Unhide", Activator.UNHIDE_ICON, EventResource.URIs.Hidden, false, input);
+    }
+
+    private IAction hideAction(List<Resource> input) {
+        return contentChangingTagAction("Hide", Activator.HIDE_ICON, EventResource.URIs.Hidden, true, input);
+    }
+
+    private IAction tagAction(String label, ImageDescriptor image, String tagURI, boolean tag, List<Resource> input) {
+        return new TagAction(label, image, VG_EXPERIMENTS, tagURI, tag, input);
+    }
+
+    private IAction contentChangingTagAction(String label, ImageDescriptor image, String tagURI, boolean tag, List<Resource> input) {
+        return new ContentChangingTagAction(label, image, VG_EXPERIMENTS, tagURI, tag, input);
+    }
+
+    private IAction setBaseline(Resource eventLog, Resource event) {
+        return new SetBaseline(VG_EXPERIMENTS, eventLog, event);
+    }
+
+    private IAction removeBaseline(List<Resource> logs) {
+        return new DenyAction("Remove Baseline", Activator.REMOVE_BASELINE_ICON, VG_EXPERIMENTS, EventResource.URIs.EventLog_HasBaselineEvent, logs);
+    }
+
+    private IAction performDefaultAction(Resource input, String sourceName) {
+        String title = "Show Event Source";
+        if (sourceName != null)
+            title += " (" + sourceName + ")";
+        return new PerformDefaultAction(title, null, input);
+    }
+
+    private static class ContentChangingTagAction extends TagAction {
+        public ContentChangingTagAction(String label, ImageDescriptor image, String virtualGraphId, String tagURI, boolean tag, List<Resource> input) {
+            super(label, image, virtualGraphId, tagURI, tag, input);
+        }
+
+        @Override
+        public void postTagWrite(WriteGraph graph) throws DatabaseException {
+            EventUtils.bumpModificationCounter(graph, resources);
+        }
+    }
+
+    private static class ToClipboardAction extends Action {
+        private String text;
+
+        public ToClipboardAction(String label, String text) {
+            super(label, Activator.CLIPBOARD_ICON);
+            this.text = text;
+        }
+
+        @Override
+        public void run() {
+            Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay());
+            clipboard.setContents(
+                    new Object[] { text },
+                    new Transfer[] { TextTransfer.getInstance() }
+                    );
+            clipboard.dispose();
+
+            // Show a message in the status line if possible
+            IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();
+            if (part != null) {
+                IStatusLineManager status = WorkbenchUtils.getStatusLine(part);
+                if (status != null) {
+                    status.setErrorMessage(null);
+                    status.setMessage("Copied '" + text + "' to clipboard");
+                }
+            }
+        }
+    }
+
+    private static class UnmarkMilestone extends TagAction {
+        private final Resource eventLog;
+        private final List<Resource> events;
+        public UnmarkMilestone(String virtualGraphId, Resource eventLog, List<Resource> events) {
+            super("Unmark Milestone", Activator.UNMARK_MILESTONE_ICON, virtualGraphId, EventResource.URIs.Milestone, false, events);
+            this.eventLog = eventLog;
+            this.events = events;
+        }
+        @Override
+        public void postTagWrite(WriteGraph graph) throws DatabaseException {
+            EventResource EVENT = EventResource.getInstance(graph);
+            Resource baselineEvent = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent);
+            if (baselineEvent != null) {
+                if (events.contains(baselineEvent)) {
+                    graph.deny(eventLog, EVENT.EventLog_HasBaselineEvent);
+                }
+            }
+        }
+    }
+
+    private static class SetBaseline extends ClaimAction {
+        public SetBaseline(String virtualGraphId, Resource subject, Resource object) {
+            super("Set Baseline", Activator.SET_BASELINE_ICON, virtualGraphId, subject, EventResource.URIs.EventLog_HasBaselineEvent, object);
+        }
+        @Override
+        public void claim(WriteGraph graph) throws DatabaseException {
+            super.claim(graph);
+            EventResource EVENT = EventResource.getInstance(graph);
+            if (!graph.hasStatement(object, EVENT.Milestone)) {
+                graph.claim(object, EVENT.Milestone, object);
+                CorrectMilestoneLabelsAction.correctMilestoneLabels(graph, graph.getProvider(), Collections.singleton(object));
+            }
+        }
+    }
 }
\ No newline at end of file