KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.ILaunchMode;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.actions.LaunchShortcutAction;
21 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
22 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension;
23 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.ActionContributionItem;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.action.IMenuCreator;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.swt.events.MenuAdapter;
30 import org.eclipse.swt.events.MenuEvent;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Menu;
33 import org.eclipse.swt.widgets.MenuItem;
34 import org.eclipse.ui.IPerspectiveDescriptor;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.activities.WorkbenchActivityHelper;
40
41 /**
42  * A cascading sub-menu that shows all launch shortcuts pertinent to a
43  * launch group.
44  * <p>
45  * Clients may instantiate this class; not intended to be subclassed.
46  * </p>
47  * @since 2.1
48  * @deprecated The use of perspective based launch shortcuts has been deprecated
49  * in the 3.1 release. Instead, selection sensitive launch is supported in the top level
50  * menus. Use <code>LaunchShorcutsAction</code> instead.
51  */

52 public class LaunchAsAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 {
53     
54     /**
55      * Cascading menu
56      */

57     private Menu fCreatedMenu;
58     
59     /**
60      * Launch group identifier
61      */

62     private String JavaDoc fLaunchGroupIdentifier;
63     
64     /**
65      * Presentation wrapper for this action
66      */

67     private IAction fAction;
68     
69     /**
70      * Creates a cascading menu action to populate with shortcuts in the given
71      * launch group.
72      *
73      * @param launchGroupIdentifier launch group identifier
74      */

75     public LaunchAsAction(String JavaDoc launchGroupIdentifier) {
76         super();
77         fLaunchGroupIdentifier = launchGroupIdentifier;
78         ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(getMode());
79         setText(launchMode.getLaunchAsLabel());
80         setMenuCreator(this);
81     }
82     
83     /**
84      * Returns the launch group associated with this action.
85      *
86      * @return the launch group associated with this action
87      */

88     private LaunchGroupExtension getLaunchGroup() {
89         return getLaunchConfigurationManager().getLaunchGroup(fLaunchGroupIdentifier);
90     }
91
92     /**
93      * @see IAction#run()
94      */

95     public void run() {
96         //do nothing, this action just creates a cascading menu.
97
}
98         
99     private void createAction(Menu parent, IAction action, int count) {
100         StringBuffer JavaDoc label= new StringBuffer JavaDoc();
101         //add the numerical accelerator
102
if (count < 10) {
103             label.append('&');
104             label.append(count);
105             label.append(' ');
106         }
107         label.append(action.getText());
108         action.setText(label.toString());
109         ActionContributionItem item= new ActionContributionItem(action);
110         item.fill(parent, -1);
111     }
112     
113     /**
114      * @see IMenuCreator#dispose()
115      */

116     public void dispose() {
117         if (getCreatedMenu() != null) {
118             getCreatedMenu().dispose();
119         }
120     }
121     
122     /**
123      * @see IMenuCreator#getMenu(Control)
124      */

125     public Menu getMenu(Control parent) {
126         return null;
127     }
128     
129     /**
130      * @see IMenuCreator#getMenu(Menu)
131      */

132     public Menu getMenu(Menu parent) {
133         if (getCreatedMenu() != null) {
134              getCreatedMenu().dispose();
135          }
136         setCreatedMenu(new Menu(parent));
137         fillMenu();
138         initMenu();
139         return getCreatedMenu();
140     }
141     
142     private void fillMenu() {
143         //Retrieve the current perspective and the registered shortcuts
144
List JavaDoc shortcuts = null;
145          String JavaDoc activePerspID = getActivePerspectiveID();
146          if (activePerspID != null) {
147              shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(activePerspID, getCategory());
148          }
149     
150          // If NO shortcuts are listed in the current perspective, add ALL shortcuts
151
// to avoid an empty cascading menu
152
if (shortcuts == null || shortcuts.isEmpty()) {
153              shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(getCategory());
154          }
155
156          int menuCount = 1;
157          
158          Iterator JavaDoc iter = shortcuts.iterator();
159          while (iter.hasNext()) {
160              LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next();
161              if (ext.getModes().contains(getMode()) && !WorkbenchActivityHelper.filterItem(ext)) {
162                 populateMenu(ext, getCreatedMenu(), menuCount);
163                 menuCount++;
164              }
165          }
166     }
167     
168     /**
169      * Creates the menu for the action
170      */

171     private void initMenu() {
172         // Add listener to repopulate the menu each time
173
// it is shown to reflect changes in selection or active perspective
174
fCreatedMenu.addMenuListener(new MenuAdapter() {
175             public void menuShown(MenuEvent e) {
176                 Menu m = (Menu)e.widget;
177                 MenuItem[] items = m.getItems();
178                 for (int i=0; i < items.length; i++) {
179                     items[i].dispose();
180                 }
181                 fillMenu();
182             }
183         });
184     }
185         
186     /**
187      * Add the shortcut to the menu.
188      */

189     private void populateMenu(LaunchShortcutExtension ext, Menu menu, int menuCount) {
190         LaunchShortcutAction action = new LaunchShortcutAction(getMode(), ext);
191         action.setActionDefinitionId(ext.getId() + "." + getMode()); //$NON-NLS-1$
192
String JavaDoc helpContextId = ext.getHelpContextId();
193         if (helpContextId != null) {
194             PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId);
195         }
196         /*if (fKeyBindingService != null) {
197             fKeyBindingService.registerGlobalAction(action);
198         }*/

199         createAction(menu, action, menuCount);
200     }
201     
202     /**
203      * Return the ID of the currently active perspective, or <code>null</code>
204      * if there is none.
205      */

