]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/action/ChooseActionRequest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / action / ChooseActionRequest.java
index 9bc1c87e1d8d947b1d349c9fb487f3d8eeb2825b..210bdf91107d1a8fc276a982a4a397148f28cf3f 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 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
- *******************************************************************************/\r
-package org.simantics.ui.workbench.action;\r
-\r
-import org.eclipse.jface.action.IAction;\r
-import org.eclipse.jface.window.Window;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.ui.DoubleClickEvent;\r
-import org.simantics.ui.DoubleClickExtensionManager;\r
-import org.simantics.ui.IDoubleClickExtension;\r
-import org.simantics.ui.dialogs.ActionChooserDialog;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.ui.action.IPriorityAction;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ChooseActionRequest extends ReadRequest {\r
-\r
-    protected Shell             parent;\r
-\r
-    protected Object            widget;\r
-\r
-    protected Object            input;\r
-\r
-    protected String            perspectiveId;\r
-\r
-    protected IPriorityAction[] actions;\r
-\r
-    protected String            resourceName;\r
-\r
-    protected boolean           rememberAction;\r
-\r
-    protected boolean           alwaysAsk;\r
-\r
-    /**\r
-     * Set to true to never prompt for an action if the selection is ambiguous.\r
-     */\r
-    protected boolean           neverPromptForAction;\r
-\r
-    /**\r
-     * @param parent\r
-     * @param input\r
-     * @param forPerspectiveId\r
-     */\r
-    public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId) {\r
-        this(parent, null, input, forPerspectiveId, true, false);\r
-    }\r
-\r
-    /**\r
-     * @param parent\r
-     * @param input\r
-     * @param forPerspectiveId\r
-     */\r
-    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId) {\r
-        this(parent, widget, input, forPerspectiveId, true, false);\r
-    }\r
-\r
-    /**\r
-     * @param parent\r
-     * @param input\r
-     * @param forPerspectiveId\r
-     * @param rememberAction\r
-     * @param alwaysAsk\r
-     */\r
-    public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {\r
-        this(parent, null, input, forPerspectiveId, true, false);\r
-    }\r
-\r
-    /**\r
-     * @param parent\r
-     * @param input\r
-     * @param forPerspectiveId\r
-     * @param rememberAction\r
-     * @param alwaysAsk\r
-     */\r
-    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {\r
-        this(parent, widget, input, forPerspectiveId, rememberAction, alwaysAsk, false);\r
-    }\r
-\r
-    /**\r
-     * @param parent\r
-     * @param input\r
-     * @param forPerspectiveId\r
-     * @param rememberAction\r
-     * @param alwaysAsk\r
-     * @param neverPromptForAction true to never prompt/run an action if the\r
-     *        action selection is ambiguous\r
-     */\r
-    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk, boolean neverPromptForAction) {\r
-        if (input == null)\r
-            throw new NullPointerException("null input");\r
-\r
-        this.parent = parent;\r
-        this.widget = widget;\r
-        this.input = input;\r
-        this.perspectiveId = forPerspectiveId;\r
-        this.rememberAction = rememberAction;\r
-        this.alwaysAsk = alwaysAsk;\r
-        this.neverPromptForAction = neverPromptForAction;\r
-    }\r
-\r
-    private String getResourceName(ReadGraph graph, Object resource) throws DatabaseException {\r
-        Resource r = ResourceAdaptionUtils.toSingleResource(resource);\r
-        if (r == null)\r
-            return resource.toString();\r
-        return NameUtils.getSafeName(graph, r);\r
-    }\r
-\r
-    @Override\r
-    public void run(ReadGraph graph) throws DatabaseException {\r
-        resourceName = getResourceName(graph, input);\r
-        actions = findActions(graph, widget, input, perspectiveId, rememberAction, alwaysAsk);\r
-        if (actions != null)\r
-            scheduleChooseAction(actions);\r
-    }\r
-\r
-//    @Override\r
-//    public void handleException(Throwable e) {\r
-//        ExceptionUtils.logAndShowError(e);\r
-//    }\r
-\r
-    public void scheduleChooseAction(final IPriorityAction[] actions) {\r
-        if(parent == null) {\r
-            if (actions.length > 0) {\r
-                actions[0].run();\r
-                return;\r
-            }\r
-        } else {\r
-            parent.getDisplay().asyncExec(new Runnable() {\r
-                @Override\r
-                public void run() {\r
-                    if (parent.isDisposed())\r
-                        return;\r
-\r
-//                    System.out.println("ACTIONS: " + Arrays.toString(actions));\r
-                    IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);\r
-                    if (action != null) {\r
-                        action.run();\r
-                        return;\r
-                    }\r
-\r
-// // 2. No actions ran, thus just open/close the tree\r
-// // node.\r
-// if (viewer.getExpandedState(singleSelection)) {\r
-// viewer.collapseToLevel(singleSelection, 1);\r
-// } else {\r
-// viewer.expandToLevel(singleSelection, 1);\r
-// }\r
-\r
-                }\r
-            });\r
-        }\r
-    }\r
-\r
-    public static IPriorityAction[] findActions(ReadGraph g, Resource r, String forPerspectiveId) throws DatabaseException {\r
-        return findActions(g, null, r, forPerspectiveId, true, false);\r
-    }\r
-\r
-    public static IPriorityAction[] findActions(ReadGraph g, Object widget, Resource r, String forPerspectiveId) throws DatabaseException {\r
-        return findActions(g, widget, r, forPerspectiveId, true, false);\r
-    }\r
-\r
-    public static IPriorityAction[] findActions(ReadGraph g, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {\r
-        return findActions(g, null, r, forPerspectiveId, true, false);\r
-    }\r
-\r
-    public static IPriorityAction[] findActions(ReadGraph g, Object widget, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {\r
-        DoubleClickEvent dbe = new DoubleClickEvent(ChooseActionRequest.class, g, r);\r
-        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_CURRENTPERSPECTIVE, forPerspectiveId);\r
-        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_REMEMBER, rememberAction);\r
-        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_ALWAYS_ASK, alwaysAsk);\r
-        if (widget != null)\r
-            dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_WIDGET, widget);\r
-\r
-        for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {\r
-            ext.getAction().doubleClickEvent(dbe);\r
-            // Found handler?\r
-            if (dbe.isConsumed())\r
-                break;\r
-        }\r
-        return dbe.getOrderedActions();\r
-    }\r
-\r
-    public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {\r
-        return chooseAction(parentShell, actions, resourceName, false);\r
-    }\r
-\r
-    public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {\r
-        int len = actions.length;\r
-        if (len == 1) {\r
-            // Just use the only action if its priority is >= 0.\r
-            return actions[0].getPriority() >= 0 ? actions[0] : null;\r
-        } else if (len > 1) {\r
-            // If there is a single highest priority action, use\r
-            // it if its priority is >= 0.\r
-            if (actions[0].getPriority() > actions[1].getPriority()) {\r
-                return actions[0].getPriority() >= 0 ? actions[0] : null;\r
-            }\r
-\r
-            if (neverPromptForAction)\r
-                return null;\r
-\r
-            // Otherwise give the user a chance to choose from\r
-            // the available actions.\r
-            ActionChooserDialog dlg = new ActionChooserDialog(parentShell, actions, "Choose Action",\r
-                    "Choose action for '" + resourceName + "'");\r
-            int ret = dlg.open();\r
-            if (ret != Window.OK)\r
-                return null;\r
-\r
-            return dlg.getChosenAction();\r
-        }\r
-        return null;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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.ui.workbench.action;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.ui.DoubleClickEvent;
+import org.simantics.ui.DoubleClickExtensionManager;
+import org.simantics.ui.IDoubleClickExtension;
+import org.simantics.ui.dialogs.ActionChooserDialog;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+import org.simantics.utils.ui.action.IPriorityAction;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ChooseActionRequest extends ReadRequest {
+
+    protected Shell             parent;
+
+    protected Object            widget;
+
+    protected Object            input;
+
+    protected String            perspectiveId;
+
+    protected IPriorityAction[] actions;
+
+    protected String            resourceName;
+
+    protected boolean           rememberAction;
+
+    protected boolean           alwaysAsk;
+
+    /**
+     * Set to true to never prompt for an action if the selection is ambiguous.
+     */
+    protected boolean           neverPromptForAction;
+
+    /**
+     * @param parent
+     * @param input
+     * @param forPerspectiveId
+     */
+    public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId) {
+        this(parent, null, input, forPerspectiveId, true, false);
+    }
+
+    /**
+     * @param parent
+     * @param input
+     * @param forPerspectiveId
+     */
+    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId) {
+        this(parent, widget, input, forPerspectiveId, true, false);
+    }
+
+    /**
+     * @param parent
+     * @param input
+     * @param forPerspectiveId
+     * @param rememberAction
+     * @param alwaysAsk
+     */
+    public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
+        this(parent, null, input, forPerspectiveId, true, false);
+    }
+
+    /**
+     * @param parent
+     * @param input
+     * @param forPerspectiveId
+     * @param rememberAction
+     * @param alwaysAsk
+     */
+    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
+        this(parent, widget, input, forPerspectiveId, rememberAction, alwaysAsk, false);
+    }
+
+    /**
+     * @param parent
+     * @param input
+     * @param forPerspectiveId
+     * @param rememberAction
+     * @param alwaysAsk
+     * @param neverPromptForAction true to never prompt/run an action if the
+     *        action selection is ambiguous
+     */
+    public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk, boolean neverPromptForAction) {
+        if (input == null)
+            throw new NullPointerException("null input");
+
+        this.parent = parent;
+        this.widget = widget;
+        this.input = input;
+        this.perspectiveId = forPerspectiveId;
+        this.rememberAction = rememberAction;
+        this.alwaysAsk = alwaysAsk;
+        this.neverPromptForAction = neverPromptForAction;
+    }
+
+    private String getResourceName(ReadGraph graph, Object resource) throws DatabaseException {
+        Resource r = ResourceAdaptionUtils.toSingleResource(resource);
+        if (r == null)
+            return resource.toString();
+        return NameUtils.getSafeName(graph, r);
+    }
+
+    @Override
+    public void run(ReadGraph graph) throws DatabaseException {
+        resourceName = getResourceName(graph, input);
+        actions = findActions(graph, widget, input, perspectiveId, rememberAction, alwaysAsk);
+        if (actions != null)
+            scheduleChooseAction(actions);
+    }
+
+//    @Override
+//    public void handleException(Throwable e) {
+//        ExceptionUtils.logAndShowError(e);
+//    }
+
+    public void scheduleChooseAction(final IPriorityAction[] actions) {
+        if(parent == null) {
+            if (actions.length > 0) {
+                actions[0].run();
+                return;
+            }
+        } else {
+            parent.getDisplay().asyncExec(new Runnable() {
+                @Override
+                public void run() {
+                    if (parent.isDisposed())
+                        return;
+
+//                    System.out.println("ACTIONS: " + Arrays.toString(actions));
+                    IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);
+                    if (action != null) {
+                        action.run();
+                        return;
+                    }
+
+// // 2. No actions ran, thus just open/close the tree
+// // node.
+// if (viewer.getExpandedState(singleSelection)) {
+// viewer.collapseToLevel(singleSelection, 1);
+// } else {
+// viewer.expandToLevel(singleSelection, 1);
+// }
+
+                }
+            });
+        }
+    }
+
+    public static IPriorityAction[] findActions(ReadGraph g, Resource r, String forPerspectiveId) throws DatabaseException {
+        return findActions(g, null, r, forPerspectiveId, true, false);
+    }
+
+    public static IPriorityAction[] findActions(ReadGraph g, Object widget, Resource r, String forPerspectiveId) throws DatabaseException {
+        return findActions(g, widget, r, forPerspectiveId, true, false);
+    }
+
+    public static IPriorityAction[] findActions(ReadGraph g, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
+        return findActions(g, null, r, forPerspectiveId, true, false);
+    }
+
+    public static IPriorityAction[] findActions(ReadGraph g, Object widget, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
+        DoubleClickEvent dbe = new DoubleClickEvent(ChooseActionRequest.class, g, r);
+        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_CURRENTPERSPECTIVE, forPerspectiveId);
+        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_REMEMBER, rememberAction);
+        dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_ALWAYS_ASK, alwaysAsk);
+        if (widget != null)
+            dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_WIDGET, widget);
+
+        for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {
+            ext.getAction().doubleClickEvent(dbe);
+            // Found handler?
+            if (dbe.isConsumed())
+                break;
+        }
+        return dbe.getOrderedActions();
+    }
+
+    public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {
+        return chooseAction(parentShell, actions, resourceName, false);
+    }
+
+    public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {
+        int len = actions.length;
+        if (len == 1) {
+            // Just use the only action if its priority is >= 0.
+            return actions[0].getPriority() >= 0 ? actions[0] : null;
+        } else if (len > 1) {
+            // If there is a single highest priority action, use
+            // it if its priority is >= 0.
+            if (actions[0].getPriority() > actions[1].getPriority()) {
+                return actions[0].getPriority() >= 0 ? actions[0] : null;
+            }
+
+            if (neverPromptForAction)
+                return null;
+
+            // Otherwise give the user a chance to choose from
+            // the available actions.
+            ActionChooserDialog dlg = new ActionChooserDialog(parentShell, actions, "Choose Action",
+                    "Choose action for '" + resourceName + "'");
+            int ret = dlg.open();
+            if (ret != Window.OK)
+                return null;
+
+            return dlg.getChosenAction();
+        }
+        return null;
+    }
+
+}