KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.core.runtime.*;
13 /**
14  * Progress Monitor, that accumulates work without communicating it immidiately
15  * to the underlying monitor. The work is sent in larger chunks for performance
16  * reasons.
17  */

18 class LazyProgressMonitor extends ProgressMonitorWrapper {
19     // maximum number of times worked() should be called
20
// on underlying progress monitor
21
private static final int MAX_STEPS = 100;
22     private final IProgressMonitor monitor;
23     private int totalWork;
24     private int work;
25     private int lastWorked;
26     private int treshold;
27     protected LazyProgressMonitor(IProgressMonitor monitor) {
28         super(monitor);
29         this.monitor = monitor;
30     }
31     /**
32      * @see IProgressMonitor#beginTask
33      */

34     public void beginTask(String JavaDoc name, int totalWork) {
35         if (totalWork > 0) {
36             this.totalWork = totalWork;
37         }
38         monitor.beginTask(name, totalWork);
39         work = 0;
40         lastWorked = 0;
41         treshold = 1 + totalWork / MAX_STEPS;
42     }
43     /**
44      * @see IProgressMonitor#worked
45      */

46     public void worked(int newWork) {
47         this.work += newWork;
48         if (work >= treshold) {
49             monitor.worked(work - lastWorked);
50             lastWorked = work;
51             treshold = work + 1 + totalWork / MAX_STEPS;
52         }
53
54     }
55 }
56
Popular Tags