KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > local > InfiniteProgress


1 /*******************************************************************************
2  * Copyright (c) 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.core.internal.filesystem.local;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.ProgressMonitorWrapper;
15
16 /**
17  * This class provides a simulation of progress. This is useful
18  * for situations where computing the amount of work to do in advance
19  * is too costly. The monitor will accept any number of calls to
20  * {@link #worked(int)}, and will scale the actual reported work appropriately
21  * so that the progress never quite completes.
22  */

23 public class InfiniteProgress extends ProgressMonitorWrapper {
24     /*
25      * Fields for progress monitoring algorithm.
26      * Initially, give progress for every 4 resources, double
27      * this value at halfway point, then reset halfway point
28      * to be half of remaining work. (this gives an infinite
29      * series that converges at total work after an infinite
30      * number of resources).
31      */

32     private int totalWork;
33     private int currentIncrement = 4;
34     private int halfWay;
35     private int nextProgress = currentIncrement;
36     private int worked = 0;
37
38     protected InfiniteProgress(IProgressMonitor monitor) {
39         super(monitor);
40     }
41
42     public void beginTask(String JavaDoc name, int work) {
43         super.beginTask(name, work);
44         this.totalWork = work;
45         this.halfWay = totalWork / 2;
46     }
47
48     public void worked(int work) {
49         if (--nextProgress <= 0) {
50             //we have exhausted the current increment, so report progress
51
super.worked(1);
52             worked++;
53             if (worked >= halfWay) {
54                 //we have passed the current halfway point, so double the
55
//increment and reset the halfway point.
56
currentIncrement *= 2;
57                 halfWay += (totalWork - halfWay) / 2;
58             }
59             //reset the progress counter to another full increment
60
nextProgress = currentIncrement;
61         }
62     }
63
64 }
65
Popular Tags