Thanks anyway Pedro, it's a good insight, and pretty original ;-) Didn't
think of it, nor knew about visitors...

Sadly, I'm on Wicket 1.3.5 and it wouldn't be working anyway, as
MarkupContainer does not seem to provide '
getDefaultModelObjectAsString', apart from what you said about
MessageContainer...

Just out of curiosity, wouldn't it be possible to do that traversal upwards
(from formComponent to Form) ?
And I suppose the <wicket:message> tags get converted somehow to these
MessageContainers (which are tried to localize in the visitor by means of
"_message_" in its id)

Thanks,
Xavier


2009/11/25 Pedro Santos <[email protected]>

> ops, I just see: if you have an form with more than one form componente,
> that behavior will not work. So, it was just an idea :)
>
> On Wed, Nov 25, 2009 at 2:13 PM, Pedro Santos <[email protected]> wrote:
>
> > Possible alternative that will work on current wicket version, since
> > MessageContainer is an private inner class
> >
> > public class AdjustLabel extends AbstractBehavior
> >     {
> >         @Override
> >         public void beforeRender(Component component)
> >         {
> >             if (component instanceof FormComponent)
> >             {
> >                 final FormComponent formComponent =
> > (FormComponent)component;
> >
> component.getParent().visitChildren(MarkupContainer.class,
> >                         new IVisitor<MarkupContainer>()
> >                         {
> >
> >                             public Object component(MarkupContainer
> > component)
> >                             {
> >                                 if
> > (component.getId().startsWith("_message_"))
> >                                 {
> >                                     formComponent.setLabel(new
> > ResourceModel(component
> >
> > .getDefaultModelObjectAsString()));
> >                                     return IVisitor.STOP_TRAVERSAL;
> >                                 }
> >                                 else
> >                                 {
> >                                     return null;
> >                                 }
> >                             }
> >                         });
> >             }
> >         }
> >     }
> >
> > then you set:
> >
> > input.add(new AdjustLabel());
> >
> >
> > On Wed, Nov 25, 2009 at 1:51 PM, Xavier López <[email protected]>
> wrote:
> >
> >> I forgot,
> >>
> >> A possible solution would be using a shared StringResourceModel for both
> >> the
> >> setLabel() method and providing the <label> tag like this:
> >>
> >> <label wicket:id="labelX"><span wicket:id="labelXText"></span></label>
> >>
> >> StringResourceModel m = new StringResourceModel("form.x.label", this,
> >> null);
> >> x.setLabel(m);
> >> FormComponentLabel labelX = new FormComponentLabel("labelX", x);
> >> form.add(labelX);
> >> Label labelXText = new Label("labelXText", m);
> >>
> >> But I'm looking for a less intrusive solution on pages already
> >> developed...
> >>
> >> Thanks,
> >> Xavier
> >>
> >> ---------- Forwarded message ----------
> >> From: Xavier López <[email protected]>
> >> Date: 2009/11/25
> >> Subject: Component.setLabel and <label> tags
> >> To: [email protected]
> >>
> >>
> >> Hi,
> >>
> >> In a form, I have multiple fields each one with its corresponding
> >> FormComponentLabel, with proper <wicket:message> content in the markup
> >> file.
> >> Now, I'm facing the fact that I need to provide the same keys I provided
> >> in
> >> <wicket:message> in a StringResourceModel in component's setLabel()
> >> method.
> >>
> >> Although I'm aware it's not straightforward (setLabel requires a Model,
> >> and
> >> <label>'s body is just body markup), it would be great if there was some
> >> way
> >> to avoid this duplication telling Wicket to use the same key provided in
> >> the
> >> FormComponentLabel's body...
> >>
> >> Does anyone have a hint ?
> >>
> >> Thanks,
> >> Xavier
> >>
> >>
> >>
> >>
> >> --
> >> "Klein bottle for rent--inquire within."
> >>
> >
> >
> >
> > --
> > Pedro Henrique Oliveira dos Santos
> >
>
>
>
> --
> Pedro Henrique Oliveira dos Santos
>



-- 
"Klein bottle for rent--inquire within."

Reply via email to