Thanks Igor,
I still have a long way to go before I have a good understanding of Wicket.
I ended up with this:
item.add(new Label("name", new DefaultTextModel(new Model("No name"), new
PropertyModel(item.getModel(),"name"))));
// maybe I should figure out how I can omit the new Propertymodel part and
use the
// CompoundPropertyModel
public class DefaultTextModel extends AbstractReadOnlyModel {
private static final long serialVersionUID = 2723350577347498281L;
private final IModel delegate;
private final IModel defaultModel;
public DefaultTextModel(IModel defaultModel, IModel delegate) {
this.defaultModel = defaultModel;
this.delegate=delegate;
}
public DefaultTextModel(String defaultText, IModel delegate) {
this(new Model(defaultText), delegate);
}
public Object getObject(Component component) {
String s = (String) delegate.getObject(component);
return (s == null) ? (String) defaultModel.getObject(component) : s;
}
public void detach() {
delegate.detach();
}
}
2007/6/14, Igor Vaynberg <[EMAIL PROTECTED]>:
imho more reusable if you just do this
class defaulttextmodel extends abstractreadonlymodel {
private final imodel delegate;
private final string default;
public defaulttextmodel(string default, imodel delegate) {
this.default=default;this.delegate=delegate; }
public object getobject() { String s=delegate.getobject(); return
(strings.isempty(s))?default:s; }
public void detach() { delegate.detach(); }
}
then just add(new label("label", new defaulttextmodel("default text",
model));
-igor
On 6/14/07, Pieter Cogghe <[EMAIL PROTECTED]> wrote:
> So if the property is first not null and I change it later on to null,
> I would run in trouble?
>
> I changed it to this;
>
> public class LabelWithDefaultModel extends Label {
>
> private IModel defaultModel;
>
> public LabelWithDefaultModel(String id, IModel model, IModel
defaultModel) {
> super(id, model);
> this.defaultModel = defaultModel;
> }
>
> public LabelWithDefaultModel(String id, IModel model, String
defaultModel) {
> this(id, model, new Model(defaultModel));
> }
>
> public LabelWithDefaultModel(String id, IModel defaultModel) {
> super(id);
> this.defaultModel = defaultModel;
> }
>
> public LabelWithDefaultModel(String id, String defaultModel) {
> this (id, new Model(defaultModel));
> }
>
> @Override
> protected void onComponentTagBody(MarkupStream markupStream,
> ComponentTag openTag) {
> String model = (String) getModelObject();
> replaceComponentTagBody(markupStream,
openTag, (String) ((model !=
> null) ? model : (String) defaultModel.getObject (this)));
> }
>
> }
>
> 2007/6/14, Martijn Dashorst <[EMAIL PROTECTED]>:
> > I wouldn't do that... If you change the property that should be shown
> > when not null, it will not be reflected here, as you have lost the
> > connection between the label and the original model.
> >
> > Martijn
> >
> > On 6/14/07, Pieter Cogghe < [EMAIL PROTECTED]> wrote:
> > > Hi,
> > >
> > > thanks for the help, I stripped it down to this:
> > > (seems to work fine)
> > >
> > > public class LabelWithDefaultModel extends Label {
> > >
> > > public LabelWithDefaultModel(String id, IModel model, IModel
defaultModel) {
> > > super(id, model);
> > > if (this.getModelObject() == null){
> > > this.setModel(defaultModel);
> > > }
> > > }
> > >
> > > }
> > >
> > > 2007/6/14, Martijn Dashorst < [EMAIL PROTECTED]>:
> > > > Make it a proper class:
> > > >
> > > > public class LabelWithDefaultText extends Label {
> > > > private String defaultText;
> > > >
> > > > public LabelWithDefaultText(String id, IModel model,
String
defaultText ) {
> > > > super(id, model);
> > > > this.defaultText = defaultText;
> > > > }
> > > > @Override
> > > > protected void onComponentTagBody(MarkupStream markupStream,
> > > > ComponentTag openTag) {
> > > > String model = (String) getModelObject();
> > > > replaceComponentTagBody(markupStream,
openTag, model == null ?
> > > > defaultText : model);
> > > > }
> > > > });
> > > > }
> > > >
> > > > Or make a model wrapper
> > > >
> > > > On 6/14/07, Pieter Cogghe <[EMAIL PROTECTED]> wrote:
> > > > > Hi,
> > > > >
> > > > > I'v got this in a list:
> > > > >
> > > > > add(new Label("name"))
> > > > >
> > > > > sometimes there's no name defined, then I want to set a default
text,
> > > > > I'd do it like this:
> > > > >
> > > > > add(new Label("name").setDefaultModel(new
Model("No name defined")));
> > > > >
> > > > > Now I have it like this:
> > > > >
> > > > > item.add(new Label("name", new
PropertyModel(item.getModel(),"name")){
> > > > > @Override
> > > > > protected void onComponentTagBody(MarkupStream markupStream,
> > > > > ComponentTag openTag) {
> > > > > String model = (String) getModelObject();
> > > > > if (model == null){
> > > > > replaceComponentTagBody(markupStream,
openTag, "no language selected");
> > > > > } else {
> > > > > super.onComponentTagBody(markupStream, openTag)
> > > > > }
> > > > > }
> > > > > });
> > > > >
> > > > > This is really verbose. Anybody knows a better way?
> > > > >
> > > > > thanks a lot,
> > > > >
> > > > > Pieter
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Pieter Cogghe
> > > > > Ganzendries 186
> > > > > 9000 Gent
> > > > > 0487 10 14 21
> > > > >
> > > > >
-------------------------------------------------------------------------
> > > > > This SF.net email is sponsored by DB2 Express
> > > > > Download DB2 Express C - the FREE version of DB2 express and
take
> > > > > control of your XML. No limits. Just data. Click to get it now.
> > > > > http://sourceforge.net/powerbar/db2/
> > > > > _______________________________________________
> > > > > Wicket-user mailing list
> > > > > Wicket-user@lists.sourceforge.net
> > > > >
https://lists.sourceforge.net/lists/listinfo/wicket-user
> > > > >
> > > >
> > > >
> > > > --
> > > > Join the wicket community at irc.freenode.net: ##wicket
> > > > Wicket 1.2.6 contains a very important fix. Download Wicket now!
> > > > http://wicketframework.org
> > > >
> > > >
-------------------------------------------------------------------------
> > > > This SF.net email is sponsored by DB2 Express
> > > > Download DB2 Express C - the FREE version of DB2 express and take
> > > > control of your XML. No limits. Just data. Click to get it now.
> > > > http://sourceforge.net/powerbar/db2/
> > > > _______________________________________________
> > > > Wicket-user mailing list
> > > > Wicket-user@lists.sourceforge.net
> > > >
https://lists.sourceforge.net/lists/listinfo/wicket-user
> > > >
> > >
> > >
> > > --
> > > Pieter Cogghe
> > > Ganzendries 186
> > > 9000 Gent
> > > 0487 10 14 21
> > >
> > >
-------------------------------------------------------------------------
> > > This SF.net email is sponsored by DB2 Express
> > > Download DB2 Express C - the FREE version of DB2 express and take
> > > control of your XML. No limits. Just data. Click to get it now.
> > > http://sourceforge.net/powerbar/db2/
> > > _______________________________________________
> > > Wicket-user mailing list
> > > Wicket-user@lists.sourceforge.net
> > >
https://lists.sourceforge.net/lists/listinfo/wicket-user
> > >
> >
> >
> > --
> > Join the wicket community at irc.freenode.net: ##wicket
> > Wicket 1.2.6 contains a very important fix. Download Wicket now!
> > http://wicketframework.org
> >
> >
-------------------------------------------------------------------------
> > This SF.net email is sponsored by DB2 Express
> > Download DB2 Express C - the FREE version of DB2 express and take
> > control of your XML. No limits. Just data. Click to get it now.
> > http://sourceforge.net/powerbar/db2/
> > _______________________________________________
> > Wicket-user mailing list
> > Wicket-user@lists.sourceforge.net
> >
https://lists.sourceforge.net/lists/listinfo/wicket-user
> >
>
>
> --
> Pieter Cogghe
> Ganzendries 186
> 9000 Gent
> 0487 10 14 21
>
>
-------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Wicket-user mailing list
> Wicket-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/wicket-user
>
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user
--
Pieter Cogghe
Ganzendries 186
9000 Gent
0487 10 14 21
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user