Skip to content

Instantly share code, notes, and snippets.

@kevinsawicki
Created April 26, 2011 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinsawicki/942643 to your computer and use it in GitHub Desktop.
Save kevinsawicki/942643 to your computer and use it in GitHub Desktop.
SWT utility method for disposing of a resource when the disposal of a widget occurs.
/**
* SWT Utilities.
*
* @author Kevin Sawicki (kevin@github.com)
*/
public abstract class SwtUtils {
/**
* Dispose of resource when widget is disposed.
*
* This method immediately disposes of the resource if the widget is already
* disposed.
*
* @param resource
* @param widget
*/
public static void hookDispose(final Resource resource, Widget widget) {
if (widget == null || resource == null)
return;
if (!widget.isDisposed())
widget.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
resource.dispose();
}
});
else
resource.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment