1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IResourceDelta; 18 import org.eclipse.jdt.core.ElementChangedEvent; 19 import org.eclipse.jdt.core.IElementChangedListener; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaElementDelta; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.search.ui.IQueryListener; 24 import org.eclipse.search.ui.ISearchQuery; 25 import org.eclipse.search.ui.NewSearchUI; 26 import org.eclipse.search.ui.text.Match; 27 28 public class SearchResultUpdater implements IElementChangedListener, IQueryListener { 29 30 JavaSearchResult fResult; 31 private static final int REMOVED_FLAGS= IJavaElementDelta.F_MOVED_TO | 32 IJavaElementDelta.F_REMOVED_FROM_CLASSPATH | 33 IJavaElementDelta.F_CLOSED | 34 IJavaElementDelta.F_CONTENT; 35 36 public SearchResultUpdater(JavaSearchResult result) { 37 fResult= result; 38 NewSearchUI.addQueryListener(this); 39 JavaCore.addElementChangedListener(this); 40 } 42 43 public void elementChanged(ElementChangedEvent event) { 44 IJavaElementDelta delta= event.getDelta(); 46 Set removedElements= new HashSet (); 47 Set potentiallyRemovedElements= new HashSet (); 48 collectRemoved(potentiallyRemovedElements, removedElements, delta); 49 if (removedElements.size() > 0) 50 handleRemoved(removedElements); 51 if (potentiallyRemovedElements.size() > 0) 52 handleRemoved(potentiallyRemovedElements); 53 } 55 56 private void handleRemoved(Set removedElements) { 57 Object [] elements= fResult.getElements(); 58 for (int i= 0; i < elements.length; i++) { 59 if (isContainedInRemoved(removedElements, elements[i])) { 60 if (elements[i] instanceof IJavaElement) { 61 IJavaElement je= (IJavaElement)elements[i]; 62 if (!je.exists()) { 63 Match[] matches= fResult.getMatches(elements[i]); 65 for (int j= 0; j < matches.length; j++) { 66 fResult.removeMatch(matches[j]); 67 } 68 } 69 } else if (elements[i] instanceof IResource) { 70 IResource resource= (IResource)elements[i]; 71 if (!resource.exists()) { 72 Match[] matches= fResult.getMatches(elements[i]); 74 for (int j= 0; j < matches.length; j++) { 75 fResult.removeMatch(matches[j]); 76 } 77 } 78 79 } 80 } 81 } 82 } 83 84 private boolean isContainedInRemoved(Set removedElements, Object object) { 85 for (Iterator elements= removedElements.iterator(); elements.hasNext();) { 86 if (isParentOf(elements.next(), object)) 87 return true; 88 } 89 return false; 90 } 91 92 private boolean isParentOf(Object ancestor, Object descendant) { 93 while (descendant != null && !ancestor.equals(descendant)) 94 descendant= getParent(descendant); 95 return descendant != null; 96 } 97 98 private Object getParent(Object object) { 99 if (object instanceof IJavaElement) 100 return ((IJavaElement)object).getParent(); 101 else if (object instanceof IResource) 102 return ((IResource)object).getParent(); 103 return null; 104 } 105 106 private void collectRemoved(Set potentiallyRemovedSet, Set removedElements, IJavaElementDelta delta) { 107 if (delta.getKind() == IJavaElementDelta.REMOVED) 108 removedElements.add(delta.getElement()); 109 else if (delta.getKind() == IJavaElementDelta.CHANGED) { 110 int flags= delta.getFlags(); 111 if ((flags & REMOVED_FLAGS) != 0) { 112 potentiallyRemovedSet.add(delta.getElement()); 113 } else { 114 IJavaElementDelta[] childDeltas= delta.getAffectedChildren(); 115 for (int i= 0; i < childDeltas.length; i++) { 116 collectRemoved(potentiallyRemovedSet, removedElements, childDeltas[i]); 117 } 118 } 119 } 120 IResourceDelta[] resourceDeltas= delta.getResourceDeltas(); 121 if (resourceDeltas != null) { 122 for (int i= 0; i < resourceDeltas.length; i++) { 123 collectRemovals(removedElements, resourceDeltas[i]); 124 } 125 } 126 } 127 128 public void queryAdded(ISearchQuery query) { 129 } 131 132 public void queryRemoved(ISearchQuery query) { 133 if (fResult.equals(query.getSearchResult())) { 134 JavaCore.removeElementChangedListener(this); 135 NewSearchUI.removeQueryListener(this); 136 } 137 } 138 139 private void collectRemovals(Set removals, IResourceDelta delta) { 140 if (delta.getKind() == IResourceDelta.REMOVED) 141 removals.add(delta.getResource()); 142 else { 143 IResourceDelta[] children= delta.getAffectedChildren(); 144 for (int i= 0; i < children.length; i++) { 145 collectRemovals(removals, children[i]); 146 } 147 } 148 } 149 150 public void queryStarting(ISearchQuery query) { 151 } 153 154 public void queryFinished(ISearchQuery query) { 155 } 157 158 } 159 | Popular Tags |