KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IAdaptable;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IMarker;
21
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.Transfer;
24
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.MenuManager;
27 import org.eclipse.jface.viewers.DecoratingLabelProvider;
28 import org.eclipse.jface.viewers.ILabelProvider;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredViewer;
32 import org.eclipse.jface.viewers.TableViewer;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.viewers.ViewerComparator;
36
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IMemento;
39 import org.eclipse.ui.IPageLayout;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.actions.ActionContext;
43 import org.eclipse.ui.actions.ActionGroup;
44 import org.eclipse.ui.ide.IDE;
45 import org.eclipse.ui.part.IPageSite;
46 import org.eclipse.ui.part.IShowInTargetList;
47 import org.eclipse.ui.part.ResourceTransfer;
48 import org.eclipse.ui.texteditor.ITextEditor;
49
50 import org.eclipse.search.ui.IContextMenuConstants;
51 import org.eclipse.search.ui.ISearchResultViewPart;
52 import org.eclipse.search.ui.NewSearchUI;
53 import org.eclipse.search.ui.text.AbstractTextSearchResult;
54 import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
55 import org.eclipse.search.ui.text.Match;
56
57 import org.eclipse.search.internal.ui.Messages;
58 import org.eclipse.search.internal.ui.SearchMessages;
59
60 import org.eclipse.search2.internal.ui.OpenSearchPreferencesAction;
61
62
63 public class FileSearchPage extends AbstractTextSearchViewPage implements IAdaptable {
64     
65     public static class DecoratorIgnoringViewerSorter extends ViewerComparator {
66         private final ILabelProvider fLabelProvider;
67         
68         public DecoratorIgnoringViewerSorter(ILabelProvider labelProvider) {
69             fLabelProvider= labelProvider;
70         }
71         
72         /* (non-Javadoc)
73          * @see org.eclipse.jface.viewers.ViewerComparator#category(java.lang.Object)
74          */

75         public int category(Object JavaDoc element) {
76             if (element instanceof IContainer) {
77                 return 1;
78             }
79             return 2;
80         }
81         
82         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
83             int cat1 = category(e1);
84             int cat2 = category(e2);
85
86             if (cat1 != cat2) {
87                 return cat1 - cat2;
88             }
89             
90             String JavaDoc name1= fLabelProvider.getText(e1);
91             String JavaDoc name2= fLabelProvider.getText(e2);
92             if (name1 == null)
93                 name1 = "";//$NON-NLS-1$
94
if (name2 == null)
95                 name2 = "";//$NON-NLS-1$
96
return getComparator().compare(name1, name2);
97         }
98     }
99     
100     private static final String JavaDoc KEY_SORTING= "org.eclipse.search.resultpage.sorting"; //$NON-NLS-1$
101
private static final String JavaDoc KEY_LIMIT= "org.eclipse.search.resultpage.limit"; //$NON-NLS-1$
102

