1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench.action;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.window.Window;
16 import org.eclipse.swt.widgets.Shell;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.ui.DoubleClickEvent;
23 import org.simantics.ui.DoubleClickExtensionManager;
24 import org.simantics.ui.IDoubleClickExtension;
25 import org.simantics.ui.dialogs.ActionChooserDialog;
26 import org.simantics.ui.utils.ResourceAdaptionUtils;
27 import org.simantics.utils.ui.action.IPriorityAction;
30 * @author Tuukka Lehtonen
32 public class ChooseActionRequest extends ReadRequest {
34 protected Shell parent;
36 protected Object widget;
38 protected Object input;
40 protected String perspectiveId;
42 protected IPriorityAction[] actions;
44 protected String resourceName;
46 protected boolean rememberAction;
48 protected boolean alwaysAsk;
51 * Set to true to never prompt for an action if the selection is ambiguous.
53 protected boolean neverPromptForAction;
58 * @param forPerspectiveId
60 public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId) {
61 this(parent, null, input, forPerspectiveId, true, false);
67 * @param forPerspectiveId
69 public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId) {
70 this(parent, widget, input, forPerspectiveId, true, false);
76 * @param forPerspectiveId
77 * @param rememberAction
80 public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
81 this(parent, null, input, forPerspectiveId, true, false);
87 * @param forPerspectiveId
88 * @param rememberAction
91 public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
92 this(parent, widget, input, forPerspectiveId, rememberAction, alwaysAsk, false);
98 * @param forPerspectiveId
99 * @param rememberAction
101 * @param neverPromptForAction true to never prompt/run an action if the
102 * action selection is ambiguous
104 public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk, boolean neverPromptForAction) {
106 throw new NullPointerException("null input");
108 this.parent = parent;
109 this.widget = widget;
111 this.perspectiveId = forPerspectiveId;
112 this.rememberAction = rememberAction;
113 this.alwaysAsk = alwaysAsk;
114 this.neverPromptForAction = neverPromptForAction;
117 private String getResourceName(ReadGraph graph, Object resource) throws DatabaseException {
118 Resource r = ResourceAdaptionUtils.toSingleResource(resource);
120 return resource.toString();
121 return NameUtils.getSafeName(graph, r);
125 public void run(ReadGraph graph) throws DatabaseException {
126 resourceName = getResourceName(graph, input);
127 actions = findActions(graph, widget, input, perspectiveId, rememberAction, alwaysAsk);
129 scheduleChooseAction(actions);
133 // public void handleException(Throwable e) {
134 // ExceptionUtils.logAndShowError(e);
137 public void scheduleChooseAction(final IPriorityAction[] actions) {
139 if (actions.length > 0) {
144 parent.getDisplay().asyncExec(new Runnable() {
147 if (parent.isDisposed())
150 // System.out.println("ACTIONS: " + Arrays.toString(actions));
151 IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);
152 if (action != null) {
157 // // 2. No actions ran, thus just open/close the tree
159 // if (viewer.getExpandedState(singleSelection)) {
160 // viewer.collapseToLevel(singleSelection, 1);
162 // viewer.expandToLevel(singleSelection, 1);
170 public static IPriorityAction[] findActions(ReadGraph g, Resource r, String forPerspectiveId) throws DatabaseException {
171 return findActions(g, null, r, forPerspectiveId, true, false);
174 public static IPriorityAction[] findActions(ReadGraph g, Object widget, Resource r, String forPerspectiveId) throws DatabaseException {
175 return findActions(g, widget, r, forPerspectiveId, true, false);
178 public static IPriorityAction[] findActions(ReadGraph g, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
179 return findActions(g, null, r, forPerspectiveId, true, false);
182 public static IPriorityAction[] findActions(ReadGraph g, Object widget, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
183 DoubleClickEvent dbe = new DoubleClickEvent(ChooseActionRequest.class, g, r);
184 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_CURRENTPERSPECTIVE, forPerspectiveId);
185 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_REMEMBER, rememberAction);
186 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_ALWAYS_ASK, alwaysAsk);
188 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_WIDGET, widget);
190 for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {
191 ext.getAction().doubleClickEvent(dbe);
193 if (dbe.isConsumed())
196 return dbe.getOrderedActions();
199 public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {
200 return chooseAction(parentShell, actions, resourceName, false);
203 public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {
204 int len = actions.length;
206 // Just use the only action if its priority is >= 0.
207 return actions[0].getPriority() >= 0 ? actions[0] : null;
208 } else if (len > 1) {
209 // If there is a single highest priority action, use
210 // it if its priority is >= 0.
211 if (actions[0].getPriority() > actions[1].getPriority()) {
212 return actions[0].getPriority() >= 0 ? actions[0] : null;
215 if (neverPromptForAction)
218 // Otherwise give the user a chance to choose from
219 // the available actions.
220 ActionChooserDialog dlg = new ActionChooserDialog(parentShell, actions, "Choose Action",
221 "Choose action for '" + resourceName + "'");
222 int ret = dlg.open();
223 if (ret != Window.OK)
226 return dlg.getChosenAction();