KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > ClassPathContainer


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.ui.packageview;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IPath;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.model.IWorkbenchAdapter;
28
29 import org.eclipse.ui.ide.IDE;
30
31 import org.eclipse.jdt.core.ClasspathContainerInitializer;
32 import org.eclipse.jdt.core.IClasspathContainer;
33 import org.eclipse.jdt.core.IClasspathEntry;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.IPackageFragmentRoot;
36 import org.eclipse.jdt.core.JavaCore;
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.JavaPluginImages;
43
44 /**
45  * Representation of class path containers in Java UI.
46  */

47 public class ClassPathContainer extends PackageFragmentRootContainer {
48
49     private IClasspathEntry fClassPathEntry;
50     private IClasspathContainer fContainer;
51
52     public static class RequiredProjectWrapper implements IAdaptable, IWorkbenchAdapter {
53
54         private final ClassPathContainer fParent;
55         private final IJavaProject fProject;
56         
57         public RequiredProjectWrapper(ClassPathContainer parent, IJavaProject project) {
58             fParent= parent;
59             fProject= project;
60         }
61         
62         public IJavaProject getProject() {
63             return fProject;
64         }
65         
66         public ClassPathContainer getParentClassPathContainer() {
67             return fParent;
68         }
69         
70         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
71             if (adapter == IWorkbenchAdapter.class)
72                 return this;
73             return null;
74         }
75
76         public Object JavaDoc[] getChildren(Object JavaDoc o) {
77             return new Object JavaDoc[0];
78         }
79
80         public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
81             return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
82         }
83
84         public String JavaDoc getLabel(Object JavaDoc o) {
85             return fProject.getElementName();
86         }
87
88         public Object JavaDoc getParent(Object JavaDoc o) {
89             return fParent;
90         }
91     }
92
93     public ClassPathContainer(IJavaProject parent, IClasspathEntry entry) {
94         super(parent);
95         fClassPathEntry= entry;
96         try {
97             fContainer= JavaCore.getClasspathContainer(entry.getPath(), parent);
98         } catch (JavaModelException e) {
99             fContainer= null;
100         }
101     }
102
103     public boolean equals(Object JavaDoc obj) {
104         if (obj instanceof ClassPathContainer) {
105             ClassPathContainer other = (ClassPathContainer)obj;
106             if (getJavaProject().equals(other.getJavaProject()) &&
107                 fClassPathEntry.equals(other.fClassPathEntry)) {
108                 return true;
109             }
110             
111         }
112         return false;
113     }
114
115     public int hashCode() {
116         return getJavaProject().hashCode()*17+fClassPathEntry.hashCode();
117     }
118
119     public IPackageFragmentRoot[] getPackageFragmentRoots() {
120         return getJavaProject().findPackageFragmentRoots(fClassPathEntry);
121     }
122
123     public IAdaptable[] getChildren() {
124         List JavaDoc list= new ArrayList JavaDoc();
125         IPackageFragmentRoot[] roots= getPackageFragmentRoots();
126         for (int i= 0; i < roots.length; i++) {
127             list.add(roots[i]);
128         }
129         if (fContainer != null) {
130             IClasspathEntry[] classpathEntries= fContainer.getClasspathEntries();
131             if (classpathEntries == null) {
132                 // invalid implementation of a classpath container
133
JavaPlugin.log(new IllegalArgumentException JavaDoc("Invalid classpath container implementation: getClasspathEntries() returns null. " + fContainer.getPath())); //$NON-NLS-1$
134
} else {
135                 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
136                 for (int i= 0; i < classpathEntries.length; i++) {
137                     IClasspathEntry entry= classpathEntries[i];
138                     if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
139                         IResource resource= root.findMember(entry.getPath());
140                         if (resource instanceof IProject)
141                             list.add(new RequiredProjectWrapper(this, JavaCore.create((IProject) resource)));
142                     }
143                 }
144             }
145         }
146         return (IAdaptable[]) list.toArray(new IAdaptable[list.size()]);
147     }
148
149     public ImageDescriptor getImageDescriptor() {
150         return JavaPluginImages.DESC_OBJS_LIBRARY;
151     }
152
153     public String JavaDoc getLabel() {
154         if (fContainer != null)
155             return fContainer.getDescription();
156         
157         IPath path= fClassPathEntry.getPath();
158         String JavaDoc containerId= path.segment(0);
159         ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerId);
160         if (initializer != null) {
161             String JavaDoc description= initializer.getDescription(path, getJavaProject());
162             return Messages.format(PackagesMessages.ClassPathContainer_unbound_label, description);
163         }
164         return Messages.format(PackagesMessages.ClassPathContainer_unknown_label, path.toString());
165     }
166     
167     public IClasspathEntry getClasspathEntry() {
168         return fClassPathEntry;
169     }
170     
171     static boolean contains(IJavaProject project, IClasspathEntry entry, IPackageFragmentRoot root) {
172         IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(entry);
173         for (int i= 0; i < roots.length; i++) {
174             if (roots[i].equals(root))
175                 return true;
176         }
177         return false;
178     }
179
180 }
181
Popular Tags