KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > SubProgressMonitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.runtime;
12
13 /**
14  * For new implementations consider using {@link SubMonitor}.
15  *
16  * A progress monitor that uses a given amount of work ticks
17  * from a parent monitor. It can be used as follows:
18  * <pre>
19  * try {
20  * pm.beginTask("Main Task", 100);
21  * doSomeWork(pm, 30);
22  * SubProgressMonitor subMonitor= new SubProgressMonitor(pm, 40);
23  * try {
24  * subMonitor.beginTask("", 300);
25  * doSomeWork(subMonitor, 300);
26  * } finally {
27  * subMonitor.done();
28  * }
29  * doSomeWork(pm, 30);
30  * } finally {
31  * pm.done();
32  * }
33  * </pre>
34  * <p>
35  * This class can be used without OSGi running.
36  * </p><p>
37  * This class may be instantiated or subclassed by clients.
38  * </p>
39  *
40  * @see SubMonitor
41  */

42 public class SubProgressMonitor extends ProgressMonitorWrapper {
43
44     /**
45      * Style constant indicating that calls to <code>subTask</code>
46      * should not have any effect.
47      *
48      * @see #SubProgressMonitor(IProgressMonitor,int,int)
49      */

50     public static final int SUPPRESS_SUBTASK_LABEL = 1 << 1;
51     /**
52      * Style constant indicating that the main task label
53      * should be prepended to the subtask label.
54      *
55      * @see #SubProgressMonitor(IProgressMonitor,int,int)
56      */

57     public static final int PREPEND_MAIN_LABEL_TO_SUBTASK = 1 << 2;
58
59     private int parentTicks = 0;
60     private double sentToParent = 0.0;
61     private double scale = 0.0;
62     private int nestedBeginTasks = 0;
63     private boolean usedUp = false;
64     private boolean hasSubTask = false;
65     private int style;
66     private String JavaDoc mainTaskLabel;
67
68     /**
69      * Creates a new sub-progress monitor for the given monitor. The sub
70      * progress monitor uses the given number of work ticks from its
71      * parent monitor.
72      *
73      * @param monitor the parent progress monitor
74      * @param ticks the number of work ticks allocated from the
75      * parent monitor
76      */

77     public SubProgressMonitor(IProgressMonitor monitor, int ticks) {
78         this(monitor, ticks, 0);
79     }
80
81     /**
82      * Creates a new sub-progress monitor for the given monitor. The sub
83      * progress monitor uses the given number of work ticks from its
84      * parent monitor.
85      *
86      * @param monitor the parent progress monitor
87      * @param ticks the number of work ticks allocated from the
88      * parent monitor
89      * @param style one of
90      * <ul>
91      * <li> <code>SUPPRESS_SUBTASK_LABEL</code> </li>
92      * <li> <code>PREPEND_MAIN_LABEL_TO_SUBTASK</code> </li>
93      * </ul>
94      * @see #SUPPRESS_SUBTASK_LABEL
95      * @see #PREPEND_MAIN_LABEL_TO_SUBTASK
96      */

97     public SubProgressMonitor(IProgressMonitor monitor, int ticks, int style) {
98         super(monitor);
99         this.parentTicks = (ticks > 0) ? ticks : 0;
100         this.style = style;
101     }
102
103     /* (Intentionally not javadoc'd)
104      * Implements the method <code>IProgressMonitor.beginTask</code>.
105      *
106      * Starts a new main task. Since this progress monitor is a sub
107      * progress monitor, the given name will NOT be used to update
108      * the progress bar's main task label. That means the given
109      * string will be ignored. If style <code>PREPEND_MAIN_LABEL_TO_SUBTASK
110      * <code> is specified, then the given string will be prepended to
111      * every string passed to <code>subTask(String)</code>.
112      */

113     public void beginTask(String JavaDoc name, int totalWork) {
114         nestedBeginTasks++;
115         // Ignore nested begin task calls.
116
if (nestedBeginTasks > 1) {
117             return;
118         }
119         // be safe: if the argument would cause math errors (zero or
120
// negative), just use 0 as the scale. This disables progress for
121
// this submonitor.
122
scale = totalWork <= 0 ? 0 : (double) parentTicks / (double) totalWork;
123         if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0) {
124             mainTaskLabel = name;
125         }
126     }
127
128     /* (Intentionally not javadoc'd)
129      * Implements the method <code>IProgressMonitor.done</code>.
130      */

131     public void done() {
132         // Ignore if more done calls than beginTask calls or if we are still
133
// in some nested beginTasks
134
if (nestedBeginTasks == 0 || --nestedBeginTasks > 0)
135             return;
136         // Send any remaining ticks and clear out the subtask text
137
double remaining = parentTicks - sentToParent;
138         if (remaining > 0)
139             super.internalWorked(remaining);
140         //clear the sub task if there was one
141
if (hasSubTask)
142             subTask(""); //$NON-NLS-1$
143
sentToParent = 0;
144     }
145
146     /* (Intentionally not javadoc'd)
147      * Implements the internal method <code>IProgressMonitor.internalWorked</code>.
148      */

149     public void internalWorked(double work) {
150         if (usedUp || nestedBeginTasks != 1) {
151             return;
152         }
153
154         double realWork = (work > 0.0d) ? scale * work : 0.0d;
155         super.internalWorked(realWork);
156         sentToParent += realWork;
157         if (sentToParent >= parentTicks) {
158             usedUp = true;
159         }
160     }
161
162     /* (Intentionally not javadoc'd)
163      * Implements the method <code>IProgressMonitor.subTask</code>.
164      */

165     public void subTask(String JavaDoc name) {
166         if ((style & SUPPRESS_SUBTASK_LABEL) != 0) {
167             return;
168         }
169         hasSubTask = true;
170         String JavaDoc label = name;
171         if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0 && mainTaskLabel != null && mainTaskLabel.length() > 0) {
172             label = mainTaskLabel + ' ' + label;
173         }
174         super.subTask(label);
175     }
176
177     /* (Intentionally not javadoc'd)
178      * Implements the method <code>IProgressMonitor.worked</code>.
179      */

180     public void worked(int work) {
181         internalWorked(work);
182     }
183 }
184
Popular Tags