1 11 package org.eclipse.jdt.ui; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IFolder; 20 import org.eclipse.core.resources.IResource; 21 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.jface.viewers.Viewer; 24 25 import org.eclipse.jdt.core.IClassFile; 26 import org.eclipse.jdt.core.ICompilationUnit; 27 import org.eclipse.jdt.core.IJarEntryResource; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaElementDelta; 30 import org.eclipse.jdt.core.IJavaModel; 31 import org.eclipse.jdt.core.IJavaProject; 32 import org.eclipse.jdt.core.IPackageFragment; 33 import org.eclipse.jdt.core.IPackageFragmentRoot; 34 import org.eclipse.jdt.core.IParent; 35 import org.eclipse.jdt.core.ISourceReference; 36 import org.eclipse.jdt.core.JavaCore; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 66 public class StandardJavaElementContentProvider implements ITreeContentProvider, IWorkingCopyProvider { 67 68 protected static final Object [] NO_CHILDREN= new Object [0]; 69 protected boolean fProvideMembers; 70 protected boolean fProvideWorkingCopy; 71 72 76 public StandardJavaElementContentProvider() { 77 this(false); 78 } 79 80 85 public StandardJavaElementContentProvider(boolean provideMembers, boolean provideWorkingCopy) { 86 this(provideMembers); 87 } 88 89 90 96 public StandardJavaElementContentProvider(boolean provideMembers) { 97 fProvideMembers= provideMembers; 98 fProvideWorkingCopy= provideMembers; 99 } 100 101 108 public boolean getProvideMembers() { 109 return fProvideMembers; 110 } 111 112 120 public void setProvideMembers(boolean b) { 121 fProvideMembers= b; 123 } 124 125 129 public boolean getProvideWorkingCopy() { 130 return fProvideWorkingCopy; 131 } 132 133 137 public void setProvideWorkingCopy(boolean b) { 138 fProvideWorkingCopy= b; 139 } 140 141 144 public boolean providesWorkingCopies() { 145 return getProvideWorkingCopy(); 146 } 147 148 151 public Object [] getElements(Object parent) { 152 return getChildren(parent); 153 } 154 155 158 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 159 } 160 161 164 public void dispose() { 165 } 166 167 170 public Object [] getChildren(Object element) { 171 if (!exists(element)) 172 return NO_CHILDREN; 173 174 try { 175 if (element instanceof IJavaModel) 176 return getJavaProjects((IJavaModel)element); 177 178 if (element instanceof IJavaProject) 179 return getPackageFragmentRoots((IJavaProject)element); 180 181 if (element instanceof IPackageFragmentRoot) 182 return getPackageFragmentRootContent((IPackageFragmentRoot)element); 183 184 if (element instanceof IPackageFragment) 185 return getPackageContent((IPackageFragment)element); 186 187 if (element instanceof IFolder) 188 return getFolderContent((IFolder)element); 189 190 if (element instanceof IJarEntryResource) { 191 return ((IJarEntryResource) element).getChildren(); 192 } 193 194 if (getProvideMembers() && element instanceof ISourceReference && element instanceof IParent) { 195 return ((IParent)element).getChildren(); 196 } 197 } catch (CoreException e) { 198 return NO_CHILDREN; 199 } 200 return NO_CHILDREN; 201 } 202 203 206 public boolean hasChildren(Object element) { 207 if (getProvideMembers()) { 208 if (element instanceof ICompilationUnit || 210 element instanceof IClassFile) { 211 return true; 212 } 213 } else { 214 if (element instanceof ICompilationUnit || 216 element instanceof IClassFile || 217 element instanceof IFile) 218 return false; 219 } 220 221 if (element instanceof IJavaProject) { 222 IJavaProject jp= (IJavaProject)element; 223 if (!jp.getProject().isOpen()) { 224 return false; 225 } 226 } 227 228 if (element instanceof IParent) { 229 try { 230 if (((IParent)element).hasChildren()) 232 return true; 233 } catch(JavaModelException e) { 234 return true; 235 } 236 } 237 Object [] children= getChildren(element); 238 return (children != null) && children.length > 0; 239 } 240 241 244 public Object getParent(Object element) { 245 if (!exists(element)) 246 return null; 247 return internalGetParent(element); 248 } 249 250 259 protected Object [] getPackageFragmentRootContent(IPackageFragmentRoot root) throws JavaModelException { 260 IJavaElement[] fragments= root.getChildren(); 261 if (isProjectPackageFragmentRoot(root)) { 262 return fragments; 263 } 264 Object [] nonJavaResources= root.getNonJavaResources(); 265 if (nonJavaResources == null) 266 return fragments; 267 return concatenate(fragments, nonJavaResources); 268 } 269 270 277 protected Object [] getPackageFragmentRoots(IJavaProject project) throws JavaModelException { 278 if (!project.getProject().isOpen()) 279 return NO_CHILDREN; 280 281 IPackageFragmentRoot[] roots= project.getPackageFragmentRoots(); 282 List list= new ArrayList (roots.length); 283 for (int i= 0; i < roots.length; i++) { 286 IPackageFragmentRoot root= roots[i]; 287 if (isProjectPackageFragmentRoot(root)) { 288 Object [] fragments= getPackageFragmentRootContent(root); 289 for (int j= 0; j < fragments.length; j++) { 290 list.add(fragments[j]); 291 } 292 } else { 293 list.add(root); 294 } 295 } 296 Object [] resources= project.getNonJavaResources(); 297 for (int i= 0; i < resources.length; i++) { 298 list.add(resources[i]); 299 } 300 return list.toArray(); 301 } 302 303 306 protected Object [] getJavaProjects(IJavaModel jm) throws JavaModelException { 307 return jm.getJavaProjects(); 308 } 309 310 319 protected Object [] getPackageContent(IPackageFragment fragment) throws JavaModelException { 320 if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) { 321 return concatenate(fragment.getCompilationUnits(), fragment.getNonJavaResources()); 322 } 323 return concatenate(fragment.getClassFiles(), fragment.getNonJavaResources()); 324 } 325 326 334 protected Object [] getFolderContent(IFolder folder) throws CoreException { 335 IResource[] members= folder.members(); 336 IJavaProject javaProject= JavaCore.create(folder.getProject()); 337 if (javaProject == null || !javaProject.exists()) 338 return members; 339 boolean isFolderOnClasspath = javaProject.isOnClasspath(folder); 340 List nonJavaResources= new ArrayList (); 341 for (int i= 0; i < members.length; i++) { 343 IResource member= members[i]; 344 if (isFolderOnClasspath) { 349 if (javaProject.findPackageFragmentRoot(member.getFullPath()) == null) { 350 nonJavaResources.add(member); 351 } 352 } else if (!javaProject.isOnClasspath(member)) { 353 nonJavaResources.add(member); 354 } 355 } 356 return nonJavaResources.toArray(); 357 } 358 359 362 protected boolean isClassPathChange(IJavaElementDelta delta) { 363 364 if (delta.getElement().getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) 366 return false; 367 368 int flags= delta.getFlags(); 369 return (delta.getKind() == IJavaElementDelta.CHANGED && 370 ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0) || 371 ((flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) || 372 ((flags & IJavaElementDelta.F_REORDER) != 0)); 373 } 374 375 378 protected Object skipProjectPackageFragmentRoot(IPackageFragmentRoot root) { 379 if (isProjectPackageFragmentRoot(root)) 380 return root.getParent(); 381 return root; 382 } 383 384 387 protected boolean isPackageFragmentEmpty(IJavaElement element) throws JavaModelException { 388 if (element instanceof IPackageFragment) { 389 IPackageFragment fragment= (IPackageFragment)element; 390 if (fragment.exists() && !(fragment.hasChildren() || fragment.getNonJavaResources().length > 0) && fragment.hasSubpackages()) 391 return true; 392 } 393 return false; 394 } 395 396 399 protected boolean isProjectPackageFragmentRoot(IPackageFragmentRoot root) { 400 IJavaProject javaProject= root.getJavaProject(); 401 return javaProject != null && javaProject.getPath().equals(root.getPath()); 402 } 403 404 407 protected boolean exists(Object element) { 408 if (element == null) { 409 return false; 410 } 411 if (element instanceof IResource) { 412 return ((IResource)element).exists(); 413 } 414 if (element instanceof IJavaElement) { 415 return ((IJavaElement)element).exists(); 416 } 417 return true; 418 } 419 420 423 protected Object internalGetParent(Object element) { 424 425 if (element instanceof IResource) { 427 IResource parent= ((IResource)element).getParent(); 428 IJavaElement jParent= JavaCore.create(parent); 429 if (jParent != null && jParent.exists()) 431 return jParent; 432 return parent; 433 } else if (element instanceof IJavaElement) { 434 IJavaElement parent= ((IJavaElement) element).getParent(); 435 if (element instanceof IPackageFragment) { 438 return skipProjectPackageFragmentRoot((IPackageFragmentRoot) parent); 439 } 440 return parent; 441 } else if (element instanceof IJarEntryResource) { 442 return ((IJarEntryResource) element).getParent(); 443 } 444 return null; 445 } 446 447 450 protected static Object [] concatenate(Object [] a1, Object [] a2) { 451 int a1Len= a1.length; 452 int a2Len= a2.length; 453 if (a1Len == 0) return a2; 454 if (a2Len == 0) return a1; 455 Object [] res= new Object [a1Len + a2Len]; 456 System.arraycopy(a1, 0, res, 0, a1Len); 457 System.arraycopy(a2, 0, res, a1Len, a2Len); 458 return res; 459 } 460 461 462 } 463 | Popular Tags |