]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/action/ChooseActionRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / action / ChooseActionRequest.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench.action;\r
13 \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
28 \r
29 /**\r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public class ChooseActionRequest extends ReadRequest {\r
33 \r
34     protected Shell             parent;\r
35 \r
36     protected Object            widget;\r
37 \r
38     protected Object            input;\r
39 \r
40     protected String            perspectiveId;\r
41 \r
42     protected IPriorityAction[] actions;\r
43 \r
44     protected String            resourceName;\r
45 \r
46     protected boolean           rememberAction;\r
47 \r
48     protected boolean           alwaysAsk;\r
49 \r
50     /**\r
51      * Set to true to never prompt for an action if the selection is ambiguous.\r
52      */\r
53     protected boolean           neverPromptForAction;\r
54 \r
55     /**\r
56      * @param parent\r
57      * @param input\r
58      * @param forPerspectiveId\r
59      */\r
60     public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId) {\r
61         this(parent, null, input, forPerspectiveId, true, false);\r
62     }\r
63 \r
64     /**\r
65      * @param parent\r
66      * @param input\r
67      * @param forPerspectiveId\r
68      */\r
69     public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId) {\r
70         this(parent, widget, input, forPerspectiveId, true, false);\r
71     }\r
72 \r
73     /**\r
74      * @param parent\r
75      * @param input\r
76      * @param forPerspectiveId\r
77      * @param rememberAction\r
78      * @param alwaysAsk\r
79      */\r
80     public ChooseActionRequest(Shell parent, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk) {\r
81         this(parent, null, input, forPerspectiveId, true, false);\r
82     }\r
83 \r
84     /**\r
85      * @param parent\r
86      * @param input\r
87      * @param forPerspectiveId\r
88      * @param rememberAction\r
89      * @param alwaysAsk\r
90      */\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
93     }\r
94 \r
95     /**\r
96      * @param parent\r
97      * @param input\r
98      * @param forPerspectiveId\r
99      * @param rememberAction\r
100      * @param alwaysAsk\r
101      * @param neverPromptForAction true to never prompt/run an action if the\r
102      *        action selection is ambiguous\r
103      */\r
104     public ChooseActionRequest(Shell parent, Object widget, Object input, String forPerspectiveId, boolean rememberAction, boolean alwaysAsk, boolean neverPromptForAction) {\r
105         if (input == null)\r
106             throw new NullPointerException("null input");\r
107 \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
115     }\r
116 \r
117     private String getResourceName(ReadGraph graph, Object resource) throws DatabaseException {\r
118         Resource r = ResourceAdaptionUtils.toSingleResource(resource);\r
119         if (r == null)\r
120             return resource.toString();\r
121         return NameUtils.getSafeName(graph, r);\r
122     }\r
123 \r
124     @Override\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
130     }\r
131 \r
132 //    @Override\r
133 //    public void handleException(Throwable e) {\r
134 //        ExceptionUtils.logAndShowError(e);\r
135 //    }\r
136 \r
137     public void scheduleChooseAction(final IPriorityAction[] actions) {\r
138         if(parent == null) {\r
139             if (actions.length > 0) {\r
140                 actions[0].run();\r
141                 return;\r
142             }\r
143         } else {\r
144             parent.getDisplay().asyncExec(new Runnable() {\r
145                 @Override\r
146                 public void run() {\r
147                     if (parent.isDisposed())\r
148                         return;\r
149 \r
150 //                    System.out.println("ACTIONS: " + Arrays.toString(actions));\r
151                     IAction action = chooseAction(parent, actions, resourceName, neverPromptForAction);\r
152                     if (action != null) {\r
153                         action.run();\r
154                         return;\r
155                     }\r
156 \r
157 // // 2. No actions ran, thus just open/close the tree\r
158 // // node.\r
159 // if (viewer.getExpandedState(singleSelection)) {\r
160 // viewer.collapseToLevel(singleSelection, 1);\r
161 // } else {\r
162 // viewer.expandToLevel(singleSelection, 1);\r
163 // }\r
164 \r
165                 }\r
166             });\r
167         }\r
168     }\r
169 \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
172     }\r
173 \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
176     }\r
177 \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
180     }\r
181 \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
189 \r
190         for (IDoubleClickExtension ext : DoubleClickExtensionManager.getInstance().getExtensions()) {\r
191             ext.getAction().doubleClickEvent(dbe);\r
192             // Found handler?\r
193             if (dbe.isConsumed())\r
194                 break;\r
195         }\r
196         return dbe.getOrderedActions();\r
197     }\r
198 \r
199     public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName) {\r
200         return chooseAction(parentShell, actions, resourceName, false);\r
201     }\r
202 \r
203     public static IAction chooseAction(Shell parentShell, IPriorityAction[] actions, String resourceName, boolean neverPromptForAction) {\r
204         int len = actions.length;\r
205         if (len == 1) {\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
213             }\r
214 \r
215             if (neverPromptForAction)\r
216                 return null;\r
217 \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
224                 return null;\r
225 \r
226             return dlg.getChosenAction();\r
227         }\r
228         return null;\r
229     }\r
230 \r
231 }\r