KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.jdt.core.IJavaModel;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.ui.JavaElementLabelProvider;
29 import org.eclipse.jface.operation.IRunnableContext;
30 import org.eclipse.jface.operation.IRunnableWithProgress;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.dialogs.TwoPaneElementSelector;
36
37 /**
38  * A dialog to select a type that extends <code>java.applet.Applet</code>.
39  */

40 public class AppletSelectionDialog extends TwoPaneElementSelector {
41
42     private IRunnableContext fRunnableContext;
43     private IJavaProject fProject;
44     private static final IType[] EMPTY_TYPE_ARRAY = new IType[] {};
45
46     private static class PackageRenderer extends JavaElementLabelProvider {
47         public PackageRenderer() {
48             super(JavaElementLabelProvider.SHOW_PARAMETERS | JavaElementLabelProvider.SHOW_POST_QUALIFIED | JavaElementLabelProvider.SHOW_ROOT);
49         }
50
51         public Image getImage(Object JavaDoc element) {
52             return super.getImage(((IType) element).getPackageFragment());
53         }
54
55         public String JavaDoc getText(Object JavaDoc element) {
56             return super.getText(((IType) element).getPackageFragment());
57         }
58     }
59
60     public AppletSelectionDialog(Shell shell, IRunnableContext context, IJavaProject project) {
61         super(shell, new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_OVERLAY_ICONS), new PackageRenderer());
62
63         Assert.isNotNull(context);
64
65         fRunnableContext = context;
66         fProject = project;
67     }
68
69     /**
70      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
71      */

72     protected void configureShell(Shell newShell) {
73         super.configureShell(newShell);
74     }
75
76     /**
77      * @see org.eclipse.jface.window.Window#open()
78      */

79     public int open() {
80         IType[] types = getAppletTypes();
81         if (types == null) {
82             return CANCEL;
83         }
84         setElements(types);
85         return super.open();
86     }
87     
88     /**
89      * Return all types extending <code>java.lang.Applet</code> in the project, or
90      * all types extending Applet in the workspace if the project is <code>null</code>.
91      * If the search is canceled, return <code>null</code>.
92      */

93     private IType[] getAppletTypes() {
94         // Populate an array of java projects with either the project specified in
95
// the constructor, or ALL projects in the workspace if no project was specified
96
final IJavaProject[] javaProjects;
97         if (fProject == null) {
98             try {
99                 javaProjects = getJavaModel().getJavaProjects();
100             } catch (JavaModelException jme) {
101                 return EMPTY_TYPE_ARRAY;
102             }
103         } else {
104             javaProjects = new IJavaProject[] {fProject};
105         }
106         
107         // For each java project, calculate the Applet types it contains and add
108
// them to the results
109
final int projectCount = javaProjects.length;
110         final Set JavaDoc results = new HashSet JavaDoc(projectCount);
111         boolean canceled = false;
112         try {
113             fRunnableContext.run(true, true, new IRunnableWithProgress() {
114                 public void run(IProgressMonitor monitor) {
115                     monitor.beginTask(LauncherMessages.AppletSelectionDialog_Searching____1, projectCount);
116                     for (int i = 0; i < projectCount; i++) {
117                         IJavaProject javaProject = javaProjects[i];
118                         SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
119                         results.addAll(AppletLaunchConfigurationUtils.collectAppletTypesInProject(subMonitor, javaProject));
120                         monitor.worked(1);
121                     }
122                     monitor.done();
123                 }
124             });
125         } catch (InvocationTargetException JavaDoc ite) {
126         } catch (InterruptedException JavaDoc ie) {
127             canceled = true;
128         }
129
130         // Convert the results to an array and return it
131
if (canceled) {
132             return null;
133         }
134         IType[] types = null;
135         types = (IType[]) results.toArray(new IType[results.size()]);
136         return types;
137     }
138
139     /**
140      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
141      */

142     public Control createDialogArea(Composite parent) {
143         Control control = super.createDialogArea(parent);
144         applyDialogFont(control);
145         return control;
146     }
147
148     /**
149      * Convenience method to get access to the java model.
150      */

151     private IJavaModel getJavaModel() {
152         return JavaCore.create(getWorkspaceRoot());
153     }
154
155     /**
156      * Convenience method to get the workspace root.
157      */

158     private IWorkspaceRoot getWorkspaceRoot() {
159         return ResourcesPlugin.getWorkspace().getRoot();
160     }
161     
162 }
163
Popular Tags