KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > SearchRunner


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.update.internal.ui.wizards;
12
13 import java.lang.reflect.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.jface.operation.*;
18 import org.eclipse.swt.widgets.*;
19 import org.eclipse.update.core.*;
20 import org.eclipse.update.internal.ui.*;
21 import org.eclipse.update.search.*;
22
23 /**
24  * Runs a search query
25  */

26 public class SearchRunner {
27     private Shell shell;
28     private IRunnableContext context;
29     private ISearchProvider searchProvider;
30     private IUpdateSearchResultCollector collector;
31     private boolean newSearchNeeded;
32     
33     public SearchRunner(Shell shell, IRunnableContext context) {
34         this.shell = shell;
35         this.context = context;
36     }
37     
38     public void setResultCollector(IUpdateSearchResultCollector collector) {
39         this.collector = collector;
40     }
41     
42     public ISearchProvider getSearchProvider() {
43         return searchProvider;
44     }
45     
46     public void setSearchProvider(ISearchProvider searchProvider) {
47         if (this.searchProvider!=searchProvider)
48             newSearchNeeded = true;
49         this.searchProvider = searchProvider;
50     }
51     
52     public void setNewSearchNeeded(boolean value) {
53         newSearchNeeded = value;
54     }
55     
56     public boolean isNewSearchNeeded() {
57         return newSearchNeeded;
58     }
59
60     public void runSearch() {
61         if (searchProvider==null) return;
62         try {
63             context.run(true, true, getSearchOperation(collector));
64             newSearchNeeded=false;
65         } catch (InterruptedException JavaDoc e) {
66             if (!"cancel".equals(e.getMessage()))
67                 UpdateUI.logException(e);
68             newSearchNeeded=true;
69             return;
70         } catch (InvocationTargetException e) {
71             Throwable JavaDoc t = e.getTargetException();
72             if (t instanceof CoreException) {
73                 CoreException ce = (CoreException)t;
74                 IStatus status = ce.getStatus();
75                 if (status!=null &&
76                     status.getCode()==ISite.SITE_ACCESS_EXCEPTION) {
77                     // Just show this but do not throw exception
78
// because there may be results anyway.
79
ErrorDialog.openError(shell,UpdateUI.getString("SearchRunner.connectionError"), //$NON-NLS-1$
80
null,
81                         status);
82                     return;
83                 }
84             }
85             UpdateUI.logException(e);
86             return;
87         }
88     }
89
90     private IRunnableWithProgress getSearchOperation(final IUpdateSearchResultCollector collector) {
91         final UpdateSearchRequest request = searchProvider.getSearchRequest();
92         
93         IRunnableWithProgress op = new IRunnableWithProgress () {
94             public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException JavaDoc {
95                 try {
96                     request.performSearch(collector, monitor);
97                 } catch (CoreException e) {
98                     throw new InvocationTargetException(e);
99                 } finally {
100                     monitor.done();
101                     if (monitor.isCanceled()) {
102                         newSearchNeeded = true;
103                         throw new InterruptedException JavaDoc("cancel");
104                     }
105                 }
106             }
107         };
108         return op;
109     }
110 }
111
Popular Tags