com.sun.jbi.bc.common.window.progress (henceforth Progress) is a library package that simplifies the creation of a visual task progress display. The library utilizes the NetBeans Progress API
and Dialogs API
to render a consistent and cleanly manipulable progress display for use by GUI applications in the NetBeans platform.
Progress consists of these principal components:
Applications can use Progress in the following manner.
package trackertest;
import com.sun.jbi.bc.common.window.progress.ProgressController;
import com.sun.jbi.bc.common.window.progress.ProgressDescriptor;
import com.sun.jbi.bc.common.window.progress.ProgressDialogFactory;
import java.awt.Component;
import java.awt.Window;
import javax.swing.SwingUtilities;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
public static void main(String[] args) {
// Create a ProgressDescriptor
ProgressDescriptor progress =
ProgressDialogFactory.createProgressDialog("Sample Process", true);
// This is the control object for updating progress, canceling it, etc.
final ProgressController tracker = progress.getController();
// This is the visual component that displays the tracking.
final Component view = progress.getGUIComponent();
// Create thread to do the work being tracked.
// Since this is a standalone program, I could have skipped creating
// a separate thread for the task.
//
// But as this library is for GUI applications, where all this code
// would likely be executing in the Event Dispatch thread, I'm pretending
// that 'main' is executing in the dispatch thread as well.
Thread process = new Thread(new Runnable() {
public void run() {
// Start tracking progress consisting of 4 workunits.
tracker.start(4);
// Announce task phase, then do it, then check if I should cancel
tracker.progress("Sub task 1...", 1);
doSubtask1();
if (tracker.isCanceled()) {
return;
}
tracker.progress("Sub task 2...", 2);
doSubtask2();
if (tracker.isCanceled()) {
return;
}
// Lockout user from being able to cancel the progress
// from this point on.
tracker.lockout();
tracker.progress("Sub task 3...", 3);
doSubtask3();
tracker.progress("Sub task 4...", 4);
doSubtask4();
// Task complete!
tracker.finish();
}
});
// In a GUI application, where this code is already running in the event
// dispatch thread, there would be no need to push the setVisible call
// like this (and, in fact, it would be wrong to do so).
SwingUtilities.invokeLater(new Runnable() {
public void run() {
view.setVisible(true);
}
});
// Start the progressive task
try {
process.start();
process.join();
} catch (InterruptedException ex) {
;
}
if (tracker.isCanceled()) {
// Handle cancelation
} else {
// Handle completion
}
// (Only for the purpose of this example)
if (view instanceof Window) {
((Window) view).dispose();
}
}
private static void doSubtask1() {
delay(1500);
}
private static void doSubtask2() {
delay(2000);
}
private static void doSubtask3() {
delay(2500);
}
private static void doSubtask4() {
delay(4000);
}
private static void delay(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ex) {
;
}
}
}
Below: Sub task 2 (screenshot of phase 1 omitted) is underway. Note the cancelation button is still active. If a non-cancelable progress tracking was requested from the ProgressDialogFactory, the button would be grayed out from the start.
http://alaska.stc.com:10000/alaska/attach/BCGUIProgressLibrary/progressdialog2.jpg
Below: Sub task 3 is underway, and now the cancelation button is disabled, as a result of the programmatic call to ProgressController.lockout.
http://alaska.stc.com:10000/alaska/attach/BCGUIProgressLibrary/progressdialog3.jpg
Below: Sub task 4 is underway. Once the cancelation button is disabled via lockout call, it cannot be enabled again for the duration of the progress tracking.
http://alaska.stc.com:10000/alaska/attach/BCGUIProgressLibrary/progressdialog4.jpg
Below: ProgressController.finish has been called. The tracking display will briefly display its indication before automatically dismissing the dialog.
http://alaska.stc.com:10000/alaska/attach/BCGUIProgressLibrary/progressdialog5.jpg