KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > RelaunchLastAction


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.internal.ui.actions;
12
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
17 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationType;
21 import org.eclipse.debug.internal.ui.DebugUIPlugin;
22 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
23 import org.eclipse.debug.internal.ui.contextlaunching.ContextRunner;
24 import org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager;
25 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsDialog;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.ILaunchGroup;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.commands.ICommandService;
36
37 import com.ibm.icu.text.MessageFormat;
38
39 /**
40  * Relaunches the last launch.
41  *
42  * @see ContextRunner
43  * @see org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager
44  * @see ILaunchConfiguration
45  * @see RunLastAction
46  * @see DebugLastAction
47  * @see ProfileLastAction
48  *
49  */

50 public abstract class RelaunchLastAction implements IWorkbenchWindowActionDelegate, IPropertyChangeListener {
51
52     private IWorkbenchWindow fWorkbenchWindow;
53     
54     private IAction fAction;
55     
56     /**
57      * @see IWorkbenchWindowActionDelegate
58      */

59     public void dispose(){
60         DebugUIPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(this);
61     }
62
63     /**
64      * @see IWorkbenchWindowActionDelegate
65      */

66     public void init(IWorkbenchWindow window){
67         fWorkbenchWindow = window;
68         DebugUIPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this);
69     }
70
71     /**
72      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
73      */

74     public void run(IAction action){
75         if(LaunchingResourceManager.isContextLaunchEnabled()) {
76             ILaunchGroup group = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchGroup(getLaunchGroupId());
77             ContextRunner.getDefault().launch(group);
78             return;
79         }
80         try {
81             final ILaunchConfiguration configuration = getLastLaunch();
82             if (configuration != null) {
83                 if (configuration.supportsMode(getMode())) {
84                     DebugUITools.launch(configuration, getMode());
85                 } else {
86                     String JavaDoc configName = configuration.getName();
87                     String JavaDoc title = ActionMessages.RelaunchLastAction_Cannot_relaunch_1;
88                     String JavaDoc message = MessageFormat.format(ActionMessages.RelaunchLastAction_Cannot_relaunch___0___because_it_does_not_support__2__mode_2, new String JavaDoc[] {configName, getMode()});
89                     MessageDialog.openError(getShell(), title, message);
90                 }
91             } else {
92                 // If the history is empty, just open the launch config dialog
93
openLaunchConfigurationDialog();
94             }
95         } catch (CoreException ce) {
96             DebugUIPlugin.errorDialog(getShell(), ActionMessages.RelaunchLastAction_Error_relaunching_3, ActionMessages.RelaunchLastAction_Error_encountered_attempting_to_relaunch_4, ce); //
97
}
98     }
99     
100     /**
101      * Open the launch configuration dialog, passing in the current workbench selection.
102      */

103     private void openLaunchConfigurationDialog() {
104         IWorkbenchWindow dwindow= DebugUIPlugin.getActiveWorkbenchWindow();
105         if (dwindow == null) {
106             return;
107         }
108         LaunchConfigurationsDialog dialog = new LaunchConfigurationsDialog(DebugUIPlugin.getShell(), DebugUIPlugin.getDefault().getLaunchConfigurationManager().getDefaultLaunchGroup(getMode()));
109         dialog.setOpenMode(LaunchConfigurationsDialog.LAUNCH_CONFIGURATION_DIALOG_OPEN_ON_LAST_LAUNCHED);
110         dialog.open();
111     }
112     
113     /**
114      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
115      */

116     public void selectionChanged(IAction action, ISelection selection){
117         if (fAction == null) {
118             initialize(action);
119         }
120     }
121     
122     /**
123      * Set the enabled state of the underlying action based on whether there are any
124      * registered launch configuration types that understand how to launch in the
125      * mode of this action.
126      */

127     private void initialize(IAction action) {
128         fAction = action;
129         if(fAction != null) {
130             fAction.setEnabled(existsConfigTypesForMode());
131             fAction.setText(getText());
132             fAction.setToolTipText(getTooltipText());
133             String JavaDoc commandId = getCommandId();
134             ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
135             if (service != null) {
136                 Command command = service.getCommand(commandId);
137                 command.undefine();
138                 command = service.getCommand(commandId);
139                 command.define(DebugUIPlugin.removeAccelerators(getText()), getDescription(), service.getCategory("org.eclipse.debug.ui.category.run")); //$NON-NLS-1$
140
}
141         }
142     }
143     
144     /**
145      * Return whether there are any registered launch configuration types for
146      * the mode of this action.
147      *
148      * @return whether there are any registered launch configuration types for
149      * the mode of this action
150      */

151     private boolean existsConfigTypesForMode() {
152         ILaunchConfigurationType[] configTypes = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes();
153         for (int i = 0; i < configTypes.length; i++) {
154             ILaunchConfigurationType configType = configTypes[i];
155             if (configType.supportsMode(getMode())) {
156                 return true;
157             }
158         }
159         return false;
160     }
161     
162     /**
163      * Return the last launch that occurred in the workspace.
164      */

165     protected ILaunchConfiguration getLastLaunch() {
166         return DebugUIPlugin.getDefault().getLaunchConfigurationManager().getFilteredLastLaunch(getLaunchGroupId());
167     }
168     
169     /**
170      * Returns the parent shell for this menu item
171      * @return the parent shell
172      */

173     protected Shell getShell() {
174         return fWorkbenchWindow.getShell();
175     }
176
177     /**
178      * @see org.eclipse.core.runtime.Preferences$IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
179      */

180     public void propertyChange(PropertyChangeEvent event) {
181         if(event.getProperty().equals(IInternalDebugUIConstants.PREF_USE_CONTEXTUAL_LAUNCH)) {
182             initialize(fAction);
183         }
184     }
185     
186     /**
187      * Returns the mode (run or debug) of this action.
188      */

189     public abstract String JavaDoc getMode();
190     
191     /**
192      * Returns the launch group id of this action.
193      */

194     public abstract String JavaDoc getLaunchGroupId();
195
196     /**
197      * Returns the text to display on the menu item.
198      * @return the text for the menu item
199      *
200      * @since 3.3
201      */

202     protected abstract String JavaDoc getText();
203     
204     /**
205      * Returns the text to display in the menu item tooltip
206      * @return the text for the tooltip
207      *
208      * @since 3.3
209      */

210     protected abstract String JavaDoc getTooltipText();
211     
212     /**
213      * Returns the command id this action is associated with.
214      *
215      * @return command id
216      * @since 3.3
217      */

218     protected abstract String JavaDoc getCommandId();
219     
220     /**
221      * Returns a description for this action (to associate with command).
222      *
223      * @return command description
224      * @since 3.3
225      */

226     protected abstract String JavaDoc getDescription();
227 }
228
229
Popular Tags