KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > sourcelookup > containers > JavaProjectSourceContainer


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  *******************************************************************************/

11 package org.eclipse.jdt.launching.sourcelookup.containers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
23 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
24 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
25 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
26 import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer;
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.JavaCore;
30 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
31
32 /**
33  * Java project source container. Searches for source in a project's
34  * source folders.
35  * <p>
36  * This class may be instantiated; this class is not intended to be
37  * subclassed.
38  * </p>
39  *
40  * @since 3.0
41  */

42 public class JavaProjectSourceContainer extends CompositeSourceContainer {
43         
44     // Java project
45
private IJavaProject fProject;
46     // Source folders
47
private ISourceContainer[] fSourceFolders;
48     // Generic project container
49
private ISourceContainer[] fOthers;
50     
51     private static String JavaDoc[] fgJavaExtensions = null;
52     
53     /**
54      * initialize java file extensions
55      */

56     static {
57         String JavaDoc[] extensions = JavaCore.getJavaLikeExtensions();
58         fgJavaExtensions = new String JavaDoc[extensions.length];
59         for (int i = 0; i < extensions.length; i++) {
60             String JavaDoc ext = extensions[i];
61             fgJavaExtensions[i] = '.' + ext;
62         }
63     }
64     
65     /**
66      * Unique identifier for Java project source container type
67      * (value <code>org.eclipse.jdt.launching.sourceContainer.javaProject</code>).
68      */

69     public static final String JavaDoc TYPE_ID = LaunchingPlugin.getUniqueIdentifier() + ".sourceContainer.javaProject"; //$NON-NLS-1$
70

71     /**
72      * Constructs a source container on the given Java project.
73      *
74      * @param project project to look for source in
75      */

76     public JavaProjectSourceContainer(IJavaProject project) {
77         fProject = project;
78     }
79     /* (non-Javadoc)
80      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
81      */

82     public String JavaDoc getName() {
83         return fProject.getElementName();
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getType()
88      */

89     public ISourceContainerType getType() {
90         return getSourceContainerType(TYPE_ID);
91     }
92     
93     /**
94      * Returns the Java project associated with this source container.
95      *
96      * @return Java project
97      */

98     public IJavaProject getJavaProject() {
99         return fProject;
100     }
101     /* (non-Javadoc)
102      * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
103      */

104     protected ISourceContainer[] createSourceContainers() throws CoreException {
105         List JavaDoc containers = new ArrayList JavaDoc();
106         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
107         if (fProject.getProject().isOpen()) {
108             IClasspathEntry[] entries = fProject.getRawClasspath();
109             for (int i = 0; i < entries.length; i++) {
110                 IClasspathEntry entry = entries[i];
111                 switch (entry.getEntryKind()) {
112                     case IClasspathEntry.CPE_SOURCE:
113                         IPath path = entry.getPath();
114                         IResource resource = root.findMember(path);
115                         if (resource instanceof IContainer) {
116                             containers.add(new FolderSourceContainer((IContainer)resource, false));
117                         }
118                         break;
119                 }
120             }
121         }
122         // cache the Java source folders to search for java like files in
123
fSourceFolders = (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
124         ISourceContainer theProject = new ProjectSourceContainer(fProject.getProject(), false);
125         fOthers = new ISourceContainer[] {theProject};
126         containers.add(theProject);
127         return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
128     }
129     /* (non-Javadoc)
130      * @see java.lang.Object#equals(java.lang.Object)
131      */

132     public boolean equals(Object JavaDoc obj) {
133         if (obj instanceof JavaProjectSourceContainer) {
134             return getJavaProject().equals(((JavaProjectSourceContainer)obj).getJavaProject());
135         }
136         return super.equals(obj);
137     }
138     /* (non-Javadoc)
139      * @see java.lang.Object#hashCode()
140      */

141     public int hashCode() {
142         return getJavaProject().hashCode();
143     }
144     /* (non-Javadoc)
145      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
146      */

147     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
148         // force container initialization
149
getSourceContainers();
150         
151         if (isJavaLikeFileName(name)) {
152             // only look in source folders
153
Object JavaDoc[] objects = findSourceElements(name, fSourceFolders);
154             List JavaDoc filtered = null;
155             for (int i = 0; i < objects.length; i++) {
156                 Object JavaDoc object = objects[i];
157                 if (object instanceof IResource) {
158                     if (!getJavaProject().isOnClasspath((IResource)object)) {
159                         if (filtered == null) {
160                             filtered = new ArrayList JavaDoc(objects.length);
161                             for (int j = 0; j < objects.length; j++) {
162                                 filtered.add(objects[j]);
163                             }
164                         }
165                         filtered.remove(object);
166                     }
167                 }
168             }
169             if (filtered == null) {
170                 return objects;
171             }
172             return filtered.toArray();
173         }
174         // look elsewhere if not a java like file
175
return findSourceElements(name, fOthers);
176     }
177     public void dispose() {
178         fSourceFolders = null;
179         fOthers = null;
180         super.dispose();
181     }
182     
183     private boolean isJavaLikeFileName(String JavaDoc name) {
184         for (int i = 0; i < fgJavaExtensions.length; i++) {
185             String JavaDoc ext = fgJavaExtensions[i];
186             if (name.endsWith(ext)) {
187                 return true;
188             }
189         }
190         return false;
191     }
192 }
193
Popular Tags