KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > InternalSearchUI


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  * Markus Schorn (Wind River Systems)
11  *******************************************************************************/

12 package org.eclipse.search2.internal.ui;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24
25 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
26 import org.eclipse.jface.operation.IRunnableContext;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28
29 import org.eclipse.ui.IWorkbenchPartSite;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
32
33 import org.eclipse.search.ui.IQueryListener;
34 import org.eclipse.search.ui.ISearchQuery;
35 import org.eclipse.search.ui.ISearchResult;
36 import org.eclipse.search.ui.ISearchResultViewPart;
37
38 import org.eclipse.search.internal.ui.SearchPlugin;
39 import org.eclipse.search.internal.ui.SearchPluginImages;
40 import org.eclipse.search.internal.ui.SearchPreferencePage;
41
42 import org.eclipse.search2.internal.ui.text.PositionTracker;
43
44 public class InternalSearchUI {
45     
46     //The shared instance.
47
private static InternalSearchUI fgInstance;
48     
49     // contains all running jobs
50
private HashMap JavaDoc fSearchJobs;
51     
52     private QueryManager fSearchResultsManager;
53     private PositionTracker fPositionTracker;
54     
55     private SearchViewManager fSearchViewManager;
56
57     public static final Object JavaDoc FAMILY_SEARCH = new Object JavaDoc();
58
59     private class SearchJobRecord {
60         public ISearchQuery query;
61         public Job job;
62         public boolean background;
63         public boolean isRunning;
64
65         public SearchJobRecord(ISearchQuery job, boolean bg) {
66             this.query= job;
67             this.background= bg;
68             this.isRunning= false;
69             this.job= null;
70         }
71     }
72     
73
74     private class InternalSearchJob extends Job {
75         
76         private SearchJobRecord fSearchJobRecord;
77         
78         public InternalSearchJob(SearchJobRecord sjr) {
79             super(sjr.query.getLabel());
80             
81             fSearchJobRecord= sjr;
82         }
83         
84         protected IStatus run(IProgressMonitor monitor) {
85             fSearchJobRecord.job= this;
86             searchJobStarted(fSearchJobRecord);
87             IStatus status= null;
88             int origPriority= Thread.currentThread().getPriority();
89             try {
90                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
91             }
92             catch (SecurityException JavaDoc e) {}
93             try{
94                 status= fSearchJobRecord.query.run(monitor);
95             } finally {
96                 try {
97                     Thread.currentThread().setPriority(origPriority);
98                 }
99                 catch (SecurityException JavaDoc e) {}
100                 searchJobFinished(fSearchJobRecord);
101             }
102             fSearchJobRecord.job= null;
103             return status;
104         }
105         public boolean belongsTo(Object JavaDoc family) {
106             return family == InternalSearchUI.FAMILY_SEARCH;
107         }
108
109     }
110
111     private void searchJobStarted(SearchJobRecord record) {
112         record.isRunning= true;
113         getSearchManager().queryStarting(record.query);
114     }
115     
116     private void searchJobFinished(SearchJobRecord record) {
117         record.isRunning= false;
118         fSearchJobs.remove(record);
119         getSearchManager().queryFinished(record.query);
120     }
121     
122     /**
123      * The constructor.
124      */

125     public InternalSearchUI() {
126         fgInstance= this;
127         fSearchJobs= new HashMap JavaDoc();
128         fSearchResultsManager= new QueryManager();
129         fPositionTracker= new PositionTracker();
130         
131         fSearchViewManager= new SearchViewManager(fSearchResultsManager);
132         
133         PlatformUI.getWorkbench().getProgressService().registerIconForFamily(SearchPluginImages.DESC_VIEW_SEARCHRES, FAMILY_SEARCH);
134     }
135
136     /**
137      * @return returns the shared instance.
138      */

139     public static InternalSearchUI getInstance() {
140         if (fgInstance ==null)
141             fgInstance= new InternalSearchUI();
142         return fgInstance;
143     }
144
145     public ISearchResultViewPart getSearchView() {
146         return getSearchViewManager().getActiveSearchView();
147     }
148
149     private IWorkbenchSiteProgressService getProgressService() {
150         ISearchResultViewPart view= getSearchView();
151         if (view != null) {
152             IWorkbenchPartSite site= view.getSite();
153             if (site != null)
154                 return (IWorkbenchSiteProgressService)view.getSite().getAdapter(IWorkbenchSiteProgressService.class);
155         }
156         return null;
157     }
158     
159     public boolean runSearchInBackground(ISearchQuery query, ISearchResultViewPart view) {
160         if (isQueryRunning(query))
161             return false;
162         
163         // prepare view
164
if (view == null) {
165             getSearchViewManager().activateSearchView(true);
166         } else {
167             getSearchViewManager().activateSearchView(view);
168         }
169                 
170         addQuery(query);
171
172         SearchJobRecord sjr= new SearchJobRecord(query, true);
173         fSearchJobs.put(query, sjr);
174                 
175         Job job= new InternalSearchJob(sjr);
176         job.setPriority(Job.BUILD);
177         job.setUser(true);
178
179         IWorkbenchSiteProgressService service= getProgressService();
180         if (service != null) {
181             service.schedule(job, 0, true);
182         } else {
183             job.schedule();
184         }
185         
186         return true;
187     }
188
189     public boolean isQueryRunning(ISearchQuery query) {
190         SearchJobRecord sjr= (SearchJobRecord) fSearchJobs.get(query);
191         return sjr != null && sjr.isRunning;
192     }
193
194     public IStatus runSearchInForeground(IRunnableContext context, final ISearchQuery query, ISearchResultViewPart view) {
195         if (isQueryRunning(query)) {
196             return Status.CANCEL_STATUS;
197         }
198         
199         // prepare view
200
if (view == null) {
201             getSearchViewManager().activateSearchView(true);
202         } else {
203             getSearchViewManager().activateSearchView(view);
204         }
205
206         addQuery(query);
207         
208         SearchJobRecord sjr= new SearchJobRecord(query, false);
209         fSearchJobs.put(query, sjr);
210         
211         if (context == null)
212             context= new ProgressMonitorDialog(null);
213         
214         return doRunSearchInForeground(sjr, context);
215     }
216     
217     private IStatus doRunSearchInForeground(final SearchJobRecord rec, IRunnableContext context) {
218         try {
219             context.run(true, true, new IRunnableWithProgress() {
220                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
221                     searchJobStarted(rec);
222                     try {
223                         IStatus status= rec.query.run(monitor);
224                         if (status.matches(IStatus.CANCEL)) {
225                             throw new InterruptedException JavaDoc();
226                         }
227                         if (!status.isOK()) {
228                             throw new InvocationTargetException JavaDoc(new CoreException(status));
229                         }
230                     } catch (OperationCanceledException e) {
231                         throw new InterruptedException JavaDoc();
232                     } finally {
233                         searchJobFinished(rec);
234                     }
235                 }
236             });
237         } catch (InvocationTargetException JavaDoc e) {
238             Throwable JavaDoc innerException= e.getTargetException();
239             if (innerException instanceof CoreException) {
240                 return ((CoreException) innerException).getStatus();
241             }
242             return new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.InternalSearchUI_error_unexpected, innerException);
243         } catch (InterruptedException JavaDoc e) {
244             return Status.CANCEL_STATUS;
245         }
246         return Status.OK_STATUS;
247     }
248
249     public static void shutdown() {
250         InternalSearchUI instance= fgInstance;
251         if (instance != null)
252             instance.doShutdown();
253     }
254     
255     private void doShutdown() {
256         Iterator JavaDoc jobRecs= fSearchJobs.values().iterator();
257         while (jobRecs.hasNext()) {
258             SearchJobRecord element= (SearchJobRecord) jobRecs.next();
259             if (element.job != null)
260                 element.job.cancel();
261         }
262         fPositionTracker.dispose();
263         
264         fSearchViewManager.dispose(fSearchResultsManager);
265         
266     }
267
268     public void cancelSearch(ISearchQuery job) {
269         SearchJobRecord rec= (SearchJobRecord) fSearchJobs.get(job);
270         if (rec != null && rec.job != null)
271             rec.job.cancel();
272     }
273
274
275
276     public QueryManager getSearchManager() {
277         return fSearchResultsManager;
278     }
279     
280     public SearchViewManager getSearchViewManager() {
281         return fSearchViewManager;
282     }
283
284     public PositionTracker getPositionTracker() {
285         return fPositionTracker;
286     }
287         
288     public void addQueryListener(IQueryListener l) {
289         getSearchManager().addQueryListener(l);
290     }
291     public ISearchQuery[] getQueries() {
292         return getSearchManager().getQueries();
293     }
294     public void removeQueryListener(IQueryListener l) {
295         getSearchManager().removeQueryListener(l);
296     }
297
298     public void removeQuery(ISearchQuery query) {
299         if (query == null) {
300             throw new IllegalArgumentException JavaDoc();
301         }
302         cancelSearch(query);
303         getSearchManager().removeQuery(query);
304         fSearchJobs.remove(query);
305     }
306
307     public void addQuery(ISearchQuery query) {
308         if (query == null) {
309             throw new IllegalArgumentException JavaDoc();
310         }
311             
312         QueryManager searchManager= getSearchManager();
313         int removeCount= searchManager.getSize() + 1 - SearchPreferencePage.getHistoryLimit();
314         for (int i = 0; i < removeCount; i++) {
315             ISearchQuery oldestQuery= searchManager.getOldestQuery();
316             if (oldestQuery != null) {
317                 removeQuery(oldestQuery);
318             }
319         }
320         searchManager.addQuery(query);
321     }
322     
323     public void removeAllQueries() {
324         for (Iterator JavaDoc queries= fSearchJobs.keySet().iterator(); queries.hasNext();) {
325             ISearchQuery query= (ISearchQuery) queries.next();
326             cancelSearch(query);
327         }
328         fSearchJobs.clear();
329         getSearchManager().removeAll();
330     }
331     
332     public void showSearchResult(SearchView searchView, ISearchResult result, boolean openInNew) {
333         if (openInNew) {
334             boolean isPinned= searchView.isPinned();
335             searchView.setPinned(true);
336             try {
337                 SearchView newPart= (SearchView) InternalSearchUI.getInstance().getSearchViewManager().activateSearchView(true);
338                 showSearchResult(newPart, result);
339             } finally {
340                 searchView.setPinned(isPinned);
341             }
342         } else {
343             showSearchResult(searchView, result);
344         }
345     }
346     
347     private void showSearchResult(SearchView searchView, ISearchResult result) {
348         getSearchManager().touch(result.getQuery());
349         searchView.showSearchResult(result);
350     }
351     
352     
353 }
354
Popular Tags