KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.ILaunchConfigurationType;
17 import org.eclipse.debug.core.ILaunchManager;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.ILaunchHistoryChangedListener;
20 import org.eclipse.debug.internal.ui.ILaunchLabelChangedListener;
21 import org.eclipse.debug.internal.ui.actions.ActionMessages;
22 import org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager;
23 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
24 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
25 import org.eclipse.debug.ui.ILaunchGroup;
26 import org.eclipse.jface.action.ActionContributionItem;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.MenuAdapter;
31 import org.eclipse.swt.events.MenuEvent;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Menu;
34 import org.eclipse.swt.widgets.MenuItem;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
37
38 import com.ibm.icu.text.MessageFormat;
39
40 /**
41  * Abstract implementation of an action that displays a drop-down launch
42  * history for a specific launch group.
43  *
44  * @see LaunchingResourceManager
45  * @see ILaunchLabelChangedListener
46  *
47  * <p>
48  * Clients may subclass this class.
49  * </p>
50  * @since 2.1
51  */

52 public abstract class AbstractLaunchHistoryAction implements IWorkbenchWindowPulldownDelegate2, ILaunchHistoryChangedListener {
53
54     /**
55      * The menu created by this action
56      */

57     private Menu fMenu;
58         
59     /**
60      * The action used to render this delegate.
61      */

62     private IAction fAction;
63     
64     /**
65      * The associated <code>ILaunchGroup</code>
66      * @since 3.3
67      */

68     private ILaunchGroup fLaunchGroup = null;
69     
70     /**
71      * Indicates whether the launch history has changed and
72      * the sub menu needs to be recreated.
73      */

74     protected boolean fRecreateMenu = false;
75     
76     /**
77      * Constructs a launch history action.
78      *
79      * @param launchGroupIdentifier unique identifier of the launch group
80      * extension that this action displays a launch history for.
81      */

82     public AbstractLaunchHistoryAction(String JavaDoc launchGroupIdentifier) {
83         fLaunchGroup = getLaunchConfigurationManager().getLaunchGroup(launchGroupIdentifier);
84     }
85     
86     /**
87      * A listener to be notified of launch label updates
88      * @since 3.3
89      */

90     private ILaunchLabelChangedListener fLabelListener = new ILaunchLabelChangedListener() {
91         public ILaunchGroup getLaunchGroup() {
92             return fLaunchGroup;
93         }
94         public void labelChanged() {
95             updateTooltip();
96         }
97     };
98     
99     /**
100      * Sets the action used to render this delegate.
101      *
102      * @param action the action used to render this delegate
103      */

104     private void setAction(IAction action) {
105         fAction = action;
106     }
107
108     /**
109      * Returns the action used to render this delegate.
110      *
111      * @return the action used to render this delegate
112      */

113     protected IAction getAction() {
114         return fAction;
115     }
116     
117     /**
118      * Adds the given action to the specified menu with an accelerator specified
119      * by the given number.
120      *
121      * @param menu the menu to add the action to
122      * @param action the action to add
123      * @param accelerator the number that should appear as an accelerator
124      */

125     protected void addToMenu(Menu menu, IAction action, int accelerator) {
126         StringBuffer JavaDoc label= new StringBuffer JavaDoc();
127         if (accelerator >= 0 && accelerator < 10) {
128             //add the numerical accelerator
129
label.append('&');
130             label.append(accelerator);
131             label.append(' ');
132         }
133         label.append(action.getText());
134         action.setText(label.toString());
135         ActionContributionItem item= new ActionContributionItem(action);
136         item.fill(menu, -1);
137     }
138
139     /**
140      * Initialize this action so that it can dynamically set its tool-tip. Also set the enabled state
141      * of the underlying action based on whether there are any registered launch configuration types that
142      * understand how to launch in the mode of this action.
143      */

144     private void initialize(IAction action) {
145         getLaunchConfigurationManager().addLaunchHistoryListener(this);
146         setAction(action);
147         updateTooltip();
148         action.setEnabled(existsConfigTypesForMode());
149     }
150     
151     /**
152      * Return whether there are any registered launch configuration types for
153      * the mode of this action.
154      *
155      * @return whether there are any registered launch configuration types for
156      * the mode of this action
157      */

158     private boolean existsConfigTypesForMode() {
159         ILaunchConfigurationType[] configTypes = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes();
160         for (int i = 0; i < configTypes.length; i++) {
161             if (configTypes[i].supportsMode(getMode())) {
162                 return true;
163             }
164         }
165         return false;
166     }
167     
168     /**
169      * Updates this action's tool-tip. The tooltip is based on user preference settings
170      * for launching - either the previous launch, or based on the selection and which
171      * configuration will be launched.
172      * <p>
173      * Subclasses may override as required.
174      * </p>
175      */

176     protected void updateTooltip() {
177         getAction().setToolTipText(getToolTip());
178     }
179     
180     /**
181      * Returns the tooltip specific to a configuration.
182      *
183      * @param configuration a <code>ILauncConfiguration</code>
184      * @return the string for the tool tip
185      */

186     protected String JavaDoc getToolTip(ILaunchConfiguration configuration) {
187         String JavaDoc launchName= configuration.getName();
188         String JavaDoc mode= getMode();
189         String JavaDoc label;
190         if (mode.equals(ILaunchManager.RUN_MODE)) {
191             label= ActionMessages.AbstractLaunchHistoryAction_1;
192         } else if (mode.equals(ILaunchManager.DEBUG_MODE)){
193             label= ActionMessages.AbstractLaunchHistoryAction_2;
194         } else if (mode.equals(ILaunchManager.PROFILE_MODE)){
195             label= ActionMessages.AbstractLaunchHistoryAction_3;
196         } else {
197             label= ActionMessages.AbstractLaunchHistoryAction_4;
198         }
199         return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_0, new String JavaDoc[] {label, launchName});
200     }
201     
202     /**
203      * Returns this action's tooltip. The tooltip is retrieved from the launch resource manager
204      * which builds tool tips asynchronously for context launching support.
205      *
206      * @return the string for the tool tip
207      */

