KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.internal.ui.text;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IResourceChangeEvent;
18 import org.eclipse.core.resources.IResourceChangeListener;
19 import org.eclipse.core.resources.IResourceDelta;
20 import org.eclipse.core.resources.IResourceDeltaVisitor;
21 import org.eclipse.core.resources.ResourcesPlugin;
22
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.AbstractTextSearchResult;
27 import org.eclipse.search.ui.text.Match;
28
29 import org.eclipse.search.internal.ui.SearchPlugin;
30
31 public class SearchResultUpdater implements IResourceChangeListener, IQueryListener {
32     private AbstractTextSearchResult fResult;
33
34     public SearchResultUpdater(AbstractTextSearchResult result) {
35         fResult= result;
36         NewSearchUI.addQueryListener(this);
37         ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
38     }
39
40     public void resourceChanged(IResourceChangeEvent event) {
41         IResourceDelta delta= event.getDelta();
42         if (delta != null)
43             handleDelta(delta);
44     }
45
46     private void handleDelta(IResourceDelta d) {
47         try {
48             d.accept(new IResourceDeltaVisitor() {
49                 public boolean visit(IResourceDelta delta) throws CoreException {
50                     switch (delta.getKind()) {
51                         case IResourceDelta.ADDED :
52                             return false;
53                         case IResourceDelta.REMOVED :
54                             IResource res= delta.getResource();
55                             if (res instanceof IFile) {
56                                 Match[] matches= fResult.getMatches(res);
57                                 fResult.removeMatches(matches);
58                             }
59                             break;
60                         case IResourceDelta.CHANGED :
61                             // handle changed resource
62
break;
63                     }
64                     return true;
65                 }
66             });
67         } catch (CoreException e) {
68             SearchPlugin.log(e);
69         }
70     }
71
72     public void queryAdded(ISearchQuery query) {
73         // don't care
74
}
75
76     public void queryRemoved(ISearchQuery query) {
77         if (fResult.equals(query.getSearchResult())) {
78             ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
79             NewSearchUI.removeQueryListener(this);
80         }
81     }
82     
83     public void queryStarting(ISearchQuery query) {
84         // don't care
85
}
86
87     public void queryFinished(ISearchQuery query) {
88         // don't care
89
}
90 }
91
Popular Tags