• 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      */
launch(ISelection selection, String mode)44     public void launch(ISelection selection, String mode) {
45         if (selection instanceof IStructuredSelection) {
46 
47             // get the object and the project from it
48             IStructuredSelection structSelect = (IStructuredSelection)selection;
49             Object o = structSelect.getFirstElement();
50 
51             // get the first (and normally only) element
52             if (o instanceof IAdaptable) {
53                 IResource r = (IResource)((IAdaptable)o).getAdapter(IResource.class);
54 
55                 // get the project from the resource
56                 if (r != null) {
57                     IProject project = r.getProject();
58 
59                     if (project != null)  {
60                         ProjectState state = Sdk.getProjectState(project);
61                         if (state != null && state.isLibrary()) {
62 
63                             MessageDialog.openError(
64                                     PlatformUI.getWorkbench().getDisplay().getActiveShell(),
65                                     "Android Launch",
66                                     "Android library projects cannot be launched.");
67                         } else{
68                             // and launch
69                             launch(project, mode);
70                         }
71                     }
72                 }
73             }
74         }
75     }
76 
77     /* (non-Javadoc)
78      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
79      * org.eclipse.ui.IEditorPart, java.lang.String)
80      */
launch(IEditorPart editor, String mode)81     public void launch(IEditorPart editor, String mode) {
82         // since we force the shortcut to only work on selection in the
83         // package explorer, this will never be called.
84     }
85 
86 
87     /**
88      * Launch a config for the specified project.
89      * @param project The project to launch
90      * @param mode The launch mode ("debug", "run" or "profile")
91      */
launch(IProject project, String mode)92     private void launch(IProject project, String mode) {
93         // get an existing or new launch configuration
94         ILaunchConfiguration config = AndroidLaunchController.getLaunchConfig(project);
95 
96         if (config != null) {
97             // and launch!
98             DebugUITools.launch(config, mode);
99         }
100     }
101 }
102