Disable a com.itmill.toolkit.ui.Form and all components on it. How-to?

Hi,

When I disable a Form using setEnabled(false) I would expect that everything on it would be disabled, but that’s not what happens. All child components remain enabled. How do I get the desired behavior? Must I disable all the components on the Form one by one?

Thank you for your attention,

Johnny

Hi again!

The easiest way I could think up on doing what you want, is by overriding the setEnabled() function to match your needs. This is easier to achieve inline than making an own form that extends the toolkit’s form. So instead of just changing the form elements setEnabled you get the elements from the form and use their setEnabled().

Here’s the code I came up:

So instead of

Form form = new Form();

you have


        form = new Form() {
            @Override
            public void setEnabled(boolean enabled) {
                Collection c = form.getItemPropertyIds();
                Object[] array = c.toArray();
                for (Object entry : array) {
                    Field field = (Field) form.getItemProperty(entry);
                    field.setEnabled(enabled);
                }
                // super.setEnabled(enabled);
            }
        };

The code is maybe not the neatest possible, but the best that I could whip up fast. It has missing generics and the casting from collection to array may be unnecessary. Work with my example however :wink:

Remove the comment marks from the last line if you want to make the form field itself disabled

I noticed a little bug in my code. You will get the wrong info out of form.isEnabled() is you don’t use the super.setEnabled(), as the form itself actually isn’t disabled. I could not re-activate my form as I had a single button to that switched the ‘enabled’ -state by checking isEnabled();


if (form.isEnabled()) {
  form.setEnabled(false);
} else {
  form.setEnabled(true);
}

That’s why I had to override the isEnabled() function too to get the right info out of it. As I said, this is not needed if you run super.setEnabled(enabled);


Form form = new Form() {
    private boolean enabled = true;

    @Override
    public void setEnabled(boolean enabled) {
        Collection c = form.getItemPropertyIds();
        Object[] array = c.toArray();
        for (Object entry : array) {
            Field field = (Field) form.getItemProperty(entry);
            field.setEnabled(enabled);
        }
        this.enabled = enabled;
        // super.setEnabled(enabled);
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
};

Hi,

First of all, sorry for not having replied any sooner. I copied and pasted your example into my code and it worked! Sort of.

I had to change the line that reads [quote]
Field field = (Field) form.getItemProperty(entry);

[/quote] to [quote]
Field field = (Field) form.getField(entry);
[/quote] to make it work and still can’t disable all of the Form’s fields - a DateField, for instance, resists now and forever being disabled. Also, if I edit a TextField, the whole Form (DateField and all) is disabled - not just the Form’s fields.

Regards,

Johnny

[quote]
a DateField, for instance, resists now and forever being disabled.
[/quote] Correction - The DateField is being disabled, it just doesn’t look disabled.

Hey,

I tested it again. I didn’t notice the problem about dateFields not being disabled visually. They look just fine to me. I’m on ITMill Toolkit version 5.2.5.

It’s true that there is a bug about the whole form getting disabled when some info is in the fields. I noticed that the reason to this is the overriding of the isEnabled() method. I guess that some other method calls upon that and it gets all whacked up when it returns different info.

There is two solutions that comes to mind now:

If you don’t need the isEnabled information, don’t override the method and don’t use it. I had earlier a button that flipped ‘enabled’ back and forth which check isEnabled to decide what to do next. By replacing it to two different buttons, ‘enable’ and ‘disable’, i removed the need of isEnabled() and everything worked like a charm. Check if you need the enabled information in your code.

Second option is to rename the method isEnabled to something else, say isFieldsEnabled(). That way you can have your own check that doesn’t harm other functions. You will probably have to make an own class that extends the form (like, public class myFormOfGreatness extends Form, where you define the setEnabled() from earlier code and isFieldsEnabled()), as inline definitions of functions can only be called from the method that created the object.

Tell me if the info was confusing, and I’ll explain it more thoroughly.

Just found out that the problem with the DateField happens in IE only. In Firefox everything goes OK.

As to the isEnabled() method, I don’t really need it, so all is well.

Best regards,

Johnny

Right. Didn’t test with IE earlier. It does the same for me. I’ll make a ticket about this and the dev team will probably make a fix / show how make it right.

Did you have any more problems than with datefield in ie? Everything else shows up correctly?

No. That was it, so far.

Regards,

Johnny

Ticket made about the case:


IT Mill Trac

Hi!

It was a CSS issue with IE dependent transparency hack. I fixed this just a minute ago into trunk.

cheers,
matti