1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.util.ArrayList ; 14 import java.util.Map ; 15 import java.util.Properties ; 16 import java.util.StringTokenizer ; 17 import java.util.TreeMap ; 18 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.Preferences; 22 import org.eclipse.debug.core.DebugPlugin; 23 import org.eclipse.debug.core.ILaunchConfiguration; 24 import org.eclipse.debug.core.ILaunchConfigurationType; 25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 26 import org.eclipse.debug.core.ILaunchManager; 27 import org.eclipse.debug.ui.DebugUITools; 28 import org.eclipse.debug.ui.IDebugModelPresentation; 29 import org.eclipse.debug.ui.ILaunchShortcut; 30 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.window.Window; 33 import org.eclipse.pde.core.plugin.IPluginModelBase; 34 import org.eclipse.pde.internal.core.ICoreConstants; 35 import org.eclipse.pde.internal.core.PDECore; 36 import org.eclipse.pde.internal.core.PluginModelManager; 37 import org.eclipse.pde.internal.core.TargetPlatform; 38 import org.eclipse.pde.internal.ui.PDEPlugin; 39 import org.eclipse.pde.internal.ui.PDEUIMessages; 40 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 41 import org.eclipse.ui.IEditorPart; 42 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 43 44 public class EquinoxLaunchShortcut implements ILaunchShortcut { 45 46 private static final String CLASSPATH_PROVIDER = "org.eclipse.pde.ui.workbenchClasspathProvider"; private static final String CONFIGURATION_TYPE = "org.eclipse.pde.ui.EquinoxLauncher"; 49 public void run(IProject project) { 50 launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.RUN_MODE); 51 } 52 53 public void debug(IProject project) { 54 launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.DEBUG_MODE); 55 } 56 57 public void launch(ISelection selection, String mode) { 58 launch((IPluginModelBase)null, mode); 59 } 60 61 public void launch(IEditorPart editor, String mode) { 62 launch((IPluginModelBase)null, mode); 63 } 64 65 private void launch(IPluginModelBase model, String mode) { 66 ILaunchConfiguration[] configs = getLaunchConfigurations(); 67 ILaunchConfiguration configuration = null; 68 if (configs.length == 0) { 69 PluginModelManager manager = PDECore.getDefault().getModelManager(); 70 IPluginModelBase[] models = (model == null) ? manager.getWorkspaceModels() : new IPluginModelBase[] {model}; 71 if (models.length == 0) { 72 IPluginModelBase osgi = manager.findModel("org.eclipse.osgi"); if (osgi != null) 74 models = new IPluginModelBase[] {osgi}; 75 } 76 configuration = createNewConfiguration(models, mode); 77 } else if (configs.length == 1) { 78 configuration = configs[0]; 79 } else { 80 configuration = chooseConfiguration(configs, mode); 81 } 82 83 if (configuration != null) 84 DebugUITools.launch(configuration, mode); 85 } 86 87 private ILaunchConfiguration[] getLaunchConfigurations() { 88 ArrayList result = new ArrayList (); 89 try { 90 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 91 ILaunchConfiguration[] configs = manager.getLaunchConfigurations(getLaunchConfigurationType()); 92 for (int i = 0; i < configs.length; i++) { 93 if (!DebugUITools.isPrivate(configs[i])) { 94 result.add(configs[i]); 95 } 96 } 97 } catch (CoreException e) { 98 } 99 return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]); 100 } 101 102 private ILaunchConfigurationType getLaunchConfigurationType() { 103 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 104 return manager.getLaunchConfigurationType(CONFIGURATION_TYPE); 105 } 106 107 protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String mode) { 108 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 109 ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider); 110 dialog.setElements(configs); 111 dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title); 112 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 113 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug); 114 } else { 115 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run); 116 } 117 dialog.setMultipleSelection(false); 118 int result= dialog.open(); 119 labelProvider.dispose(); 120 if (result == Window.OK) { 121 return (ILaunchConfiguration)dialog.getFirstResult(); 122 } 123 return null; 124 } 125 126 private ILaunchConfiguration createNewConfiguration(IPluginModelBase[] selected, String mode) { 127 ILaunchConfiguration config = null; 128 try { 129 ILaunchConfigurationType configType = getLaunchConfigurationType(); 130 String computedName = getComputedName("Equinox"); ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName); 132 setJavaArguments(wc); 133 wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE); 134 wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true); 135 initializePluginState(wc, selected); 136 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, CLASSPATH_PROVIDER); 137 config = wc.doSave(); 138 } catch (CoreException ce) { 139 PDEPlugin.logException(ce); 140 } 141 return config; 142 } 143 144 private void setJavaArguments(ILaunchConfigurationWorkingCopy wc) { 145 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 146 String progArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS); 147 if (progArgs.indexOf("-console") == -1) progArgs = "-console " + progArgs; wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, progArgs); String vmArgs = preferences.getString(ICoreConstants.VM_ARGS); 151 if (vmArgs.length() > 0) 152 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs); 153 } 154 155 public static void initializePluginState(ILaunchConfigurationWorkingCopy wc, IPluginModelBase[] selected) { 156 Map startLevelMap = getStartLevelMap(); 157 TreeMap pluginMap = new TreeMap (); 158 for (int i = 0; i < selected.length; i++) 159 RuntimeWorkbenchShortcut.addPluginAndDependencies(selected[i], pluginMap); 160 Object [] models = pluginMap.values().toArray(); 161 StringBuffer wsplugins = new StringBuffer (); 162 StringBuffer explugins = new StringBuffer (); 163 for (int i = 0; i < models.length; i++) { 164 IPluginModelBase model = (IPluginModelBase)models[i]; 165 String id = model.getPluginBase().getId(); 166 String value = "org.eclipse.osgi".equals(id) ? "@:" : "@default:default"; if (startLevelMap.containsKey(id)) 168 value = (String )startLevelMap.get(id); 169 if (model.getUnderlyingResource() == null) { 170 if (explugins.length() > 0) 171 explugins.append(","); explugins.append(id); 173 explugins.append(value); 174 } else { 175 if (wsplugins.length() > 0) 176 wsplugins.append(","); wsplugins.append(id); 178 wsplugins.append(value); 179 } 180 } 181 wc.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES, wsplugins.toString()); 182 wc.setAttribute(IPDELauncherConstants.TARGET_BUNDLES, explugins.toString()); 183 184 } 185 186 private static TreeMap getStartLevelMap() { 187 TreeMap startLevels = new TreeMap (); 188 Properties props = TargetPlatform.getConfigIniProperties(); 189 if (props != null) { 190 String value = (String )props.get("osgi.bundles"); if (value != null) { 192 StringTokenizer tokenizer = new StringTokenizer (value, ","); while (tokenizer.hasMoreTokens()) { 194 String tokenValue = tokenizer.nextToken(); 195 int index = tokenValue.indexOf("@"); if (index > 0) { 197 String plugin = tokenValue.substring(0,index).trim(); 198 startLevels.put(plugin, getStartValue(tokenValue.substring(index))); 199 } 200 } 201 } 202 } 203 return startLevels; 204 } 205 206 private static String getStartValue(String value) { 207 StringBuffer buffer = new StringBuffer (value); 208 209 StringBuffer result = new StringBuffer ("@"); result.append(":"); 212 int index = value.indexOf("start"); result.append(Boolean.toString(index != -1)); 214 215 if (index != -1) 216 buffer.delete(index, index + 5); 217 218 int colon = value.indexOf(':'); 219 if (colon != -1) 220 buffer.deleteCharAt(colon); 221 222 buffer.deleteCharAt(0); 224 225 try { 226 result.insert(1, Integer.parseInt(buffer.toString().trim())); 227 } catch (NumberFormatException e) { 228 result.insert(1, "default"); } 230 return result.toString(); 231 } 232 233 private String getComputedName(String prefix) { 234 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 235 return lm.generateUniqueLaunchConfigurationNameFrom(prefix); 236 } 237 238 } 239 | Popular Tags |