I think that this is a general remark that some users make about Wicket. It
is very difficult to reuse part of the code.

I do not agree... refactore your code, apply some design pattern, do
somethin... In your first case, here is an draft. You can creante an class
of this anonimus sub classes and reuse the code anywere.

    link = new Link("linkRequest") {
        @Override
        public void onClick() {
        setResponsePage(new RequestPage(requestFormModel));
        }
            @Override
            public boolean isVisible() {
        return audit.getRequest() != null;
        }
    };
    link.add(new Label("linkLabel", new AbstractReadOnlyModel() {
        @Override
        public Object getObject() {
        return String.valueOf(audit.getRequest().getId());
        }
    }));

On Thu, Sep 17, 2009 at 8:23 AM, cmoulliard <cmoulli...@gmail.com> wrote:

>
> Mattias,
>
> OK about what you propose but we repeat the test a second time so the code
> still remains very verbose.
> Thanks for the suggestion.
>
> I think that this is a general remark that some users make about Wicket. It
> is very difficult to reuse part of the code.
>
> Here is another example :
>
> I have a label called id which is used in different page. The way proposes
> by Wicket to code it is
>
> Page Request
>
> item.add(new Label("id", String.valueOf(request.getId())));
>
> Page Notification
>
> item.add(new Label("id", String.valueOf(notification.getId())));
>
> When we compare page request and page notification, we see that we repeat
> the same code two times.
>
> It could be interesting to set the value of the label in the page and to
> declare it separately
>
> e.g
>
> public class Commons {
>
>        public static Label labelId = new Label("id");
>        ...
> }
>
>
> Page Request
>
> item.add( labelId.setValue( String.valueOf(request.getId()) );
>
> Page Notification
>
> item.add( labelId.setValue( String.valueOf(notification.getId()) );
>
> Regards,
>
> Charles
>
>
> Matthias Keller wrote:
> >
> > Hi
> >
> > As I said in my previous mail, just initialize the link once using the
> > upper variant.
> > Use an if before to create the requestFormModel, in case 2, the
> > requestFormModel can be null - as the onClick is never executed this
> > will not matter.
> > Have something like:
> >
> > final Model requestFormModel;
> > if (case1)
> >     requestFormModel = new MyModel(...);
> > else
> >     requestFormModel = null;
> >
> > // Creating the one and only Link instance.
> > link = new Link("linkRequest") {
> >     public void onClick() {
> >        setResponsePage(new RequestPage(requestFormModel));
> >     }
> > }
> > item.add(link);
> >
> > if (case1) {
> >     link.add(whatever);
> >     ...
> > } else {
> >     link.add(whatever for case 2);
> >     link.setEnabled(false);
> > }
> >
> >
> > cmoulliard wrote:
> >> You are right. I don't need the setEnabled(false) in the onClick method
> >>
> >> I have changed my code :
> >>
> >>                                      link = new Link("linkRequest") {
> >>
> >>                                              @Override
> >>                                              public void onClick() {
> >>                                                      setResponsePage(new
> RequestPage(requestFormModel));
> >>                                              }
> >>
> >>                                      };
> >>
> >>                                      link.add(new
> >> Label("linkRequestTxt",String.valueOf(audit.getRequest().getId())));
> >>                                      item.add(link);
> >>
> >>                              } else {
> >>                                      link = new Link("linkRequest") {
> >>
> >>                                              @Override
> >>                                              public void onClick() { }
> >>
> >>                                      };
> >>                                      Label label = new
> Label("linkRequestTxt","");
> >>                                      link.add(label);
> >>                                      link.setEnabled(false);
> >>                                      item.add(link);
> >>                              }
> >>
> >> Remark :
> >>
> >> It should be interesting to be able to create an instance of the Link
> >> class
> >> without having to override the onclick() method. Otherwise the code
> >> becomes
> >> very verbose and requires as here that we instantiate two times the
> class
> >> Link
> >>
> >>
> >> Matthias Keller wrote:
> >>
> >>> Sure, just create it (including the onClick which might be invalid),
> but
> >>> afterwards set the  setEnabled()  as needed.
> >>> As long as the link is disabled, it can never be clicked thus the
> >>> onClick() will never be executed anyway, so it's irrelevant if the
> >>> requestFormModel is valid or not...
> >>> I dont think you want the setEnabled(false) inside the onClick - do
> you?
> >>> That would deactivate the link after the first click which doesn't make
> >>> that much sense?
> >>>
> >>> Matt
> >>>
> >>> Charles Moulliard wrote:
> >>>
> >>>> Hi,
> >>>>
> >>>> I would like to know if there is a better way to code this case : What
> >>>> I would like to do is to enable/disable a link depending on a
> >>>> condition. If the condition is true, than we create the link otherwise
> >>>> we disable it. In both case, a label must be defined for the Link.
> >>>>
> >>>> I mean, is it possible to avoid to double the creation of the link =
> >>>> new Link() ....
> >>>>
> >>>> Here is my code
> >>>>
> >>>> 1) HTML
> >>>>
> >>>>             #  wicket:id="linkRequestTxt"/>
> >>>>
> >>>>
> >>>> 2) JAVA
> >>>>
> >>>>                 // Set link for requestId
> >>>>                 Link link;
> >>>>
> >>>>                 if (audit.getRequest() != null) {
> >>>>
> >>>>                     final RequestFormModel requestFormModel = new
> >>>> RequestFormModel();
> >>>>                     requestFormModel.setRequestId( (int)
> >>>> audit.getRequest().getId() );
> >>>>
> >>>>                     link = new Link("linkRequest") {
> >>>>
> >>>>                         @Override
> >>>>                         public void onClick() {
> >>>>                             setResponsePage(new
> >>>> RequestPage(requestFormModel));
> >>>>                         }
> >>>>
> >>>>                     };
> >>>>
> >>>>                     link.add(new
> >>>> Label("linkRequestTxt",String.valueOf(audit.getRequest().getId())));
> >>>>                     item.add(link);
> >>>>
> >>>>                 } else {
> >>>>                     link = new Link("linkRequest") {
> >>>>
> >>>>                         @Override
> >>>>                         public void onClick() {
> >>>>                             this.setEnabled(false);
> >>>>                         }
> >>>>
> >>>>                     };
> >>>>                     Label label = new Label("linkRequestTxt","");
> >>>>                     link.add(label);
> >>>>                     item.add(link);
> >
> >>>>
> >>>>
> >>>> Charles Moulliard
> >>>> Senior Enterprise Architect
> >>>> Apache Camel Committer
> >>>>
> >>>> *****************************
> >>>> blog : http://cmoulliard.blogspot.com
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >>>> For additional commands, e-mail: users-h...@wicket.apache.org
> >>>>
> >>>>
> >>>>
> >>> --
> >>> matthias.kel...@ergon.ch  +41 44 268 83 98
> >>> Ergon Informatik AG, Kleinstrasse 15, CH-8008 Zürich
> >>> http://www.ergon.ch
> >>> ______________________________________________________________
> >>> e r g o n    smart people - smart software
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>
> >>
> >> -----
> >> Charles Moulliard
> >> SOA Architect
> >>
> >> My Blog :  http://cmoulliard.blogspot.com/
> >> http://cmoulliard.blogspot.com/
> >>
> >
> >
> >
> >
> >
>
>
> -----
> Charles Moulliard
> SOA Architect
>
> My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/
> --
> View this message in context:
> http://www.nabble.com/Is-it-the-best-way-to-code-a-Link-depending-on-a-condition-tp25488603p25489428.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to