1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * ident "%Z%%M% %I% %E% SMI"
24 *
25 * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28 package com.sun.dhcpmgr.ui;
29
30 import javax.swing.ProgressMonitor;
31 import javax.swing.SwingUtilities;
32 import java.awt.Component;
33
34 /**
35 * Provides a framework for doing long-running operations and keeping the
36 * user apprised of the results. Also provides an exception if Cancel is
37 * pressed in the progress dialog.
38 */
39 public class ProgressManager {
40 private ProgressMonitor monitor;
41 private int count;
42 private String message;
43 private Runnable progressUpdater;
44
45 /**
46 * Create a new ProgressManager; see ProgressMonitor for description
47 * of the parameters here.
48 * @see javax.swing.ProgressMonitor
49 */
50 public ProgressManager(Component comp, Object msg, String note, int min,
51 int max) {
52 // Initialize count so we can auto-increment
53 count = min;
54 monitor = new ProgressMonitor(comp, msg, note, min, max);
55 // Create background object to update monitor
56 progressUpdater = new Runnable() {
57 public void run() {
58 monitor.setProgress(count);
59 monitor.setNote(message);
60 }
61 };
62 }
63
64 /**
65 * Update the progress display. Throws InterruptedException if user
66 * has pressed the Cancel button on the progress dialog
67 * @param progress the amount of the task that has been completed
68 * @param msg the message to be displayed at this time
69 * @throws java.lang.InterruptedException
70 */
71 public void update(int progress, String msg) throws InterruptedException {
72 count = progress;
73 message = msg;
74 SwingUtilities.invokeLater(progressUpdater);
75 if (monitor.isCanceled()) {
76 throw new InterruptedException();
77 }
78 }
79
80 /**
81 * Update the progress display, automatically incrementing the count
82 * by one. Throws InterruptedException if the user has pressed the
83 * Cancel button in the progress dialog
84 * @param msg the message to be display at this time
85 * @throws java.lang.InterruptedException
86 */
87 public void update(String msg) throws InterruptedException {
88 update(count+1, msg);
89 }
90
91 }