OSDN Git Service

Merge commit '6f56ebc73762945f2757c66c91ef4702edd979b8' into donut
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / launch / LaunchShortcut.java
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 org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.ui.DebugUITools;
24 import org.eclipse.debug.ui.ILaunchShortcut;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.ui.IEditorPart;
28
29 /**
30  * Launch shortcut to launch debug/run configuration directly.
31  */
32 public class LaunchShortcut implements ILaunchShortcut {
33
34
35     /* (non-Javadoc)
36      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
37      * org.eclipse.jface.viewers.ISelection, java.lang.String)
38      */
39     public void launch(ISelection selection, String mode) {
40         if (selection instanceof IStructuredSelection) {
41
42             // get the object and the project from it
43             IStructuredSelection structSelect = (IStructuredSelection)selection;
44             Object o = structSelect.getFirstElement();
45
46             // get the first (and normally only) element
47             if (o instanceof IAdaptable) {
48                 IResource r = (IResource)((IAdaptable)o).getAdapter(IResource.class);
49
50                 // get the project from the resource
51                 if (r != null) {
52                     IProject project = r.getProject();
53
54                     if (project != null)  {
55                         // and launch
56                         launch(project, mode);
57                     }
58                 }
59             }
60         }
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
65      * org.eclipse.ui.IEditorPart, java.lang.String)
66      */
67     public void launch(IEditorPart editor, String mode) {
68         // since we force the shortcut to only work on selection in the
69         // package explorer, this will never be called.
70     }
71
72
73     /**
74      * Launch a config for the specified project.
75      * @param project The project to launch
76      * @param mode The launch mode ("debug", "run" or "profile")
77      */
78     private void launch(IProject project, String mode) {
79         // get an existing or new launch configuration
80         ILaunchConfiguration config = AndroidLaunchController.getLaunchConfig(project);
81
82         if (config != null) {
83             // and launch!
84             DebugUITools.launch(config, mode);
85         }
86     }
87 }