Introducing YippieMove '09. Easy email transfers. Now open for all destinations.

SwtCallback 0.1 was released only a little more than a week ago, but that’s not stopping us from releasing 0.2 today! In line with the original philosophy, SwtCallback has been extended to again reduce clutter and deep nesting in your Java code. This time SwtCallback attacks the Runnable interface.

As I wrote in my release post about SwtCallback, SwtCallback is a java library for SWT applications which adds an alternative way to handle events: callbacks. Instead of clunky listener interfaces or messy anonymous classes, SwtCallback enables one line solutions that make it easy to keep your event handling code in a single flat class – this while still being more readable and (surprisingly) improving encapsulation in some cases.

This latest release extends this functionality from just listeners to anything that takes a runnable. Prime examples in SWT are display.syncExec(), display.asyncExec() and timers.

For example, maybe you wish to refresh some UI components to reflect a change in data. But you’re not on the SWT thread so you have to use syncExec. Normally, you’d do something like,

display.syncExec(new Runnable() {
  public void run() {
    doUpdateSwtLabels();
  }
});

With a Callback you can turn this into one highly readable line:

display.syncExec(new Callback(this, "doUpdateSwtLabels"));

The most useful application may be for timers. In SWT, a timer needs to be reestablished at the end of timer execution if you wish it to recur. You may end up with unwieldy code such as,

void initWidgets() {
  /* ... */

  // Prime the timer.
  display.timerExec(INITIAL_DELAY, new Runnable() {
    public void run() {
      refresh();
    }
  }); 
}

void refresh() {
  /* ... update stuff ... */
  
  // Set the timer again.
  display.timerExec(TIMER_DELAY, new Runnable() {
    public void run() {
      refresh();
    }
  }); 
}

With a Callback you can now do this:

void initWidgets() {
  /* ... */

  // Prime the timer.
  display.timerExec(INITIAL_DELAY, 
    new Callback(this, "refresh")); 
}

void refresh() {
  /* ... update stuff ... */
  
  // Set the timer again.
  display.timerExec(TIMER_DELAY, 
    new Callback(this, "refresh")); 
}

This is much easier to read and follow, at least in my personal opinion. If you agree, go ahead and grab the download below – SwtCallback is released under the BSD license so you should be able to integrate it with whatever you’re working on.

Download the source and binary here:

Download SwtCallBack. More information is available here.

There’s some extra javadoc in this release to make it easier to get started. Still, there isn’t much in the way of documentation – we’ll go back and write some if there’s demand for it.

Author: Tags: , ,

Trackbacks/Pingbacks

  1. Playing With Wire » WireLoad releases SwtCallback Java Library

© 2006-2009 WireLoad, LLC.
Logo photo by William Picard. Theme based on BlueMod © 2005 - 2009 FrederikM.de, based on blueblog_DE by Oliver Wunder.
Sitemap