KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.search.internal.ui.text;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceProxy;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21
22 import org.eclipse.ui.actions.ActionGroup;
23
24 import org.eclipse.search.ui.IActionGroupFactory;
25 import org.eclipse.search.ui.ISearchResultView;
26 import org.eclipse.search.ui.SearchUI;
27
28 import org.eclipse.search.internal.core.text.ITextSearchResultCollector;
29 import org.eclipse.search.internal.ui.SearchMessages;
30 import org.eclipse.search.internal.ui.util.FileLabelProvider;
31
32 public class TextSearchResultCollector implements ITextSearchResultCollector {
33     
34     private static final String JavaDoc MATCH= SearchMessages.getString("SearchResultCollector.match"); //$NON-NLS-1$
35
private static final String JavaDoc MATCHES= SearchMessages.getString("SearchResultCollector.matches"); //$NON-NLS-1$
36
private static final String JavaDoc DONE= SearchMessages.getString("SearchResultCollector.done"); //$NON-NLS-1$
37

38     private IProgressMonitor fMonitor;
39     private ISearchResultView fView;
40     private TextSearchOperation fOperation;
41     private int fMatchCount= 0;
42     private Integer JavaDoc[] fMessageFormatArgs= new Integer JavaDoc[1];
43     private long fLastUpdateTime;
44
45
46     private static class TextSearchActionGroupFactory implements IActionGroupFactory {
47         public ActionGroup createActionGroup(ISearchResultView part) {
48             return new TextSearchActionGroup(part);
49         }
50     }
51             
52     /**
53      * Returns the progress monitor used to setup and report progress.
54      */

55     public IProgressMonitor getProgressMonitor() {
56         return fMonitor;
57     }
58     
59     void setProgressMonitor(IProgressMonitor pm) {
60         fMonitor= pm;
61     }
62     
63     /**
64      * Called before the actual search starts.
65      */

66     public void aboutToStart() throws CoreException {
67         fView= SearchUI.getSearchResultView();
68         fMatchCount= 0;
69         fLastUpdateTime= 0;
70         if (fView != null) {
71             fView.searchStarted(
72                 new TextSearchActionGroupFactory(),
73                 fOperation.getSingularLabel(),
74                 fOperation.getPluralLabelPattern(),
75                 fOperation.getImageDescriptor(),
76                 TextSearchPage.EXTENSION_POINT_ID,
77                 new FileLabelProvider(FileLabelProvider.SHOW_LABEL_PATH),
78                 new GotoMarkerAction(),
79                 new GroupByKeyComputer(),
80                 fOperation);
81         }
82     }
83      
84     /**
85      * Accepts the given search result.
86      */

87     public void accept(final IResourceProxy proxy, String JavaDoc line, int start, int length, final int lineNumber) throws CoreException {
88         IResource resource= proxy.requestResource();
89         IMarker marker= resource.createMarker(SearchUI.SEARCH_MARKER);
90         HashMap JavaDoc attributes= new HashMap JavaDoc(4);
91         attributes.put(SearchUI.LINE, line);
92         attributes.put(IMarker.CHAR_START, new Integer JavaDoc(start));
93         attributes.put(IMarker.CHAR_END, new Integer JavaDoc(start + length));
94         attributes.put(IMarker.LINE_NUMBER, new Integer JavaDoc(lineNumber));
95         marker.setAttributes(attributes);
96         
97         String JavaDoc description= resource.getFullPath().lastSegment();
98         if (description == null)
99             description= ""; //$NON-NLS-1$
100

101         fView.addMatch(description, resource, resource, marker);
102         
103         fMatchCount++;
104
105         if (!getProgressMonitor().isCanceled() && System.currentTimeMillis() - fLastUpdateTime > 1000) {
106             getProgressMonitor().subTask(getFormattedMatchesString(fMatchCount));
107             fLastUpdateTime= System.currentTimeMillis();
108         }
109     }
110     
111     /**
112      * Called when the search has ended.
113      */

114     public void done() {
115         if (!getProgressMonitor().isCanceled()) {
116             String JavaDoc matchesString= getFormattedMatchesString(fMatchCount);
117             getProgressMonitor().setTaskName(MessageFormat.format(DONE, new String JavaDoc[]{matchesString}));
118         }
119
120         if (fView != null)
121             fView.searchFinished();
122             
123         // Cut no longer unused references because the collector might be re-used
124
fView= null;
125         fMonitor= null;
126     }
127
128     void setOperation(TextSearchOperation operation) {
129         fOperation= operation;
130     }
131
132     private String JavaDoc getFormattedMatchesString(int count) {
133         if (fMatchCount == 1)
134             return MATCH;
135         fMessageFormatArgs[0]= new Integer JavaDoc(count);
136         return MessageFormat.format(MATCHES, fMessageFormatArgs);
137
138     }
139 }
140
Popular Tags