plugins/org.symbian.tools.tmw.debug/src/org/symbian/tools/wrttools/debug/ui/launch/WidgetLaunchShortcut.java
changeset 471 06589bf52fa7
parent 470 d4809db37847
child 472 bd9f2d7c64a6
equal deleted inserted replaced
470:d4809db37847 471:06589bf52fa7
     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).getFirstElement();
       
    50             IResource resource = null;
       
    51             if (object instanceof IResource) {
       
    52                 resource = (IResource) object;
       
    53             } else if (object instanceof IAdaptable) {
       
    54                 resource = (IResource) ((IAdaptable) object).getAdapter(IResource.class);
       
    55             }
       
    56             return resource;
       
    57         }
       
    58         return null;
       
    59     }
       
    60 
       
    61     public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) {
       
    62         return getLaunchConfigurations(getLaunchableResource(editorpart));
       
    63     }
       
    64 
       
    65     private ILaunchConfiguration getLaunchConfigurations(IProject project) throws CoreException {
       
    66         ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
       
    67         ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(WidgetLaunchDelegate.ID);
       
    68         ILaunchConfiguration configuration = null;
       
    69         ILaunchConfiguration[] configurations = launchManager.getLaunchConfigurations(type);
       
    70         for (ILaunchConfiguration c : configurations) {
       
    71             if (project.getName().equals(c.getAttribute(IConstants.PROP_PROJECT_NAME, (String) null))) {
       
    72                 configuration = c;
       
    73                 break;
       
    74             }
       
    75         }
       
    76         return configuration;
       
    77     }
       
    78 
       
    79     private ILaunchConfiguration[] getLaunchConfigurations(IResource resource) {
       
    80         if (resource != null) {
       
    81             try {
       
    82                 ILaunchConfiguration launchConfigurations = getLaunchConfigurations(resource.getProject());
       
    83                 if (launchConfigurations != null) {
       
    84                     return new ILaunchConfiguration[] { launchConfigurations };
       
    85                 }
       
    86             } catch (CoreException e) {
       
    87                 Activator.log(e);
       
    88             }
       
    89         }
       
    90         return null;
       
    91     }
       
    92 
       
    93     public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
       
    94         return getLaunchConfigurations(getLaunchableResource(selection));
       
    95     }
       
    96 
       
    97     public void launch(IEditorPart editor, String mode) {
       
    98         launch(getLaunchableResource(editor), mode);
       
    99     }
       
   100 
       
   101     private void launch(IResource launchableResource, String mode) {
       
   102         try {
       
   103             IProject project = launchableResource.getProject();
       
   104             ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
       
   105             ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(WidgetLaunchDelegate.ID);
       
   106             ILaunchConfiguration configuration = getLaunchConfigurations(project);
       
   107             if (configuration == null) {
       
   108                 ILaunchConfigurationWorkingCopy copy = type.newInstance(null, launchManager
       
   109                         .generateLaunchConfigurationName(project.getName()));
       
   110                 copy.setAttribute(IConstants.PROP_PROJECT_NAME, project.getName());
       
   111                 copy.setMappedResources(new IResource[] { project });
       
   112                 configuration = copy.doSave();
       
   113             }
       
   114             DebugUITools.launch(configuration, mode);
       
   115         } catch (CoreException e) {
       
   116             Activator.log(e);
       
   117         }
       
   118     }
       
   119 
       
   120     public void launch(ISelection selection, String mode) {
       
   121         launch(getLaunchableResource(selection), mode);
       
   122     }
       
   123 }