KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
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.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29 import org.eclipse.jdt.core.IClassFile;
30 import org.eclipse.jdt.core.ICompilationUnit;
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.IMember;
34 import org.eclipse.jdt.core.IType;
35 import org.eclipse.jdt.core.ITypeHierarchy;
36 import org.eclipse.jdt.core.JavaCore;
37 import org.eclipse.jdt.core.JavaModelException;
38 import org.eclipse.jdt.core.Signature;
39 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
40 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
41 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
42 import org.eclipse.jface.operation.IRunnableContext;
43 import org.eclipse.jface.operation.IRunnableWithProgress;
44
45 public class AppletLaunchConfigurationUtils {
46     
47     /**
48      * Throws a core exception with an error status object built from
49      * the given message, lower level exception, and error code.
50      *
51      * @param message the status message
52      * @param exception lower level exception associated with the
53      * error, or <code>null</code> if none
54      * @param code error code
55      */

56     public static void abort(String JavaDoc message, Throwable JavaDoc exception, int code)
57         throws CoreException {
58         throw new CoreException(
59             new Status(
60                 IStatus.ERROR,
61                 JDIDebugUIPlugin.getUniqueIdentifier(),
62                 code,
63                 message,
64                 exception));
65     }
66
67     /**
68      * Return the <code>IType</code> referenced by the specified name and contained in
69      * the specified project or throw a <code>CoreException</code> whose message explains why
70      * this couldn't be done.
71      */

72     public static IType getMainType(String JavaDoc mainTypeName, IJavaProject javaProject) throws CoreException {
73         if ((mainTypeName == null) || (mainTypeName.trim().length() < 1)) {
74             abort(LauncherMessages.appletlauncher_utils_error_main_type_not_specified, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
75         }
76         IType mainType = null;
77         try {
78             mainType = findType(javaProject, mainTypeName);
79         } catch (JavaModelException jme) {
80         }
81         if (mainType == null) {
82             abort(MessageFormat.format(LauncherMessages.appletlauncher_utils_error_main_type_does_not_exist, new String JavaDoc[] {mainTypeName, javaProject.getElementName()}), null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
83         }
84         return mainType;
85     }
86     
87
88     /**
89      * Find the specified (fully-qualified) type name in the specified java project.
90      */

91     public static IType findType(IJavaProject javaProject, String JavaDoc mainTypeName) throws CoreException {
92         IJavaElement javaElement= JavaDebugUtils.findElement(mainTypeName, javaProject);
93         if (javaElement == null) {
94             return null;
95         } else if (javaElement instanceof IType) {
96             return (IType)javaElement;
97         } else if (javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
98             String JavaDoc simpleName= Signature.getSimpleName(mainTypeName);
99             return ((ICompilationUnit) javaElement).getType(simpleName);
100         } else if (javaElement.getElementType() == IJavaElement.CLASS_FILE) {
101             return ((IClassFile) javaElement).getType();
102         }
103         return null;
104     }
105
106     /**
107      *
108      */

109     public static Set JavaDoc collectAppletTypesInProject(IProgressMonitor monitor, IJavaProject project) {
110         IType[] types;
111         HashSet JavaDoc result = new HashSet JavaDoc(5);
112         try {
113             IType javaLangApplet = AppletLaunchConfigurationUtils.getMainType("java.applet.Applet", project); //$NON-NLS-1$
114
ITypeHierarchy hierarchy = javaLangApplet.newTypeHierarchy(project, new SubProgressMonitor(monitor, 1));
115             types = hierarchy.getAllSubtypes(javaLangApplet);
116             int length = types.length;
117             if (length != 0) {
118                 for (int i = 0; i < length; i++) {
119                     if (!types[i].isBinary()) {
120                         result.add(types[i]);
121                     }
122                 }
123             }
124         } catch(JavaModelException jme) {
125         } catch(CoreException e) {
126         }
127         monitor.done();
128         return result;
129     }
130     
131     public static void collectTypes(Object JavaDoc element, IProgressMonitor monitor, Set JavaDoc result) throws JavaModelException/*, InvocationTargetException*/ {
132         element= computeScope(element);
133         while(element instanceof IMember) {
134             if(element instanceof IType) {
135                 if (isSubclassOfApplet(monitor, (IType)element)) {
136                     result.add(element);
137                     monitor.done();
138                     return;
139                 }
140             }
141             element= ((IJavaElement)element).getParent();
142         }
143         if (element instanceof ICompilationUnit) {
144             ICompilationUnit cu= (ICompilationUnit)element;
145             IType[] types= cu.getAllTypes();
146             for (int i= 0; i < types.length; i++) {
147                 if (isSubclassOfApplet(monitor, types[i])) {
148                     result.add(types[i]);
149                 }
150             }
151         } else if (element instanceof IClassFile) {
152             IType type = ((IClassFile)element).getType();
153             if (isSubclassOfApplet(monitor, type)) {
154                 result.add(type);
155             }
156         } else if (element instanceof IJavaElement) {
157             IJavaElement parent = (IJavaElement) element;
158             List JavaDoc found= searchSubclassesOfApplet(monitor, (IJavaElement)element);
159             // filter within the parent element
160
Iterator JavaDoc iterator = found.iterator();
161             while (iterator.hasNext()) {
162                 IJavaElement target = (IJavaElement) iterator.next();
163                 IJavaElement child = target;
164                 while (child != null) {
165                     if (child.equals(parent)) {
166                         result.add(target);
167                         break;
168                     }
169                     child = child.getParent();
170                 }
171             }
172         }
173         monitor.done();
174     }
175
176     private static List JavaDoc searchSubclassesOfApplet(IProgressMonitor pm, IJavaElement javaElement) {
177         return new ArrayList JavaDoc(collectAppletTypesInProject(pm, javaElement.getJavaProject()));
178     }
179     
180     private static boolean isSubclassOfApplet(IProgressMonitor pm, IType type) {
181         return collectAppletTypesInProject(pm, type.getJavaProject()).contains(type);
182     }
183     
184     private static Object JavaDoc computeScope(Object JavaDoc element) {
185         if (element instanceof IJavaElement) {
186             return element;
187         }
188         if (element instanceof IAdaptable) {
189             element = ((IAdaptable)element).getAdapter(IResource.class);
190         }
191         if (element instanceof IResource) {
192             IJavaElement javaElement = JavaCore.create((IResource)element);
193             if (javaElement != null && !javaElement.exists()) {
194                 // do not consider the resource - corresponding java element does not exist
195
element = null;
196             } else {
197                 element= javaElement;
198             }
199             
200         }
201         return element;
202     }
203
204     /**
205      * Searches for applets from within the given scope of elements
206      * @param context
207      * @param elements the search scope
208      * @return and array of <code>IType</code>s of matches for java types that extend <code>Applet</code> (directly or indirectly)
209      * @throws InvocationTargetException
210      * @throws InterruptedException
211      */

212     public static IType[] findApplets(IRunnableContext context, final Object JavaDoc[] elements) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
213         final Set JavaDoc result= new HashSet JavaDoc();
214     
215         if (elements.length > 0) {
216             IRunnableWithProgress runnable= new IRunnableWithProgress() {
217                 public void run(IProgressMonitor pm) throws InterruptedException JavaDoc {
218                     int nElements= elements.length;
219                     pm.beginTask(LauncherMessages.appletlauncher_search_task_inprogress, nElements);
220                     try {
221                         for (int i= 0; i < nElements; i++) {
222                             try {
223                                 collectTypes(elements[i], new SubProgressMonitor(pm, 1), result);
224                             } catch (JavaModelException jme) {
225                                 JDIDebugUIPlugin.log(jme.getStatus());
226                             }
227                             if (pm.isCanceled()) {
228                                 throw new InterruptedException JavaDoc();
229                             }
230                         }
231                     } finally {
232                         pm.done();
233                     }
234                 }
235             };
236             context.run(true, true, runnable);
237         }
238         return (IType[]) result.toArray(new IType[result.size()]) ;
239     }
240 }
241
242
Popular Tags