Java SWT: syncExec and asyncExec packaging for code cleaning

I have a Java application using SWT as a toolkit and am tired of all the ugly code table code needed to update a GUI element.

Just to set the disabled button to enable, I need to go through something like this:

shell.getDisplay().asyncExec(new Runnable() {
    public void run() {
        buttonOk.setEnabled(true);
    }
});

I prefer my source code to be as flat as possible, but I need a whopping 3 levels of indentation to make something simple.

Is there any way to wrap it? I need a class like:

public class UIUpdater {
    public static void updateUI(Shell shell, *function_ptr*) {
        shell.getDisplay().asyncExec(new Runnable() {
           public void run() {
              //Execute function_ptr
           }
        });
    }
}

And you can use it like this:

UIUpdater.updateUI(shell, buttonOk.setEnabled(true));

Something like this would be great in order to hide this terrible mess, which apparently seems to need to be done.

, Java . Java 7 - Closures, , . - , , ?

, , Swing, - SWT.

+5
3

SWT-.

, , asyncExec syncExec. GUI, .

GUI .

, , GUI, setEnabled(shell, true)

public void setEnabled(Shell shell, boolean flag), .

+4

. API :

SWTUtils.asyncExec(display).on(this.getClass(), this).runInUIThread(123);

runInUIThread(.) this 123. - this.getClass().

:

SWTUtils.asyncExec(shell.getDisplay()).on(Button.class, buttonOk).setEnabled(true);

CGLib . on(.), display.asyncExec(.). Objenesis .

, . , API- , , java 1.6, .

, ;) , , , . .

+2

, - :

public class UIUpdater {
    public static void updateUI(Shell shell, *function_ptr*) {
        shell.getDisplay().asyncExec(new Runnable() {
           public void run() {
              //Execute function_ptr
           }
        });
    }
}

, , SWT, Java . Java DSL, , , , , , .

, , , Scala, , . , - , , SWT SWT.

- :

val enabled = model.isEditable
Swt {
  text.setEnabled(enabled);
  composite.refresh();
}
+2

All Articles