debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/AbstractSymbianLaunchShortcut.java
changeset 956 d1e221a2875f
child 994 1ffe668575e7
child 1168 8e38fdef1cea
equal deleted inserted replaced
948:ee68c935ffb7 956:d1e221a2875f
       
     1 package com.nokia.cdt.internal.debug.launch;
       
     2 
       
     3 import java.util.List;
       
     4 
       
     5 import org.eclipse.cdt.debug.core.executables.Executable;
       
     6 import org.eclipse.core.resources.IFile;
       
     7 import org.eclipse.core.resources.IProject;
       
     8 import org.eclipse.core.resources.IResource;
       
     9 import org.eclipse.core.runtime.CoreException;
       
    10 import org.eclipse.core.runtime.IAdaptable;
       
    11 import org.eclipse.core.runtime.IPath;
       
    12 import org.eclipse.debug.core.ILaunchConfiguration;
       
    13 import org.eclipse.debug.ui.DebugUITools;
       
    14 import org.eclipse.debug.ui.ILaunchShortcut2;
       
    15 import org.eclipse.jface.viewers.ISelection;
       
    16 import org.eclipse.jface.viewers.IStructuredSelection;
       
    17 import org.eclipse.ui.IEditorInput;
       
    18 import org.eclipse.ui.IEditorPart;
       
    19 import org.eclipse.ui.IFileEditorInput;
       
    20 
       
    21 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    22 
       
    23 public abstract class AbstractSymbianLaunchShortcut implements ILaunchShortcut2 {
       
    24 
       
    25 	protected abstract void launchProject(IProject project, Executable executable, IPath defaultMMP, String mode);
       
    26 
       
    27 	public void launch(IEditorPart editor, String mode) {
       
    28 		// launch an existing config if one exists
       
    29 		ILaunchConfiguration[] configs = getLaunchConfigurations(editor);
       
    30 		if (configs.length > 0) {
       
    31 			// just launch the first one that supports the mode
       
    32 			for (ILaunchConfiguration config : configs) {
       
    33 				try {
       
    34 					if (config.supportsMode(mode)) {
       
    35 						DebugUITools.launch(configs[0], mode);
       
    36 						return;
       
    37 					}
       
    38 				} catch (CoreException e) {
       
    39 					e.printStackTrace();
       
    40 				}
       
    41 			}
       
    42 		}
       
    43 
       
    44 		IEditorInput editorInput = editor.getEditorInput();
       
    45 		if (editorInput instanceof IFileEditorInput) {
       
    46 			IFile file = ((IFileEditorInput)editorInput).getFile();
       
    47 			if (file != null) {
       
    48 				launchProject(file.getProject(), null, null, mode);
       
    49 			}
       
    50 		}
       
    51 	}
       
    52 
       
    53 	public void launch(ISelection selection, String mode) {
       
    54 
       
    55 		// launch an existing config if one exists
       
    56 		ILaunchConfiguration[] configs = getLaunchConfigurations(selection);
       
    57 		if (configs.length > 0) {
       
    58 			// just launch the first one that supports the mode
       
    59 			for (ILaunchConfiguration config : configs) {
       
    60 				try {
       
    61 					if (config.supportsMode(mode)) {
       
    62 						DebugUITools.launch(configs[0], mode);
       
    63 						return;
       
    64 					}
       
    65 				} catch (CoreException e) {
       
    66 					e.printStackTrace();
       
    67 				}
       
    68 			}
       
    69 		}
       
    70 		
       
    71 		IPath defaultMMP = null;
       
    72 		boolean launched = false;
       
    73 		Executable executable = null;
       
    74 		
       
    75 		if (selection instanceof IStructuredSelection) {
       
    76 			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
       
    77 			if (firstElement != null && firstElement instanceof Executable)
       
    78 			{
       
    79 				launchProject(((Executable)firstElement).getProject(), (Executable)firstElement, defaultMMP, mode);
       
    80 				launched = true;
       
    81 			}
       
    82 			else
       
    83 			if (firstElement != null && firstElement instanceof IAdaptable)
       
    84 			{
       
    85 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
       
    86 				if (file != null)
       
    87 				{
       
    88 					IPath filePath = file.getLocation();
       
    89 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension()))
       
    90 					{
       
    91 						defaultMMP = filePath;
       
    92 					}
       
    93 					else
       
    94 					{		
       
    95 						executable = new Executable(file.getLocation(), file.getProject(), file);
       
    96 						launchProject(file.getProject(), executable, defaultMMP, mode);
       
    97 						launched = true;					
       
    98 					}
       
    99 				}
       
   100 			}
       
   101 		}
       
   102 		
       
   103 		if (!launched)
       
   104 		{
       
   105 			List<IProject> projects = CarbideBuilderPlugin.getProjectsFromSelection(selection);		
       
   106 			if (projects.size() > 0) {
       
   107 				launchProject(projects.get(0), executable, defaultMMP, mode);
       
   108 			}
       
   109 		}
       
   110 	}
       
   111 
       
   112 	public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
       
   113 		IPath defaultMMP = null;
       
   114 		if (selection instanceof IStructuredSelection) {
       
   115 			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
       
   116 			if (firstElement != null && firstElement instanceof Executable)
       
   117 			{
       
   118 				return LaunchPlugin.getDefault().getLaunchConfigurations(((Executable)firstElement).getProject(), (Executable)firstElement, defaultMMP);
       
   119 			}
       
   120 			else
       
   121 			if (firstElement != null && firstElement instanceof IAdaptable)
       
   122 			{
       
   123 				IFile file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
       
   124 				if (file != null)
       
   125 				{
       
   126 					IPath filePath = file.getLocation();
       
   127 					if ("mmp".equalsIgnoreCase(filePath.getFileExtension()))
       
   128 					{
       
   129 						defaultMMP = filePath;
       
   130 					}
       
   131 					Executable executable = new Executable(file.getLocation(), file.getProject(), file);
       
   132 					return LaunchPlugin.getDefault().getLaunchConfigurations(file.getProject(), executable, defaultMMP);
       
   133 				}
       
   134 			}
       
   135 		}
       
   136 		
       
   137 		List<IProject> projects = CarbideBuilderPlugin.getProjectsFromSelection(selection);		
       
   138 		if (projects.size() > 0) {
       
   139 			return LaunchPlugin.getDefault().getLaunchConfigurations(projects.get(0), null, defaultMMP);
       
   140 		}
       
   141 		return null;
       
   142 	}
       
   143 
       
   144 	public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) {
       
   145 		IEditorInput editorInput = editorpart.getEditorInput();
       
   146 		if (editorInput instanceof IFileEditorInput) {
       
   147 			IFile file = ((IFileEditorInput)editorInput).getFile();
       
   148 			if (file != null) {
       
   149 				return LaunchPlugin.getDefault().getLaunchConfigurations(file.getProject(), null, null);
       
   150 			}
       
   151 		}
       
   152 		return null;
       
   153 	}
       
   154 
       
   155 	public IResource getLaunchableResource(ISelection selection) {
       
   156 		if (selection instanceof IStructuredSelection) {
       
   157 			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
       
   158 			if (firstElement != null && firstElement instanceof IFile)
       
   159 			{
       
   160 				IFile file = (IFile) firstElement;
       
   161 				return file.getProject();
       
   162 			}
       
   163 			if (firstElement != null && firstElement instanceof Executable)
       
   164 			{
       
   165 				return ((Executable)firstElement).getProject();
       
   166 			}
       
   167 		}
       
   168 		List<IProject> projects = CarbideBuilderPlugin.getProjectsFromSelection(selection);		
       
   169 		if (projects.size() > 0) {
       
   170 			return projects.get(0);
       
   171 		}
       
   172 		return null;
       
   173 	}
       
   174 
       
   175 	public IResource getLaunchableResource(IEditorPart editorpart) {
       
   176 		IEditorInput editorInput = editorpart.getEditorInput();
       
   177 		if (editorInput instanceof IFileEditorInput) {
       
   178 			IFile file = ((IFileEditorInput)editorInput).getFile();
       
   179 			if (file != null) {
       
   180 				return file.getProject();
       
   181 			}
       
   182 		}
       
   183 		return null;
       
   184 	}
       
   185 }