javaswteclipse-rcp

Asynchronously control the BusyIndicator


Apparently all Eclipse/SWT has in the way of managing the busy mouse indicator is

BusyIndicator.showWhile(Runnable synchronousStuffToDo)

However, I have a fundamentally event-oriented project where "stuff to do" doesn't happen within a sequential line of execution: an action gets ordered and a continuation-callback is provided to the execution manager. Therefore I have nothing meaningful to put into that synchronousStuffToDo runnable.

Is there another, however low-level and clumsy, but platform-independent way of manipulating the busy indicator asynchronously, which means two separate method calls, "activate it" and "deactivate it"?

I should add ProgressMonitorDialog to this question because it appears to suffer from the same problem. Yes, within the ProgressMonitorDialog#run method an inner event loop will be spinned, but SWT event loop is just one of my execution managers, so the chain will still be broken. Apparently without this class I can't even show a progress monitor except if I reimplement from lower-level primitives.


Solution

  • 11 years later (and using a more recent version of Java), you might consider, using Eclipse 4.33 (Q3 2024):

    Enhanced BusyIndicator API for Futures

    The BusyIndicator class defines the following new method:

     static void showWhile(Future> future)
    

    If called from a Display thread, it waits for the given Future to complete and provides busy feedback using the busy indicator.

    While waiting for completion, pending UI events are processed to prevent UI freeze.

    If there is no Display for the current thread, Future.get() will be called, ignoring any ExecutionException and no busy feedback will be displayed.

    In your case:

    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        // assynchronousStuffToDo 
    });
    
    BusyIndicator.showWhile(future);