From: Jan P. <jp...@us...> - 2006-06-01 08:03:08
|
Update of /cvsroot/e-p-i-c/org.epic.debug/src/org/epic/debug In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv19068/src/org/epic/debug Added Files: LaunchShortcut.java Log Message: Implementation of feature request [ 1351860 ] Right-click on runnable script to launch. (Contributed by Katrin Dust) --- NEW FILE: LaunchShortcut.java --- package org.epic.debug; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.ILaunchShortcut; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.part.FileEditorInput; /** * A launch shortcut is capable of launching a selection or active editor in the * workbench. A new launch configuration with default values is created, if an * existing launch configuration can't be re-used. * <p> * A launch shortcut is defined as an extension of type * <code>org.eclipse.debug.ui.launchShortcuts</code>. A shortcut specifies * the perspectives in which is should be available from the "Run/Debug" cascade * menus. * </p> * <p> * A launch shortcut extension is defined in <code>plugin.xml</code>. * Following is an example definition of a launch shortcut extension: * * <pre> * <extension * point="org.eclipse.debug.ui.launchShortcuts"> * <shortcut * category="org.eclipse.jdt.ui.JavaPerspective" * class="org.epic.debug.LaunchShortcut" * icon="icons/epic.gif" * id="org.epic.debug.LaunchShortcut" * label="Perl Local" * modes="run, debug"> * * <contextualLaunch> * <enablement> * <with variable="selection"> * <count value="1"/> * <iterate> * <or> * <test property="org.eclipse.debug.ui.matchesPattern" value="*.pl"/> * <test property="org.eclipse.debug.ui.matchesPattern" value="*.pm"/> * </or> * </iterate> * </with> * </enablement> * </contextualLaunch> * * </shortcut> * </extension> * * </pre> * * @author Katrin Dust * */ public class LaunchShortcut implements ILaunchShortcut { /* * Identifier for local Perl configuration */ private static final String DEBUG_LAUNCH_CONFIGURATION_PERL = "org.epic.debug.launchConfigurationPerl"; /** * Locates a launchable entity in the given selection and launches an * application in the specified mode. * * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, * java.lang.String) * * @param selection * workbench selection * @param mode * one of the launch modes defined by the launch manager * @see org.eclipse.debug.core.ILaunchManager */ public void launch(ISelection selection, String mode) { if (selection instanceof IStructuredSelection) { if (((IStructuredSelection) selection).getFirstElement() instanceof IFile) { launch((IFile) ((IStructuredSelection) selection) .getFirstElement(), mode); } } } /** * Locates a launchable entity in the given active editor, and launches an * application in the specified mode. * * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, * java.lang.String) * * @param editor * the active editor in the workbench * @param mode * one of the launch modes defined by the launch manager * @see org.eclipse.debug.core.ILaunchManager */ public void launch(IEditorPart editor, String mode) { FileEditorInput editorInput = (FileEditorInput) editor.getEditorInput(); launch(editorInput.getFile(), mode); } /** * Launches an application in the specified mode. * * @param bin * @param mode */ private void launch(IFile bin, String mode) { ILaunchConfiguration config = findLaunchConfiguration(bin, mode); if (config != null) { DebugUITools.launch(config, mode); } } /** * If re-usable configuration associated with the File and the project * exist, this configuration is returned. Otherwise a new * configuration is created. * * @param bin * @param mode * @return a re-useable or new config or <code>null</code> if none */ private ILaunchConfiguration findLaunchConfiguration(IFile bin, String mode) { ILaunchConfiguration configuration = null; List candidateConfigs = Collections.EMPTY_LIST; try { ILaunchConfiguration[] configs = DebugPlugin.getDefault() .getLaunchManager().getLaunchConfigurations(); candidateConfigs = new ArrayList(configs.length); for (int i = 0; i < configs.length; i++) { ILaunchConfiguration config = configs[i]; String projectName = config.getAttribute( PerlLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null); String programFile = config.getAttribute( PerlLaunchConfigurationConstants.ATTR_STARTUP_FILE, (String) null); String name = bin.getName(); if (programFile != null && programFile.equals(name)) { if (projectName != null && projectName.equals(bin.getProject().getName())) { candidateConfigs.add(config); } } } } catch (CoreException e) { PerlDebugPlugin.log(e); } // If there are no existing configs associated with the File and the // project, create one. // If there is more then one config associated with the File, return // the first one. int candidateCount = candidateConfigs.size(); if (candidateCount < 1) { configuration = createConfiguration(bin); } else { configuration = (ILaunchConfiguration) candidateConfigs.get(0); } return configuration; } /** * Creates a new configuration associated with the given file. * * @param file * @return ILaunchConfiguration */ private ILaunchConfiguration createConfiguration(IFile file) { ILaunchConfiguration config = null; String projectName = file.getProjectRelativePath().toString(); ILaunchConfigurationType[] configType = getLaunchManager() .getLaunchConfigurationTypes(); ILaunchConfigurationType type = null; for (int i = 0; i < configType.length; i++) { if (configType[i].getIdentifier().equals( DEBUG_LAUNCH_CONFIGURATION_PERL)) { type = configType[i]; } } try { if (type != null) { ILaunchConfigurationWorkingCopy wc = type.newInstance(null, getLaunchManager() .generateUniqueLaunchConfigurationNameFrom( file.getName())); wc.setAttribute( PerlLaunchConfigurationConstants.ATTR_STARTUP_FILE, projectName); wc.setAttribute( PerlLaunchConfigurationConstants.ATTR_PROJECT_NAME, file.getProject().getName()); wc.setAttribute( PerlLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String) null); config = wc.doSave(); } } catch (CoreException e) { PerlDebugPlugin.log(e); } return config; } /** * Method to get the LaunchManager * * @return ILaunchManager */ protected ILaunchManager getLaunchManager() { return DebugPlugin.getDefault().getLaunchManager(); } } |