208     private String JavaDoc getToolTip() {
209         String JavaDoc launchName = getLaunchingResourceManager().getLaunchLabel(fLaunchGroup);
210         if(launchName == null) {
211             return DebugUIPlugin.removeAccelerators(internalGetHistory().getLaunchGroup().getLabel());
212         }
213         String JavaDoc label = null;
214         String JavaDoc mode = getMode();
215         if (mode.equals(ILaunchManager.RUN_MODE)) {
216             label = ActionMessages.AbstractLaunchHistoryAction_1;
217         } else if (mode.equals(ILaunchManager.DEBUG_MODE)){
218             label = ActionMessages.AbstractLaunchHistoryAction_2;
219         } else if (mode.equals(ILaunchManager.PROFILE_MODE)){
220             label = ActionMessages.AbstractLaunchHistoryAction_3;
221         } else {
222             label = ActionMessages.AbstractLaunchHistoryAction_4;
223         }
224         if("".equals(launchName)) { //$NON-NLS-1$
225
return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_5, new String JavaDoc[] {label});
226         }
227         else {
228             return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_0, new String JavaDoc[] {label, launchName});
229         }
230     }
231
232     /**
233      * @see ILaunchHistoryChangedListener#launchHistoryChanged()
234      */

235     public void launchHistoryChanged() {
236         fRecreateMenu = true;
237     }
238
239     /**
240      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
241      */

242     public void dispose() {
243         setMenu(null);
244         getLaunchConfigurationManager().removeLaunchHistoryListener(this);
245         getLaunchingResourceManager().removeLaunchLabelChangedListener(fLabelListener);
246     }
247     
248     /**
249      * Return the last launch in this action's launch history.
250      *
251      * @return the most recent configuration that was launched from this
252      * action's launch history that is not filtered from the menu
253      */

254     protected ILaunchConfiguration getLastLaunch() {
255         return getLaunchConfigurationManager().getFilteredLastLaunch(getLaunchGroupIdentifier());
256     }
257
258     /**
259      * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
260      */

261     public Menu getMenu(Control parent) {
262         setMenu(new Menu(parent));
263         fillMenu(fMenu);
264         initMenu();
265         return fMenu;
266     }
267     
268     /**
269      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
270      */

271     public Menu getMenu(Menu parent) {
272         setMenu(new Menu(parent));
273         fillMenu(fMenu);
274         initMenu();
275         return fMenu;
276     }
277     
278     /**
279      * Creates the menu for the action
280      */

281     private void initMenu() {
282         // Add listener to re-populate the menu each time
283
// it is shown because of dynamic history list
284
fMenu.addMenuListener(new MenuAdapter() {
285             public void menuShown(MenuEvent e) {
286                 if (fRecreateMenu) {
287                     Menu m = (Menu)e.widget;
288                     MenuItem[] items = m.getItems();
289                     for (int i=0; i < items.length; i++) {
290                         items[i].dispose();
291                     }
292                     fillMenu(m);
293                     fRecreateMenu= false;
294                 }
295             }
296         });
297     }
298
299     /**
300      * Sets this action's drop-down menu, disposing the previous menu.
301      *
302      * @param menu the new menu
303      */

304     private void setMenu(Menu menu) {
305         if (fMenu != null) {
306             fMenu.dispose();
307         }
308         fMenu = menu;
309     }
310
311     /**
312      * Fills the drop-down menu with favorites and launch history
313      *
314      * @param menu the menu to fill
315      */

