KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > sourcelookup > JavaProjectSourceContainerBrowser


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.sourcelookup;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
22 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
23 import org.eclipse.debug.ui.sourcelookup.AbstractSourceContainerBrowser;
24 import org.eclipse.jdt.core.IClasspathContainer;
25 import org.eclipse.jdt.core.IClasspathEntry;
26 import org.eclipse.jdt.core.IJavaModel;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.IPackageFragmentRoot;
29 import org.eclipse.jdt.core.JavaCore;
30 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
32 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
33 import org.eclipse.jdt.internal.debug.ui.actions.ProjectSelectionDialog;
34 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathContainerSourceContainer;
35 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathVariableSourceContainer;
36 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
37 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
38 import org.eclipse.jdt.ui.JavaElementLabelProvider;
39 import org.eclipse.jface.viewers.ILabelProvider;
40 import org.eclipse.jface.viewers.IStructuredContentProvider;
41 import org.eclipse.jface.viewers.Viewer;
42 import org.eclipse.jface.window.Window;
43 import org.eclipse.swt.widgets.Shell;
44
45 /**
46  * Browser to select Java projects to add to the source lookup path.
47  *
48  * @since 3.0
49  */

50 public class JavaProjectSourceContainerBrowser extends AbstractSourceContainerBrowser {
51     
52     class ContentProvider implements IStructuredContentProvider {
53         
54         private List JavaDoc fProjects;
55         
56         public ContentProvider(List JavaDoc projects) {
57             fProjects = projects;
58         }
59         
60         /**
61          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
62          */

63         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
64             return fProjects.toArray();
65         }
66
67         /**
68          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
69          */

70         public void dispose() {
71         }
72
73         /**
74          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
75          */

76         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
77         }
78
79     }
80     /* (non-Javadoc)
81      * @see org.eclipse.debug.internal.ui.sourcelookup.ISourceContainerBrowser#createSourceContainers(org.eclipse.swt.widgets.Shell, org.eclipse.debug.core.ILaunchConfiguration)
82      */

83     public ISourceContainer[] addSourceContainers(Shell shell, ISourceLookupDirector director) {
84         List JavaDoc projects = getPossibleAdditions(director);
85         
86         ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
87         IStructuredContentProvider content = new ContentProvider(projects);
88         ProjectSelectionDialog dialog= new ProjectSelectionDialog(shell, projects, content, labelProvider, SourceLookupMessages.JavaProjectSourceContainerBrowser_0);
89         dialog.setTitle(SourceLookupMessages.JavaProjectSourceContainerBrowser_1);
90         MultiStatus status = new MultiStatus(JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, SourceLookupMessages.JavaProjectSourceContainerBrowser_2, null);
91                 
92         List JavaDoc sourceContainers = new ArrayList JavaDoc();
93         if (dialog.open() == Window.OK) {
94             Object JavaDoc[] selections = dialog.getResult();
95             List JavaDoc additions = new ArrayList JavaDoc(selections.length);
96             try {
97                 for (int i = 0; i < selections.length; i++) {
98                     IJavaProject jp = (IJavaProject)selections[i];
99                     if (dialog.isAddRequiredProjects()) {
100                         collectRequiredProjects(jp, additions);
101                     } else {
102                         additions.add(jp);
103                     }
104                 }
105             } catch (JavaModelException e) {
106                 status.add(e.getStatus());
107             }
108             
109             Iterator JavaDoc iter = additions.iterator();
110             while (iter.hasNext()) {
111                 IJavaProject jp = (IJavaProject)iter.next();
112                 sourceContainers.add(new JavaProjectSourceContainer(jp));
113                 if (dialog.isAddExportedEntries()) {
114                     try {
115                         collectExportedEntries(jp, sourceContainers);
116                     } catch (CoreException e) {
117                         status.add(e.getStatus());
118                     }
119                 }
120             }
121         }
122         
123         content.dispose();
124         labelProvider.dispose();
125         
126         if (!status.isOK()) {
127             JDIDebugUIPlugin.statusDialog(status);
128         }
129         return (ISourceContainer[])sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]);
130     }
131
132     /**
133      * Returns the possible projects that can be added
134      *
135      * @param director the source lookup director currently being edited
136      */

