1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.debug.core.DebugPlugin; 21 import org.eclipse.debug.core.ILaunchConfiguration; 22 import org.eclipse.debug.core.ILaunchConfigurationType; 23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 24 import org.eclipse.debug.core.ILaunchManager; 25 import org.eclipse.debug.ui.DebugUITools; 26 import org.eclipse.debug.ui.IDebugModelPresentation; 27 import org.eclipse.debug.ui.ILaunchShortcut; 28 import org.eclipse.jdt.core.IClasspathEntry; 29 import org.eclipse.jdt.core.IJavaElement; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.core.IMember; 32 import org.eclipse.jdt.core.IPackageFragmentRoot; 33 import org.eclipse.jdt.core.IType; 34 import org.eclipse.jdt.core.JavaModelException; 35 import org.eclipse.jdt.core.search.IJavaSearchScope; 36 import org.eclipse.jdt.core.search.SearchEngine; 37 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 38 import org.eclipse.jdt.ui.IJavaElementSearchConstants; 39 import org.eclipse.jface.dialogs.ErrorDialog; 40 import org.eclipse.jface.dialogs.MessageDialog; 41 import org.eclipse.jface.viewers.ISelection; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 import org.eclipse.jface.window.Window; 44 import org.eclipse.pde.internal.ui.PDEPlugin; 45 import org.eclipse.pde.internal.ui.PDEUIMessages; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.swt.widgets.Shell; 48 import org.eclipse.ui.IEditorInput; 49 import org.eclipse.ui.IEditorPart; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 52 53 public class SWTApplicationLaunchShortcut implements ILaunchShortcut { 54 55 60 public void searchAndLaunch(Object [] search, String mode, boolean editor) { 61 IType[] types = null; 62 if (search != null) { 63 try { 64 IJavaElement[] elements = getJavaElements(search); 65 MainMethodSearchEngine engine = new MainMethodSearchEngine(); 66 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements, false); 67 types = engine.searchMainMethods(PlatformUI.getWorkbench().getProgressService(), 68 scope, IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS, 69 true); 70 } catch (InterruptedException e) { 71 return; 72 } catch (InvocationTargetException e) { 73 MessageDialog.openError(getShell(), PDEUIMessages.SWTApplicationLaunchShortcut_failed, e.getMessage()); 74 return; 75 } 76 IType type = null; 77 if (types.length == 0) { 78 String message = null; 79 if (editor) { 80 message = PDEUIMessages.SWTApplicationLaunchShortcut_noMainInEditor; 81 } else { 82 message = PDEUIMessages.SWTApplicationLaunchShortcut_noMainInSelection; 83 } 84 MessageDialog.openError(getShell(), PDEUIMessages.SWTApplicationLaunchShortcut_failed, message); 85 } else if (types.length > 1) { 86 type = chooseType(types, mode); 87 } else { 88 type = types[0]; 89 } 90 if (type != null) { 91 launch(type, mode); 92 } 93 } 94 95 } 96 97 103 private IJavaElement[] getJavaElements(Object [] objects) { 104 ArrayList list= new ArrayList (objects.length); 105 for (int i = 0; i < objects.length; i++) { 106 Object object = objects[i]; 107 if (object instanceof IAdaptable) { 108 IJavaElement element = (IJavaElement) ((IAdaptable)object).getAdapter(IJavaElement.class); 109 if (element != null) { 110 if (element instanceof IMember) { 111 IJavaElement type= ((IMember)element).getDeclaringType(); 113 if (type != null) { 114 element= type; 115 } 116 } 117 if (element instanceof IJavaProject) { 119 IJavaProject project = (IJavaProject) element; 120 try { 121 IClasspathEntry[] cpEntries = project 122 .getRawClasspath(); 123 for (int j = 0; j < cpEntries.length; j++) { 124 if (cpEntries[j].getEntryKind() == IClasspathEntry.CPE_SOURCE 125 || cpEntries[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) { 126 IPackageFragmentRoot[] roots = project.findPackageFragmentRoots(cpEntries[j]); 127 for(int r=0; r< roots.length; r++){ 128 list.add(roots[r]); 129 } 130 } 131 } 132 } catch (JavaModelException jme) { 133 } 134 } else { 135 list.add(element); 136 } 137 } 138 } 139 } 140 return (IJavaElement[]) list.toArray(new IJavaElement[list.size()]); 141 } 142 143 148 protected IType chooseType(IType[] types, String mode) { 149 MainTypeSelectionDialog dialog= new MainTypeSelectionDialog(getShell(), types); 150 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 151 dialog.setTitle(PDEUIMessages.SWTApplicationLaunchShortcut_debug); 152 } else { 153 dialog.setTitle(PDEUIMessages.SWTApplicationLaunchShortcut_run); 154 } 155 dialog.setMultipleSelection(false); 156 if (dialog.open() == Window.OK) { 157 return (IType)dialog.getFirstResult(); 158 } 159 return null; 160 } 161 162 165 protected void launch(IType type, String mode) { 166 ILaunchConfiguration config = findLaunchConfiguration(type, mode); 167 if (config != null) { 168 DebugUITools.launch(config, mode); 169 } 170 } 171 172 177 protected ILaunchConfiguration findLaunchConfiguration(IType type, String mode) { 178 ILaunchConfigurationType configType = getSWTLaunchConfigType(); 179 java.util.List candidateConfigs = Collections.EMPTY_LIST; 180 try { 181 ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType); 182 candidateConfigs = new ArrayList (configs.length); 183 for (int i = 0; i < configs.length; i++) { 184 ILaunchConfiguration config = configs[i]; 185 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); 188 } 189 } 190 } 191 } catch (CoreException e) { 192 PDEPlugin.log(e); 193 } 194 195 int candidateCount = candidateConfigs.size(); 200 if (candidateCount < 1) { 201 return createConfiguration(type); 202 } else if (candidateCount == 1) { 203 return (ILaunchConfiguration) candidateConfigs.get(0); 204 } else { 205 ILaunchConfiguration config = chooseConfiguration(candidateConfigs, mode); 209 if (config != null) { 210 return config; 211 } 212 } 213 214 return null; 215 } 216 217 222 protected ILaunchConfiguration chooseConfiguration(java.util.List configList, String mode) { 223 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 224 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 225 dialog.setElements(configList.toArray()); 226 dialog.setTitle(PDEUIMessages.SWTApplicationLaunchShortcut_launch); 227 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 228 dialog.setMessage(PDEUIMessages.SWTApplicationLaunchShortcut_chooseRun); 229 } else { 230 dialog.setMessage(PDEUIMessages.SWTApplicationLaunchShortcut_chooseDebug); 231 } 232 dialog.setMultipleSelection(false); 233 int result = dialog.open(); 234 labelProvider.dispose(); 235 if (result == Window.OK) { 236 return (ILaunchConfiguration) dialog.getFirstResult(); 237 } 238 return null; 239 } 240 241 244 protected ILaunchConfiguration createConfiguration(IType type) { 245 ILaunchConfiguration config = null; 246 ILaunchConfigurationWorkingCopy wc = null; 247 try { 248 ILaunchConfigurationType configType = getSWTLaunchConfigType(); 249 wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(type.getElementName())); 250 } catch (CoreException exception) { 251 reportCreatingConfiguration(exception); 252 return null; 253 } 254 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type.getFullyQualifiedName()); 255 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, type.getJavaProject().getElementName()); 256 wc.setMappedResources(new IResource[] {type.getJavaProject().getProject()}); 257 try { 258 config = wc.doSave(); 259 } catch (CoreException exception) { 260 reportCreatingConfiguration(exception); 261 } 262 return config; 263 } 264 265 protected void reportCreatingConfiguration(final CoreException exception) { 266 Display.getDefault().asyncExec(new Runnable () { 267 public void run() { 268 ErrorDialog.openError(getShell(), PDEUIMessages.SWTApplicationLaunchShortcut_error, PDEUIMessages.SWTApplicationLaunchShortcut_exception, exception.getStatus()); } 270 }); 271 } 272 273 276 protected ILaunchConfigurationType getSWTLaunchConfigType() { 277 return getLaunchManager().getLaunchConfigurationType("org.eclipse.pde.ui.swtLaunchConfig"); } 279 280 protected ILaunchManager getLaunchManager() { 281 return DebugPlugin.getDefault().getLaunchManager(); 282 } 283 284 287 protected Shell getShell() { 288 return PDEPlugin.getActiveWorkbenchShell(); 289 } 290 291 294 public void launch(IEditorPart editor, String mode) { 295 IEditorInput input = editor.getEditorInput(); 296 IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class); 297 if (je != null) { 298 searchAndLaunch(new Object [] {je}, mode, true); 299 } else { 300 MessageDialog.openError(getShell(), PDEUIMessages.SWTApplicationLaunchShortcut_failed, PDEUIMessages.SWTApplicationLaunchShortcut_noMainInEditor); 301 } 302 303 } 304 305 308 public void launch(ISelection selection, String mode) { 309 if (selection instanceof IStructuredSelection) { 310 searchAndLaunch(((IStructuredSelection)selection).toArray(), mode, false); 311 } else { 312 MessageDialog.openError(getShell(), PDEUIMessages.SWTApplicationLaunchShortcut_failed, PDEUIMessages.SWTApplicationLaunchShortcut_noMainInSelection); 313 } 314 } 315 316 } 317 | Popular Tags |