KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > sourcelookup > containers > ProjectSourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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  * Mikhail Khodjaiants, QNX - Bug 110227: Possible infinite loop in ProjectSourceContainer
11  *******************************************************************************/

12 package org.eclipse.debug.core.sourcelookup.containers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
22 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
23
24 /**
25  * A project in the workspace. Source is searched for in the root project
26  * folder and all folders within the project recursively. Optionally,
27  * referenced projects may be searched as well.
28  * <p>
29  * Clients may instantiate this class. This class is not intended to
30  * be subclassed.
31  * </p>
32  * @since 3.0
33  */

34 public class ProjectSourceContainer extends ContainerSourceContainer {
35
36     boolean fReferencedProjects=false;
37     /**
38      * Unique identifier for the project source container type
39      * (value <code>org.eclipse.debug.core.containerType.project</code>).
40      */

41     public static final String JavaDoc TYPE_ID = DebugPlugin.getUniqueIdentifier() + ".containerType.project"; //$NON-NLS-1$
42

43     /**
44      * Constructs a project source container.
45      *
46      * @param project the project to search for source in
47      * @param referenced whether referenced projects should be considered
48      */

49     public ProjectSourceContainer(IProject project, boolean referenced) {
50         super(project, true);
51         fReferencedProjects = referenced;
52     }
53     
54     /**
55      * Returns whether referenced projects are considered.
56      *
57      * @return whether referenced projects are considered
58      */

59     public boolean isSearchReferencedProjects() {
60         return fReferencedProjects;
61     }
62     
63     /**
64      * Returns the project this source container references.
65      *
66      * @return the project this source container references
67      */

68     public IProject getProject() {
69         return (IProject) getContainer();
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
74      */

75     public ISourceContainerType getType() {
76         return getSourceContainerType(TYPE_ID);
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#isComposite()
81      */

82     public boolean isComposite() {
83         return true;
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
88      */

89     protected ISourceContainer[] createSourceContainers() throws CoreException {
90         if (getProject().isOpen()) {
91             if (isSearchReferencedProjects()) {
92                 IProject project = getProject();
93                 IProject[] projects = getAllReferencedProjects(project);
94                 ISourceContainer[] folders = super.createSourceContainers();
95                 List JavaDoc all = new ArrayList JavaDoc(folders.length + projects.length);
96                 for (int i = 0; i < folders.length; i++) {
97                     all.add(folders[i]);
98                 }
99                 for (int i = 0; i < projects.length; i++) {
100                     if (project.exists() && project.isOpen()) {
101                         ProjectSourceContainer container = new ProjectSourceContainer(projects[i], false);
102                         container.init(getDirector());
103                         all.add(container);
104                     }
105                 }
106                 return (ISourceContainer[]) all.toArray(new ISourceContainer[all.size()]);
107             }
108             return super.createSourceContainers();
109         }
110         return new ISourceContainer[0];
111     }
112
113     private IProject[] getAllReferencedProjects(IProject project) throws CoreException {
114         Set JavaDoc all = new HashSet JavaDoc();
115         getAllReferencedProjects(all, project);
116         return (IProject[]) all.toArray(new IProject[all.size()]);
117     }
118
119     private void getAllReferencedProjects(Set JavaDoc all, IProject project) throws CoreException {
120         IProject[] refs = project.getReferencedProjects();
121         for (int i = 0; i < refs.length; i++) {
122             if (!all.contains(refs[i]) && refs[i].exists() && refs[i].isOpen()) {
123                 all.add(refs[i]);
124                 getAllReferencedProjects(all, refs[i]);
125             }
126         }
127     }
128 }
129
Popular Tags