1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 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 getAdapter(Class adapter) { 71 if (adapter == IWorkbenchAdapter.class) 72 return this; 73 return null; 74 } 75 76 public Object [] getChildren(Object o) { 77 return new Object [0]; 78 } 79 80 public ImageDescriptor getImageDescriptor(Object object) { 81 return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT); 82 } 83 84 public String getLabel(Object o) { 85 return fProject.getElementName(); 86 } 87 88 public Object getParent(Object 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 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 list= new ArrayList (); 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 JavaPlugin.log(new IllegalArgumentException ("Invalid classpath container implementation: getClasspathEntries() returns null. " + fContainer.getPath())); } 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 getLabel() { 154 if (fContainer != null) 155 return fContainer.getDescription(); 156 157 IPath path= fClassPathEntry.getPath(); 158 String containerId= path.segment(0); 159 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerId); 160 if (initializer != null) { 161 String 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
|