KickJava   Java API By Example, From Geeks To Geeks.

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


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;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.viewers.ILabelProvider;
27 import org.eclipse.jface.viewers.ISelection;
28
29 import org.eclipse.search.ui.IActionGroupFactory;
30 import org.eclipse.search.ui.IContextMenuContributor;
31 import org.eclipse.search.ui.IGroupByKeyComputer;
32 import org.eclipse.search.ui.ISearchResultViewEntry;
33
34 import org.eclipse.search.internal.ui.util.ExceptionHandler;
35 /**
36  * @deprecated old search
37  */

38 public class Search extends Object JavaDoc {
39     private String JavaDoc fPageId;
40     private String JavaDoc fSingularLabel;
41     private String JavaDoc fPluralLabelPattern;
42     private ImageDescriptor fImageDescriptor;
43     private ILabelProvider fLabelProvider;
44     private ISelection fSelection;
45     private ArrayList JavaDoc fResults;
46     private IAction fGotoMarkerAction;
47     private IContextMenuContributor fContextMenuContributor;
48     private IActionGroupFactory fActionGroupFactory;
49     private IGroupByKeyComputer fGroupByKeyComputer;
50     private IRunnableWithProgress fOperation;
51
52
53     public Search(String JavaDoc pageId, String JavaDoc singularLabel, String JavaDoc pluralLabelPattern, ILabelProvider labelProvider, ImageDescriptor imageDescriptor, IAction gotoMarkerAction, IActionGroupFactory groupFactory, IGroupByKeyComputer groupByKeyComputer, IRunnableWithProgress operation) {
54         fPageId= pageId;
55         fSingularLabel= singularLabel;
56         fPluralLabelPattern= pluralLabelPattern;
57         fImageDescriptor= imageDescriptor;
58         fLabelProvider= labelProvider;
59         fGotoMarkerAction= gotoMarkerAction;
60         fActionGroupFactory= groupFactory;
61         fGroupByKeyComputer= groupByKeyComputer;
62         fOperation= operation;
63         
64         if (fPluralLabelPattern == null)
65             fPluralLabelPattern= ""; //$NON-NLS-1$
66
}
67
68     public Search(String JavaDoc pageId, String JavaDoc singularLabel, String JavaDoc pluralLabelPattern, ILabelProvider labelProvider, ImageDescriptor imageDescriptor, IAction gotoMarkerAction, IContextMenuContributor contextMenuContributor, IGroupByKeyComputer groupByKeyComputer, IRunnableWithProgress operation) {
69         fPageId= pageId;
70         fSingularLabel= singularLabel;
71         fPluralLabelPattern= pluralLabelPattern;
72         fImageDescriptor= imageDescriptor;
73         fLabelProvider= labelProvider;
74         fGotoMarkerAction= gotoMarkerAction;
75         fContextMenuContributor= contextMenuContributor;
76         fGroupByKeyComputer= groupByKeyComputer;
77         fOperation= operation;
78         
79         if (fPluralLabelPattern == null)
80             fPluralLabelPattern= ""; //$NON-NLS-1$
81
}
82
83     /**
84      * Returns the full description of the search.
85      * The description set by the client where
86      * {0} will be replaced by the match count.
87      */

88     String JavaDoc getFullDescription() {
89         if (fSingularLabel != null && getItemCount() == 1)
90             return fSingularLabel;
91
92         // try to replace "{0}" with the match count
93
int i= fPluralLabelPattern.lastIndexOf("{0}"); //$NON-NLS-1$
94
if (i < 0)
95             return fPluralLabelPattern;
96         return fPluralLabelPattern.substring(0, i) + getItemCount()+ fPluralLabelPattern.substring(Math.min(i + 3, fPluralLabelPattern.length()));
97     }
98
99     /**
100      * Returns a short description of the search.
101      * Cuts off after 30 characters and adds ...
102      * The description set by the client where
103      * {0} will be replaced by the match count.
104      */

105     String JavaDoc getShortDescription() {
106         String JavaDoc text= getFullDescription();
107         int separatorPos= text.indexOf(" - "); //$NON-NLS-1$
108
if (separatorPos < 1)
109             return text.substring(0, Math.min(50, text.length())) + "..."; // use first 50 characters //$NON-NLS-1$
110
if (separatorPos < 30)
111             return text; // don't cut
112
if (text.charAt(0) == '"')
113             return text.substring(0, Math.min(30, text.length())) + "...\" - " + text.substring(Math.min(separatorPos + 3, text.length())); //$NON-NLS-1$
114
return text.substring(0, Math.min(30, text.length())) + "... - " + text.substring(Math.min(separatorPos + 3, text.length())); //$NON-NLS-1$
115
}
116     /** Image used when search is displayed in a list */
117     ImageDescriptor getImageDescriptor() {
118         return fImageDescriptor;
119     }
120
121     int getItemCount() {
122         int count= 0;
123         Iterator JavaDoc iter= getResults().iterator();
124         while (iter.hasNext())
125             count += ((ISearchResultViewEntry)iter.next()).getMatchCount();
126         return count;
127     }
128
129     List JavaDoc getResults() {
130         if (fResults == null)
131             return new ArrayList JavaDoc();
132         return fResults;
133     }
134
135     ILabelProvider getLabelProvider() {
136         return fLabelProvider;
137     }
138
139     void searchAgain() {
140         if (fOperation == null)
141             return;
142         Shell shell= SearchPlugin.getActiveWorkbenchShell();
143         boolean isAutoBuilding= SearchPlugin.setAutoBuilding(false);
144         try {
145             new ProgressMonitorDialog(shell).run(true, true, fOperation);
146         } catch (InvocationTargetException JavaDoc ex) {
147             ExceptionHandler.handle(ex, shell, SearchMessages.Search_Error_search_title, SearchMessages.Search_Error_search_message);
148         } catch(InterruptedException JavaDoc e) {
149         } finally {
150             SearchPlugin.setAutoBuilding(isAutoBuilding);
151         }
152     }
153     
154     boolean isSameSearch(Search search) {
155         return search != null && search.getOperation() == fOperation && fOperation != null;
156     }
157     
158     void backupMarkers() {
159         Iterator JavaDoc iter= getResults().iterator();
160         while (iter.hasNext()) {
161             ((SearchResultViewEntry)iter.next()).backupMarkers();
162         }
163     }
164
165     String JavaDoc getPageId() {
166         return fPageId;
167     }
168     
169     IGroupByKeyComputer getGroupByKeyComputer() {
170         return fGroupByKeyComputer;
171     }
172
173     public IRunnableWithProgress getOperation() {
174         return fOperation;
175     }
176
177     IAction getGotoMarkerAction() {
178         return fGotoMarkerAction;
179     }
180
181     IContextMenuContributor getContextMenuContributor() {
182         return fContextMenuContributor;
183     }
184     
185     IActionGroupFactory getActionGroupFactory() {
186         return fActionGroupFactory;
187     }
188     
189     public void removeResults() {
190         fResults= null;
191     }
192     
193     void setResults(ArrayList JavaDoc results) {
194         Assert.isNotNull(results);
195         fResults= results;
196     }
197
198     ISelection getSelection() {
199         return fSelection;
200     }
201
202     void setSelection(ISelection selection) {
203         fSelection= selection;
204     }
205 }
206
207
Popular Tags