KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > LaunchShortcutsAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.ui.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.expressions.EvaluationContext;
21 import org.eclipse.core.expressions.Expression;
22 import org.eclipse.core.expressions.IEvaluationContext;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationType;
27 import org.eclipse.debug.core.ILaunchMode;
28 import org.eclipse.debug.internal.ui.DebugUIPlugin;
29 import org.eclipse.debug.internal.ui.actions.ActionMessages;
30 import org.eclipse.debug.internal.ui.actions.LaunchConfigurationAction;
31 import org.eclipse.debug.internal.ui.actions.LaunchShortcutAction;
32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
33 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
34 import org.eclipse.debug.ui.DebugUITools;
35 import org.eclipse.debug.ui.ILaunchGroup;
36 import org.eclipse.jface.action.Action;
37 import org.eclipse.jface.action.ActionContributionItem;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.action.IMenuCreator;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.ISelectionProvider;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.MenuAdapter;
45 import org.eclipse.swt.events.MenuEvent;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Menu;
48 import org.eclipse.swt.widgets.MenuItem;
49 import org.eclipse.ui.IEditorPart;
50 import org.eclipse.ui.IWorkbenchPage;
51 import org.eclipse.ui.IWorkbenchPart;
52 import org.eclipse.ui.IWorkbenchPartSite;
53 import org.eclipse.ui.IWorkbenchWindow;
54 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
55 import org.eclipse.ui.PlatformUI;
56 import org.eclipse.ui.activities.WorkbenchActivityHelper;
57
58 /**
59  * A cascading sub-menu that shows all launch shortcuts pertinent to a
60  * selection. This action is similar to <code>ContextualLaunchAction</code>
61  * except this action is an <code>IAction</code> rather than an action
62  * delegate.
63  * <p>
64  * This action appears in the main Run menu
65  * </p>
66  * <p>
67  * Clients may subclass this class.
68  * </p>
69  * @since 3.1
70  */

71 public class LaunchShortcutsAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 {
72     
73     /**
74      * Cascading menu
75      */

76     private Menu fCreatedMenu;
77     
78     /**
79      * Launch group
80      */

81     private ILaunchGroup fGroup;
82     
83     /**
84      * Whether this actions enablement has been initialized
85      */

86     private boolean fInitialized = false;
87         
88     /**
89      * Creates a cascading menu action to populate with shortcuts in the given
90      * launch group.
91      *
92      * @param launchGroupIdentifier launch group identifier
93      */

94     public LaunchShortcutsAction(String JavaDoc launchGroupIdentifier) {
95         super();
96         fGroup = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchGroup(launchGroupIdentifier);
97         ILaunchMode mode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(fGroup.getMode());
98         setText(mode.getLaunchAsLabel());
99         setMenuCreator(this);
100         setEnabled(existsConfigTypesForMode());
101     }
102
103     /**
104      * @see IAction#run()
105      */

106     public void run() {
107         //do nothing, this action just creates a cascading menu.
108
}
109     
110     /**
111      * @see IMenuCreator#dispose()
112      */

113     public void dispose() {
114         if (fCreatedMenu != null) {
115             fCreatedMenu.dispose();
116         }
117     }
118     
119     /**
120      * @see IMenuCreator#getMenu(Control)
121      */

122     public Menu getMenu(Control parent) {
123         return null;
124     }
125     
126     /**
127      * @see IMenuCreator#getMenu(Menu)
128      */

129     public Menu getMenu(Menu parent) {
130         if (fCreatedMenu != null) {
131              fCreatedMenu.dispose();
132          }
133         fCreatedMenu = new Menu(parent);
134         initMenu();
135         return fCreatedMenu;
136     }
137     
138     /**
139      * @return an Evaluation context with default variable = selection
140      */

141     private IEvaluationContext createContext() {
142         List JavaDoc list = null;
143         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
144         if (window != null) {
145             IWorkbenchPage page = window.getActivePage();
146             if (page != null) {
147                 IWorkbenchPart activePart = page.getActivePart();
148                 if (activePart instanceof IEditorPart) {
149                     list = new ArrayList JavaDoc();
150                     list.add(((IEditorPart)activePart).getEditorInput());
151                 } else if (activePart != null) {
152                     IWorkbenchPartSite site = activePart.getSite();
153                     if (site != null) {
154                         ISelectionProvider selectionProvider = site.getSelectionProvider();
155                         if (selectionProvider != null) {
156                             ISelection selection = selectionProvider.getSelection();
157                             if (selection instanceof IStructuredSelection) {
158                                 list = ((IStructuredSelection)selection).toList();
159                             }
160                         }
161                     }
162                 }
163             }
164         }
165         // create a default evaluation context with default variable
166
// of the user selection or editor input
167
if (list == null) {
168             list = Collections.EMPTY_LIST;
169         }
170         IEvaluationContext context = new EvaluationContext(null, list);
171         context.setAllowPluginActivation(true);
172         context.addVariable("selection", list); //$NON-NLS-1$
173
return context;
174     }
175     
176     /**
177      * Fills the flyout menu
178      */

179     private void fillMenu() {
180         IEvaluationContext context = createContext();
181         int accelerator = 1;
182         List JavaDoc allShortCuts = getLaunchConfigurationManager().getLaunchShortcuts(fGroup.getCategory());
183         Iterator JavaDoc iter = allShortCuts.iterator();
184         List JavaDoc filteredShortCuts = new ArrayList JavaDoc(10);
185         while (iter.hasNext()) {
186             LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next();
187             try {
188                 if (!WorkbenchActivityHelper.filterItem(ext) && isApplicable(ext, context)) {
189                     filteredShortCuts.add(ext);
190                 }
191             } catch (CoreException e) {/*not supported*/}
192         }
193         //first add the launch config if it is one
194
try {
195             ILaunchConfiguration config = getLaunchConfigurationManager().isSharedConfig(getSelection(context));
196             if(config != null && config.exists() && config.supportsMode(getMode())) {
197                 IAction action = new LaunchConfigurationAction(config, getMode(), config.getName(), DebugUITools.getDefaultImageDescriptor(config), accelerator++);
198                 ActionContributionItem item = new ActionContributionItem(action);
199                 item.fill(fCreatedMenu, -1);
200                 if(!filteredShortCuts.isEmpty()) {
201                     new MenuItem(fCreatedMenu, SWT.SEPARATOR);
202                 }
203             }
204         }
205         catch(CoreException ce) {DebugUIPlugin.log(ce);}
206         //second add the launch shortcuts if any
207
iter = filteredShortCuts.iterator();
208         while (iter.hasNext()) {
209             LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next();
210             Set JavaDoc modes = ext.getModes(); // supported launch modes
211
Iterator JavaDoc modeIter = modes.iterator();
212             while (modeIter.hasNext()) {
213                 String JavaDoc mode = (String JavaDoc) modeIter.next();
214                 if (mode.equals(getMode())) {
215                     populateMenuItem(mode, ext, fCreatedMenu, accelerator++);
216                 }
217             }
218         }
219         if (accelerator == 1) {
220             // No shortcuts added. Add "none available" action.
221
IAction action= new Action(ActionMessages.LaunchShortcutsAction_1) {};
222             action.setEnabled(false);
223             ActionContributionItem item= new ActionContributionItem(action);
224             item.fill(fCreatedMenu, -1);
225         }
226     }
227     
228     /**
229      * Returns the first element of the current selection
230      * @param context the current evaluation context
231      * @return the first item in the selection, or <code>null</code> if none
232      * @since 3.3
233      */

234     private Object JavaDoc getSelection(IEvaluationContext context) {
235         List JavaDoc list = (List JavaDoc) context.getVariable("selection"); //$NON-NLS-1$
236
return (list.isEmpty() ? null : list.get(0));
237     }
238     
239     /**
240      * Add the shortcut to the context menu's launch sub-menu.
241      */

242     private void populateMenuItem(String JavaDoc mode, LaunchShortcutExtension ext, Menu menu, int accelerator) {
243         LaunchShortcutAction action = new LaunchShortcutAction(mode, ext);
244         action.setActionDefinitionId(ext.getId() + "." + mode); //$NON-NLS-1$
245
String JavaDoc helpContextId = ext.getHelpContextId();
246         if (helpContextId != null) {
247             PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId);
248         }
249         StringBuffer JavaDoc label= new StringBuffer JavaDoc();
250         if (accelerator >= 0 && accelerator < 10) {
251             //add the numerical accelerator
252
label.append('&');
253             label.append(accelerator);
254             label.append(' ');
255         }
256         String JavaDoc contextLabel= ext.getContextLabel(mode);
257         // replace default action label with context label if specified.
258
label.append((contextLabel != null) ? contextLabel : action.getText());
259         action.setText(label.toString());
260         ActionContributionItem item= new ActionContributionItem(action);
261         item.fill(menu, -1);
262     }
263     
264     /**
265      * Evaluate the enablement logic in the contextualLaunch
266      * element description. A true result means that we should
267      * include this shortcut in the context menu.
268      * @return true iff shortcut should appear in context menu
269      */