103     private static final int DEFAULT_ELEMENT_LIMIT = 1000;
104
105     private ActionGroup fActionGroup;
106     private IFileSearchContentProvider fContentProvider;
107     private int fCurrentSortOrder;
108     private SortAction fSortByNameAction;
109     private SortAction fSortByPathAction;
110     
111     private EditorOpener fEditorOpener= new EditorOpener();
112
113         
114     private static final String JavaDoc[] SHOW_IN_TARGETS= new String JavaDoc[] { IPageLayout.ID_RES_NAV };
115     private static final IShowInTargetList SHOW_IN_TARGET_LIST= new IShowInTargetList() {
116         public String JavaDoc[] getShowInTargetIds() {
117             return SHOW_IN_TARGETS;
118         }
119     };
120
121     public FileSearchPage() {
122         fSortByNameAction= new SortAction(SearchMessages.FileSearchPage_sort_name_label, this, FileLabelProvider.SHOW_LABEL_PATH);
123         fSortByPathAction= new SortAction(SearchMessages.FileSearchPage_sort_path_label, this, FileLabelProvider.SHOW_PATH_LABEL);
124
125         setElementLimit(new Integer JavaDoc(DEFAULT_ELEMENT_LIMIT));
126     }
127     
128     public void setElementLimit(Integer JavaDoc elementLimit) {
129         super.setElementLimit(elementLimit);
130         int limit= elementLimit.intValue();
131         getSettings().put(KEY_LIMIT, limit);
132     }
133     
134     public StructuredViewer getViewer() {
135         return super.getViewer();
136     }
137     
138     private void addDragAdapters(StructuredViewer viewer) {
139         Transfer[] transfers= new Transfer[] { ResourceTransfer.getInstance() };
140         int ops= DND.DROP_COPY | DND.DROP_LINK;
141         viewer.addDragSupport(ops, transfers, new ResourceTransferDragAdapter(viewer));
142     }
143
144     protected void configureTableViewer(TableViewer viewer) {
145         viewer.setUseHashlookup(true);
146         FileLabelProvider innerLabelProvider= new FileLabelProvider(this, fCurrentSortOrder);
147         viewer.setLabelProvider(new DecoratingLabelProvider(innerLabelProvider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
148         viewer.setContentProvider(new FileTableContentProvider(this));
149         viewer.setComparator(new DecoratorIgnoringViewerSorter(innerLabelProvider));
150         fContentProvider= (IFileSearchContentProvider) viewer.getContentProvider();
151         addDragAdapters(viewer);
152     }
153
154     protected void configureTreeViewer(TreeViewer viewer) {
155         viewer.setUseHashlookup(true);
156         FileLabelProvider innerLabelProvider= new FileLabelProvider(this, FileLabelProvider.SHOW_LABEL);
157         viewer.setLabelProvider(new DecoratingLabelProvider(innerLabelProvider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
158         viewer.setContentProvider(new FileTreeContentProvider(this, viewer));
159         viewer.setComparator(new DecoratorIgnoringViewerSorter(innerLabelProvider));
160         fContentProvider= (IFileSearchContentProvider) viewer.getContentProvider();
161         addDragAdapters(viewer);
162     }
163
164     protected void showMatch(Match match, int offset, int length, boolean activate) throws PartInitException {
165         IFile file= (IFile) match.getElement();
166         IEditorPart editor= fEditorOpener.open(file, activate);
167         if (offset != 0 && length != 0) {
168             if (editor instanceof ITextEditor) {
169                 ITextEditor textEditor= (ITextEditor) editor;
170                 textEditor.selectAndReveal(offset, length);
171             } else if (editor != null) {
172                 showWithMarker(editor, file, offset, length);
173             }
174         }
175     }
176     
177     private void showWithMarker(IEditorPart editor, IFile file, int offset, int length) throws PartInitException {
178         IMarker marker= null;
179         try {
180             marker= file.createMarker(NewSearchUI.SEARCH_MARKER);
181             HashMap JavaDoc attributes= new HashMap JavaDoc(4);
182             attributes.put(IMarker.CHAR_START, new Integer JavaDoc(offset));
183             attributes.put(IMarker.CHAR_END, new Integer JavaDoc(offset + length));
184             marker.setAttributes(attributes);
185             IDE.gotoMarker(editor, marker);
186         } catch (CoreException e) {
187             throw new PartInitException(SearchMessages.FileSearchPage_error_marker, e);
188         } finally {
189             if (marker != null)
190                 try {
191                     marker.delete();
192                 } catch (CoreException e) {
193                     // ignore
194
}
195         }
196     }
197
198     protected void fillContextMenu(IMenuManager mgr) {
199         super.fillContextMenu(mgr);
200         addSortActions(mgr);
201         fActionGroup.setContext(new ActionContext(getSite().getSelectionProvider().getSelection()));
202         fActionGroup.fillContextMenu(mgr);
203         FileSearchQuery query= (FileSearchQuery) getInput().getQuery();
204         if (!"".equals(query.getSearchString())) { //$NON-NLS-1$
205
ReplaceAction2 replaceAction= new ReplaceAction2(this, (IStructuredSelection) getViewer().getSelection());
206         if (replaceAction.isEnabled())
207             mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAction);
208                 
209         ReplaceAction2 replaceAll= new ReplaceAction2(this);
210         if (replaceAll.isEnabled())
211             mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll);
212     }
213     }
214     
215     private void addSortActions(IMenuManager mgr) {
216         if (getLayout() != FLAG_LAYOUT_FLAT)
217             return;
218         MenuManager sortMenu= new MenuManager(SearchMessages.FileSearchPage_sort_by_label);
219         sortMenu.add(fSortByNameAction);
220         sortMenu.add(fSortByPathAction);
221         
222         fSortByNameAction.setChecked(fCurrentSortOrder == fSortByNameAction.getSortOrder());
223         fSortByPathAction.setChecked(fCurrentSortOrder == fSortByPathAction.getSortOrder());
224         
225         mgr.appendToGroup(IContextMenuConstants.GROUP_VIEWER_SETUP, sortMenu);
226     }
227
228     public void setViewPart(ISearchResultViewPart part) {
229         super.setViewPart(part);
230         fActionGroup= new NewTextSearchActionGroup(part);
231     }
232     
233     public void init(IPageSite site) {
234         super.init(site);
235         IMenuManager menuManager = site.getActionBars().getMenuManager();
236         menuManager.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, new OpenSearchPreferencesAction());
237     }
238     
239     public void dispose() {
240         fActionGroup.dispose();
241         super.dispose();
242     }
243
244     protected void elementsChanged(Object JavaDoc[] objects) {
245         if (fContentProvider != null)
246             fContentProvider.elementsChanged(objects);
247     }
248
249     protected void clear() {
250         if (fContentProvider != null)
251             fContentProvider.clear();
252     }
253
254     public void setSortOrder(int sortOrder) {
255         fCurrentSortOrder= sortOrder;
256         DecoratingLabelProvider lpWrapper= (DecoratingLabelProvider) getViewer().getLabelProvider();
257         ((FileLabelProvider) lpWrapper.getLabelProvider()).setOrder(sortOrder);
258         getViewer().refresh();
259         getSettings().put(KEY_SORTING, fCurrentSortOrder);
260     }
261     
262     public void restoreState(IMemento memento) {
263         super.restoreState(memento);
264         try {
265             fCurrentSortOrder= getSettings().getInt(KEY_SORTING);
266         } catch (NumberFormatException JavaDoc e) {
267             fCurrentSortOrder= fSortByNameAction.getSortOrder();
268         }
269         int elementLimit= DEFAULT_ELEMENT_LIMIT;
270         try {
271             elementLimit= getSettings().getInt(KEY_LIMIT);
272         } catch (NumberFormatException JavaDoc e) {
273         }
274         if (memento != null) {
275             Integer JavaDoc value= memento.getInteger(KEY_SORTING);
276             if (value != null)
277                 fCurrentSortOrder= value.intValue();
278             
279             value= memento.getInteger(KEY_LIMIT);
280             if (value != null)
281                 elementLimit= value.intValue();
282         }
283         setElementLimit(new Integer JavaDoc(elementLimit));
284     }
285     public void saveState(IMemento memento) {
286         super.saveState(memento);
287         memento.putInteger(KEY_SORTING, fCurrentSortOrder);
288         memento.putInteger(KEY_LIMIT, getElementLimit().intValue());
289     }
290     
291     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
292         if (IShowInTargetList.class.equals(adapter)) {
293             return SHOW_IN_TARGET_LIST;
294         }
295         return null;
296     }
297     
298     public String JavaDoc getLabel() {
299         String JavaDoc label= super.getLabel();
300         StructuredViewer viewer= getViewer();
301         if (viewer instanceof TableViewer) {
302             TableViewer tv= (TableViewer) viewer;
303
304             AbstractTextSearchResult result= getInput();
305             if (result != null) {
306                 int itemCount= ((IStructuredContentProvider) tv.getContentProvider()).getElements(getInput()).length;
307                 int fileCount= getInput().getElements().length;
308                 if (itemCount < fileCount) {
309                     String JavaDoc format= SearchMessages.FileSearchPage_limited_format;
310                     return Messages.format(format, new Object JavaDoc[]{label, new Integer JavaDoc(itemCount), new Integer JavaDoc(fileCount)});
311                 }
312             }
313         }
314         return label;
315     }
316
317 }
318
Popular Tags