|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 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.jdt.internal.debug.ui.launcher; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.CoreException; |
| 19 |
import org.eclipse.debug.core.DebugPlugin; |
| 20 |
import org.eclipse.debug.core.ILaunchConfiguration; |
| 21 |
import org.eclipse.debug.core.ILaunchConfigurationType; |
| 22 |
import org.eclipse.debug.core.ILaunchManager; |
| 23 |
import org.eclipse.debug.ui.DebugUITools; |
| 24 |
import org.eclipse.debug.ui.IDebugModelPresentation; |
| 25 |
import org.eclipse.debug.ui.ILaunchShortcut; |
| 26 |
import org.eclipse.jdt.core.IJavaElement; |
| 27 |
import org.eclipse.jdt.core.IType; |
| 28 |
import org.eclipse.jdt.core.JavaModelException; |
| 29 |
import org.eclipse.jdt.core.search.SearchEngine; |
| 30 |
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 31 |
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; |
| 32 |
import org.eclipse.jdt.ui.IJavaElementSearchConstants; |
| 33 |
import org.eclipse.jdt.ui.JavaUI; |
| 34 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 35 |
import org.eclipse.jface.operation.IRunnableContext; |
| 36 |
import org.eclipse.jface.viewers.ISelection; |
| 37 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 38 |
import org.eclipse.jface.window.Window; |
| 39 |
import org.eclipse.swt.widgets.Shell; |
| 40 |
import org.eclipse.ui.IEditorInput; |
| 41 |
import org.eclipse.ui.IEditorPart; |
| 42 |
import org.eclipse.ui.PlatformUI; |
| 43 |
import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
| 44 |
import org.eclipse.ui.dialogs.SelectionDialog; |
| 45 |
|
| 46 |
/** |
| 47 |
* Common behavior for Java launch shortcuts |
| 48 |
* |
| 49 |
* @since 3.2 |
| 50 |
*/ |
| 51 |
public abstract class JavaLaunchShortcut implements ILaunchShortcut { |
| 52 |
|
| 53 |
/** |
| 54 |
* @param search the java elements to search for a main type |
| 55 |
* @param mode the mode to launch in |
| 56 |
* @param editor activated on an editor (or from a selection in a viewer) |
| 57 |
*/ |
| 58 |
public void searchAndLaunch(Object[] search, String mode, String selectMessage, String emptyMessage) { |
| 59 |
IType[] types = null; |
| 60 |
try { |
| 61 |
types = findTypes(search, PlatformUI.getWorkbench().getProgressService()); |
| 62 |
} catch (InterruptedException e) { |
| 63 |
return; |
| 64 |
} catch (CoreException e) { |
| 65 |
MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_0, e.getMessage()); |
| 66 |
return; |
| 67 |
} |
| 68 |
IType type = null; |
| 69 |
if (types.length == 0) { |
| 70 |
MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_1, emptyMessage); |
| 71 |
} else if (types.length > 1) { |
| 72 |
try { |
| 73 |
type = chooseType(types, selectMessage); |
| 74 |
} catch (JavaModelException e) { |
| 75 |
reportErorr(e); |
| 76 |
return; |
| 77 |
} |
| 78 |
} else { |
| 79 |
type = types[0]; |
| 80 |
} |
| 81 |
if (type != null) { |
| 82 |
launch(type, mode); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Finds and returns the launchable types in the given selection of elements. |
| 88 |
* |
| 89 |
* @param elements scope to search for launchable types |
| 90 |
* @param context progess reporting context |
| 91 |
* @return launchable types, possibly empty |
| 92 |
* @exception InterruptedException if the search is cancelled |
| 93 |
* @exception org.eclipse.core.runtime.CoreException if the search fails |
| 94 |
*/ |
| 95 |
protected abstract IType[] findTypes(Object[] elements, IRunnableContext context) throws InterruptedException, CoreException; |
| 96 |
|
| 97 |
/** |
| 98 |
* Prompts the user to select a type from the given types. |
| 99 |
* |
| 100 |
* @param types the types to choose from |
| 101 |
* @param title the selection dialog title |
| 102 |
* |
| 103 |
* @return the selected type or <code>null</code> if none. |
| 104 |
*/ |
| 105 |
protected IType chooseType(IType[] types, String title) throws JavaModelException { |
| 106 |
SelectionDialog dialog = JavaUI.createTypeDialog( |
| 107 |
getShell(), |
| 108 |
PlatformUI.getWorkbench().getProgressService(), |
| 109 |
SearchEngine.createJavaSearchScope(types), |
| 110 |
IJavaElementSearchConstants.CONSIDER_CLASSES, |
| 111 |
false, "**"); //$NON-NLS-1$ |
| 112 |
dialog.setMessage(LauncherMessages.JavaMainTab_Choose_a_main__type_to_launch__12); |
| 113 |
dialog.setTitle(title); |
| 114 |
if (dialog.open() == Window.OK) { |
| 115 |
return (IType)dialog.getResult()[0]; |
| 116 |
} |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Launches a configuration for the given type |
| 122 |
*/ |
| 123 |
protected void launch(IType type, String mode) { |
| 124 |
ILaunchConfiguration config = findLaunchConfiguration(type, getConfigurationType()); |
| 125 |
if (config != null) { |
| 126 |
DebugUITools.launch(config, mode); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns the type of configuration this shortcut is applicable to. |
| 132 |
* |
| 133 |
* @return the type of configuration this shortcut is applicable to |
| 134 |
*/ |
| 135 |
protected abstract ILaunchConfigurationType getConfigurationType(); |
| 136 |
|
| 137 |
/** |
| 138 |
* Locate a configuration to relaunch for the given type. If one cannot be found, create one. |
| 139 |
* |
| 140 |
* @return a re-useable config or <code>null</code> if none |
| 141 |
*/ |
| 142 |
protected ILaunchConfiguration findLaunchConfiguration(IType type, ILaunchConfigurationType configType) { |
| 143 |
List candidateConfigs = Collections.EMPTY_LIST; |
| 144 |
try { |
| 145 |
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType); |
| 146 |
candidateConfigs = new ArrayList(configs.length); |
| 147 |
for (int i = 0; i < configs.length; i++) { |
| 148 |
ILaunchConfiguration config = configs[i]; |
| 149 |
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "").equals(type.getFullyQualifiedName())) { //$NON-NLS-1$ |
| 150 |
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "").equals(type.getJavaProject().getElementName())) { //$NON-NLS-1$ |
| 151 |
candidateConfigs.add(config); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} catch (CoreException e) { |
| 156 |
JDIDebugUIPlugin.log(e); |
| 157 |
} |
| 158 |
|
| 159 |
// If there are no existing configs associated with the IType, create one. |
| 160 |
// If there is exactly one config associated with the IType, return it. |
| 161 |
// Otherwise, if there is more than one config associated with the IType, prompt the |
| 162 |
// user to choose one. |
| 163 |
int candidateCount = candidateConfigs.size(); |
| 164 |
if (candidateCount < 1) { |
| 165 |
return createConfiguration(type); |
| 166 |
} else if (candidateCount == 1) { |
| 167 |
return (ILaunchConfiguration) candidateConfigs.get(0); |
| 168 |
} else { |
| 169 |
// Prompt the user to choose a config. A null result means the user |
| 170 |
// cancelled the dialog, in which case this method returns null, |
| 171 |
// since cancelling the dialog should also cancel launching anything. |
| 172 |
ILaunchConfiguration config = chooseConfiguration(candidateConfigs); |
| 173 |
if (config != null) { |
| 174 |
return config; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return null; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Show a selection dialog that allows the user to choose one of the specified |
| 183 |
* launch configurations. Return the chosen config, or <code>null</code> if the |
| 184 |
* user cancelled the dialog. |
| 185 |
*/ |
| 186 |
protected ILaunchConfiguration chooseConfiguration(List configList) { |
| 187 |
IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); |
| 188 |
ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); |
| 189 |
dialog.setElements(configList.toArray()); |
| 190 |
dialog.setTitle(getTypeSelectionTitle()); |
| 191 |
dialog.setMessage(LauncherMessages.JavaLaunchShortcut_2); |
| 192 |
dialog.setMultipleSelection(false); |
| 193 |
int result = dialog.open(); |
| 194 |
labelProvider.dispose(); |
| 195 |
if (result == Window.OK) { |
| 196 |
return (ILaunchConfiguration) dialog.getFirstResult(); |
| 197 |
} |
| 198 |
return null; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Create and returns a new configuration based on the specified <code>IType</code>. |
| 203 |
*/ |
| 204 |
protected abstract ILaunchConfiguration createConfiguration(IType type); |
| 205 |
|
| 206 |
/** |
| 207 |
* Opens an error dialog on the given excpetion. |
| 208 |
* |
| 209 |
* @param exception |
| 210 |
*/ |
| 211 |
protected void reportErorr(CoreException exception) { |
| 212 |
MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_3, exception.getStatus().getMessage()); |
| 213 |
} |
| 214 |
|
| 215 |
protected ILaunchManager getLaunchManager() { |
| 216 |
return DebugPlugin.getDefault().getLaunchManager(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Convenience method to get the window that owns this action's Shell. |
| 221 |
*/ |
| 222 |
protected Shell getShell() { |
| 223 |
return JDIDebugUIPlugin.getActiveWorkbenchShell(); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @see ILaunchShortcut#launch(IEditorPart, String) |
| 228 |
*/ |
| 229 |
public void launch(IEditorPart editor, String mode) { |
| 230 |
IEditorInput input = editor.getEditorInput(); |
| 231 |
IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class); |
| 232 |
if (je != null) { |
| 233 |
searchAndLaunch(new Object[] {je}, mode, getTypeSelectionTitle(), getEditorEmptyMessage()); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @see ILaunchShortcut#launch(ISelection, String) |
| 239 |
*/ |
| 240 |
public void launch(ISelection selection, String mode) { |
| 241 |
if (selection instanceof IStructuredSelection) { |
| 242 |
searchAndLaunch(((IStructuredSelection)selection).toArray(), mode, getTypeSelectionTitle(), getSelectionEmptyMessage()); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the title for type selection dialog for this launch shortcut. |
| 248 |
* |
| 249 |
* @return type selection dialog title |
| 250 |
*/ |
| 251 |
protected abstract String getTypeSelectionTitle(); |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns an error message to use when the editor does not contain a launchable type. |
| 255 |
* |
| 256 |
* @return error message |
| 257 |
*/ |
| 258 |
protected abstract String getEditorEmptyMessage(); |
| 259 |
|
| 260 |
/** |
| 261 |
* Returns an error message to use when the selection does not contain a launchable type. |
| 262 |
* |
| 263 |
* @return error message |
| 264 |
*/ |
| 265 |
protected abstract String getSelectionEmptyMessage(); |
| 266 |
} |