debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/AbstractSymbianLaunchShortcut.java
branchRCL_2_4
changeset 1168 8e38fdef1cea
parent 953 68b6a294ab01
child 1254 0e45b6356eac
equal deleted inserted replaced
1167:7a7fb0b5dd0b 1168:8e38fdef1cea
     1 package com.nokia.cdt.internal.debug.launch;
     1 package com.nokia.cdt.internal.debug.launch;
     2 
     2 
       
     3 import java.util.ArrayList;
     3 import java.util.List;
     4 import java.util.List;
     4 
     5 
     5 import org.eclipse.cdt.debug.core.executables.Executable;
     6 import org.eclipse.cdt.debug.core.executables.Executable;
     6 import org.eclipse.core.resources.IFile;
     7 import org.eclipse.core.resources.IFile;
     7 import org.eclipse.core.resources.IProject;
     8 import org.eclipse.core.resources.IProject;
     9 import org.eclipse.core.runtime.CoreException;
    10 import org.eclipse.core.runtime.CoreException;
    10 import org.eclipse.core.runtime.IAdaptable;
    11 import org.eclipse.core.runtime.IAdaptable;
    11 import org.eclipse.core.runtime.IPath;
    12 import org.eclipse.core.runtime.IPath;
    12 import org.eclipse.debug.core.ILaunchConfiguration;
    13 import org.eclipse.debug.core.ILaunchConfiguration;
    13 import org.eclipse.debug.ui.DebugUITools;
    14 import org.eclipse.debug.ui.DebugUITools;
       
    15 import org.eclipse.debug.ui.IDebugModelPresentation;
    14 import org.eclipse.debug.ui.ILaunchShortcut2;
    16 import org.eclipse.debug.ui.ILaunchShortcut2;
    15 import org.eclipse.jface.viewers.ISelection;
    17 import org.eclipse.jface.viewers.ISelection;
    16 import org.eclipse.jface.viewers.IStructuredSelection;
    18 import org.eclipse.jface.viewers.IStructuredSelection;
       
    19 import org.eclipse.jface.window.Window;
    17 import org.eclipse.ui.IEditorInput;
    20 import org.eclipse.ui.IEditorInput;
    18 import org.eclipse.ui.IEditorPart;
    21 import org.eclipse.ui.IEditorPart;
    19 import org.eclipse.ui.IFileEditorInput;
    22 import org.eclipse.ui.IFileEditorInput;
       
    23 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
    20 
    24 
    21 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
    25 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    26 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
    22 
    27 
    23 public abstract class AbstractSymbianLaunchShortcut implements ILaunchShortcut2 {
    28 public abstract class AbstractSymbianLaunchShortcut implements ILaunchShortcut2 {
    24 
    29 
    25 	protected abstract void launchProject(IProject project, Executable executable, IPath defaultMMP, String mode);
    30 	protected abstract void launchProject(IProject project, Executable executable, IPath defaultMMP, String mode);
    26 
    31 	
       
    32 	/**
       
    33 	 * Override to tell whether this existing configuration matches the type of
       
    34 	 * one the shortcut would create. The default implementation returns true
       
    35 	 * for all configurations.
       
    36 	 */
       
    37 	protected boolean isSupportedConfiguration(ILaunchConfiguration config) throws CoreException {
       
    38 		return true;
       
    39 	}
       
    40  	
    27 	public void launch(IEditorPart editor, String mode) {
    41 	public void launch(IEditorPart editor, String mode) {
    28 		// launch an existing config if one exists
    42 		// launch an existing config if one exists
    29 		ILaunchConfiguration[] configs = getLaunchConfigurations(editor);
    43 		ILaunchConfiguration[] configs = getLaunchConfigurations(editor);
    30 		if (configs.length > 0) {
    44 		if (configs.length > 0) {
    31 			// just launch the first one that supports the mode
    45 			// just launch the first one that supports the mode
    32 			for (ILaunchConfiguration config : configs) {
    46 			for (ILaunchConfiguration config : configs) {
    33 				try {
    47 				try {
    34 					if (config.supportsMode(mode)) {
    48 					if (config.supportsMode(mode) && isSupportedConfiguration(config)) {
    35 						DebugUITools.launch(configs[0], mode);
    49 						DebugUITools.launch(configs[0], mode);
    36 						return;
    50 						return;
    37 					}
    51 					}
    38 				} catch (CoreException e) {
    52 				} catch (CoreException e) {
    39 					e.printStackTrace();
    53 					e.printStackTrace();
    53 	public void launch(ISelection selection, String mode) {
    67 	public void launch(ISelection selection, String mode) {
    54 
    68 
    55 		// launch an existing config if one exists
    69 		// launch an existing config if one exists
    56 		ILaunchConfiguration[] configs = getLaunchConfigurations(selection);
    70 		ILaunchConfiguration[] configs = getLaunchConfigurations(selection);
    57 		if (configs.length > 0) {
    71 		if (configs.length > 0) {
    58 			// just launch the first one that supports the mode
    72 			// find all the ones that support the mode and shortcut (#11013)
    59 			for (ILaunchConfiguration config : configs) {
    73 			List<ILaunchConfiguration> matches = new ArrayList<ILaunchConfiguration>();
       
    74 			for (int i = 0; i < configs.length; i++) {
       
    75 				ILaunchConfiguration config = configs[i];
    60 				try {
    76 				try {
    61 					if (config.supportsMode(mode)) {
    77 					if (config.supportsMode(mode) && isSupportedConfiguration(config)) {
    62 						DebugUITools.launch(configs[0], mode);
    78 						matches.add(config);
    63 						return;
       
    64 					}
    79 					}
    65 				} catch (CoreException e) {
    80 				} catch (CoreException e) {
    66 					e.printStackTrace();
    81 					e.printStackTrace();
    67 				}
    82 				}
       
    83 			}
       
    84 			// if only one matches, just launch
       
    85 			if (matches.size() > 0) {
       
    86 				if (matches.size() == 1 || WorkbenchUtils.isJUnitRunning()) {
       
    87 					DebugUITools.launch(matches.get(0), mode);
       
    88 				} else {
       
    89 					// else, ask the user
       
    90 					ILaunchConfiguration selected = chooseConfiguration(matches, mode);
       
    91 					if (selected != null)
       
    92 						DebugUITools.launch(selected, mode);
       
    93 				}
       
    94 				return;
    68 			}
    95 			}
    69 		}
    96 		}
    70 		
    97 		
    71 		IPath defaultMMP = null;
    98 		IPath defaultMMP = null;
    72 		boolean launched = false;
    99 		boolean launched = false;
    84 			{
   111 			{
    85 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
   112 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
    86 				if (file != null)
   113 				if (file != null)
    87 				{
   114 				{
    88 					IPath filePath = file.getLocation();
   115 					IPath filePath = file.getLocation();
    89 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension()))
   116 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension())) //$NON-NLS-1$
    90 					{
   117 					{
    91 						defaultMMP = filePath;
   118 						defaultMMP = filePath;
    92 					}
   119 					}
    93 					else
   120 					else
    94 					{		
   121 					{		
   106 			if (projects.size() > 0) {
   133 			if (projects.size() > 0) {
   107 				launchProject(projects.get(0), executable, defaultMMP, mode);
   134 				launchProject(projects.get(0), executable, defaultMMP, mode);
   108 			}
   135 			}
   109 		}
   136 		}
   110 	}
   137 	}
       
   138 	
       
   139 	/**
       
   140 	 * Show a selection dialog that allows the user to choose one of the specified
       
   141 	 * launch configurations.  Return the chosen config, or <code>null</code> if the
       
   142 	 * user cancelled the dialog.
       
   143 	 */
       
   144 	protected ILaunchConfiguration chooseConfiguration(List<ILaunchConfiguration> configList, String mode) {
       
   145 		IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
       
   146 		ElementListSelectionDialog dialog = new ElementListSelectionDialog(WorkbenchUtils.getSafeShell(), labelProvider);
       
   147 		dialog.setElements(configList.toArray());
       
   148 		dialog.setTitle(Messages.getString("AbstractSymbianLaunchShortcut.ChooseConfigTitle"));  //$NON-NLS-1$
       
   149 		dialog.setMessage(Messages.getString("AbstractSymbianLaunchShortcut.ChooseConfigLabel"));  //$NON-NLS-1$
       
   150 		dialog.setMultipleSelection(false);
       
   151 		int result = dialog.open();
       
   152 		labelProvider.dispose();
       
   153 		if (result == Window.OK) {
       
   154 			return (ILaunchConfiguration) dialog.getFirstResult();
       
   155 		}
       
   156 		return null;
       
   157 	}
       
   158 
   111 
   159 
   112 	public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
   160 	public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
   113 		IPath defaultMMP = null;
   161 		IPath defaultMMP = null;
   114 		if (selection instanceof IStructuredSelection) {
   162 		if (selection instanceof IStructuredSelection) {
   115 			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
   163 			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
   122 			{
   170 			{
   123 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
   171 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
   124 				if (file != null)
   172 				if (file != null)
   125 				{
   173 				{
   126 					IPath filePath = file.getLocation();
   174 					IPath filePath = file.getLocation();
   127 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension()))
   175 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension())) //$NON-NLS-1$
   128 					{
   176 					{
   129 						defaultMMP = filePath;
   177 						defaultMMP = filePath;
   130 					}
   178 					}
   131 					Executable executable = new Executable(file.getLocation(), file.getProject(), file);
   179 					Executable executable = new Executable(file.getLocation(), file.getProject(), file);
   132 					return LaunchPlugin.getDefault().getLaunchConfigurations(file.getProject(), executable, defaultMMP);
   180 					return LaunchPlugin.getDefault().getLaunchConfigurations(file.getProject(), executable, defaultMMP);