Wednesday, March 31, 2010

Tip: How to Debug SWT components in Modal Dialogs

SWT and JFace components, and especially layouts can be quite time consuming to debug when some unexpected behavior is observed at runtime.

Some tools (such as the YARI tool set) can be used to debug SWT components and layouts at runtime, but when modal dialogs are displayed, such tools can't be used because the developer can't setup the inspector window and inspect the components in the list while the dialog is up.

To resolve this issue, one can

  1. Detach the inspector view so it displays in a floating window outside of the workbench.
  2. Put a breakpoint in the modal dialog source code, few lines after the setShellStyle() call, with a breakpoint condition so that it removes the MODAL SWT flag.

For example, for making the WizardDialog non modal, a breakpoint with the following condition:

   setShellStyle(SWT.CLOSE | SWT.MAX | SWT.TITLE | SWT.BORDER       
      | SWT.RESIZE | getDefaultOrientation());    
   return false; 

needs to be added to the "setWizard(newWizard)" line below:

   public WizardDialog(Shell parentShell, IWizard newWizard) {
               | SWT.APPLICATION_MODAL | SWT.RESIZE | getDefaultOrientation());

This way, you will be able to debug the SWT components at runtime even in modal dialogs: