1 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 50 public abstract class RelaunchLastAction implements IWorkbenchWindowActionDelegate, IPropertyChangeListener { 51 52 private IWorkbenchWindow fWorkbenchWindow; 53 54 private IAction fAction; 55 56 59 public void dispose(){ 60 DebugUIPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(this); 61 } 62 63 66 public void init(IWorkbenchWindow window){ 67 fWorkbenchWindow = window; 68 DebugUIPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this); 69 } 70 71 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 configName = configuration.getName(); 87 String title = ActionMessages.RelaunchLastAction_Cannot_relaunch_1; 88 String message = MessageFormat.format(ActionMessages.RelaunchLastAction_Cannot_relaunch___0___because_it_does_not_support__2__mode_2, new String [] {configName, getMode()}); 89 MessageDialog.openError(getShell(), title, message); 90 } 91 } else { 92 openLaunchConfigurationDialog(); 94 } 95 } catch (CoreException ce) { 96 DebugUIPlugin.errorDialog(getShell(), ActionMessages.RelaunchLastAction_Error_relaunching_3, ActionMessages.RelaunchLastAction_Error_encountered_attempting_to_relaunch_4, ce); } 98 } 99 100 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 116 public void selectionChanged(IAction action, ISelection selection){ 117 if (fAction == null) { 118 initialize(action); 119 } 120 } 121 122 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 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")); } 141 } 142 } 143 144 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 165 protected ILaunchConfiguration getLastLaunch() { 166 return DebugUIPlugin.getDefault().getLaunchConfigurationManager().getFilteredLastLaunch(getLaunchGroupId()); 167 } 168 169 173 protected Shell getShell() { 174 return fWorkbenchWindow.getShell(); 175 } 176 177 180 public void propertyChange(PropertyChangeEvent event) { 181 if(event.getProperty().equals(IInternalDebugUIConstants.PREF_USE_CONTEXTUAL_LAUNCH)) { 182 initialize(fAction); 183 } 184 } 185 186 189 public abstract String getMode(); 190 191 194 public abstract String getLaunchGroupId(); 195 196 202 protected abstract String getText(); 203 204 210 protected abstract String getTooltipText(); 211 212 218 protected abstract String getCommandId(); 219 220 226 protected abstract String getDescription(); 227 } 228 229 | Popular Tags |