KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > SearchResultView


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  * Michael Fraenkel (fraenkel@us.ibm.com) - contributed a fix for:
11  * o New search view sets incorrect title
12  * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=60966)
13  *******************************************************************************/

14 package org.eclipse.search.internal.ui;
15
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22
23 import org.eclipse.core.resources.IMarker;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.action.IToolBarManager;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.util.IPropertyChangeListener;
34 import org.eclipse.jface.util.PropertyChangeEvent;
35 import org.eclipse.jface.viewers.IBaseLabelProvider;
36 import org.eclipse.jface.viewers.ILabelProvider;
37 import org.eclipse.jface.viewers.ISelection;
38
39 import org.eclipse.ui.IActionBars;
40 import org.eclipse.ui.IMemento;
41 import org.eclipse.ui.IViewSite;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.actions.ActionFactory;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.part.CellEditorActionHandler;
46 import org.eclipse.ui.part.ViewPart;
47
48 import org.eclipse.search.ui.IActionGroupFactory;
49 import org.eclipse.search.ui.IContextMenuContributor;
50 import org.eclipse.search.ui.IGroupByKeyComputer;
51 import org.eclipse.search.ui.ISearchResultView;
52
53 /**
54  * @deprecated old search
55  */

56 public class SearchResultView extends ViewPart implements ISearchResultView {
57
58
59     private static Map JavaDoc fgLabelProviders= new HashMap JavaDoc(5);
60     
61     private SearchResultViewer fViewer;
62     private Map JavaDoc fResponse;
63     private IMemento fMemento;
64     private IPropertyChangeListener fPropertyChangeListener;
65     private CellEditorActionHandler fCellEditorActionHandler;
66     private SelectAllAction fSelectAllAction;
67
68     /*
69      * Implements method from IViewPart.
70      */

71     public void init(IViewSite site, IMemento memento) throws PartInitException {
72         super.init(site, memento);
73         fMemento= memento;
74     }
75
76     /*
77      * Implements method from IViewPart.
78      */

79     public void saveState(IMemento memento) {
80         if (fViewer == null) {
81             // part has not been created
82
if (fMemento != null) //Keep the old state;
83
memento.putMemento(fMemento);
84             return;
85         }
86         fViewer.saveState(memento);
87     }
88
89     /**
90      * Creates the search list inner viewer.
91      */

92     public void createPartControl(Composite parent) {
93         Assert.isTrue(fViewer == null);
94         fViewer= new SearchResultViewer(this, parent);
95         if (fMemento != null)
96             fViewer.restoreState(fMemento);
97         fMemento= null;
98         SearchManager.getDefault().addSearchChangeListener(fViewer);
99         fViewer.init();
100
101         // Add selectAll action handlers.
102
fCellEditorActionHandler = new CellEditorActionHandler(getViewSite().getActionBars());
103         fSelectAllAction= new SelectAllAction();
104         fSelectAllAction.setViewer(fViewer);
105         fCellEditorActionHandler.setSelectAllAction(fSelectAllAction);
106
107         fillActionBars(getViewSite().getActionBars());
108         
109         fPropertyChangeListener= new IPropertyChangeListener() {
110             public void propertyChange(PropertyChangeEvent event) {
111                 if (SearchPreferencePage.POTENTIAL_MATCH_FG_COLOR.equals(event.getProperty()) || SearchPreferencePage.EMPHASIZE_POTENTIAL_MATCHES.equals(event.getProperty()))
112                     if (fViewer != null)
113                         fViewer.updatedPotentialMatchFgColor();
114             }
115         };
116         
117         SearchPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
118         
119         PlatformUI.getWorkbench().getHelpSystem().setHelp(fViewer.getControl(), SearchPlugin.getDefault().getSearchViewHelpContextId());
120     }
121     
122     /**
123      * Returns the search result viewer.
124      */

125     public SearchResultViewer getViewer() {
126         return fViewer;
127     }
128     
129     //---- IWorkbenchPart ------------------------------------------------------
130

131
132     public void setFocus() {
133         fViewer.getControl().setFocus();
134     }
135     
136     public void dispose() {
137         if (fViewer != null) {
138             SearchManager.getDefault().removeSearchChangeListener(fViewer);
139             fViewer= null;
140         }
141         if (fPropertyChangeListener != null)
142             SearchPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
143         if (fCellEditorActionHandler != null) {
144             fCellEditorActionHandler.dispose();
145             fCellEditorActionHandler= null;
146         }
147         super.dispose();
148     }
149     
150     protected void setContentDescription(String JavaDoc title) {
151         super.setContentDescription(title);
152     }
153     
154     protected void setTitleToolTip(String JavaDoc text) {
155         super.setTitleToolTip(text);
156     }
157     
158     //---- Adding Action to Toolbar -------------------------------------------
159

160     private void fillActionBars(IActionBars actionBars) {
161         IToolBarManager toolBar= actionBars.getToolBarManager();
162         fillToolBar(toolBar);
163         actionBars.updateActionBars();
164         
165         // Add selectAll action handlers.
166
actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction);
167     }
168
169     private void fillToolBar(IToolBarManager tbm) {
170         fViewer.fillToolBar(tbm);
171     }
172
173     ILabelProvider getLabelProvider(String JavaDoc pageId) {
174         if (pageId != null)
175             return (ILabelProvider)fgLabelProviders.get(pageId);
176         return null;
177     }
178
179     public ILabelProvider getLabelProvider() {
180         if (fViewer == null)
181             return null;
182         IBaseLabelProvider labelProvider= fViewer.getLabelProvider();
183         if (labelProvider == null)
184             return null;
185         
186         return ((SearchResultLabelProvider)labelProvider).getLabelProvider();
187     }
188
189     private void setGotoMarkerAction(final IAction gotoMarkerAction) {
190         // Make sure we are doing it in the right thread.
191
getDisplay().syncExec(new Runnable JavaDoc() {
192             public void run() {
193                 getViewer().setGotoMarkerAction(gotoMarkerAction);
194             }
195         });
196     }
197
198
199     Display getDisplay() {
200         return fViewer.getControl().getDisplay();
201     }
202
203
204     //---- ISearchResultView --------------------------------------------------
205

