--- /dev/null
+/*******************************************************************************\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