]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/action/ChooseActionRequest.java
DefaultMouseListener was added twice in GraphExplorerComposite
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / action / ChooseActionRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench.action;
13
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class ChooseActionRequest extends ReadRequest {
33
34     protected Shell             parent;
35
36     protected Object            widget;
37
38     protected Object            input;
39
40     protected String            perspectiveId;
41
42     protected IPriorityAction[] actions;
43
44     protected String            resourceName;
45
46     protected boolean           rememberAction;
47
48     protected boolean           alwaysAsk;
49
50     /**
51      * Set to true to never prompt for an action if the selection is ambiguous.
52      */
53     protected boolean           neverPromptForAction;
54
55     /**
56      * @param parent
57      * @param input
58      * @param forPerspectiveId
59      */
60     public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId) {
61         this(parent, null, input, forPerspectiveId, true, false);
62     }
63
64     /**
65      * @param parent
66      * @param input
67      * @param forPerspectiveId
68      */
69     public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId) {
70         this(parent, widget, input, forPerspectiveId, true, false);
71     }
72
73     /**
74      * @param parent
75      * @param input
76      * @param forPerspectiveId
77      * @param rememberAction
78      * @param alwaysAsk
79      */
80     public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
81         this(parent, null, input, forPerspectiveId, true, false);
82     }
83
84     /**
85      * @param parent
86      * @param input
87      * @param forPerspectiveId
88      * @param rememberAction
89      * @param alwaysAsk
90      */
91     public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {
92         this(parent, widget, input, forPerspectiveId, rememberAction, alwaysAsk, false);
93     }
94
95     /**
96      * @param parent
97      * @param input
98      * @param forPerspectiveId
99      * @param rememberAction
100      * @param alwaysAsk
101      * @param neverPromptForAction true to never prompt/run an action if the
102      *        action selection is ambiguous
103      */
104     public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk, boolean neverPromptForAction) {
105         if (input == null)
106             throw new NullPointerException("null input");
107
108         this.parent = parent;
109         this.widget = widget;
110         this.input = input;
111         this.perspectiveId = forPerspectiveId;
112         this.rememberAction = rememberAction;
113         this.alwaysAsk = alwaysAsk;
114         this.neverPromptForAction = neverPromptForAction;
115     }
116
117     private String getResourceName(ReadGraph graph, Object resource) throws DatabaseException {
118         Resource r = ResourceAdaptionUtils.toSingleResource(resource);
119         if (r == null)
120             return resource.toString();
121         return NameUtils.getSafeName(graph, r);
122     }
123
124     @Override
125     public void run(ReadGraph graph) throws DatabaseException {
126         resourceName = getResourceName(graph, input);
127         actions = findActions(graph, widget, input, perspectiveId, rememberAction, alwaysAsk);
128         if (actions != null)
129             scheduleChooseAction(actions);
130     }
131
132 //    @Override
133 //    public void handleException(Throwable e) {
134 //        ExceptionUtils.logAndShowError(e);
135 //    }
136
137     public void scheduleChooseAction(final IPriorityAction[] actions) {
138         if(parent == null) {
139             if (actions.length > 0) {
140                 actions[0].run();
141                 return;
142             }
143         } else {
144             parent.getDisplay().asyncExec(() -> {
145                 if (parent.isDisposed())
146                     return;
147 //                System.out.println("ACTIONS: " + Arrays.toString(actions));
148                 IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);
149                 if (action != null) {
150                     action.run();
151                     return;
152                 }
153             });
154         }
155     }
156
157     public static IPriorityAction[] findActions(ReadGraph g, Resource r, String forPerspectiveId) throws DatabaseException {
158         return findActions(g, null, r, forPerspectiveId, true, false);
159     }
160
161     public static IPriorityAction[] findActions(ReadGraph g, Object widget, Resource r, String forPerspectiveId) throws DatabaseException {
162         return findActions(g, widget, r, forPerspectiveId, true, false);
163     }
164
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);
167     }
168
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);
174         if (widget != null)
175             dbe.getHintContext().setHint(IWorkbenchActionHints.KEY_WIDGET, widget);
176
177         for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {
178             ext.getAction().doubleClickEvent(dbe);
179             // Found handler?
180             if (dbe.isConsumed())
181                 break;
182         }
183         return dbe.getOrderedActions();
184     }
185
186     public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {
187         return chooseAction(parentShell, actions, resourceName, false);
188     }
189
190     public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {
191         int len = actions.length;
192         if (len == 1) {
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;
200             }
201
202             if (neverPromptForAction)
203                 return null;
204
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)
211                 return null;
212
213             return dlg.getChosenAction();
214         }
215         return null;
216     }
217
218 }