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:


Benjamin Muskalla said...

For fixing layout issues, I can highly recommend PDE Picasso that includes all layout informations as tooltips.


Serge Beauchamp said...

Thanks for the link!

Anonymous said...

Yari provides two shortcuts to come along with this kind of problem:

ctrl+alt+v: paint the control (under the mouse cursor) and its children in different colors

ctrl+alt+-: open a swt inspector in a separate shell

Since version 2.0.0 these key bindings work also in dialogs.

So no "debug stuff" is needed anymore.