206
207     /*
208      * Implements method from ISearchResultView
209      */

210     public ISelection getSelection() {
211         return fViewer.getSelection();
212     }
213
214     /*
215      * Implements method from ISearchResultView
216      */

217     public void searchStarted(
218                 IActionGroupFactory groupFactory,
219                 String JavaDoc singularLabel,
220                 String JavaDoc pluralLabelPattern,
221                 ImageDescriptor imageDescriptor,
222                 String JavaDoc pageId,
223                 ILabelProvider labelProvider,
224                 IAction gotoAction,
225                 IGroupByKeyComputer groupByKeyComputer,
226                 IRunnableWithProgress operation) {
227
228
229         Assert.isNotNull(pageId);
230         Assert.isNotNull(pluralLabelPattern);
231         Assert.isNotNull(gotoAction);
232
233         fResponse= new HashMap JavaDoc(500);
234         setGotoMarkerAction(gotoAction);
235
236         ILabelProvider oldLabelProvider= (ILabelProvider)fgLabelProviders.get(pageId);
237         if (oldLabelProvider != null)
238             oldLabelProvider.dispose();
239         fgLabelProviders.put(pageId, labelProvider);
240
241         SearchManager.getDefault().addNewSearch(
242             new Search(
243                 pageId,
244                 singularLabel,
245                 pluralLabelPattern,
246                 null,
247                 imageDescriptor,
248                 fViewer.getGotoMarkerAction(),
249                 groupFactory,
250                 groupByKeyComputer,
251                 operation));
252     }
253
254     /**
255      * Implements method from ISearchResultView
256      * @deprecated As of build > 20011107, replaced by the new version with additonal parameter
257      */

258     public void searchStarted(
259                 String JavaDoc pageId,
260                 String JavaDoc label,
261                 ImageDescriptor imageDescriptor,
262                 IContextMenuContributor contributor,
263                 ILabelProvider labelProvider,
264                 IAction gotoAction,
265                 IGroupByKeyComputer groupByKeyComputer,
266                 IRunnableWithProgress operation) {
267         
268         searchStarted(pageId, null, label, imageDescriptor, contributor, labelProvider, gotoAction, groupByKeyComputer, operation);
269     }
270
271     /**
272      * Implements method from ISearchResultView
273      * @deprecated As of build > 20020514
274      */

275     public void searchStarted(
276                 String JavaDoc pageId,
277                 String JavaDoc singularLabel,
278                 String JavaDoc pluralLabelPattern,
279                 ImageDescriptor imageDescriptor,
280                 IContextMenuContributor contributor,
281                 ILabelProvider labelProvider,
282                 IAction gotoAction,
283                 IGroupByKeyComputer groupByKeyComputer,
284                 IRunnableWithProgress operation) {
285
286
287         Assert.isNotNull(pageId);
288         Assert.isNotNull(pluralLabelPattern);
289         Assert.isNotNull(gotoAction);
290
291         fResponse= new HashMap JavaDoc(500);
292         setGotoMarkerAction(gotoAction);
293
294         ILabelProvider oldLabelProvider= (ILabelProvider)fgLabelProviders.get(pageId);
295         if (oldLabelProvider != null)
296             oldLabelProvider.dispose();
297         fgLabelProviders.put(pageId, labelProvider);
298
299         SearchManager.getDefault().addNewSearch(
300             new Search(
301                 pageId,
302                 singularLabel,
303                 pluralLabelPattern,
304                 null,
305                 imageDescriptor,
306                 fViewer.getGotoMarkerAction(),
307                 contributor,
308                 groupByKeyComputer,
309                 operation));
310     }
311
312     /*
313      * Implements method from ISearchResultView
314      */

315     public void addMatch(String JavaDoc description, Object JavaDoc groupByKey, IResource resource, IMarker marker) {
316         SearchResultViewEntry entry= (SearchResultViewEntry)fResponse.get(groupByKey);
317         if (entry == null) {
318             entry= new SearchResultViewEntry(groupByKey, resource);
319             fResponse.put(groupByKey, entry);
320         }
321         entry.add(marker);
322     }
323
324
325     /*
326      * Implements method from ISearchResultView
327      */

328     public void searchFinished() {
329         SearchManager.getDefault().searchFinished(new ArrayList JavaDoc(fResponse.values()));
330         fResponse= null;
331     }
332 }
333
Popular Tags