Search code examples
javawicketrenderer

Use of IChoiceRenderer


In wicket IChoiceRenderer for DropDownChoice is used like :

IChoiceRenderer renderer = new IChoicerRenderer() {
    public Object getDisplayValue(Object object) {
        return ((Country) object).getName();
    }

    public String getIdValue(Object object, int index) {    
        return ((Country) object).getId() + "";
    }
};

countries.setChoiceRenderer(renderer);

The specification of the IChoiceRenderer class state that:

Renders one choice. Separates the 'id' values used for internal representation from 'display values' which are the values shown to the user of components that use this renderer.

The description of getDisplayValue() is:

Get the value for displaying to an end user.

That means it helps to display the name of the country. Right?

And the description of getIdValue() is:

This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.

What does it mean?

In general id property of the models of various wicket component like DropDownChoice here, is of the type Long. Is getIdValue() helps to sort it?

Or helps to generate id tag for HTML?

What is concept of the aforesaid "Primary Key"?

Thanks and Regards.


Solution

  • Imagine that the objects will be put in a map, where the id is the key and the value is the object you want it to refer to. If two of your objects share the same id, or if the id of an object changes, your map won't work properly.

    This is what they mean by saying it should be a primary key.

    By the way, you don't have to implement IChoiceRenderer from scratch in simple situations, in your case you can use new ChoiceRenderer( "name", "id" );