KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > JavaApplicationLaunchShortcut


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.launcher;
12
13  
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationType;
23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMember;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.search.IJavaSearchScope;
30 import org.eclipse.jdt.core.search.SearchEngine;
31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
33 import org.eclipse.jface.operation.IRunnableContext;
34
35 /**
36  * Performs single click launching for local Java applications.
37  */

38 public class JavaApplicationLaunchShortcut extends JavaLaunchShortcut {
39     
40     /**
41      * Returns the Java elements corresponding to the given objects.
42      *
43      * @param objects selected objects
44      * @return corresponding Java elements
45      */

46     private IJavaElement[] getJavaElements(Object JavaDoc[] objects) {
47         List JavaDoc list= new ArrayList JavaDoc(objects.length);
48         for (int i = 0; i < objects.length; i++) {
49             Object JavaDoc object = objects[i];
50             if (object instanceof IAdaptable) {
51                 IJavaElement element = (IJavaElement) ((IAdaptable)object).getAdapter(IJavaElement.class);
52                 if (element != null) {
53                     if (element instanceof IMember) {
54                         // Use the declaring type if available
55
IJavaElement type= ((IMember)element).getDeclaringType();
56                         if (type != null) {
57                             element= type;
58                         }
59                     }
60                     list.add(element);
61                 }
62             }
63         }
64         return (IJavaElement[]) list.toArray(new IJavaElement[list.size()]);
65     }
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#createConfiguration(org.eclipse.jdt.core.IType)
69      */

70     protected ILaunchConfiguration createConfiguration(IType type) {
71         ILaunchConfiguration config = null;
72         ILaunchConfigurationWorkingCopy wc = null;
73         try {
74             ILaunchConfigurationType configType = getConfigurationType();
75             wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(type.getElementName()));
76             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type.getFullyQualifiedName());
77             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, type.getJavaProject().getElementName());
78             //CONTEXTLAUNCHING
79
wc.setMappedResources(new IResource[] {type.getUnderlyingResource()});
80             config = wc.doSave();
81         } catch (CoreException exception) {
82             reportErorr(exception);
83         }
84         return config;
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#getConfigurationType()
89      */

90     protected ILaunchConfigurationType getConfigurationType() {
91         return getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
92     }
93
94     /**
95      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#findTypes(java.lang.Object[], org.eclipse.jface.operation.IRunnableContext)
96      */

97     protected IType[] findTypes(Object JavaDoc[] elements, IRunnableContext context) throws InterruptedException JavaDoc, CoreException {
98         try {
99             if(elements.length == 1) {
100                 IType type = isMainMethod(elements[0]);
101                 if(type != null) {
102                     return new IType[] {type};
103                 }
104             }
105             IJavaElement[] javaElements = getJavaElements(elements);
106             MainMethodSearchEngine engine = new MainMethodSearchEngine();
107             int constraints = IJavaSearchScope.SOURCES;
108             constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
109             IJavaSearchScope scope = SearchEngine.createJavaSearchScope(javaElements, constraints);
110             return engine.searchMainMethods(context, scope, true);
111         } catch (InvocationTargetException JavaDoc e) {
112             throw (CoreException)e.getTargetException();
113         }
114     }
115     
116     /**
117      * Returns the smallest enclosing <code>IType</code> if the specified object is a main method, or <code>null</code>
118      * @param o the object to inspect
119      * @return the smallest enclosing <code>IType</code> of the specified object if it is a main method or <code>null</code> if it is not
120      * @since 3.3
121      */

122     protected IType isMainMethod(Object JavaDoc o) {
123         if(o instanceof IAdaptable) {
124             IAdaptable adapt = (IAdaptable) o;
125             IJavaElement element = (IJavaElement) adapt.getAdapter(IJavaElement.class);
126             if(element != null && element.getElementType() == IJavaElement.METHOD) {
127                 try {
128                     IMethod method = (IMethod) element;
129                     if(method.isMainMethod()) {
130                         return method.getDeclaringType();
131                     }
132                 }
133                 catch (JavaModelException jme) {JDIDebugUIPlugin.log(jme);}
134             }
135         }
136         return null;
137     }
138     
139     /* (non-Javadoc)
140      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#getTypeSelectionTitle()
141      */

142     protected String JavaDoc getTypeSelectionTitle() {
143         return LauncherMessages.JavaApplicationLaunchShortcut_0;
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#getEditorEmptyMessage()
148      */

149     protected String JavaDoc getEditorEmptyMessage() {
150         return LauncherMessages.JavaApplicationLaunchShortcut_1;
151     }
152
153     /* (non-Javadoc)
154      * @see org.eclipse.jdt.internal.debug.ui.launcher.JavaLaunchShortcut#getSelectionEmptyMessage()
155      */

156     protected String JavaDoc getSelectionEmptyMessage() {
157         return LauncherMessages.JavaApplicationLaunchShortcut_2;
158     }
159     
160 }
161
Popular Tags