1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.util.ArrayList ; 14 import java.util.TreeMap ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.Preferences; 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.launching.IJavaLaunchConfigurationConstants; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.window.Window; 32 import org.eclipse.pde.core.plugin.IFragmentModel; 33 import org.eclipse.pde.core.plugin.IPluginAttribute; 34 import org.eclipse.pde.core.plugin.IPluginBase; 35 import org.eclipse.pde.core.plugin.IPluginElement; 36 import org.eclipse.pde.core.plugin.IPluginExtension; 37 import org.eclipse.pde.core.plugin.IPluginImport; 38 import org.eclipse.pde.core.plugin.IPluginModelBase; 39 import org.eclipse.pde.internal.core.ICoreConstants; 40 import org.eclipse.pde.internal.core.ModelEntry; 41 import org.eclipse.pde.internal.core.PDECore; 42 import org.eclipse.pde.internal.core.PluginModelManager; 43 import org.eclipse.pde.internal.core.TargetPlatform; 44 import org.eclipse.pde.internal.ui.PDEPlugin; 45 import org.eclipse.pde.internal.ui.PDEUIMessages; 46 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 47 import org.eclipse.swt.widgets.Shell; 48 import org.eclipse.ui.IEditorPart; 49 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 50 51 54 public class RuntimeWorkbenchShortcut implements ILaunchShortcut { 55 56 public static final String CLASSPATH_PROVIDER = "org.eclipse.pde.ui.workbenchClasspathProvider"; public static final String CONFIGURATION_TYPE = "org.eclipse.pde.ui.RuntimeWorkbench"; 59 private IPluginModelBase fModel = null; 60 61 public void run(IProject project) { 62 launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.RUN_MODE); 63 } 64 65 public void debug(IProject project) { 66 launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.DEBUG_MODE); 67 } 68 69 72 public void launch(IEditorPart editor, String mode) { 73 launch(mode, null); 74 } 75 76 79 public void launch(ISelection selection, String mode) { 80 launch(getSelectedModel(selection), mode); 81 } 82 83 private void launch(IPluginModelBase model, String mode) { 84 fModel = model; 85 if (fModel != null) { 86 String [] applicationNames = getAvailableApplications(); 87 if (applicationNames.length == 0) { 88 launch(mode, null); 89 } else if (applicationNames.length == 1) { 90 launch(mode, applicationNames[0]); 91 } else { 92 ApplicationSelectionDialog dialog = new ApplicationSelectionDialog( 93 PDEPlugin.getActiveWorkbenchShell().getShell(), applicationNames, 94 mode); 95 if (dialog.open() == Window.OK) { 96 launch(mode, dialog.getSelectedApplication()); 97 } 98 } 99 } else { 100 launch(mode, null); 101 } 102 } 103 104 private IPluginModelBase getSelectedModel(ISelection selection) { 105 if (selection instanceof IStructuredSelection) { 106 IStructuredSelection ssel = (IStructuredSelection)selection; 107 if (!ssel.isEmpty()) { 108 Object object = ssel.getFirstElement(); 109 if (object instanceof IAdaptable) { 110 IProject project = (IProject)((IAdaptable)object).getAdapter(IProject.class); 111 if (project != null && project.isOpen()) 112 return PDECore.getDefault().getModelManager().findModel(project); 113 } 114 } 115 } 116 return null; 117 } 118 119 private String [] getAvailableApplications() { 120 IPluginBase plugin = fModel.getPluginBase(); 121 String id = plugin.getId(); 122 if (id == null || id.trim().length() == 0) 123 return new String [0]; 124 125 IPluginExtension[] extensions = plugin.getExtensions(); 126 ArrayList result = new ArrayList (); 127 for (int i = 0; i < extensions.length; i++) { 128 IPluginExtension extension = extensions[i]; 129 if ("org.eclipse.core.runtime.applications".equals(extension.getPoint())) { String extensionID = extension.getId(); 131 if (extensionID != null && extensionID.trim().length() > 0) { 132 result.add(id.trim() + "." + extensionID.trim()); } 134 } 135 } 136 return (String [])result.toArray(new String [result.size()]); 137 } 138 139 142 protected void launch(String mode, String applicationName) { 143 ILaunchConfiguration config = findLaunchConfiguration(mode, applicationName); 144 if (config != null) { 145 DebugUITools.launch(config, mode); 146 } 147 } 148 149 154 protected ILaunchConfiguration findLaunchConfiguration(String mode, String applicationName) { 155 ILaunchConfiguration[] configs = getLaunchConfigurations(getWorkbenchLaunchConfigType(), applicationName); 156 157 if (configs.length == 0) 158 return createConfiguration(applicationName); 159 160 if (configs.length == 1) 161 return configs[0]; 162 163 return chooseConfiguration(configs, mode); 165 } 166 167 private ILaunchConfiguration[] getLaunchConfigurations(ILaunchConfigurationType configType, String applicationName) { 168 ArrayList result = new ArrayList (); 169 try { 170 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 171 ILaunchConfiguration[] configs = manager.getLaunchConfigurations(configType); 172 for (int i = 0; i < configs.length; i++) { 173 if (!DebugUITools.isPrivate(configs[i])) { 174 if (!configs[i].getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 175 String configApp = configs[i].getAttribute(IPDELauncherConstants.APPLICATION, (String )null); 176 if ((configApp == null && applicationName == null) 177 || (configApp != null && applicationName != null && configApp.equals(applicationName))) { 178 result.add(configs[i]); 179 } 180 } else { 181 String thisProduct = configs[i].getAttribute(IPDELauncherConstants.PRODUCT, (String )null); 182 if (thisProduct != null && thisProduct.equals(getProduct(applicationName))) { 183 result.add(configs[i]); 184 } 185 } 186 187 } 188 } 189 } catch (CoreException e) { 190 } 191 return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]); 192 } 193 194 199 protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String mode) { 200 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 201 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 202 dialog.setElements(configs); 203 dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title); 204 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 205 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug); 206 } else { 207 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run); 208 } 209 dialog.setMultipleSelection(false); 210 int result= dialog.open(); 211 labelProvider.dispose(); 212 if (result == Window.OK) { 213 return (ILaunchConfiguration)dialog.getFirstResult(); 214 } 215 return null; 216 } 217 218 221 protected ILaunchConfiguration createConfiguration(String applicationName) { 222 ILaunchConfiguration config = null; 223 try { 224 ILaunchConfigurationType configType = getWorkbenchLaunchConfigType(); 225 String computedName = getComputedName(configType.getName()); 226 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName); 227 if (TargetPlatform.isRuntimeRefactored2()) 228 wc.setAttribute("pde.version", "3.2a"); else if (TargetPlatform.isRuntimeRefactored1()) 230 wc.setAttribute("pde.version", "3.2"); wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(computedName)); setJavaArguments(wc); 233 wc.setAttribute(IPDELauncherConstants.USEFEATURES, false); 234 wc.setAttribute(IPDELauncherConstants.DOCLEAR, false); 235 wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true); 236 wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE); 237 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, applicationName == null); 238 if (applicationName != null) { 239 String product = getProduct(applicationName); 240 if (product == null) { 241 wc.setAttribute(IPDELauncherConstants.APPLICATION, applicationName); 242 } else { 243 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true); 244 wc.setAttribute(IPDELauncherConstants.PRODUCT, product); 245 } 246 wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false); 247 TreeMap map = new TreeMap (); 248 addPluginAndDependencies(fModel, map); 249 Object [] models = map.values().toArray(); 250 StringBuffer wsplugins = new StringBuffer (); 251 StringBuffer explugins = new StringBuffer (); 252 for (int i = 0; i < models.length; i++) { 253 IPluginModelBase model = (IPluginModelBase)models[i]; 254 String id = model.getPluginBase().getId(); 255 if (model.getUnderlyingResource() == null) { 256 if (explugins.length() > 0) 257 explugins.append(","); explugins.append(id); 259 } else { 260 if (wsplugins.length() > 0) 261 wsplugins.append(","); wsplugins.append(id); 263 } 264 } 265 wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString()); 266 wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString()); 267 } else { 268 String defaultProduct = TargetPlatform.getDefaultProduct(); 269 if (defaultProduct != null) { 270 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, true); 271 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true); 272 wc.setAttribute(IPDELauncherConstants.PRODUCT, defaultProduct); 273 } 274 } 275 276 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, CLASSPATH_PROVIDER); 277 config= wc.doSave(); 278 } catch (CoreException ce) { 279 PDEPlugin.logException(ce); 280 } 281 return config; 282 } 283 284 private void setJavaArguments(ILaunchConfigurationWorkingCopy wc) { 285 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 286 String programArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS); 287 if (programArgs.length() > 0) 288 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArgs); 289 String vmArgs = preferences.getString(ICoreConstants.VM_ARGS); 290 if (vmArgs.length() > 0) 291 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs); 292 } 293 294 private String getProduct(String appName) { 295 if (appName == null) 296 return TargetPlatform.getDefaultProduct(); 297 if (fModel != null && appName != null) { 298 IPluginExtension[] extensions = fModel.getPluginBase().getExtensions(); 299 for (int i = 0; i < extensions.length; i++) { 300 IPluginExtension ext = extensions[i]; 301 String point = ext.getPoint(); 302 if ("org.eclipse.core.runtime.products".equals(point)) { if (ext.getChildCount() == 1) { 304 IPluginElement prod = (IPluginElement)ext.getChildren()[0]; 305 if (prod.getName().equals("product")) { IPluginAttribute attr = prod.getAttribute("application"); if (attr != null && appName.equals(attr.getValue())) { 308 return fModel.getPluginBase().getId() + "." + ext.getId(); } 310 } 311 } 312 } 313 } 314 } 315 return null; 316 } 317 318 321 protected ILaunchConfigurationType getWorkbenchLaunchConfigType() { 322 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 323 return lm.getLaunchConfigurationType(CONFIGURATION_TYPE); 324 } 325 326 private String getComputedName(String prefix) { 327 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 328 return lm.generateUniqueLaunchConfigurationNameFrom(prefix); 329 } 330 331 334 protected Shell getShell() { 335 return PDEPlugin.getActiveWorkbenchShell(); 336 } 337 338 public static void addPluginAndDependencies(IPluginModelBase model, TreeMap map) { 339 if (model == null) 340 return; 341 342 String id = model.getPluginBase().getId(); 343 if (id == null || map.containsKey(id)) 344 return; 345 346 map.put(id, model); 347 348 if (model instanceof IFragmentModel) { 349 IPluginModelBase parent = 350 findPlugin(((IFragmentModel) model).getFragment().getPluginId()); 351 addPluginAndDependencies(parent, map); 352 } else { 353 IFragmentModel[] fragments = findFragments(model.getPluginBase()); 354 for (int i = 0; i < fragments.length; i++) { 355 String fragmentID = fragments[i].getPluginBase().getId(); 356 if (!"org.eclipse.ui.workbench.compatibility".equals(fragmentID)) addPluginAndDependencies(fragments[i], map); 358 } 359 } 360 361 IPluginImport[] imports = model.getPluginBase().getImports(); 362 for (int i = 0; i < imports.length; i++) { 363 addPluginAndDependencies(findPlugin(imports[i].getId()), map); 364 } 365 } 366 367 private static IPluginModelBase findPlugin(String id) { 368 PluginModelManager manager = PDECore.getDefault().getModelManager(); 369 ModelEntry entry = manager.findEntry(id); 370 return (entry != null) ? entry.getActiveModel() : null; 371 } 372 373 private static IFragmentModel[] findFragments(IPluginBase plugin) { 374 ModelEntry[] entries = PDECore.getDefault().getModelManager().getEntries(); 375 ArrayList result = new ArrayList (); 376 for (int i = 0; i < entries.length; i++) { 377 ModelEntry entry = entries[i]; 378 IPluginModelBase model = entry.getActiveModel(); 379 if (model instanceof IFragmentModel && TargetPlatform.matchesCurrentEnvironment(model)) { 380 String id = ((IFragmentModel) model).getFragment().getPluginId(); 381 if (id.equals(plugin.getId())) { 382 result.add(model); 383 } 384 } 385 } 386 return (IFragmentModel[]) result.toArray(new IFragmentModel[result.size()]); 387 } 388 } 389 | Popular Tags |