On Thu, Jan 31, 2008 at 01:18:50PM +0100, Maeder Thomas wrote:
> Why insist on a CompoundPropertyModel? My first instinct would be to
> create a custom model for the checkboxes (which sets/unsets a single
> bit).
> 
> Thomas 

Agreed. Here's a model that I use to solve the same problem:

public class BitmappedFlagModel implements IModel {

    private IModel bitmapModel;
    private int mask;

    public BitmappedFlagModel(IModel bitmapModel, int mask) {
        this.bitmapModel = bitmapModel;
        this.mask = mask;
    }

    public Object getObject() {
        return (getBitmap() & mask) > 0;
    }

    public void setObject(Object object) {
        
        int bitmap = getBitmap();
        
        boolean b = ((Boolean) object).booleanValue();
        if (b) {
            bitmap |= mask;
        } else {
            bitmap &= ~mask;
        }
        
        bitmapModel.setObject(bitmap);
        
    }

    public void detach() {
        bitmapModel.detach();
    }

    private int getBitmap() {
        return ((Integer) bitmapModel.getObject()).intValue();
    }
}

Note that you can still use the CompoundPropertyModel for other
properties:

Form myForm = new Form("myForm", new CompoundPropertyModel(pojo));

myForm.add(new TextField("name"));
myForm.add(new CheckBox("enabled",
  new BitmappedFlagModel(
    new PropertyModel(pojo, "flags"), FLAG_ENABLED)));

jk

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to