270     private boolean isApplicable(LaunchShortcutExtension ext, IEvaluationContext context) throws CoreException {
271         Expression expr = ext.getContextualLaunchEnablementExpression();
272         return ext.evalEnablementExpression(context, expr);
273     }
274     
275     /**
276      * Creates the menu for the action
277      */

278     private void initMenu() {
279         // Add listener to re-populate the menu each time
280
// it is shown to reflect changes in selection or active perspective
281
fCreatedMenu.addMenuListener(new MenuAdapter() {
282             public void menuShown(MenuEvent e) {
283                 Menu m = (Menu)e.widget;
284                 MenuItem[] items = m.getItems();
285                 for (int i=0; i < items.length; i++) {
286                     items[i].dispose();
287                 }
288                 fillMenu();
289             }
290         });
291     }
292         
293     /**
294      * Returns the mode of this action - run or debug
295      *
296      * @return the mode of this action - run or debug
297      */

298     protected String JavaDoc getMode() {
299         return fGroup.getMode();
300     }
301     
302     /**
303      * Returns the launch configuration manager.
304      *
305      * @return launch configuration manager
306      */

307     private LaunchConfigurationManager getLaunchConfigurationManager() {
308         return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
309     }
310     
311     /**
312      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
313      */

314     public void init(IWorkbenchWindow window) {}
315
316     /**
317      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
318      */

319     public void run(IAction action) {
320         // do nothing - this is just a menu
321
}
322
323     /**
324      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
325      */

326     public void selectionChanged(IAction action, ISelection selection) {
327         if (!fInitialized) {
328             action.setEnabled(existsConfigTypesForMode());
329             fInitialized = true;
330         }
331     }
332
333     /**
334      * Return whether there are any registered launch configuration types for
335      * the mode of this action.
336      *
337      * @return whether there are any registered launch configuration types for
338      * the mode of this action
339      */

340     private boolean existsConfigTypesForMode() {
341         ILaunchConfigurationType[] configTypes = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes();
342         for (int i = 0; i < configTypes.length; i++) {
343             ILaunchConfigurationType configType = configTypes[i];
344             if (configType.supportsMode(getMode())) {
345                 return true;
346             }
347         }
348         return false;
349     }
350 }
351
352
Popular Tags