1 11 package org.eclipse.debug.internal.ui.contextlaunching; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.ListIterator ; 17 import java.util.Set ; 18 19 import org.eclipse.core.expressions.EvaluationContext; 20 import org.eclipse.core.expressions.IEvaluationContext; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.ILaunchConfiguration; 26 import org.eclipse.debug.core.ILaunchMode; 27 import org.eclipse.debug.internal.ui.DebugUIPlugin; 28 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 29 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; 30 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationSelectionDialog; 31 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension; 32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutSelectionDialog; 33 import org.eclipse.debug.internal.ui.stringsubstitution.SelectedResourceManager; 34 import org.eclipse.debug.ui.DebugUITools; 35 import org.eclipse.debug.ui.ILaunchGroup; 36 import org.eclipse.jface.dialogs.IDialogConstants; 37 import org.eclipse.jface.dialogs.MessageDialog; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 import org.eclipse.jface.window.Window; 40 import org.eclipse.ui.activities.WorkbenchActivityHelper; 41 42 import com.ibm.icu.text.MessageFormat; 43 44 56 public final class ContextRunner { 57 58 61 private static ContextRunner fgInstance = null; 62 63 67 public static ContextRunner getDefault() { 68 if(fgInstance == null) { 69 fgInstance = new ContextRunner(); 70 } 71 return fgInstance; 72 } 73 74 78 public void launch(ILaunchGroup group) { 79 IResource resource = SelectedResourceManager.getDefault().getSelectedResource(); 80 if(resource != null) { 82 selectAndLaunch(resource, group); 83 return; 84 } 85 if(!launchLast(group)) { 87 List shortcuts = getLaunchShortcutsForEmptySelection(); 89 if(!shortcuts.isEmpty()) { 90 showShortcutSelectionDialog(resource, shortcuts, group.getMode()); 91 } 92 else { 93 MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, ContextMessages.ContextRunner_7); 94 } 95 } 96 } 97 98 103 protected boolean launchLast(ILaunchGroup group) { 104 ILaunchConfiguration config = null; 105 if(group != null) { 106 config = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getFilteredLastLaunch(group.getIdentifier()); 107 } 108 if(config != null) { 109 launch(config, group.getMode()); 110 return true; 111 } 112 return false; 113 } 114 115 120 public List getLaunchShortcutsForEmptySelection() { 121 List list = new ArrayList (); 122 List sc = getLaunchConfigurationManager().getLaunchShortcuts(); 123 List ctxt = new ArrayList (); 124 IEvaluationContext context = new EvaluationContext(null, ctxt); 125 context.addVariable("selection", ctxt); LaunchShortcutExtension ext = null; 127 for(Iterator iter = sc.iterator(); iter.hasNext();) { 128 ext = (LaunchShortcutExtension) iter.next(); 129 try { 130 if(ext.evalEnablementExpression(context, ext.getContextualLaunchEnablementExpression()) && !WorkbenchActivityHelper.filterItem(ext)) { 131 if(!list.contains(ext)) { 132 list.add(ext); 133 } 134 } 135 } 136 catch(CoreException ce) {} 137 } 138 return list; 139 } 140 141 148 protected void selectAndLaunch(IResource resource, ILaunchGroup group) { 149 if(group == null) { 150 return; 151 } 152 ILaunchConfiguration config = getLaunchConfigurationManager().isSharedConfig(resource); 153 if(config != null) { 154 launch(config, group.getMode()); 155 return; 156 } 157 List configs = getLaunchConfigurationManager().getApplicableLaunchConfigurations(resource); 158 configs = getConfigsApplicableToMode(configs, group.getMode()); 159 int csize = configs.size(); 160 if(csize == 1) { 161 launch((ILaunchConfiguration) configs.get(0), group.getMode()); 162 return; 163 } 164 if(csize < 1) { 165 List exts = getLaunchConfigurationManager().getLaunchShortcuts(resource); 166 List modeSpecificExts = getLaunchConfigurationManager().getShortcutsSupportingMode(exts, group.getMode()); 167 int esize = modeSpecificExts.size(); 168 if(esize == 1) { 169 LaunchShortcutExtension ext = (LaunchShortcutExtension) modeSpecificExts.get(0); 170 ext.launch(new StructuredSelection(resource), group.getMode()); 171 return; 172 } 173 if(esize > 1) { 174 showShortcutSelectionDialog(resource, modeSpecificExts, group.getMode()); 175 return; 176 } 177 if(esize < 1) { 178 if(DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_LAUNCH_LAST_IF_NOT_LAUNCHABLE)) { 179 if(!launchLast(group)) { 180 MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, ContextMessages.ContextRunner_7); 181 } 182 return; 183 } 184 if (exts.size() > 0) { 185 ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(group.getMode()); 187 if (launchMode == null) { 188 DebugUIPlugin.logErrorMessage("Unsupported launch mode: " + group.getMode()); } else { 190 String label = launchMode.getLabel(); 191 String modeLabel = DebugUIPlugin.removeAccelerators(label); 192 MessageDialog.openInformation(DebugUIPlugin.getShell(), MessageFormat.format(ContextMessages.ContextRunner_1, new String []{modeLabel}), 193 MessageFormat.format(ContextMessages.ContextRunner_2, new String []{modeLabel.toLowerCase()})); 194 } 195 } else { 196 IProject project = resource.getProject(); 197 if(project != null && !project.equals(resource)) { 198 selectAndLaunch(project, group); 199 } 200 else { 201 String msg = ContextMessages.ContextRunner_7; 202 if(!resource.isAccessible()) { 203 msg = MessageFormat.format(ContextMessages.ContextRunner_13, new String [] {resource.getName()}); 204 } 205 MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, msg); 206 } 207 } 208 } 209 } 210 else if(csize > 1){ 211 config = getLaunchConfigurationManager().getMRUConfiguration(configs, group, resource); 212 if(config != null) { 213 launch(config, group.getMode()); 214 } else { 215 showConfigurationSelectionDialog(configs, group.getMode()); 216 } 217 } 218 } 219 220 226 private void launch(ILaunchConfiguration configuration, String mode) { 227 if (validateMode(configuration, mode)) { 228 DebugUITools.launch(configuration, mode); 229 } 230 } 231 232 240 private boolean validateMode(ILaunchConfiguration configuration, String mode) { 241 try { 242 if (configuration.getModes().isEmpty()) { 245 if (!configuration.supportsMode(mode)) { 246 ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(mode); 247 if (launchMode == null) { 248 DebugUIPlugin.logErrorMessage("Unsupported launch mode: " + mode); } else { 250 String label = launchMode.getLabel(); 251 String modeLabel = DebugUIPlugin.removeAccelerators(label); 252 MessageDialog.openInformation(DebugUIPlugin.getShell(), MessageFormat.format(ContextMessages.ContextRunner_1, new String []{modeLabel}), 253 MessageFormat.format(ContextMessages.ContextRunner_3, new String []{configuration.getName(), modeLabel.toLowerCase()})); 254 } 255 return false; 256 } 257 } 258 } catch (CoreException e) { 259 DebugUIPlugin.log(e.getStatus()); 260 return false; 261 } 262 return true; 263 } 264 265 272 protected void showConfigurationSelectionDialog(List configurations, String mode) { 273 LaunchConfigurationSelectionDialog lsd = new LaunchConfigurationSelectionDialog(DebugUIPlugin.getShell()); 274 if(configurations != null) { 275 lsd.setInput(configurations); 276 } 277 if(lsd.open() == IDialogConstants.OK_ID) { 278 ILaunchConfiguration config = (ILaunchConfiguration) lsd.getResult()[0]; 279 launch(config, mode); 280 } 281 } 282 283 290 protected void showShortcutSelectionDialog(IResource resource, List shortcuts, String mode) { 291 LaunchShortcutSelectionDialog dialog = new LaunchShortcutSelectionDialog(resource, mode); 292 dialog.setInput(shortcuts); 293 if (dialog.open() == Window.OK) { 294 Object [] result = dialog.getResult(); 295 if(result.length > 0) { 296 LaunchShortcutExtension method = (LaunchShortcutExtension) result[0]; 297 if(method != null) { 298 method.launch((resource == null ? new StructuredSelection() : new StructuredSelection(resource)), mode); 299 } 300 } 301 } 302 } 303 304 308 protected LaunchConfigurationManager getLaunchConfigurationManager() { 309 return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); 310 } 311 312 318 private List getConfigsApplicableToMode(List configs, String mode) { 319 ArrayList applicable = new ArrayList (configs); 320 ListIterator iterator = applicable.listIterator(); 321 while (iterator.hasNext()) { 322 ILaunchConfiguration config = (ILaunchConfiguration) iterator.next(); 323 try { 324 Set modes = config.getModes(); 325 modes.add(mode); 326 if (!config.getType().supportsModeCombination(modes)) { 327 iterator.remove(); 328 } 329 } 330 catch (CoreException e) {} 331 } 332 return applicable; 333 } 334 } 335 | Popular Tags |