org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/ui/launch/WidgetLaunchShortcut.java
changeset 2 e4420d2515f1
child 313 25282116ebcf
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  *******************************************************************************/
       
    19 package org.symbian.tools.wrttools.debug.ui.launch;
       
    20 
       
    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.core.runtime.IAdaptable;
       
    25 import org.eclipse.debug.core.DebugPlugin;
       
    26 import org.eclipse.debug.core.ILaunchConfiguration;
       
    27 import org.eclipse.debug.core.ILaunchConfigurationType;
       
    28 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
       
    29 import org.eclipse.debug.core.ILaunchManager;
       
    30 import org.eclipse.debug.ui.DebugUITools;
       
    31 import org.eclipse.debug.ui.ILaunchShortcut2;
       
    32 import org.eclipse.jface.viewers.ISelection;
       
    33 import org.eclipse.jface.viewers.IStructuredSelection;
       
    34 import org.eclipse.ui.IEditorInput;
       
    35 import org.eclipse.ui.IEditorPart;
       
    36 import org.symbian.tools.wrttools.debug.internal.Activator;
       
    37 import org.symbian.tools.wrttools.debug.internal.IConstants;
       
    38 import org.symbian.tools.wrttools.debug.internal.launch.WidgetLaunchDelegate;
       
    39 
       
    40 public class WidgetLaunchShortcut implements ILaunchShortcut2 {
       
    41 
       
    42 	public IResource getLaunchableResource(IEditorPart editorpart) {
       
    43 		IEditorInput input = editorpart.getEditorInput();
       
    44 		return (IResource) input.getAdapter(IResource.class);
       
    45 	}
       
    46 
       
    47 	public IResource getLaunchableResource(ISelection selection) {
       
    48 		if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
       
    49 			Object object = ((IStructuredSelection) selection)
       
    50 					.getFirstElement();
       
    51 			IResource resource = null;
       
    52 			if (object instanceof IResource) {
       
    53 				resource = (IResource) object;
       
    54 			} else if (object instanceof IAdaptable) {
       
    55 				resource = (IResource) ((IAdaptable) object)
       
    56 						.getAdapter(IResource.class);
       
    57 			}
       
    58 			return resource;
       
    59 		}
       
    60 		return null;
       
    61 	}
       
    62 
       
    63 	public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) {
       
    64 		return getLaunchConfigurations(getLaunchableResource(editorpart));
       
    65 	}
       
    66 
       
    67 	private ILaunchConfiguration getLaunchConfigurations(IProject project)
       
    68 			throws CoreException {
       
    69 		ILaunchManager launchManager = DebugPlugin.getDefault()
       
    70 				.getLaunchManager();
       
    71 		ILaunchConfigurationType type = launchManager
       
    72 				.getLaunchConfigurationType(WidgetLaunchDelegate.ID);
       
    73 		ILaunchConfiguration configuration = null;
       
    74 		ILaunchConfiguration[] configurations = launchManager
       
    75 				.getLaunchConfigurations(type);
       
    76 		for (ILaunchConfiguration c : configurations) {
       
    77 			if (project.getName()
       
    78 					.equals(
       
    79 							c.getAttribute(IConstants.PROP_PROJECT_NAME,
       
    80 									(String) null))) {
       
    81 				configuration = c;
       
    82 				break;
       
    83 			}
       
    84 		}
       
    85 		return configuration;
       
    86 	}
       
    87 
       
    88 	private ILaunchConfiguration[] getLaunchConfigurations(IResource resource) {
       
    89 		if (resource != null) {
       
    90 			try {
       
    91 				ILaunchConfiguration launchConfigurations = getLaunchConfigurations(resource
       
    92 						.getProject());
       
    93 				if (launchConfigurations != null) {
       
    94 					return new ILaunchConfiguration[] { launchConfigurations };
       
    95 				}
       
    96 			} catch (CoreException e) {
       
    97 				Activator.log(e);
       
    98 			}
       
    99 		}
       
   100 		return null;
       
   101 	}
       
   102 
       
   103 	public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
       
   104 		return getLaunchConfigurations(getLaunchableResource(selection));
       
   105 	}
       
   106 
       
   107 	public void launch(IEditorPart editor, String mode) {
       
   108 		launch(getLaunchableResource(editor), mode);
       
   109 	}
       
   110 
       
   111 	private void launch(IResource launchableResource, String mode) {
       
   112 		try {
       
   113 			IProject project = launchableResource.getProject();
       
   114 			ILaunchManager launchManager = DebugPlugin.getDefault()
       
   115 					.getLaunchManager();
       
   116 			ILaunchConfigurationType type = launchManager
       
   117 					.getLaunchConfigurationType(WidgetLaunchDelegate.ID);
       
   118 			ILaunchConfiguration configuration = getLaunchConfigurations(project);
       
   119 			if (configuration == null) {
       
   120 				ILaunchConfigurationWorkingCopy copy = type
       
   121 						.newInstance(
       
   122 								null,
       
   123 								launchManager
       
   124 										.generateUniqueLaunchConfigurationNameFrom(project
       
   125 												.getName()));
       
   126 				copy.setAttribute(IConstants.PROP_PROJECT_NAME, project
       
   127 						.getName());
       
   128 				copy.setMappedResources(new IResource[] {project});
       
   129 				configuration = copy.doSave();
       
   130 			}
       
   131 			DebugUITools.launch(configuration, mode);
       
   132 		} catch (CoreException e) {
       
   133 			Activator.log(e);
       
   134 		}
       
   135 	}
       
   136 
       
   137 	public void launch(ISelection selection, String mode) {
       
   138 		launch(getLaunchableResource(selection), mode);
       
   139 	}
       
   140 }