• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.launch;
18 
19 import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
20 import com.android.ide.eclipse.adt.internal.sdk.Sdk;
21 
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.ILaunchShortcut;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.PlatformUI;
33 
34 /**
35  * Launch shortcut to launch debug/run configuration directly.
36  */
37 public class LaunchShortcut implements ILaunchShortcut {
38 
39 
40     /* (non-Javadoc)
41      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
42      * org.eclipse.jface.viewers.ISelection, java.lang.String)
43      */
44     @Override
launch(ISelection selection, String mode)45     public void launch(ISelection selection, String mode) {
46         if (selection instanceof IStructuredSelection) {
47 
48             // get the object and the project from it
49             IStructuredSelection structSelect = (IStructuredSelection)selection;
50             Object o = structSelect.getFirstElement();
51 
52             // get the first (and normally only) element
53             if (o instanceof IAdaptable) {
54                 IResource r = (IResource)((IAdaptable)o).getAdapter(IResource.class);
55 
56                 // get the project from the resource
57                 if (r != null) {
58                     IProject project = r.getProject();
59 
60                     if (project != null)  {
61                         ProjectState state = Sdk.getProjectState(project);
62                         if (state != null && state.isLibrary()) {
63 
64                             MessageDialog.openError(
65                                     PlatformUI.getWorkbench().getDisplay().getActiveShell(),
66                                     "Android Launch",
67                                     "Android library projects cannot be launched.");
68                         } else{
69                             // and launch
70                             launch(project, mode);
71                         }
72                     }
73                 }
74             }
75         }
76     }
77 
78     /* (non-Javadoc)
79      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
80      * org.eclipse.ui.IEditorPart, java.lang.String)
81      */
82     @Override
launch(IEditorPart editor, String mode)83     public void launch(IEditorPart editor, String mode) {
84         // since we force the shortcut to only work on selection in the
85         // package explorer, this will never be called.
86     }
87 
88 
89     /**
90      * Launch a config for the specified project.
91      * @param project The project to launch
92      * @param mode The launch mode ("debug", "run" or "profile")
93      */
launch(IProject project, String mode)94     private void launch(IProject project, String mode) {
95         // get an existing or new launch configuration
96         ILaunchConfiguration config = AndroidLaunchController.getLaunchConfig(project,
97                 LaunchConfigDelegate.ANDROID_LAUNCH_TYPE_ID);
98 
99         if (config != null) {
100             // and launch!
101             DebugUITools.launch(config, mode);
102         }
103     }
104 }
105