Skip to main content
summaryrefslogtreecommitdiffstats
blob: cab9b7a3fda95211c9243ca1a97a1445ac235e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.search;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.HelpBasePlugin;

/**
 * Progress monitor for search
 *
 * @since 2.0
 */
public class SearchProgressMonitor implements IProgressMonitor {

	// Progress monitors, indexed by locale
	protected static Map<String, SearchProgressMonitor> progressMonitors = new HashMap<String, SearchProgressMonitor>();

	// Dummy collector for triggering a progress monitor
	protected static ISearchHitCollector dummy_collector;

	private boolean started, done, canceled;

	private int totalWork = IProgressMonitor.UNKNOWN;

	private double currWork;

	static {
		dummy_collector = new ISearchHitCollector() {
			@Override
			public void addHits(List<SearchHit> hits, String s) {
			}

			@Override
			public void addQTCException(QueryTooComplexException exception) throws QueryTooComplexException {
                throw exception;
			}
		};
	}

	/**
	 * Constructor.
	 */
	public SearchProgressMonitor() {
		started = done = canceled = false;
	}

	@Override
	public void beginTask(String name, int totalWork) {
		this.totalWork = totalWork;
		this.started = true;
	}

	@Override
	public void done() {
		currWork = totalWork;
		this.done = true;
		this.started = true;
	}

	@Override
	public void setTaskName(String name) {
	}

	@Override
	public void subTask(String name) {
	}

	@Override
	public void worked(int work) {
		internalWorked(work);
	}

	@Override
	public void internalWorked(double work) {
		currWork += work;
		if (currWork > totalWork)
			currWork = totalWork;
		else if (currWork < 0)
			currWork = 0;
	}

	public int getPercentage() {
		if (done) {
			return 100;
		}
		if (totalWork == IProgressMonitor.UNKNOWN)
			return 0;
		if (currWork >= totalWork)
			return 100;
		return (int)(100 * currWork / totalWork);
	}

	/**
	 * Gets the isCancelled.
	 *
	 * @return Returns a boolean
	 */
	@Override
	public boolean isCanceled() {
		return canceled;
	}

	/**
	 * Sets the isStarted.
	 */
	public void started() {
		this.started = true;
	}

	/**
	 * Gets the isStarted.
	 *
	 * @return Returns a boolean
	 */
	public boolean isStarted() {
		return started;
	}

	/**
	 * Gets the isDone.
	 *
	 * @return Returns a boolean
	 */
	public boolean isDone() {
		return done;
	}

	/**
	 * Sets the isCanceled.
	 *
	 * @param canceled
	 *            The isCanceled to set
	 */
	@Override
	public void setCanceled(boolean canceled) {
		this.canceled = canceled;
	}

	/**
	 * Returns a progress monitor for specified query and locale
	 */
	public static synchronized SearchProgressMonitor getProgressMonitor(
			final String locale) {

		// return an existing progress monitor if there is one
		if (progressMonitors.get(locale) != null)
			return progressMonitors.get(locale);

		final SearchProgressMonitor pm = new SearchProgressMonitor();
		progressMonitors.put(locale, pm);

		// spawn a thread that will cause indexing if needed
		Thread indexer = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					BaseHelpSystem.getSearchManager().search(
							new DummySearchQuery(locale), dummy_collector, pm);
				} catch (OperationCanceledException oce) {
					// operation cancelled
					// throw out the progress monitor
					progressMonitors.remove(locale);
				} catch (Exception e) {
					progressMonitors.remove(locale);
					if (HelpBasePlugin.getDefault() != null) {
						HelpBasePlugin
								.logError(
										"Problem occurred during indexing of documentation.", //$NON-NLS-1$
										e);
					} else {
						// Plugin has been shut down
					}
				}
			}
		});
		indexer.setName("HelpSearchIndexer"); //$NON-NLS-1$
		indexer.start();
		// give pm chance to start
		// this will avoid seing progress if there is no work to do
		while (!pm.isStarted()) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException ie) {
			}
			if (progressMonitors.get(locale) == null)
				// operation got canceled
				break;
		}

		return pm;
	}

	static class DummySearchQuery implements ISearchQuery {
		private String l;

		DummySearchQuery(String loc) {
			l = loc;
		}

		/**
		 * Obtains names of fields in addition to default field
		 */
		@Override
		public Collection<String> getFieldNames() {
			return new ArrayList<String>();
		}

		/**
		 * Obtains search word (user query)
		 */
		@Override
		public String getSearchWord() {
			return "dummy"; //$NON-NLS-1$
		}

		/**
		 * @return true if search only in specified fields, not the default
		 *         field
		 */
		@Override
		public boolean isFieldSearch() {
			return false;
		}

		/**
		 * Obtains locale
		 */
		@Override
		public String getLocale() {
			return l;
		}
	}

	public synchronized static void reinit(String locale) {
		progressMonitors.remove(locale);
	}

}

Back to the top