1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 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.IContainer; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IJavaModel; 24 import org.eclipse.jdt.core.IJavaProject; 25 import org.eclipse.jdt.core.IPackageFragmentRoot; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 29 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestinationValidator; 30 31 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 32 33 import org.eclipse.jdt.internal.ui.JavaPlugin; 34 35 36 public final class DestinationContentProvider extends StandardJavaElementContentProvider { 37 38 private IReorgDestinationValidator fValidator; 39 40 public DestinationContentProvider(IReorgDestinationValidator validator) { 41 super(true); 42 fValidator= validator; 43 } 44 45 public boolean hasChildren(Object element) { 46 if (element instanceof IJavaElement){ 47 IJavaElement javaElement= (IJavaElement) element; 48 if (! fValidator.canChildrenBeDestinations(javaElement)) 49 return false; 50 if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT){ 51 if (((IPackageFragmentRoot)javaElement).isArchive()) 52 return false; 53 } 54 } else if (element instanceof IResource) { 55 IResource resource= (IResource) element; 56 if (! fValidator.canChildrenBeDestinations(resource)) 57 return false; 58 } 59 return super.hasChildren(element); 60 } 61 62 public Object [] getChildren(Object element) { 63 try { 64 if (element instanceof IJavaModel) { 65 return concatenate(getJavaProjects((IJavaModel)element), getOpenNonJavaProjects((IJavaModel)element)); 66 } else { 67 Object [] children= doGetChildren(element); 68 ArrayList result= new ArrayList (children.length); 69 for (int i= 0; i < children.length; i++) { 70 if (children[i] instanceof IJavaElement) { 71 IJavaElement javaElement= (IJavaElement) children[i]; 72 if (fValidator.canElementBeDestination(javaElement) || fValidator.canChildrenBeDestinations(javaElement)) 73 result.add(javaElement); 74 } else if (children[i] instanceof IResource) { 75 IResource resource= (IResource) children[i]; 76 if (fValidator.canElementBeDestination(resource) || fValidator.canChildrenBeDestinations(resource)) 77 result.add(resource); 78 } 79 } 80 return result.toArray(); 81 } 82 } catch (JavaModelException e) { 83 JavaPlugin.log(e); 84 return new Object [0]; 85 } 86 } 87 88 private Object [] doGetChildren(Object parentElement) { 89 if (parentElement instanceof IContainer) { 90 final IContainer container= (IContainer) parentElement; 91 return getResources(container); 92 } 93 return super.getChildren(parentElement); 94 } 95 96 private Object [] getResources(IContainer container) { 98 try { 99 IResource[] members= container.members(); 100 IJavaProject javaProject= JavaCore.create(container.getProject()); 101 if (javaProject == null || !javaProject.exists()) 102 return members; 103 boolean isFolderOnClasspath = javaProject.isOnClasspath(container); 104 List nonJavaResources= new ArrayList (); 105 for (int i= 0; i < members.length; i++) { 107 IResource member= members[i]; 108 if (isFolderOnClasspath) { 113 if (javaProject.findPackageFragmentRoot(member.getFullPath()) == null) { 114 nonJavaResources.add(member); 115 } 116 } else if (!javaProject.isOnClasspath(member)) { 117 nonJavaResources.add(member); 118 } 119 } 120 return nonJavaResources.toArray(); 121 } catch(CoreException e) { 122 return NO_CHILDREN; 123 } 124 } 125 126 private static Object [] getOpenNonJavaProjects(IJavaModel model) throws JavaModelException { 127 Object [] nonJavaProjects= model.getNonJavaResources(); 128 ArrayList result= new ArrayList (nonJavaProjects.length); 129 for (int i= 0; i < nonJavaProjects.length; i++) { 130 IProject project = (IProject) nonJavaProjects[i]; 131 if (project.isOpen()) 132 result.add(project); 133 } 134 return result.toArray(); 135 } 136 137 } 138 | Popular Tags |