1 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.internal.debug.ui.JDIDebugUIPlugin; 29 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.operation.IRunnableContext; 32 import org.eclipse.jface.viewers.ISelection; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.window.Window; 35 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.ui.IEditorInput; 37 import org.eclipse.ui.IEditorPart; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 40 41 46 public abstract class JavaLaunchShortcut implements ILaunchShortcut { 47 48 53 public void searchAndLaunch(Object [] search, String mode, String selectMessage, String emptyMessage) { 54 IType[] types = null; 55 try { 56 types = findTypes(search, PlatformUI.getWorkbench().getProgressService()); 57 } 58 catch (InterruptedException e) {return;} 59 catch (CoreException e) { 60 MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_0, e.getMessage()); 61 return; 62 } 63 IType type = null; 64 if (types.length == 0) { 65 MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_1, emptyMessage); 66 } 67 else if (types.length > 1) { 68 type = chooseType(types, selectMessage); 69 } 70 else { 71 type = types[0]; 72 } 73 if (type != null) { 74 launch(type, mode); 75 } 76 } 77 78 87 protected abstract IType[] findTypes(Object [] elements, IRunnableContext context) throws InterruptedException , CoreException; 88 89 97 protected IType chooseType(IType[] types, String title) { 98 DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(JDIDebugUIPlugin.getShell(), types, title); 99 if (mmsd.open() == Window.OK) { 100 return (IType)mmsd.getResult()[0]; 101 } 102 return null; 103 } 104 105 108 protected void launch(IType type, String mode) { 109 ILaunchConfiguration config = findLaunchConfiguration(type, getConfigurationType()); 110 if (config != null) { 111 DebugUITools.launch(config, mode); 112 } 113 } 114 115 120 protected abstract ILaunchConfigurationType getConfigurationType(); 121 122 127 protected ILaunchConfiguration findLaunchConfiguration(IType type, ILaunchConfigurationType configType) { 128 List candidateConfigs = Collections.EMPTY_LIST; 129 try { 130 ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType); 131 candidateConfigs = new ArrayList (configs.length); 132 for (int i = 0; i < configs.length; i++) { 133 ILaunchConfiguration config = configs[i]; 134 if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "").equals(type.getFullyQualifiedName())) { if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "").equals(type.getJavaProject().getElementName())) { candidateConfigs.add(config); 137 } 138 } 139 } 140 } catch (CoreException e) { 141 JDIDebugUIPlugin.log(e); 142 } 143 144 int candidateCount = candidateConfigs.size(); 149 if (candidateCount < 1) { 150 return createConfiguration(type); 151 } else if (candidateCount == 1) { 152 return (ILaunchConfiguration) candidateConfigs.get(0); 153 } else { 154 ILaunchConfiguration config = chooseConfiguration(candidateConfigs); 158 if (config != null) { 159 return config; 160 } 161 } 162 163 return null; 164 } 165 166 171 protected ILaunchConfiguration chooseConfiguration(List configList) { 172 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 173 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 174 dialog.setElements(configList.toArray()); 175 dialog.setTitle(getTypeSelectionTitle()); 176 dialog.setMessage(LauncherMessages.JavaLaunchShortcut_2); 177 dialog.setMultipleSelection(false); 178 int result = dialog.open(); 179 labelProvider.dispose(); 180 if (result == Window.OK) { 181 return (ILaunchConfiguration) dialog.getFirstResult(); 182 } 183 return null; 184 } 185 186 189 protected abstract ILaunchConfiguration createConfiguration(IType type); 190 191 196 protected void reportErorr(CoreException exception) { 197 MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_3, exception.getStatus().getMessage()); 198 } 199 200 protected ILaunchManager getLaunchManager() { 201 return DebugPlugin.getDefault().getLaunchManager(); 202 } 203 204 207 protected Shell getShell() { 208 return JDIDebugUIPlugin.getActiveWorkbenchShell(); 209 } 210 211 214 public void launch(IEditorPart editor, String mode) { 215 IEditorInput input = editor.getEditorInput(); 216 IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class); 217 if (je != null) { 218 searchAndLaunch(new Object [] {je}, mode, getTypeSelectionTitle(), getEditorEmptyMessage()); 219 } 220 } 221 222 225 public void launch(ISelection selection, String mode) { 226 if (selection instanceof IStructuredSelection) { 227 searchAndLaunch(((IStructuredSelection)selection).toArray(), mode, getTypeSelectionTitle(), getSelectionEmptyMessage()); 228 } 229 } 230 231 236 protected abstract String getTypeSelectionTitle(); 237 238 243 protected abstract String getEditorEmptyMessage(); 244 245 250 protected abstract String getSelectionEmptyMessage(); 251 } 252 | Popular Tags |