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