Just found out a component that does what I proposed below more cleanly:
SimpleFormComponentLabel.
I'll finally be using this component, and seize the opportunity to insert
automatically the 'required' textflag (*):

public class MyLabel extends SimpleFormComponentLabel {

String mandatory = "";

public MyLabel(String id, final LabeledWebMarkupContainer labelProvider) {
        super(id, labelProvider);
        boolean required = false;
        if (labelProvider instanceof FormComponent){
            required = ((FormComponent)labelProvider).isRequired();
        }
        if (required){
            mandatory = " " + new
StringResourceModel(MyHelper.KEY_MANDATORY_FIELD,labelProvider,
null).getString();
        }
    }
    protected void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag)
    {
        replaceComponentTagBody(markupStream, openTag,
getModelObjectAsString() + mandatory);
    }
}


2009/11/26 Martijn Dashorst <martijn.dasho...@gmail.com>

> s/getDefaultModelObjectAsString/getModelObjectAsString/...
>
> On Wed, Nov 25, 2009 at 5:29 PM, Xavier López <xavil...@gmail.com> wrote:
> > 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 <pedros...@gmail.com>
> >
> >> 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 <pedros...@gmail.com>
> 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 <xavil...@gmail.com>
> >> 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 <xavil...@gmail.com>
> >> >> Date: 2009/11/25
> >> >> Subject: Component.setLabel and <label> tags
> >> >> To: users@wicket.apache.org
> >> >>
> >> >>
> >> >> 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."
> >
>
>
>
> --
> Become a Wicket expert, learn from the best: http://wicketinaction.com
> Apache Wicket 1.4 increases type safety for web applications
> Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.4.0
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>


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

Reply via email to