In fact, the example createLabel is not really the most interesting. Let's me
explaining what I consider as a good candidate and what I have done

1) A HTML page contains a table which is populated dynamically using a
service collecting data from a DB. the last column of the table is a link
(wicket id="linkRequest") that we would like to show or not depending if we
have receive from the DB, the id (= primary key of the table DB)
corresponding to the record Request to be opened in the screen page
"Request.html / RequestPage)

2) The populateItem method of the DataView allows me for each record to link
the model (= item) returned by the IDataprovider to the columns of my table.
To create the link to the HTML page, I use the Link class that I have
modified like this :

public class LinkRequest<T> extends Link<T> {

        private static final long serialVersionUID = 3283912033862898645L;
        private RequestFormModel requestFormModel;

        public LinkRequest(String id) {
                super(id);
        }
        
        @Override
        public void onClick() {
                setResponsePage(new RequestPage(requestFormModel));             
        }
        
        public RequestFormModel getRequestFormModel() {
                return requestFormModel;
        }

        public void setRequestFormModel(RequestFormModel requestFormModel) {
                this.requestFormModel = requestFormModel;
        }       

}

As you see the onClick() method is defined so I don't need to modify it when
I add the link to my item. To create the link, I call a createLinkRequest
Method who play the role of a factory for me 

        public static LinkRequest createLinkRequest(String id) {
                
                LinkRequest linkReq = new LinkRequest("linkRequest");
                
                if ( id != null ) {
                        
                        // Link is enable with parameters required to open 
RequestPage
                        RequestFormModel requestFormModel = new 
RequestFormModel();
                        requestFormModel.setId( Integer.parseInt( id ) );
                        linkReq.setRequestFormModel(requestFormModel);
                        
                        linkReq.add(getLinkRequestTxt( id ));

                } else {
                        // Link is disable
                        linkReq.add(getLinkRequestTxt(""));
                        linkReq.setEnabled(false);

                }
                
                return linkReq;
                
        }

and I call this method from my polulateItem like this 

item.add( createLinkRequest( id which is equal to null or to a value ) );

Depending if the value is null or not, the link will be enable or disabled
and the model required by my page request created accordingly.

Is it a correct implementation or a stupid one ? In a previous reply,
someone argues that we must override the method isEnabled() instead of using
setEnable() ?

Regards,

Charles


josephpachod wrote:
> 
> hi Charles
> 
> The whole issue is that you don't know how the data (in this case a
> String) is to be retrieved. Can it be read only once ? Should it be
> refresh on each request cycle ? On each access to the data ? Does it come
> from a database ?
> 
> Wicket's model allows you to go away from all these considerations : you
> just want to be able to get the string. Just let the users of your wicket
> component decide how they want to provide the data.
> 
> As such, your example is, I think, broken :
>>      private Label labelTitle;
>>      public static Label createLabelTitle(String title) {
>>              return new Label(title,new PropertyModel( ModelClass, title ));
>>      }
> 
> What if someone wants to change this string ? title = "my new title" won't
> work there !
> 
> Thus, it should be, IMO :
>>      public static Label createLabelTitle(final IModel<String> titleModel) {
>>              return new Label(title,titleModel.get());
>>      }
> 
> ++
> 
> NB : you might be interested by this article
> http://blog.jteam.nl/2009/09/16/wicket-dos-and-donts/
> 
>>
>> Joseph,
>>
>> Can you explain a little bit what you mean by provide it with attribute
>> (IModel<String>) ?
>>
>>      private Label labelTitle;
>>      public static Label createLabelTitle(String title) {
>>              return new Label("title",new Model( title ));
>>      }
>>
>> --> becomes
>>
>>      private Label labelTitle;
>>      public static Label createLabelTitle(String title) {
>>              return new Label(title,new PropertyModel( ModelClass, title ));
>>      }
>>
>> Is it right what I create ?
>>
>>
>> Joseph Pachod wrote:
>>>
>>> cmoulliard wrote:
>>>> What I have done to avoid to repeat the creation of the labels is to
>>>> define
>>>> and use static method
>>>>
>>>>    private Label labelTitle;
>>>>    public static Label getLabelTitle(String title) {
>>>>            return new Label("title",new Model( title ));
>>>>    }
>>>>
>>> I personally would name this method createLabelTitle(String title) or
>>> getNewLabelTitle(String title), for explicitness.
>>>
>>> Furthermore, I would directly provide it with a "final IModel<String>
>>> title" attribute, not to dictate how the data has to be provided
>>> (dynamic or not for example).
>>>
>>> In the end, this method is fine for just a label, but for anything more
>>> complex a panel would be the way to go I would say. The main exception
>>> here I see right now is the case of pages.
>>>
>>> For example, if we're speaking of a page title, then I would define it
>>> in my base page and make an abstract String getTitle() method in the
>>> base page so I'm sure everyone set it up later on. I would do the same
>>> if it's a specific kind of structured page, for example an abstract
>>> class ContentHoldingPage extend TheBasePage.
>>>
>>> ++
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
>>> For additional commands, e-mail: users-h...@wicket.apache.org
>>>
>>>
>>>
>>
>>
>> -----
>> 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-tp25488603p25530206.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
>>
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
> 
> 
> 


-----
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-tp25488603p25609212.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