You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ian MacLarty <ia...@gmail.com> on 2009/04/04 05:30:17 UTC

dynamic component

Hello,

I'd like to create a Component that behaves like a CheckBox if its
model is a Boolean and a TextField if its model is a String.  I'd like
to be able to add the component to a form with a
CompoundPropertyModel, just like you can with a CheckBox or TextField.
 How would you go about creating such a component?

Cheers,
Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: dynamic component

Posted by Ian MacLarty <ia...@gmail.com>.
On Sat, Apr 4, 2009 at 3:16 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> create a formcomponentpanel, add a checkbox and a textfield and make
> only one of them visible.
>

Thanks.

Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: dynamic component

Posted by Ian MacLarty <ia...@gmail.com>.
On Sun, Apr 5, 2009 at 12:03 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> the reason i suggested adding both is that the builtin textfiend and
> checkbox components check the input type of the markup they are
> attached to, so textfield can only be attached to type="text" and
> checkbox to type="checkbox". since you are not using the builtin
> components it looks like, you can safely do what you are.
>

Cool, thanks for your help.

Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: dynamic component

Posted by Igor Vaynberg <ig...@gmail.com>.
the reason i suggested adding both is that the builtin textfiend and
checkbox components check the input type of the markup they are
attached to, so textfield can only be attached to type="text" and
checkbox to type="checkbox". since you are not using the builtin
components it looks like, you can safely do what you are.

-igor

On Sat, Apr 4, 2009 at 6:49 PM, Ian MacLarty <ia...@gmail.com> wrote:
> On Sat, Apr 4, 2009 at 2:16 PM, Igor Vaynberg <ig...@gmail.com> wrote:
>> create a formcomponentpanel, add a checkbox and a textfield and make
>> only one of them visible.
>>
>
> I think I found a more elegant (and efficient?) way to do this if I
> want to cover more than 2 model classes.
>
> Instead of adding a component for each possible class in the
> constructor and setting one of them to be visible in onBeforeRender, I
> add only one component in onBeforeRender (it cannot be added in the
> constructor, because I don't have access to the model at that point).
> Once I've added the component I set a flag so that it isn't added
> twice.
>
> This seems to work, but I just wanted to check if there are any issues
> with adding the component in onBeforeRender instead of the
> constructor?
>
> Here is the code (StringField and BooleanField are FormComponentPanels
> with one TextField and CheckBox respectively):
>
> public class DynamicComponent extends FormComponentPanel {
>    private FormComponent component;
>    private boolean initialized = false;
>
>    public DynamicComponent(String id) {
>        super(id);
>    }
>
>    public DynamicComponent(String id, IModel model) {
>        super(id, model);
>    }
>
>    @Override
>    protected void convertInput() {
>        setConvertedInput(component.getConvertedInput());
>    }
>
>    @Override
>    protected void onBeforeRender() {
>        IModel model = getModel();
>        if (!initialized) {
>            Class clazz;
>            if (model instanceof AbstractPropertyModel) {
>                // Get the type from the AbstractPropertyModel if we can,
>                // since that will work even if the value is null.
>                clazz = ((AbstractPropertyModel)model).getObjectClass();
>            } else {
>                Object modelVal = model.getObject();
>                if (modelVal != null) {
>                    clazz = modelVal.getClass();
>                } else {
>                    clazz = String.class;
>                }
>            }
>            if (clazz.equals(String.class)) {
>                component = new StringField("component", model);
>            } else if (clazz.equals(Boolean.class)) {
>                component = new BooleanField("component", model);
>            } else {
>                throw new RuntimeException("Unhandled type: " + clazz);
>            }
>            add(component);
>            initialized = true;
>        }
>        super.onBeforeRender();
>    }
> }
>
> Cheers,
> Ian.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: dynamic component

Posted by Ian MacLarty <ia...@gmail.com>.
On Sat, Apr 4, 2009 at 2:16 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> create a formcomponentpanel, add a checkbox and a textfield and make
> only one of them visible.
>

I think I found a more elegant (and efficient?) way to do this if I
want to cover more than 2 model classes.

Instead of adding a component for each possible class in the
constructor and setting one of them to be visible in onBeforeRender, I
add only one component in onBeforeRender (it cannot be added in the
constructor, because I don't have access to the model at that point).
Once I've added the component I set a flag so that it isn't added
twice.

This seems to work, but I just wanted to check if there are any issues
with adding the component in onBeforeRender instead of the
constructor?

Here is the code (StringField and BooleanField are FormComponentPanels
with one TextField and CheckBox respectively):

public class DynamicComponent extends FormComponentPanel {
    private FormComponent component;
    private boolean initialized = false;

    public DynamicComponent(String id) {
        super(id);
    }

    public DynamicComponent(String id, IModel model) {
        super(id, model);
    }

    @Override
    protected void convertInput() {
        setConvertedInput(component.getConvertedInput());
    }

    @Override
    protected void onBeforeRender() {
        IModel model = getModel();
        if (!initialized) {
            Class clazz;
            if (model instanceof AbstractPropertyModel) {
                // Get the type from the AbstractPropertyModel if we can,
                // since that will work even if the value is null.
                clazz = ((AbstractPropertyModel)model).getObjectClass();
            } else {
                Object modelVal = model.getObject();
                if (modelVal != null) {
                    clazz = modelVal.getClass();
                } else {
                    clazz = String.class;
                }
            }
            if (clazz.equals(String.class)) {
                component = new StringField("component", model);
            } else if (clazz.equals(Boolean.class)) {
                component = new BooleanField("component", model);
            } else {
                throw new RuntimeException("Unhandled type: " + clazz);
            }
            add(component);
            initialized = true;
        }
        super.onBeforeRender();
    }
}

Cheers,
Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: dynamic component

Posted by Igor Vaynberg <ig...@gmail.com>.
create a formcomponentpanel, add a checkbox and a textfield and make
only one of them visible.

-igor

On Fri, Apr 3, 2009 at 8:30 PM, Ian MacLarty <ia...@gmail.com> wrote:
> Hello,
>
> I'd like to create a Component that behaves like a CheckBox if its
> model is a Boolean and a TextField if its model is a String.  I'd like
> to be able to add the component to a form with a
> CompoundPropertyModel, just like you can with a CheckBox or TextField.
>  How would you go about creating such a component?
>
> Cheers,
> Ian.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org