tags:

views:

117

answers:

3

I have a org.eclipse.swt.widgets.Composite that I want to be able to enable/disable programatically. The org.eclipse.swt.widgets.Control.setEnabled(boolean enabled) method works fine, but it does not give any visual information that the widget(s) are disabled.

What I would like to do is to have the disabled state mean the widgets are greyed out. Right now they just enter a weird state where the user is unable to click or perform any action on them.

+2  A: 

A Composite is a container control that hold other controls using a layout - you can't see a composite really, you can only see the controls it holds. To disable and visually see then disabled, you'll have to call setEnabled(false) on all the children, assuming they're not containers too. Basically, to have to enable/disable the leaf widgets and you will see visual indication.

The reason you can't do anything with the widgets when disabling the Composite is because the Composite is eating all the events. Although the child widgets are not getting the events forwarded, they know nothing about the state of their parent, so they aren't greyed out.

arcticpenguin
A: 

In other words, you need to write code like this, given a Composite c:

for (Control child : c.getChildren())
  child.setEnabled(false);
dplass
not really - you need to recurse down the entire control stack. This will not work if say you have a sash in a composite and your widgets are in the sash.
arcticpenguin
Yes, I know. I was just giving an example.
dplass
+1  A: 

The problem was indeed that I was disabling the composite and not the controls inside it. What I ended up doing was something like this:

public void recursive setEnabled(Control ctr, boolean enabled) {
   if (ctrl instanceof Composite) {
      Composite comp = (Composite) ctrl;
      for (Control c : comp.getChildren())
         recursiveSetEnabled(c, enabled);
   } else {
      ctrl.setEnabled(enabled)
   }
}
Fredrik