Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
void archiveInternet(ProgressMonitor pm) {
	try {
    	pm.beginTask("ComputingArchiving foointernet", 100);
		// Here: perform 20% of the work (download)
		pm.worked(20);
		// Here: perform 50% of the work (zip)
		pm.worked(50);
		// Here: perform remaining 30% work (copy)
		pm.worked(30);
	} finally {
		pm.done();
	}
}

...

Code Block
languagejava
void archiveInternet(ProgressMonitor pm) {
	try {
    	pm.beginTask("ComputingArchiving foointernet", 100);
		// Here: perform 20% of the work (download)
		downloadInternet(SubProgressMonitor.create(pm, 20));
		// Here: perform 50% of the work (zip)
		zipDownloadedInternet(SubProgressMonitor.create(pm, 50));
		// Here: perform remaining 30% work (copy)
		copyInternetZip(SubProgressMonitor.create(pm, 30));
	} finally {
		pm.done();
	}
}

...

 

Code Block
languagejava
archiveInternet(ProgressMonitor.NULL);

From command-line tools, you can use:

Code Block
languagejava
archiveInternet(new PrintWriterProgressMonitor(System.out));

From GUI code, we ustilise the adapter class org.esa.snap.rcp.util.ProgressHandleMonitor to report progress through ProgressMonitor interface but using NetBeans progress bars. As long running tasks should not be called from the Swing event dispatcher thread (EDT), we will have to create or reuse background threads to do so. The NetBeans org.netbeans.api.progress.ProgressUtils API provides some useful methods for this:

Code Block
languagejava
ProgressHandleMonitor pm = ProgressHandleMonitor.create("DownloadingArchiving internet");
Runnable operation = () -> {
	archiveInternet(pm);
};
ProgressUtils.runOffEventThreadWithProgressDialog(operation, "DownloadingArchiving internet", 
                                                  pm.getProgressHandle(), 
                                                  true, 
                                                  50,  // time in ms after which wait cursor is shown
                                                  1000);  // time in ms after which dialog with "Cancel" button is shown

Or using the NetBeans class org.openide.util.RequestProcessor:

Code Block
languagejava
RequestProcessor.getDefault().post(() -> {
	ProgressHandle handle = ProgressHandleFactory.createHandle("DownloadingArchiving internet");
	ProgressMonitor pm = new ProgressHandleMonitor(handle);
	archiveInternet(pm);
});

See https://github.com/senbox-org/snap-desktop/blob/master/snap-rcp/src/main/java/org/esa/snap/rcp/util/ProgressHandleMonitor.java.

Documentation of NetBeans Progress API:

...