1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.debug.core.DebugPlugin; 24 import org.eclipse.debug.core.ILaunchConfiguration; 25 import org.eclipse.debug.core.ILaunchConfigurationType; 26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 27 import org.eclipse.debug.core.ILaunchManager; 28 import org.eclipse.debug.ui.DebugUITools; 29 import org.eclipse.debug.ui.IDebugModelPresentation; 30 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 31 import org.eclipse.jface.action.Action; 32 import org.eclipse.jface.window.Window; 33 import org.eclipse.pde.core.plugin.IPluginModelBase; 34 import org.eclipse.pde.core.plugin.PluginRegistry; 35 import org.eclipse.pde.internal.core.FeatureModelManager; 36 import org.eclipse.pde.internal.core.PDECore; 37 import org.eclipse.pde.internal.core.TargetPlatformHelper; 38 import org.eclipse.pde.internal.core.ifeature.IFeature; 39 import org.eclipse.pde.internal.core.ifeature.IFeatureChild; 40 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 41 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin; 42 import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo; 43 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo; 44 import org.eclipse.pde.internal.core.iproduct.IProduct; 45 import org.eclipse.pde.internal.core.iproduct.IProductFeature; 46 import org.eclipse.pde.internal.core.iproduct.IProductPlugin; 47 import org.eclipse.pde.internal.core.util.CoreUtility; 48 import org.eclipse.pde.internal.ui.IPDEUIConstants; 49 import org.eclipse.pde.internal.ui.PDEPlugin; 50 import org.eclipse.pde.internal.ui.PDEUIMessages; 51 import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut; 52 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 53 import org.eclipse.pde.ui.launcher.PDESourcePathProvider; 54 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 55 56 public class LaunchAction extends Action { 57 58 private IProduct fProduct; 59 private String fMode; 60 private String fPath; 61 62 public LaunchAction(IProduct product, String path, String mode) { 63 fProduct = product; 64 fMode = mode; 65 fPath = path; 66 } 67 68 public void run() { 69 try { 70 ILaunchConfiguration config = findLaunchConfiguration(); 71 if (config != null) 72 DebugUITools.launch(config, fMode); 73 } catch (CoreException e) { 74 } 75 } 76 77 private ILaunchConfiguration findLaunchConfiguration() throws CoreException { 78 ILaunchConfiguration[] configs = getLaunchConfigurations(); 79 80 if (configs.length == 0) 81 return createConfiguration(); 82 83 ILaunchConfiguration config = null; 84 if (configs.length == 1) { 85 config = configs[0]; 86 } else { 87 config = chooseConfiguration(configs); 89 } 90 91 if (config != null) { 92 config = refreshConfiguration(config.getWorkingCopy()); 93 } 94 return config; 95 } 96 97 private ILaunchConfiguration refreshConfiguration(ILaunchConfigurationWorkingCopy wc) throws CoreException { 98 wc.setAttribute(IPDELauncherConstants.PRODUCT, fProduct.getId()); 99 String os = Platform.getOS(); 100 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, getVMArguments(os)); 101 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, getProgramArguments(os)); 102 StringBuffer wsplugins = new StringBuffer (); 103 StringBuffer explugins = new StringBuffer (); 104 IPluginModelBase[] models = getModels(); 105 for (int i = 0; i < models.length; i++) { 106 IPluginModelBase model = models[i]; 107 String id = model.getPluginBase().getId(); 108 if (model.getUnderlyingResource() == null) { 109 explugins.append(id); 110 explugins.append(","); } else { 112 wsplugins.append(id); 113 wsplugins.append(","); } 115 } 116 wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString()); 117 wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString()); 118 String configIni = getTemplateConfigIni(); 119 wc.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, configIni == null); 120 if (configIni != null) 121 wc.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, configIni); 122 return wc.doSave(); 123 } 124 125 private String getProgramArguments(String os) { 126 StringBuffer buffer = new StringBuffer (LaunchArgumentsHelper.getInitialProgramArguments()); 127 IArgumentsInfo info = fProduct.getLauncherArguments(); 128 String userArgs = (info != null) ? CoreUtility.normalize(info.getCompleteProgramArguments(os)) : ""; if (userArgs.length() > 0) { 130 buffer.append(" "); buffer.append(userArgs); 132 } 133 return buffer.toString(); 134 } 135 136 private String getVMArguments(String os) { 137 IArgumentsInfo info = fProduct.getLauncherArguments(); 138 return (info != null) ? CoreUtility.normalize(info.getCompleteVMArguments(os)) : ""; } 140 141 private IPluginModelBase[] getModels() { 142 HashMap map = new HashMap (); 143 if (fProduct.useFeatures()) { 144 IFeatureModel[] features = getUniqueFeatures(); 145 for (int i = 0; i < features.length; i++) { 146 addFeaturePlugins(features[i].getFeature(), map); 147 } 148 } else { 149 IProductPlugin[] plugins = fProduct.getPlugins(); 150 for (int i = 0; i < plugins.length; i++) { 151 String id = plugins[i].getId(); 152 if (id == null || map.containsKey(id)) 153 continue; 154 IPluginModelBase model = PluginRegistry.findModel(id); 155 if (model != null && TargetPlatformHelper.matchesCurrentEnvironment(model)) 156 map.put(id, model); 157 } 158 } 159 return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]); 160 } 161 162 private IFeatureModel[] getUniqueFeatures() { 163 ArrayList list = new ArrayList (); 164 IProductFeature[] features = fProduct.getFeatures(); 165 for (int i = 0; i < features.length; i++) { 166 String id = features[i].getId(); 167 String version = features[i].getVersion(); 168 addFeatureAndChildren(id, version, list); 169 } 170 return (IFeatureModel[])list.toArray(new IFeatureModel[list.size()]); 171 } 172 173 private void addFeatureAndChildren(String id, String version, List list) { 174 FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager(); 175 IFeatureModel model = manager.findFeatureModel(id, version); 176 if (model == null || list.contains(model)) 177 return; 178 179 list.add(model); 180 181 IFeatureChild[] children = model.getFeature().getIncludedFeatures(); 182 for (int i = 0; i < children.length; i++) { 183 addFeatureAndChildren(children[i].getId(), children[i].getVersion(), list); 184 } 185 } 186 187 private void addFeaturePlugins(IFeature feature, HashMap map) { 188 IFeaturePlugin[] plugins = feature.getPlugins(); 189 for (int i = 0; i < plugins.length; i++) { 190 String id = plugins[i].getId(); 191 if (id == null || map.containsKey(id)) 192 continue; 193 IPluginModelBase model = PluginRegistry.findModel(id); 194 if (model != null && TargetPlatformHelper.matchesCurrentEnvironment(model)) 195 map.put(id, model); 196 } 197 } 198 199 private String getTemplateConfigIni() { 200 IConfigurationFileInfo info = fProduct.getConfigurationFileInfo(); 201 if (info != null && info.getUse().equals("custom")) { String path = getExpandedPath(info.getPath()); 203 if (path != null) { 204 File file = new File (path); 205 if (file.exists() && file.isFile()) 206 return file.getAbsolutePath(); 207 } 208 } 209 return null; 210 } 211 212 private String getExpandedPath(String path) { 213 if (path == null || path.length() == 0) 214 return null; 215 IResource resource = PDEPlugin.getWorkspace().getRoot().findMember(new Path(path)); 216 if (resource != null) { 217 IPath fullPath = resource.getLocation(); 218 return fullPath == null ? null : fullPath.toOSString(); 219 } 220 return null; 221 } 222 223 224 private ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs) { 225 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 226 ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider); 227 dialog.setElements(configs); 228 dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title); 229 if (fMode.equals(ILaunchManager.DEBUG_MODE)) { 230 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug); 231 } else { 232 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run); 233 } 234 dialog.setMultipleSelection(false); 235 int result= dialog.open(); 236 labelProvider.dispose(); 237 if (result == Window.OK) { 238 return (ILaunchConfiguration)dialog.getFirstResult(); 239 } 240 return null; 241 } 242 243 private ILaunchConfiguration createConfiguration() throws CoreException { 244 ILaunchConfigurationType configType = getWorkbenchLaunchConfigType(); 245 String computedName = getComputedName(new Path(fPath).lastSegment()); 246 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName); 247 wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(computedName)); wc.setAttribute(IPDELauncherConstants.USEFEATURES, false); 249 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, false); 250 wc.setAttribute(IPDELauncherConstants.DOCLEAR, false); 251 wc.setAttribute(IPDEUIConstants.DOCLEARLOG, false); 252 wc.setAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true); 253 wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true); 254 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true); 255 wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false); 256 wc.setAttribute(IPDELauncherConstants.PRODUCT_FILE, fPath); 257 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, PDESourcePathProvider.ID); 258 return refreshConfiguration(wc); 259 } 260 261 private String getComputedName(String prefix) { 262 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 263 return lm.generateUniqueLaunchConfigurationNameFrom(prefix); 264 } 265 266 private ILaunchConfiguration[] getLaunchConfigurations() throws CoreException { 267 ArrayList result = new ArrayList (); 268 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 269 ILaunchConfigurationType type = manager.getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE); 270 ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type); 271 for (int i = 0; i < configs.length; i++) { 272 if (!DebugUITools.isPrivate(configs[i])) { 273 String path = configs[i].getAttribute(IPDELauncherConstants.PRODUCT_FILE, ""); if (new Path(fPath).equals(new Path(path))) { 275 result.add(configs[i]); 276 } 277 } 278 } 279 return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]); 280 } 281 282 protected ILaunchConfigurationType getWorkbenchLaunchConfigType() { 283 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 284 return lm.getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE); 285 } 286 287 } 288 | Popular Tags |