1 11 package org.eclipse.pde.ui.launcher; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IAdaptable; 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.jdt.launching.IJavaLaunchConfigurationConstants; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.pde.core.plugin.IPluginAttribute; 29 import org.eclipse.pde.core.plugin.IPluginBase; 30 import org.eclipse.pde.core.plugin.IPluginElement; 31 import org.eclipse.pde.core.plugin.IPluginExtension; 32 import org.eclipse.pde.core.plugin.IPluginModelBase; 33 import org.eclipse.pde.core.plugin.PluginRegistry; 34 import org.eclipse.pde.core.plugin.TargetPlatform; 35 import org.eclipse.pde.internal.core.DependencyManager; 36 import org.eclipse.pde.internal.core.TargetPlatformHelper; 37 import org.eclipse.pde.internal.core.product.WorkspaceProductModel; 38 import org.eclipse.pde.internal.core.util.IdUtil; 39 import org.eclipse.pde.internal.ui.IPDEUIConstants; 40 import org.eclipse.pde.internal.ui.PDEPlugin; 41 import org.eclipse.pde.internal.ui.launcher.ApplicationSelectionDialog; 42 import org.eclipse.pde.internal.ui.launcher.LaunchAction; 43 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper; 44 import org.eclipse.ui.IEditorPart; 45 46 55 public class EclipseLaunchShortcut extends AbstractLaunchShortcut { 56 57 public static final String CONFIGURATION_TYPE = "org.eclipse.pde.ui.RuntimeWorkbench"; 59 private IPluginModelBase fModel = null; 60 61 private String fApplicationName = null; 62 63 67 public void launch(IEditorPart editor, String mode) { 68 fApplicationName = null; 69 fModel = null; 70 launch(mode); 71 } 72 73 77 public void launch(ISelection selection, String mode) { 78 IPluginModelBase model = null; 79 if (selection instanceof IStructuredSelection) { 80 IStructuredSelection ssel = (IStructuredSelection)selection; 81 if (!ssel.isEmpty()) { 82 Object object = ssel.getFirstElement(); 83 IProject project = null; 84 if (object instanceof IFile) { 85 if ("product".equals(((IFile)object).getFileExtension())) { WorkspaceProductModel productModel = new WorkspaceProductModel((IFile)object, false); 88 try { 89 productModel.load(); 90 new LaunchAction(productModel.getProduct(), ((IFile)object).getFullPath().toOSString(), mode).run(); 91 } catch (CoreException e) { 92 PDEPlugin.log(e); 93 } 94 return; 95 } 96 project = ((IFile)object).getProject(); 99 } 100 else if (object instanceof IAdaptable) { 101 project = (IProject)((IAdaptable)object).getAdapter(IProject.class); 102 } 103 if (project != null && project.isOpen()) 104 model = PluginRegistry.findModel(project); 105 } 106 } 107 launch(model, mode); 108 } 109 110 private void launch(IPluginModelBase model, String mode) { 111 fModel = model; 112 fApplicationName = null; 113 if (fModel != null) { 114 String [] applicationNames = getAvailableApplications(); 115 if (applicationNames.length == 1) { 116 fApplicationName = applicationNames[0]; 117 } else if (applicationNames.length > 1){ 118 ApplicationSelectionDialog dialog = new ApplicationSelectionDialog( 119 PDEPlugin.getActiveWorkbenchShell().getShell(), applicationNames, 120 mode); 121 if (dialog.open() == Window.OK) { 122 fApplicationName = dialog.getSelectedApplication(); 123 } 124 } 125 } 126 launch(mode); 127 } 128 129 private String [] getAvailableApplications() { 130 IPluginBase plugin = fModel.getPluginBase(); 131 String id = plugin.getId(); 132 if (id == null || id.trim().length() == 0) 133 return new String [0]; 134 135 IPluginExtension[] extensions = plugin.getExtensions(); 136 ArrayList result = new ArrayList (); 137 for (int i = 0; i < extensions.length; i++) { 138 IPluginExtension extension = extensions[i]; 139 if ("org.eclipse.core.runtime.applications".equals(extension.getPoint())) { String extensionID = extension.getId(); 141 if (extensionID != null) { 142 result.add(IdUtil.getFullId(extensions[i])); 143 } 144 } 145 } 146 return (String [])result.toArray(new String [result.size()]); 147 } 148 149 private String getProduct(String appName) { 150 if (appName == null) 151 return TargetPlatform.getDefaultProduct(); 152 if (fModel != null && appName != null) { 153 IPluginExtension[] extensions = fModel.getPluginBase().getExtensions(); 154 for (int i = 0; i < extensions.length; i++) { 155 IPluginExtension ext = extensions[i]; 156 String point = ext.getPoint(); 157 if ("org.eclipse.core.runtime.products".equals(point)) { if (ext.getChildCount() == 1) { 159 IPluginElement prod = (IPluginElement)ext.getChildren()[0]; 160 if (prod.getName().equals("product")) { IPluginAttribute attr = prod.getAttribute("application"); if (attr != null && appName.equals(attr.getValue())) { 163 return IdUtil.getFullId(ext); 164 } 165 } 166 } 167 } 168 } 169 } 170 return null; 171 } 172 173 183 protected boolean isGoodMatch(ILaunchConfiguration configuration) { 184 try { 185 if (!configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 186 String configApp = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String )null); 187 return (configApp == null && fApplicationName == null) 188 || (fApplicationName != null && fApplicationName.equals(configApp)); 189 } 190 String thisProduct = configuration.getAttribute(IPDELauncherConstants.PRODUCT, (String )null); 191 return thisProduct != null && thisProduct.equals(getProduct(fApplicationName)); 192 193 } catch (CoreException e) { 194 } 195 return false; 196 } 197 198 214 protected void initializeConfiguration(ILaunchConfigurationWorkingCopy wc) { 215 if (TargetPlatformHelper.usesNewApplicationModel()) 216 wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, "3.3"); else if (TargetPlatformHelper.getTargetVersion() >= 3.2) 218 wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, "3.2a"); wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(wc.getName())); initializeProgramArguments(wc); 221 initializeVMArguments(wc); 222 wc.setAttribute(IPDELauncherConstants.USEFEATURES, false); 223 wc.setAttribute(IPDELauncherConstants.DOCLEAR, false); 224 wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true); 225 wc.setAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true); 226 wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE); 227 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, fApplicationName == null); 228 if (fApplicationName != null) { 229 String product = getProduct(fApplicationName); 230 if (product == null) { 231 wc.setAttribute(IPDELauncherConstants.APPLICATION, fApplicationName); 232 } else { 233 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true); 234 wc.setAttribute(IPDELauncherConstants.PRODUCT, product); 235 } 236 wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false); 237 238 StringBuffer wsplugins = new StringBuffer (); 239 StringBuffer explugins = new StringBuffer (); 240 Set plugins = DependencyManager.getSelfAndDependencies(fModel); 241 Iterator iter = plugins.iterator(); 242 while (iter.hasNext()) { 243 String id = iter.next().toString(); 244 IPluginModelBase model = PluginRegistry.findModel(id); 245 if (model == null || !model.isEnabled()) 246 continue; 247 if (model.getUnderlyingResource() == null) { 248 if (explugins.length() > 0) 249 explugins.append(","); explugins.append(id); 251 } else { 252 if (wsplugins.length() > 0) 253 wsplugins.append(","); wsplugins.append(id); 255 } 256 } 257 wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString()); 258 wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString()); 259 } else { 260 String defaultProduct = TargetPlatform.getDefaultProduct(); 261 if (defaultProduct != null) { 262 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, true); 263 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true); 264 wc.setAttribute(IPDELauncherConstants.PRODUCT, defaultProduct); 265 } 266 } 267 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, PDESourcePathProvider.ID); 268 } 269 270 private void initializeProgramArguments(ILaunchConfigurationWorkingCopy wc) { 271 String programArgs = LaunchArgumentsHelper.getInitialProgramArguments(); 272 if (programArgs.length() > 0) 273 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArgs); 274 } 275 276 private void initializeVMArguments(ILaunchConfigurationWorkingCopy wc) { 277 String vmArgs = LaunchArgumentsHelper.getInitialVMArguments(); 278 if (vmArgs.length() > 0) 279 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs); 280 } 281 282 287 protected String getLaunchConfigurationTypeName() { 288 return CONFIGURATION_TYPE; 289 } 290 291 294 protected String getName(ILaunchConfigurationType type) { 295 if (fApplicationName == null) 297 return super.getName(type); 298 String product = getProduct(fApplicationName); 299 return (product == null) ? fApplicationName : product; 300 } 301 } 302 | Popular Tags |