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(() -> {
145 if (parent.isDisposed())
147 // System.out.println("ACTIONS: " + Arrays.toString(actions));
148 IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);
149 if (action != null) {
157 public static IPriorityAction[] findActions(ReadGraph g, Resource r, String forPerspectiveId) throws DatabaseException {
158 return findActions(g, null, r, forPerspectiveId, true, false);
161 public static IPriorityAction[] findActions(ReadGraph g, Object widget, Resource r, String forPerspectiveId) throws DatabaseException {
162 return findActions(g, widget, r, forPerspectiveId, true, false);
165 public static IPriorityAction[] findActions(ReadGraph g, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
166 return findActions(g, null, r, forPerspectiveId, true, false);
169 public static IPriorityAction[] findActions(ReadGraph g, Object widget, Object r, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) throws DatabaseException {
170 DoubleClickEvent dbe = new DoubleClickEvent(ChooseActionRequest.class, g, r);
171 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_CURRENTPERSPECTIVE, forPerspectiveId);
172 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_REMEMBER, rememberAction);
173 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_ALWAYS_ASK, alwaysAsk);
175 dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_WIDGET, widget);
177 for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {
178 ext.getAction().doubleClickEvent(dbe);
180 if (dbe.isConsumed())
183 return dbe.getOrderedActions();
186 public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {
187 return chooseAction(parentShell, actions, resourceName, false);
190 public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {
191 int len = actions.length;
193 // Just use the only action if its priority is >= 0.
194 return actions[0].getPriority() >= 0 ? actions[0] : null;
195 } else if (len > 1) {
196 // If there is a single highest priority action, use
197 // it if its priority is >= 0.
198 if (actions[0].getPriority() > actions[1].getPriority()) {
199 return actions[0].getPriority() >= 0 ? actions[0] : null;
202 if (neverPromptForAction)
205 // Otherwise give the user a chance to choose from
206 // the available actions.
207 ActionChooserDialog dlg = new ActionChooserDialog(parentShell, actions, "Choose Action",
208 "Choose action for '" + resourceName + "'");
209 int ret = dlg.open();
210 if (ret != Window.OK)
213 return dlg.getChosenAction();