1 11 package org.eclipse.pde.ui.launcher; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.debug.core.DebugPlugin; 17 import org.eclipse.debug.core.ILaunchConfiguration; 18 import org.eclipse.debug.core.ILaunchConfigurationType; 19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 20 import org.eclipse.debug.core.ILaunchManager; 21 import org.eclipse.debug.ui.DebugUITools; 22 import org.eclipse.debug.ui.IDebugModelPresentation; 23 import org.eclipse.debug.ui.ILaunchShortcut; 24 import org.eclipse.jface.window.Window; 25 import org.eclipse.pde.internal.ui.PDEPlugin; 26 import org.eclipse.pde.internal.ui.PDEUIMessages; 27 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 28 29 36 public abstract class AbstractLaunchShortcut implements ILaunchShortcut { 37 38 47 protected void launch(String mode) { 48 ILaunchConfiguration configuration = findLaunchConfiguration(mode); 49 if (configuration != null) 50 DebugUITools.launch(configuration, mode); 51 } 52 53 69 protected ILaunchConfiguration findLaunchConfiguration(String mode) { 70 ILaunchConfiguration[] configs = getLaunchConfigurations(); 71 ILaunchConfiguration configuration = null; 72 if (configs.length == 0) { 73 configuration = createNewConfiguration(); 74 } else if (configs.length == 1) { 75 configuration = configs[0]; 76 } else { 77 configuration = chooseConfiguration(configs, mode); 78 } 79 return configuration; 80 } 81 82 88 private ILaunchConfiguration[] getLaunchConfigurations() { 89 ArrayList result = new ArrayList (); 90 try { 91 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 92 ILaunchConfigurationType type = manager.getLaunchConfigurationType(getLaunchConfigurationTypeName()); 93 ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type); 94 for (int i = 0; i < configurations.length; i++) { 95 if (!DebugUITools.isPrivate(configurations[i]) && isGoodMatch(configurations[i])) { 96 result.add(configurations[i]); 97 } 98 } 99 } catch (CoreException e) { 100 } 101 return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]); 102 } 103 104 114 protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String mode) { 115 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); 116 ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider); 117 dialog.setElements(configs); 118 dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title); 119 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 120 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug); 121 } else { 122 dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run); 123 } 124 dialog.setMultipleSelection(false); 125 int result = dialog.open(); 126 labelProvider.dispose(); 127 return (result == Window.OK) ? (ILaunchConfiguration)dialog.getFirstResult() : null; 128 } 129 130 135 private ILaunchConfiguration createNewConfiguration() { 136 try { 137 ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); 138 ILaunchConfigurationType type = lm.getLaunchConfigurationType(getLaunchConfigurationTypeName()); 139 String name = lm.generateUniqueLaunchConfigurationNameFrom(getName(type)); 140 ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name); 141 initializeConfiguration(wc); 142 return wc.doSave(); 143 } catch (CoreException ce) { 144 PDEPlugin.logException(ce); 145 } 146 return null; 147 } 148 149 154 protected String getName(ILaunchConfigurationType type) { 155 return type.getName(); 156 } 157 158 167 protected abstract void initializeConfiguration(ILaunchConfigurationWorkingCopy wc); 168 169 175 protected abstract String getLaunchConfigurationTypeName(); 176 177 188 protected abstract boolean isGoodMatch(ILaunchConfiguration configuration); 189 190 } 191 | Popular Tags |