Re: Wrapping a POJO

2008-01-31 Thread Markus Strickler

Hi-

thanks, that's just what I was looking for.
Somehow all the wrapping of models inside of models still doesn't  
come naturally to me.


-markus

Am 31.01.2008 um 14:16 schrieb John Krasnay:


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]




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



Re: Wrapping a POJO

2008-01-31 Thread John Krasnay
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]



RE: Wrapping a POJO

2008-01-31 Thread Maeder Thomas
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 

> -Original Message-
> From: Markus Strickler [mailto:[EMAIL PROTECTED] 
> Sent: Donnerstag, 31. Januar 2008 13:00
> To: users@wicket.apache.org
> Subject: Re: Wrapping a POJO
> 
> Hi-
> 
> thanks for the quick reply. My description probably wasn't 
> clear enough.
> My problem is that I have several checkboxes in the interface 
> that all map to a single Integer in the POJO. So there is not 
> accessor that accepts a boolean, which is why I need some way 
> to translate between the POJO and the model that backs the form.
> The wiki actually has an example that is somewhat similar
> (<http://cwiki.apache.org/WICKET/listview-with-checkboxes.html
> >) but only wraps a String.
> But coming to think of it, I can probably just access the 
> POJO fields through the wrapper like this: wrapper.pojo.field.
> 
> OK, I'll probably try this.
> 
> Thanks again,
> -markus
> 
> Zitat von Per Newgro <[EMAIL PROTECTED]>:
> 
> > Hi Markus,
> >
> > you simply have to provide the POJO to the CompoundPropertyModel.
> > Provide simple PropertyModels related to the compound model 
> for the fields.
> >
> > You can imagine the whole concept as "the path to the value".
> >
> > Pojo myPojo = new Pojo();
> > IModel model = new CompoundPropertyModel(myPojo); IModel 
> > aPropertyModel = new PropertyModel(model, 
> "theNameOfAccessorInPojo"); 
> > Label myPojoProperty = new Label("aWicketId", new aPropertyModel);
> >
> > I use a label. Replace it by your component.
> >
> > Cheers
> > Per
> >
> > PS: If you new to wicket - checkout the wiki and the examples. They 
> > explain alot.
> >
> > 
> -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> 
> 
> 
> This message was sent using IMP, the Internet Messaging Program.
> 
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

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



Re: Wrapping a POJO

2008-01-31 Thread Markus Strickler

Hi-

thanks for the quick reply. My description probably wasn't clear enough.
My problem is that I have several checkboxes in the interface that all 
map to a

single Integer in the POJO. So there is not accessor that accepts a boolean,
which is why I need some way to translate between the POJO and the model that
backs the form.
The wiki actually has an example that is somewhat similar
() but 
only wraps

a String.
But coming to think of it, I can probably just access the POJO fields through
the wrapper like this: wrapper.pojo.field.

OK, I'll probably try this.

Thanks again,
-markus

Zitat von Per Newgro <[EMAIL PROTECTED]>:


Hi Markus,

you simply have to provide the POJO to the CompoundPropertyModel.
Provide simple PropertyModels related to the compound model for the fields.

You can imagine the whole concept as "the path to the value".

Pojo myPojo = new Pojo();
IModel model = new CompoundPropertyModel(myPojo);
IModel aPropertyModel = new PropertyModel(model, "theNameOfAccessorInPojo");
Label myPojoProperty = new Label("aWicketId", new aPropertyModel);

I use a label. Replace it by your component.

Cheers
Per

PS: If you new to wicket - checkout the wiki and the examples. They explain
alot.

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







This message was sent using IMP, the Internet Messaging Program.


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



Re: Wrapping a POJO

2008-01-31 Thread Nino Saturnino Martinez Vazquez Wael

And the label here with compoundmodel..

IModel model = new CompoundPropertyModel(myPojo);


Page.setmodel(model)

Label myPojoProperty = new Label("propertyName");
add(myPojoProperty);



Per Newgro wrote:

Hi Markus,

you simply have to provide the POJO to the CompoundPropertyModel.
Provide simple PropertyModels related to the compound model for the fields.

You can imagine the whole concept as "the path to the value".

Pojo myPojo = new Pojo();
IModel model = new CompoundPropertyModel(myPojo);
IModel aPropertyModel = new PropertyModel(model, "theNameOfAccessorInPojo");
Label myPojoProperty = new Label("aWicketId", new aPropertyModel);

I use a label. Replace it by your component.

Cheers
Per

PS: If you new to wicket - checkout the wiki and the examples. They explain 
alot.


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


  


--
Nino Martinez Wael
Java Specialist @ Jayway DK
http://www.jayway.dk
+45 2936 7684


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



Re: Wrapping a POJO

2008-01-31 Thread Per Newgro
Hi Markus,

you simply have to provide the POJO to the CompoundPropertyModel.
Provide simple PropertyModels related to the compound model for the fields.

You can imagine the whole concept as "the path to the value".

Pojo myPojo = new Pojo();
IModel model = new CompoundPropertyModel(myPojo);
IModel aPropertyModel = new PropertyModel(model, "theNameOfAccessorInPojo");
Label myPojoProperty = new Label("aWicketId", new aPropertyModel);

I use a label. Replace it by your component.

Cheers
Per

PS: If you new to wicket - checkout the wiki and the examples. They explain 
alot.

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