KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > SearchProgressMonitor


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.help.internal.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.help.internal.base.BaseHelpSystem;
22 import org.eclipse.help.internal.base.HelpBasePlugin;
23
24 /**
25  * Progress monitor for search
26  *
27  * @since 2.0
28  */

29 public class SearchProgressMonitor implements IProgressMonitor {
30
31     // Progress monitors, indexed by locale
32
protected static Map JavaDoc progressMonitors = new HashMap JavaDoc();
33
34     // Dummy collector for triggering a progress monitor
35
protected static ISearchHitCollector dummy_collector;
36
37     private boolean started, done, canceled;
38
39     private int totalWork = IProgressMonitor.UNKNOWN;
40
41     private double currWork;
42
43     static {
44         dummy_collector = new ISearchHitCollector() {
45             public void addHits(List JavaDoc hits, String JavaDoc s) {
46             }
47         };
48     }
49
50     /**
51      * Constructor.
52      */

53     public SearchProgressMonitor() {
54         started = done = canceled = false;
55     }
56
57     public void beginTask(String JavaDoc name, int totalWork) {
58         this.totalWork = totalWork;
59         this.started = true;
60     }
61
62     public void done() {
63         currWork = totalWork;
64         this.done = true;
65         this.started = true;
66     }
67
68     public void setTaskName(String JavaDoc name) {
69     }
70
71     public void subTask(String JavaDoc name) {
72     }
73
74     public void worked(int work) {
75         internalWorked(work);
76     }
77
78     public void internalWorked(double work) {
79         currWork += work;
80         if (currWork > totalWork)
81             currWork = totalWork;
82         else if (currWork < 0)
83             currWork = 0;
84     }
85
86     public int getPercentage() {
87         if (done) {
88             return 100;
89         }
90         if (totalWork == IProgressMonitor.UNKNOWN)
91             return 0;
92         if (currWork >= totalWork)
93             return 100;
94         return (int)(100 * currWork / totalWork);
95     }
96
97     /**
98      * Gets the isCancelled.
99      *
100      * @return Returns a boolean
101      */

102     public boolean isCanceled() {
103         return canceled;
104     }
105
106     /**
107      * Sets the isStarted.
108      */

109     public void started() {
110         this.started = true;
111     }
112
113     /**
114      * Gets the isStarted.
115      *
116      * @return Returns a boolean
117      */

118     public boolean isStarted() {
119         return started;
120     }
121
122     /**
123      * Gets the isDone.
124      *
125      * @return Returns a boolean
126      */

127     public boolean isDone() {
128         return done;
129     }
130
131     /**
132      * Sets the isCanceled.
133      *
134      * @param canceled
135      * The isCanceled to set
136      */

137     public void setCanceled(boolean canceled) {
138         this.canceled = canceled;
139     }
140
141     /**
142      * Returns a progress monitor for specified query and locale
143      */

144     public static synchronized SearchProgressMonitor getProgressMonitor(
145             final String JavaDoc locale) {
146
147         // return an existing progress monitor if there is one
148
if (progressMonitors.get(locale) != null)
149             return (SearchProgressMonitor) progressMonitors.get(locale);
150
151         final SearchProgressMonitor pm = new SearchProgressMonitor();
152         progressMonitors.put(locale, pm);
153
154         // spawn a thread that will cause indexing if needed
155
Thread JavaDoc indexer = new Thread JavaDoc(new Runnable JavaDoc() {
156             public void run() {
157                 try {
158                     BaseHelpSystem.getSearchManager().search(
159                             new DummySearchQuery(locale), dummy_collector, pm);
160                 } catch (OperationCanceledException oce) {
161                     // operation cancelled
162
// throw out the progress monitor
163
progressMonitors.remove(locale);
164                 } catch (Exception JavaDoc e) {
165                     progressMonitors.remove(locale);
166                     if (HelpBasePlugin.getDefault() != null) {
167                         HelpBasePlugin
168                                 .logError(
169                                         "Problem occurred during indexing of documentation.", //$NON-NLS-1$
170
e);
171                     } else {
172                         // Plugin has been shut down
173
}
174                 }
175             }
176         });
177         indexer.setName("HelpSearchIndexer"); //$NON-NLS-1$
178
indexer.start();
179         // give pm chance to start
180
// this will avoid seing progress if there is no work to do
181
while (!pm.isStarted()) {
182             try {
183                 Thread.sleep(50);
184             } catch (InterruptedException JavaDoc ie) {
185             }
186             if (progressMonitors.get(locale) == null)
187                 // operation got canceled
188
break;
189         }
190
191         return pm;
192     }
193
194     static class DummySearchQuery implements ISearchQuery {
195         private String JavaDoc l;
196
197         DummySearchQuery(String JavaDoc loc) {
198             l = loc;
199         }
200
201         /**
202          * Obtains names of fields in addition to default field
203          */

204         public Collection JavaDoc getFieldNames() {
205             return new ArrayList JavaDoc();
206         }
207
208         /**
209          * Obtains search word (user query)
210          */

211         public String JavaDoc getSearchWord() {
212             return "dummy"; //$NON-NLS-1$
213
}
214
215         /**
216          * @return true if search only in specified fields, not the default
217          * field
218          */

219         public boolean isFieldSearch() {
220             return false;
221         }
222
223         /**
224          * Obtains locale
225          */

226         public String JavaDoc getLocale() {
227             return l;
228         }
229     }
230
231     public synchronized static void reinit(String JavaDoc locale) {
232         progressMonitors.remove(locale);
233     }
234
235 }
236
Popular Tags