206     private String JavaDoc getActivePerspectiveID() {
207         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
208         if (window != null) {
209             IWorkbenchPage page = window.getActivePage();
210             if (page != null) {
211                 IPerspectiveDescriptor persp = page.getPerspective();
212                 if (persp != null) {
213                     return persp.getId();
214                 }
215             }
216         }
217         return null;
218     }
219         
220     /**
221      * Returns the mode of this action - run or debug
222      *
223      * @return the mode of this action - run or debug
224      */

225     private String JavaDoc getMode() {
226         return getLaunchGroup().getMode();
227     }
228     
229     /**
230      * Returns the category of this action - possibly <code>null</code>
231      *
232      * @return the category of this action - possibly <code>null</code>
233      */

234     private String JavaDoc getCategory() {
235         return getLaunchGroup().getCategory();
236     }
237     
238     private Menu getCreatedMenu() {
239         return fCreatedMenu;
240     }
241     
242     private void setCreatedMenu(Menu createdMenu) {
243         fCreatedMenu = createdMenu;
244     }
245     
246     /**
247      * Returns the launch configuration manager.
248      *
249      * @return launch configuration manager
250      */

251     private LaunchConfigurationManager getLaunchConfigurationManager() {
252         return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
253     }
254     
255     /**
256      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
257      */

258     public void init(IWorkbenchWindow window) {
259 // if (window instanceof WorkbenchWindow) {
260
// fKeyBindingService= ((WorkbenchWindow)window).getKeyBindingService();
261
// }
262
}
263
264     /**
265      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
266      */

267     public void run(IAction action) {
268         // do nothing - this is just a menu
269
}
270
271     /**
272      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
273      */

274     public void selectionChanged(IAction action, ISelection selection) {
275         if (fAction == null) {
276             initialize(action);
277         }
278     }
279     
280     /**
281      * Set the enabled state of the underlying action based on whether there are any
282      * registered launch shortcuts for this launch mode.
283      */

284     private void initialize(IAction action) {
285         fAction = action;
286         action.setEnabled(existsShortcutsForMode());
287     }
288
289     /**
290      * Return whether there are any registered launch shortcuts for
291      * the mode of this action.
292      *
293      * @return whether there are any registered launch shortcuts for
294      * the mode of this action
295      */

296     private boolean existsShortcutsForMode() {
297         List JavaDoc shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(getCategory());
298         Iterator JavaDoc iter = shortcuts.iterator();
299         while (iter.hasNext()) {
300             LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next();
301             if (ext.getModes().contains(getMode())) {
302                 return true;
303             }
304         }
305         return false;
306     }
307 }
308
309
Popular Tags