Binding a Map to a DropDownChoice in Wicket App

February 5, 2010


Wicket

One day i had to change a page (WebPage) in a way that it would allow me to persist a simple attribute of an entity. First i tried to use an Enum, however i’m using JPA persistence with Hibernate and was a little bit concerned about how Hibernate would deal with this Enum in searches or some other more advanced stuff. So i decided to create a simple Integer attribute and set a constant on it.

This is a sample of code using a Map to exhibit a list of options (DropDownChoice) in a Wicket application to persist a constant (Integer) in an entity.

Code of ControlSerum entity where the attribute status will be persisted:

@Entity
public class ControlSerum {
  private Integer status;

  public final static int STATUS_PREP = 0;
  public final static int STATUS_ACTIVE = 1;
  public final static int STATUS_INACTIVE = 2;
  private final static Map<Integer, String> statusMap = new HashMap<Integer, String>();

  static{
    statusMap.put(STATUS_PREP, "Em preparo");
    statusMap.put(STATUS_ACTIVE, "Ativo");
    statusMap.put(STATUS_INACTIVE, "Inativo");
  }

  @Column(nullable=false)
  public Integer getStatus() {
    return status;
  }
  public void setStatus(Integer status) {
    this.status = status;
  }

  @Transient
  public static Map<Integer, String> getStatusMap(){
    return new HashMap<Integer, String>(statusMap);
  }
}

At ControlSerumEditPage page class, i implemented a IChoiceRenderer to renderize correctly the options of select tag using the Map key as the value of option tag and the Map value as the name for exibition of option tag.

ControlSerumEditPage class constructor snippet where a renderizer for DropDownChoice is instantiated and added to Form:

IChoiceRenderer choicesRenderer = new IChoiceRenderer() {
  @Override
  public String getDisplayValue(Integer object) {
    return ControlSerum.getStatusMap().get(object);
  }
  @Override
  public String getIdValue(Integer object, int index) {
    return object.toString();
  }
};

IModel optionsModel = new Model(){
  @Override
  public Serializable getObject() {
    return new ArrayList(ControlSerum.getStatusMap().keySet());
  }
};

IModel statusModel = new PropertyModel<Integer>(controlSerum, "status");

DropDownChoice<Integer> status = new DropDownChoice<Integer>("status", statusModel, optionsModel, choicesRenderer);
form.add(status);

To download the full code of this application (still in development):
http://github.com/ziglee/biomedcalib

Please post your doubts or comments and i’ll promptly answer you!

About these ads

6 Responses to “Binding a Map to a DropDownChoice in Wicket App”


  1. OP: I could be slow (lord knows I have been told lol) but that made absolutely no sense what so ever…

  2. David Says:

    I think I get it… and I am definitely slow…

    So Cassio, is the idea that instead of using a raw enum (which can’t be mapped with Hibernate since enums aren’t classes), you’re creating a class that wraps your enum so it can be mapped by Hibernate, correct?


    • The Integer is being directly persisted in ControlSerum class. Hibernate does persist Enums, but to keep it simple, I didn’t use it. The objective of this code sample is just to show how to use a DropDownChoice to persist a Integer directly into an Entity.
      Please, be confortable to ask anything! =D

  3. Dmitry Says:

    Hi, Cassio

    Thank you very much for that great example, especially for carefully naming of variables.

    statusModel and optionsModel names just save my day and give me more than I could dig out of plenty of tutorials on DropDownChoice for understanding and implementing DDC for my own needs.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: