Date: Fri, 29 Mar 2024 08:54:47 +0000 (UTC) Message-ID: <1305832251.3.1711702487469@ac354d91bd8a> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2_896651455.1711702487468" ------=_Part_2_896651455.1711702487468 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The class com.bc.ceres.ProgressMonitor
is generally used to=
report progress in log running tasks. Many framework callbacks provid=
e an instance of this interface to client functions.
void ar= chiveInternet(ProgressMonitor pm) { =09try { =09pm.beginTask("Archiving internet", 100); =09=09// Here: perform 20% of the work (download) =09=09pm.worked(20); =09=09// Here: perform 50% of the work (zip) =09=09pm.worked(50); =09=09// Here: perform remaining 30% work (copy) =09=09pm.worked(30); =09} finally { =09=09pm.done(); =09} }
Or pass sub-progess monitors to sub-functions:
void ar= chiveInternet(ProgressMonitor pm) { =09try { =09pm.beginTask("Archiving internet", 100); =09=09// Here: perform 20% of the work (download) =09=09downloadInternet(SubProgressMonitor.create(pm, 20)); =09=09// Here: perform 50% of the work (zip) =09=09zipDownloadedInternet(SubProgressMonitor.create(pm, 50)); =09=09// Here: perform remaining 30% work (copy) =09=09copyInternetZip(SubProgressMonitor.create(pm, 30)); =09} finally { =09=09pm.done(); =09} }
Not shown in the code above is the check for cancelation. A user may hav=
e requested to cancel a long-running task. This is reported through the
void ar= chiveInternet(ProgressMonitor pm) { =09... =09for (...) { =09=09... =09=09if (pm.isCancelled()) { =09=09 =09return; // or throw new CanceledException(); =09=09} ... =09} =09... }
null
):
archive= Internet(ProgressMonitor.NULL);
From command-line tools, you can use:
archive= Internet(new PrintWriterProgressMonitor(System.out));
From GUI code, we ustilise the adapter class org.esa.snap.rcp=
.util.ProgressHandleMonitor
to report progress through Pr=
ogressMonitor
interface but using NetBeans progress bars. As long ru=
nning tasks should not be called from the Swing event dispatcher thread (ED=
T), we will have to create or reuse background threads to do so. The NetBea=
ns org.netbeans.api.progress.ProgressUtils
API provi=
des some useful methods for this:
Progres= sHandleMonitor pm =3D ProgressHandleMonitor.create("Archiving internet"); Runnable operation =3D () -> { =09archiveInternet(pm); }; ProgressUtils.runOffEventThreadWithProgressDialog(operation, "Archiving int= ernet", pm.getProgressHandle(),&n= bsp; true, 50, // time in ms after = which wait cursor is shown 1000); // time in ms aft= er which dialog with "Cancel" button is shown
Or using the NetBeans class org.openide.util.RequestProcessor=
:
Request= Processor.getDefault().post(() -> { =09ProgressHandle handle =3D ProgressHandleFactory.createHandle("Archiving = internet"); =09ProgressMonitor pm =3D new ProgressHandleMonitor(handle); =09archiveInternet(pm); });
Documentation of NetBeans Progress API:
In older code you will find uses of the com.bc.ceres.swing.pr=
ogress.ProgressMonitorSwingWorker
class. Don't use it as i=
t will cause the main frame to go into background and let other desktop win=
dows hide it. Very annoying for users (and us developers).