Marko Topolnik Marko Topolnik - 6 months ago 25
Java Question

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.

Answer

There is no way you can manipulate the Cursor using the BusyIndicator class. You can invoke the below util method to show a Busy Icon while running your job on a background Thread

public static void imBusy(final boolean busy){

    Display.getDefault().asyncExec(new Runnable()
    {
        @Override
        public void run()
        {
            Shell shell = Display.getDefault().getActiveShell();
            if(busy){ //show Busy Cursor
                Cursor cursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT);          
                shell.setCursor(cursor);
            }else{  
                shell.setCursor(null);
            }   
        }
    });

}
Comments