316     protected void fillMenu(Menu menu) {
317         ILaunchConfiguration[] historyList= getHistory();
318         ILaunchConfiguration[] favoriteList = getFavorites();
319         
320         // Add favorites
321
int accelerator = 1;
322         for (int i = 0; i < favoriteList.length; i++) {
323             ILaunchConfiguration launch= favoriteList[i];
324             LaunchAction action= new LaunchAction(launch, getMode());
325             addToMenu(menu, action, accelerator);
326             accelerator++;
327         }
328         
329         // Separator between favorites and history
330
if (favoriteList.length > 0 && historyList.length > 0) {
331             addSeparator(menu);
332         }
333         
334         // Add history launches next
335
for (int i = 0; i < historyList.length; i++) {
336             ILaunchConfiguration launch= historyList[i];
337             LaunchAction action= new LaunchAction(launch, getMode());
338             addToMenu(menu, action, accelerator);
339             accelerator++;
340         }
341     }
342     
343     /**
344      * Adds a separator to the given menu
345      *
346      * @param menu
347      */

348     protected void addSeparator(Menu menu) {
349         new MenuItem(menu, SWT.SEPARATOR);
350     }
351     
352     /**
353      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
354      */

355     public void run(IAction action) {
356         // do nothing - this is just a menu
357
}
358
359     /**
360      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
361      */

362     public void selectionChanged(IAction action, ISelection selection){
363         if (fAction == null) {
364             initialize(action);
365         }
366     }
367     
368     /**
369      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
370      */

371     public void init(IWorkbenchWindow window) {
372         if (this instanceof AbstractLaunchToolbarAction) {
373             getLaunchingResourceManager().addLaunchLabelUpdateListener(fLabelListener);
374         }
375     }
376     
377     /**
378      * Returns the launch history associated with this action's launch group.
379      *
380      * @return the launch history associated with this action's launch group
381      * @deprecated this method returns a class that is not API and is not intended
382      * for clients of the debug platform. Instead, use <code>getHistory()</code>,
383      * <code>getFavorites()</code>, and <code>getLastLaunch()</code>.
384      */

385     protected LaunchHistory getLaunchHistory() {
386         return getLaunchConfigurationManager().getLaunchHistory(getLaunchGroupIdentifier());
387     }
388     
389     /**
390      * Returns the launch history associated with this action's launch group.
391      *
392      * @return the launch history associated with this action's launch group
393      * @since 3.3
394      */

395     private LaunchHistory internalGetHistory() {
396         return getLaunchConfigurationManager().getLaunchHistory(getLaunchGroupIdentifier());
397     }
398     
399     /**
400      * Returns the launch history associated with this action's launch mode and group in most
401      * recently launched order. Configurations associated with disabled activities are not included
402      * in the list. As well, configurations are filtered based on workspace preference settings
403      * to filter configurations from closed projects, deleted projects, working sets and to filter
404      * specific launch configuration types.
405      *
406      * @return launch history
407      * @since 3.3
408      */

409     protected ILaunchConfiguration[] getHistory() {
410         return LaunchConfigurationManager.filterConfigs(internalGetHistory().getHistory());
411     }
412     
413     /**
414      * Returns the launch favorites associated with this action's launch mode and group in user
415      * preference order. Configurations associated with disabled activities are not included
416      * in the list. As well, configurations are filtered based on workspace preference settings
417      * to filter configurations from closed projects, deleted projects, working sets and to filter
418      * specific launch configuration types.
419      *
420      * @return favorite launch configurations
421      * @since 3.3
422      */

423     protected ILaunchConfiguration[] getFavorites() {
424         return LaunchConfigurationManager.filterConfigs(internalGetHistory().getFavorites());
425     }
426         
427     /**
428      * Returns the mode (e.g., 'run' or 'debug') of this drop down.
429      *
430      * @return the mode of this action
431      */

432     protected String JavaDoc getMode() {
433         return internalGetHistory().getLaunchGroup().getMode();
434     }
435     
436     /**
437      * Returns the launch configuration manager.
438      *
439      * @return launch configuration manager
440      */

441     private LaunchConfigurationManager getLaunchConfigurationManager() {
442         return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
443     }
444     
445     /**
446      * Returns the <code>ContextualLaunchingResourceManager</code>
447      *
448      * @return <code>ContextualLaunchingResourceManager</code>
449      */

450     private LaunchingResourceManager getLaunchingResourceManager() {
451         return DebugUIPlugin.getDefault().getLaunchingResourceManager();
452     }
453     
454     /**
455      * Returns the identifier of the launch group this action is associated
456      * with.
457      *
458      * @return the identifier of the launch group this action is associated
459      * with
460      */

461     protected String JavaDoc getLaunchGroupIdentifier() {
462         return fLaunchGroup.getIdentifier();
463     }
464 }
465
Popular Tags