137     protected List JavaDoc getPossibleAdditions(ISourceLookupDirector director) {
138         IJavaProject[] projects;
139         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
140         try {
141             projects= JavaCore.create(root).getJavaProjects();
142         } catch (JavaModelException e) {
143             JDIDebugUIPlugin.log(e);
144             projects= new IJavaProject[0];
145         }
146         List JavaDoc remaining = new ArrayList JavaDoc();
147         for (int i = 0; i < projects.length; i++) {
148             remaining.add(projects[i]);
149         }
150         List JavaDoc alreadySelected = new ArrayList JavaDoc();
151         ISourceContainer[] containers = director.getSourceContainers();
152         for (int i = 0; i < containers.length; i++) {
153             ISourceContainer container = containers[i];
154             if (container.getType().getId().equals(JavaProjectSourceContainer.TYPE_ID)) {
155                 alreadySelected.add(((JavaProjectSourceContainer)container).getJavaProject());
156             }
157         }
158         remaining.removeAll(alreadySelected);
159         return remaining;
160     }
161
162     /**
163      * Adds all projects required by <code>proj</code> to the list
164      * <code>res</code>
165      *
166      * @param proj the project for which to compute required
167      * projects
168      * @param res the list to add all required projects too
169      */

170     protected void collectRequiredProjects(IJavaProject proj, List JavaDoc res) throws JavaModelException {
171         if (!res.contains(proj)) {
172             res.add(proj);
173             
174             IJavaModel model= proj.getJavaModel();
175             
176             IClasspathEntry[] entries= proj.getRawClasspath();
177             for (int i= 0; i < entries.length; i++) {
178                 IClasspathEntry curr= entries[i];
179                 if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
180                     IJavaProject ref= model.getJavaProject(curr.getPath().segment(0));
181                     if (ref.exists()) {
182                         collectRequiredProjects(ref, res);
183                     }
184                 }
185             }
186         }
187     }
188     
189     /**
190      * Adds all exported entries defined by <code>proj</code> to the list
191      * <code>list</code>.
192      *
193      * @param proj
194      * @param list
195      * @throws JavaModelException
196      */

197     protected void collectExportedEntries(IJavaProject proj, List JavaDoc list) throws CoreException {
198         IClasspathEntry[] entries = proj.getRawClasspath();
199         for (int i = 0; i < entries.length; i++) {
200             IClasspathEntry entry = entries[i];
201             ISourceContainer sourceContainer = null;
202             if (entry.isExported()) {
203                 switch (entry.getEntryKind()) {
204                     case IClasspathEntry.CPE_CONTAINER:
205                         IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj);
206                         sourceContainer = new ClasspathContainerSourceContainer(container.getPath());
207                         break;
208                     case IClasspathEntry.CPE_LIBRARY:
209                         IPackageFragmentRoot[] roots = proj.findPackageFragmentRoots(entry);
210                         if (roots != null && roots.length > 0) {
211                             sourceContainer = new PackageFragmentRootSourceContainer(roots[0]);
212                         }
213                         break;
214                     case IClasspathEntry.CPE_PROJECT:
215                         String JavaDoc name = entry.getPath().segment(0);
216                         IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
217                         if (p.exists()) {
218                             IJavaProject jp = JavaCore.create(p);
219                             if (jp.exists()) {
220                                 sourceContainer = new JavaProjectSourceContainer(jp);
221                             }
222                         }
223                         break;
224                     case IClasspathEntry.CPE_VARIABLE:
225                         sourceContainer = new ClasspathVariableSourceContainer(entry.getPath());
226                         break;
227                     default:
228                         break;
229                 }
230                 if (sourceContainer != null) {
231                     if (!list.contains(sourceContainer)) {
232                         list.add(sourceContainer);
233                     }
234                 }
235             }
236         }
237     }
238 }
239
Popular Tags