KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > InstallMonitor


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.update.core;
12
13 import java.util.Stack JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.update.internal.core.Messages;
18
19 /**
20  * Install progress monitor
21  * Delegating wrapper for IProgressMonitor used for installation handling.
22  *
23  * <p>
24  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
25  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
26  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
27  * (repeatedly) as the API evolves.
28  * </p>
29  * @since 2.0
30  */

31 public class InstallMonitor implements IProgressMonitor {
32
33     protected IProgressMonitor monitor;
34     protected Stack JavaDoc tasks;
35
36     protected String JavaDoc taskString;
37     protected String JavaDoc subTaskString;
38     protected boolean showDetails;
39     protected long totalCopyCount;
40     protected long currentCount = 0;
41
42     protected class MonitorState {
43
44         private String JavaDoc taskString;
45         private String JavaDoc subTaskString;
46         private boolean showDetails;
47         private long totalCopyCount;
48         private long currentCount;
49
50         private MonitorState(
51             String JavaDoc taskString,
52             String JavaDoc subTaskString,
53             boolean showDetails,
54             long currentCount,
55             long totalCopyCount) {
56             this.taskString = taskString;
57             this.subTaskString = subTaskString;
58             this.showDetails = showDetails;
59             this.currentCount = currentCount;
60             this.totalCopyCount = totalCopyCount;
61         }
62
63         private String JavaDoc getTaskString() {
64             return this.taskString;
65         }
66
67         private String JavaDoc getSubTaskString() {
68             return this.subTaskString;
69         }
70
71         private boolean getShowDetails() {
72             return this.showDetails;
73         }
74
75         private long getCurrentCount() {
76             return this.currentCount;
77         }
78         
79         private long getTotalCopyCount() {
80             return this.totalCopyCount;
81         }
82     }
83     
84     protected InstallMonitor() {
85     }
86
87     /**
88      * Install monitor constructor
89      *
90      * @param monitor base install monitor
91      * @since 2.0
92      */

93     public InstallMonitor(IProgressMonitor monitor) {
94         this.monitor = monitor;
95         this.tasks = new Stack JavaDoc();
96         this.taskString = ""; //$NON-NLS-1$
97
this.subTaskString = ""; //$NON-NLS-1$
98
this.showDetails = false;
99         this.totalCopyCount = 0;
100     }
101
102     /**
103      * Begin new monitor task.
104      *
105      * @see IProgressMonitor#beginTask(String, int)
106      * @since 2.0
107      */

108     public void beginTask(String JavaDoc name, int totalWork) {
109         taskString = name;
110         monitor.beginTask(name, totalWork);
111     }
112
113     /**
114      * Indicate completion of monitor activity.
115      *
116      * @see IProgressMonitor#done()
117      * @since 2.0
118      */

119     public void done() {
120         monitor.done();
121     }
122
123     /**
124      * Indicate monitor progress.
125      *
126      * @see IProgressMonitor#internalWorked(double)
127      * @since 2.0
128      */

129     public void internalWorked(double work) {
130         monitor.internalWorked(work);
131     }
132
133     /**
134      * Check is use indicated that the operation be cancelled.
135      *
136      * @see IProgressMonitor#isCanceled()
137      * @since 2.0
138      */

139     public boolean isCanceled() {
140         return monitor.isCanceled();
141     }
142
143     /**
144      * Set the cancellation state.
145      *
146      * @see IProgressMonitor#setCanceled(boolean)
147      * @since 2.0
148      */

149     public void setCanceled(boolean value) {
150         monitor.setCanceled(value);
151     }
152
153     /**
154      * Set task name.
155      *
156      * @see IProgressMonitor#setTaskName(String)
157      * @since 2.0
158      */

159     public void setTaskName(String JavaDoc name) {
160         this.taskString = name;
161         this.subTaskString = ""; //$NON-NLS-1$
162
this.showDetails = false;
163         this.totalCopyCount = 0;
164         monitor.subTask(""); //$NON-NLS-1$
165
monitor.setTaskName(name);
166     }
167
168     /**
169      * Set subtask name.
170      *
171      * @see IProgressMonitor#subTask(String)
172      * @since 2.0
173      */

174     public void subTask(String JavaDoc name) {
175         this.subTaskString = name;
176         this.showDetails = false;
177         this.totalCopyCount = 0;
178         monitor.subTask(name);
179     }
180
181     /**
182      * Indicate monitor progress.
183      *
184      * @see IProgressMonitor#worked(int)
185      * @since 2.0
186      */

187     public void worked(int work) {
188         monitor.worked(work);
189     }
190
191     /**
192      * Save the current monitor state.
193      * The states are saved on a push-down stack. Prior states
194      * can be restored by calling restorState()
195      *
196      * @see #restoreState()
197      * @since 2.0
198      */

199     public void saveState() {
200         tasks.push(
201             new MonitorState(taskString, subTaskString, showDetails, currentCount, totalCopyCount));
202     }
203
204     /**
205      * Restore the monitor state.
206      *
207      * @see #saveState()
208      * @since 2.0
209      */

210     public void restoreState() {
211         if (tasks.size() > 0) {
212             MonitorState state = (MonitorState) tasks.pop();
213             setTaskName(state.getTaskString());
214             subTask(state.getSubTaskString());
215             this.showDetails = state.getShowDetails();
216             this.currentCount = state.getCurrentCount();
217             this.totalCopyCount = state.getTotalCopyCount();
218         }
219     }
220
221     /**
222      * Indicate whether the monitor subtask message should include
223      * copy progress counts.
224      *
225      * @see #setCopyCount(long)
226      * @see #setTotalCount(long)
227      * @param setting <code>true</code> to show the copy count,
228      * <code>false</code> otherwise
229      * @since 2.0
230      */

231     public void showCopyDetails(boolean setting) {
232         this.showDetails = setting;
233     }
234
235     /**
236      * Sets the total number of bytes to copy.
237      *
238      * @see #showCopyDetails(boolean)
239      * @see #setCopyCount(long)
240      * @param count total number of bytes to copy.
241      * @since 2.0
242      */

243     public void setTotalCount(long count) {
244         this.totalCopyCount = count;
245     }
246
247     /**
248      * Sets the number of bytes already copied.
249      *
250      * @see #showCopyDetails(boolean)
251      * @see #setTotalCount(long)
252      * @param count number of bytes already copied.
253      * @since 2.0
254      */

255     public void setCopyCount(long count) {
256         if (showDetails && count > 0) {
257             currentCount = count;
258             long countK = count / 1024;
259             long totalK = totalCopyCount / 1024;
260             String JavaDoc msg =
261                 (totalK <= 0)
262                     ? NLS.bind(Messages.InstallMonitor_DownloadSize, (new String JavaDoc[] { Long.toString(countK) }))
263                     : NLS.bind(Messages.InstallMonitor_DownloadSizeLong, (new String JavaDoc[] { Long.toString(countK), Long.toString(totalK) }));
264             monitor.subTask(subTaskString + msg);
265         }
266     }
267     
268     /**
269      * Increments the number of bytes copied.
270      *
271      * @param increment number of new bytes copied.
272      * @since 3.0
273      */

274     public void incrementCount(long increment) {
275         setCopyCount(currentCount + increment);
276     }
277 }
278
Popular Tags