DropDownChoice has been edited by Erik van Oosten (Mar 17, 2008).

(View changes)

Content:

Scope

This page describes the use of the DropDownChoice component. It is not meant to replace the available javadoc. Common usage questions (FAQ) of the component will be addressed here, along with background/design information, and examples where applicable.

Background

FAQ

How To Substitute Your Own Message In Place Of 'Choose One' (Answer provided by Igor Vaynberg)

  • Create a .properties file for your subclass of form/panel and define the null key for the value you want. For example:
    <p>
    mypanel.properties
    formid.dropdownid.null=Choose One

    or

    mypanel.properties
    null=Choose One


How do I provide the default choice when the underlying model is null?

Option 1: just make sure the underlying is set!

Option 2: use a model wrapper in a subclass of the dropdownchoice you want to use.

Here is an example usage code:

public class DefaultingCountryDropDown extends CountryDropDown {

    public DefaultingCountryDropDown(String id, IModel model) {
        super(id, new DefaultingModelWrapper(model, new Model("NL")));
    }

    public DefaultingCountryDropDown(String id) {
        super(id, new DefaultingModelWrapper(getModel(), new Model("NL")));
    }
}

And here is the wrapper:

import org.apache.wicket.model.IChainingModel;
import org.apache.wicket.model.AbstractWrapModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;

/**
  *
  * @author Erik van Oosten
  */
public class DefaultingModelWrapper implements IWrapModel {

    private IModel wrappedModel;
    private IModel defaultModel;

    public DefaultingModelWrapper(IModel wrappedModel, IModel
defaultModel) {
        this.wrappedModel = wrappedModel;
        this.defaultModel = defaultModel;
    }

    public IModel getWrappedModel() {
        return wrappedModel;
    }

    public Object getObject() {
        Object value = wrappedModel.getObject();
        if (value == null) {
            value = defaultModel.getObject();
        }
        return value;
    }

    public void setObject(Object object) {
        wrappedModel.setObject(object);
    }

    public void detach() {
        wrappedModel.detach();
        defaultModel.detach();
    }
}

Examples

See DropDownChoice Examples for a start...

Reply via email to