KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > SearchResultUpdater


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
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         // TODO make this work with resources
41
}
42
43     public void elementChanged(ElementChangedEvent event) {
44         //long t0= System.currentTimeMillis();
45
IJavaElementDelta delta= event.getDelta();
46         Set JavaDoc removedElements= new HashSet JavaDoc();
47         Set JavaDoc potentiallyRemovedElements= new HashSet JavaDoc();
48         collectRemoved(potentiallyRemovedElements, removedElements, delta);
49         if (removedElements.size() > 0)
50             handleRemoved(removedElements);
51         if (potentiallyRemovedElements.size() > 0)
52             handleRemoved(potentiallyRemovedElements);
53         //System.out.println(this+"handled delta in: "+(System.currentTimeMillis()-t0));
54
}
55
56     private void handleRemoved(Set JavaDoc removedElements) {
57         Object JavaDoc[] 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                         //System.out.println("removing: "+je+" in "+fResult.getUserData());
64
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                         //System.out.println("removing: "+resource+" in "+fResult.getUserData());
73
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 JavaDoc removedElements, Object JavaDoc object) {
85         for (Iterator JavaDoc 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 JavaDoc ancestor, Object JavaDoc descendant) {
93         while (descendant != null && !ancestor.equals(descendant))
94             descendant= getParent(descendant);
95         return descendant != null;
96     }
97
98     private Object JavaDoc getParent(Object JavaDoc 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 JavaDoc potentiallyRemovedSet, Set JavaDoc 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         // don't care
130
}
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 JavaDoc 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         // not interested
152
}
153
154     public void queryFinished(ISearchQuery query) {
155         // not interested
156
}
157
158 }
159
Popular Tags