KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.ui.search;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import org.eclipse.core.resources.IMarker;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMember;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.core.search.IJavaSearchResultCollector;
24 import org.eclipse.jdt.internal.ui.JavaPlugin;
25 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
26 import org.eclipse.jdt.ui.JavaUI;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.search.ui.IActionGroupFactory;
30 import org.eclipse.search.ui.ISearchResultView;
31 import org.eclipse.search.ui.SearchUI;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.ide.IDE;
35
36
37 public class JavaSearchResultCollector implements IJavaSearchResultCollector {
38
39     private static final String JavaDoc MATCH= SearchMessages.getString("SearchResultCollector.match"); //$NON-NLS-1$
40
private static final String JavaDoc MATCHES= SearchMessages.getString("SearchResultCollector.matches"); //$NON-NLS-1$
41
private static final String JavaDoc DONE= SearchMessages.getString("SearchResultCollector.done"); //$NON-NLS-1$
42
private static final Boolean JavaDoc POTENTIAL_MATCH_VALUE= new Boolean JavaDoc(true);
43     private static final String JavaDoc POTENTIAL_MATCH_DIALOG_ID= "Search.PotentialMatchDialog"; //$NON-NLS-1$
44

45     private IProgressMonitor fMonitor;
46     private ISearchResultView fView;
47     private JavaSearchOperation fOperation;
48     private int fMatchCount= 0;
49     private int fPotentialMatchCount= 0;
50     private long fLastUpdateTime;
51     private Integer JavaDoc[] fMessageFormatArgs= new Integer JavaDoc[1];
52     
53     private class ActionGroupFactory implements IActionGroupFactory {
54         public ActionGroup createActionGroup(ISearchResultView part) {
55             return new SearchViewActionGroup(part);
56         }
57     }
58     
59     public JavaSearchResultCollector() {
60         // This ensures that the image class is loaded correctly
61
JavaPlugin.getDefault().getImageRegistry();
62     }
63
64     /**
65      * @see IJavaSearchResultCollector#aboutToStart().
66      */

67     public void aboutToStart() {
68         fPotentialMatchCount= 0;
69         fView= SearchUI.getSearchResultView();
70         fMatchCount= 0;
71         fLastUpdateTime= 0;
72         if (fView != null) {
73             fView.searchStarted(
74                 new ActionGroupFactory(),
75                 fOperation.getSingularLabel(),
76                 fOperation.getPluralLabelPattern(),
77                 fOperation.getImageDescriptor(),
78                 JavaSearchPage.EXTENSION_POINT_ID,
79                 new JavaSearchResultLabelProvider(),
80                 new GotoMarkerAction(),
81                 new GroupByKeyComputer(),
82                 fOperation);
83         }
84     }
85     
86     /**
87      * @see IJavaSearchResultCollector#accept
88      */

89     public void accept(IResource resource, int start, int end, IJavaElement enclosingElement, int accuracy) throws CoreException {
90         if (enclosingElement == null || accuracy == POTENTIAL_MATCH && SearchUI.arePotentialMatchesIgnored())
91             return;
92         
93         IMarker marker= resource.createMarker(SearchUI.SEARCH_MARKER);
94         HashMap JavaDoc attributes;
95         Object JavaDoc groupKey= enclosingElement;
96         attributes= new HashMap JavaDoc(7);
97         if (accuracy == POTENTIAL_MATCH) {
98             fPotentialMatchCount++;
99             attributes.put(SearchUI.POTENTIAL_MATCH, POTENTIAL_MATCH_VALUE);
100             if (groupKey == null)
101                 groupKey= "?:null"; //$NON-NLS-1$
102
else
103                 groupKey= "?:" + enclosingElement.getHandleIdentifier(); //$NON-NLS-1$
104
}
105         ICompilationUnit cu= SearchUtil.findCompilationUnit(enclosingElement);
106         if (cu != null && cu.isWorkingCopy())
107             attributes.put(IJavaSearchUIConstants.ATT_IS_WORKING_COPY, new Boolean JavaDoc(true)); //$NON-NLS-1$
108

109         JavaCore.addJavaElementMarkerAttributes(attributes, enclosingElement);
110         attributes.put(IJavaSearchUIConstants.ATT_JE_HANDLE_ID, enclosingElement.getHandleIdentifier());
111         attributes.put(IMarker.CHAR_START, new Integer JavaDoc(Math.max(start, 0)));
112         attributes.put(IMarker.CHAR_END, new Integer JavaDoc(Math.max(end, 0)));
113         if (enclosingElement instanceof IMember && ((IMember)enclosingElement).isBinary())
114             attributes.put(IDE.EDITOR_ID_ATTR, JavaUI.ID_CF_EDITOR);
115         else
116             attributes.put(IDE.EDITOR_ID_ATTR, JavaUI.ID_CU_EDITOR);
117         marker.setAttributes(attributes);
118
119         if (fView != null)
120             fView.addMatch(enclosingElement.getElementName(), groupKey, resource, marker);
121         fMatchCount++;
122         if (!getProgressMonitor().isCanceled() && System.currentTimeMillis() - fLastUpdateTime > 1000) {
123             getProgressMonitor().subTask(getFormattedMatchesString(fMatchCount));
124             fLastUpdateTime= System.currentTimeMillis();
125         }
126     }
127     
128     /**
129      * @see IJavaSearchResultCollector#done().
130      */

131     public void done() {
132         if (!getProgressMonitor().isCanceled()) {
133             String JavaDoc matchesString= getFormattedMatchesString(fMatchCount);
134             getProgressMonitor().setTaskName(MessageFormat.format(DONE, new String JavaDoc[]{matchesString}));
135         }
136
137         if (fView != null) {
138             if (fPotentialMatchCount > 0)
139                 explainPotentialMatch(fPotentialMatchCount);
140             fView.searchFinished();
141         }
142
143         // Cut no longer unused references because the collector might be re-used
144
fView= null;
145         fMonitor= null;
146     }
147
148     private void explainPotentialMatch(final int potentialMatchCount) {
149         // Make sure we are doing it in the right thread.
150
final Shell shell= fView.getSite().getShell();
151         final String JavaDoc title;
152         if (potentialMatchCount == 1)
153             title= new String JavaDoc(SearchMessages.getString("Search.potentialMatchDialog.title.foundPotentialMatch")); //$NON-NLS-1$
154
else
155             title= new String JavaDoc(SearchMessages.getFormattedString("Search.potentialMatchDialog.title.foundPotentialMatches", "" + potentialMatchCount)); //$NON-NLS-1$ //$NON-NLS-2$
156

157         shell.getDisplay().syncExec(new Runnable JavaDoc() {
158             public void run() {
159                 OptionalMessageDialog.open(
160                     POTENTIAL_MATCH_DIALOG_ID, //$NON-NLS-1$,
161
shell,
162                     title,
163                     null,
164                     SearchMessages.getString("Search.potentialMatchDialog.message"), //$NON-NLS-1$,
165
MessageDialog.INFORMATION,
166                     new String JavaDoc[] { IDialogConstants.OK_LABEL },
167                     0);
168             }
169         });
170     }
171     
172     /*
173      * @see IJavaSearchResultCollector#getProgressMonitor().
174      */

175     public IProgressMonitor getProgressMonitor() {
176         return fMonitor;
177     }
178     
179     void setProgressMonitor(IProgressMonitor pm) {
180         fMonitor= pm;
181     }
182     
183     void setOperation(JavaSearchOperation operation) {
184         fOperation= operation;
185     }
186     
187     private String JavaDoc getFormattedMatchesString(int count) {
188         if (fMatchCount == 1)
189             return MATCH;
190         fMessageFormatArgs[0]= new Integer JavaDoc(count);
191         return MessageFormat.format(MATCHES, fMessageFormatArgs);
192
193     }
194 }
195
Popular Tags