initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / InfiniteSubProgressMonitor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpeclipse.wiki.preferences;
12
13  
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.SubProgressMonitor;
16
17 /**
18  * Provides an infinite progress monitor by subdividing by half repeatedly.
19  * 
20  * The ticks parameter represents the number of ticks shown in the progress dialog
21  * (or propogated up to a parent IProgressMonitor). The totalWork parameter provided
22  * in actually a hint used to determine how work is translated into ticks.
23  * The number of totalWork that can actually be worked is n*totalWork/2 where
24  * 2^n = totalWork. What this means is that if you provide a totalWork of 32 (2^5) than
25  * the maximum number of ticks is 5*32/2 = 80.
26  * 
27  */
28 public class InfiniteSubProgressMonitor extends SubProgressMonitor {
29
30         int totalWork;
31         int halfWay;
32         int currentIncrement;
33         int nextProgress;
34         int worked;
35                 
36         /**
37          * Constructor for InfiniteSubProgressMonitor.
38          * @param monitor
39          * @param ticks
40          */
41         public InfiniteSubProgressMonitor(IProgressMonitor monitor, int ticks) {
42                 this(monitor, ticks, 0);
43         }
44
45         /**
46          * Constructor for InfiniteSubProgressMonitor.
47          * @param monitor
48          * @param ticks
49          * @param style
50          */
51         public InfiniteSubProgressMonitor(IProgressMonitor monitor, int ticks, int style) {
52                 super(monitor, ticks, style);
53         }
54         
55         public void beginTask(String name, int totalWork) {
56                 super.beginTask(name, totalWork);
57                 this.totalWork = totalWork;
58                 this.halfWay = totalWork / 2;
59                 this.currentIncrement = 1;
60                 this.nextProgress = currentIncrement;
61                 this.worked = 0;
62         }
63         
64         public void worked(int work) {
65                 if (worked >= totalWork) return;
66                 if (--nextProgress <= 0) {
67                         super.worked(1);
68                         worked++;
69                         if (worked >= halfWay) {
70                                 // we have passed the current halfway point, so double the
71                                 // increment and reset the halfway point.
72                                 currentIncrement *= 2;
73                                 halfWay += (totalWork - halfWay) / 2;                           
74                         }
75                         // reset the progress counter to another full increment
76                         nextProgress = currentIncrement;
77                 }                       
78         }
79
80         /**
81          * Don't allow clearing of the subtask. This will stop the flickering
82          * of the subtask in the progress dialogs.
83          * 
84          * @see IProgressMonitor#subTask(String)
85          */
86         public void subTask(String name) {
87                 if(name != null && ! name.equals("")) { //$NON-NLS-1$
88                         super.subTask(name);
89                 }
90         }
91 }