1 11 package org.eclipse.jdt.internal.ui; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 18 import org.eclipse.ui.IContainmentAdapter; 19 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaModel; 22 import org.eclipse.jdt.core.JavaCore; 23 24 public class JavaElementContainmentAdapter implements IContainmentAdapter { 25 26 private IJavaModel fJavaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 27 28 public boolean contains(Object workingSetElement, Object element, int flags) { 29 if (!(workingSetElement instanceof IJavaElement) || element == null) 30 return false; 31 32 IJavaElement workingSetJavaElement= (IJavaElement)workingSetElement; 33 IResource resource= null; 34 IJavaElement jElement= null; 35 if (element instanceof IJavaElement) { 36 jElement= (IJavaElement)element; 37 resource= jElement.getResource(); 38 } else { 39 if (element instanceof IAdaptable) { 40 resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); 41 if (resource != null) { 42 if (fJavaModel.contains(resource)) { 43 jElement= JavaCore.create(resource); 44 if (jElement != null && !jElement.exists()) 45 jElement= null; 46 } 47 } 48 } 49 } 50 51 if (jElement != null) { 52 if (contains(workingSetJavaElement, jElement, flags)) 53 return true; 54 if (workingSetJavaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT && 55 resource.getType() == IResource.FOLDER && checkIfDescendant(flags)) 56 return isChild(workingSetJavaElement, resource); 57 } else if (resource != null) { 58 return contains(workingSetJavaElement, resource, flags); 59 } 60 return false; 61 } 62 63 private boolean contains(IJavaElement workingSetElement, IJavaElement element, int flags) { 64 if (checkContext(flags) && workingSetElement.equals(element)) { 65 return true; 66 } 67 if (checkIfChild(flags) && workingSetElement.equals(element.getParent())) { 68 return true; 69 } 70 if (checkIfDescendant(flags) && check(workingSetElement, element)) { 71 return true; 72 } 73 if (checkIfAncestor(flags) && check(element, workingSetElement)) { 74 return true; 75 } 76 return false; 77 } 78 79 private boolean check(IJavaElement ancestor, IJavaElement descendent) { 80 descendent= descendent.getParent(); 81 while (descendent != null) { 82 if (ancestor.equals(descendent)) 83 return true; 84 descendent= descendent.getParent(); 85 } 86 return false; 87 } 88 89 private boolean isChild(IJavaElement workingSetElement, IResource element) { 90 IResource resource= workingSetElement.getResource(); 91 if (resource == null) 92 return false; 93 return check(element, resource); 94 } 95 96 private boolean contains(IJavaElement workingSetElement, IResource element, int flags) { 97 IResource workingSetResource= workingSetElement.getResource(); 98 if (workingSetResource == null) 99 return false; 100 if (checkContext(flags) && workingSetResource.equals(element)) { 101 return true; 102 } 103 if (checkIfChild(flags) && workingSetResource.equals(element.getParent())) { 104 return true; 105 } 106 if (checkIfDescendant(flags) && check(workingSetResource, element)) { 107 return true; 108 } 109 if (checkIfAncestor(flags) && check(element, workingSetResource)) { 110 return true; 111 } 112 return false; 113 } 114 115 private boolean check(IResource ancestor, IResource descendent) { 116 descendent= descendent.getParent(); 117 while(descendent != null) { 118 if (ancestor.equals(descendent)) 119 return true; 120 descendent= descendent.getParent(); 121 } 122 return false; 123 } 124 125 private boolean checkIfDescendant(int flags) { 126 return (flags & CHECK_IF_DESCENDANT) != 0; 127 } 128 129 private boolean checkIfAncestor(int flags) { 130 return (flags & CHECK_IF_ANCESTOR) != 0; 131 } 132 133 private boolean checkIfChild(int flags) { 134 return (flags & CHECK_IF_CHILD) != 0; 135 } 136 137 private boolean checkContext(int flags) { 138 return (flags & CHECK_CONTEXT) != 0; 139 } 140 } 141 | Popular Tags |