Re: Is it the best way to code a Link depending on a condition
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 LinkRequestT extends LinkT { 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 IModelString 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 (IModelString) ? 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
Re: Is it the best way to code a Link depending on a condition
ok, i understand, thanks, NM On Tue, Sep 22, 2009 at 9:45 PM, Jeremy Thomerson jer...@wickettraining.com wrote: It's the same reason you shouldn't use new Label(id object.getSomeText());. See this example: public MyPage () { Person p = getPersonFromSomewhere(); Label l = new Label(numberOfPhoneNumbers, p.getPhoneNumbers().size()); l.setVisible(p.getPhoneNumbers().size() 0); new Link(addPhoneNumber) { onClick() { p.addPhoneNumber(123-456-7890); } } } When you add the phone number, it would not show up. Not only would your label not be able to get the new value for the size of the phone numbers, but it would also not change it's visibility. Why? Because you statically set both of those in the constructor. They can't change unless you go to a completely new instance of the page. But clicking that link (or performing some ajax operation) won't create a new page - they'll use the one you're already on. Synopsis: You should use models and override isVisible because they always retrieve the latest information for every render. -- Jeremy Thomerson http://www.wickettraining.com On Tue, Sep 22, 2009 at 8:18 AM, Nicolas Melendez nmelen...@getsense.com.ar wrote: Jeremy:you say 2 - don't call setEnabled() - override isEnabled why is better override isEnable then setEnable? thanks NM On Mon, Sep 21, 2009 at 9:44 AM, cmoulliard cmoulli...@gmail.com wrote: Joseph, Can you explain a little bit what you mean by provide it with attribute (IModelString) ? 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 IModelString 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
Re: Is it the best way to code a Link depending on a condition
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 IModelString 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 (IModelString) ? 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 IModelString 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
Re: Is it the best way to code a Link depending on a condition
Jeremy:you say 2 - don't call setEnabled() - override isEnabled why is better override isEnable then setEnable? thanks NM On Mon, Sep 21, 2009 at 9:44 AM, cmoulliard cmoulli...@gmail.com wrote: Joseph, Can you explain a little bit what you mean by provide it with attribute (IModelString) ? 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 IModelString 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
Re: Is it the best way to code a Link depending on a condition
It's the same reason you shouldn't use new Label(id object.getSomeText());. See this example: public MyPage () { Person p = getPersonFromSomewhere(); Label l = new Label(numberOfPhoneNumbers, p.getPhoneNumbers().size()); l.setVisible(p.getPhoneNumbers().size() 0); new Link(addPhoneNumber) { onClick() { p.addPhoneNumber(123-456-7890); } } } When you add the phone number, it would not show up. Not only would your label not be able to get the new value for the size of the phone numbers, but it would also not change it's visibility. Why? Because you statically set both of those in the constructor. They can't change unless you go to a completely new instance of the page. But clicking that link (or performing some ajax operation) won't create a new page - they'll use the one you're already on. Synopsis: You should use models and override isVisible because they always retrieve the latest information for every render. -- Jeremy Thomerson http://www.wickettraining.com On Tue, Sep 22, 2009 at 8:18 AM, Nicolas Melendez nmelen...@getsense.com.ar wrote: Jeremy:you say 2 - don't call setEnabled() - override isEnabled why is better override isEnable then setEnable? thanks NM On Mon, Sep 21, 2009 at 9:44 AM, cmoulliard cmoulli...@gmail.com wrote: Joseph, Can you explain a little bit what you mean by provide it with attribute (IModelString) ? 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 IModelString 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
Re: Is it the best way to code a Link depending on a condition
Joseph, Can you explain a little bit what you mean by provide it with attribute (IModelString) ? 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 IModelString 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
Re: Is it the best way to code a Link depending on a condition
This is because you're not using models correctly. And you apparently are not using page inheritance either. Both of those things will fix this problem that you describe. Also, Wicket makes it *very* easy to create reusable code - in your link example, just make a subclass of link as a concrete class (rather than an anonymous inner class) and reuse it anywhere you want. From your posts here, I would recommend these two things: 1 - learn about models - don't do new Label(id, someStaticText) - use new Label(id, new PropertyModel(request, id)) or some other proper model 2 - don't call setEnabled() - override isEnabled Both are common errors to those who are new to Wicket - and both will come back to bite you later. http://cwiki.apache.org/WICKET/working-with-wicket-models.html -- Jeremy Thomerson http://www.wickettraining.com On Thu, Sep 17, 2009 at 6: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
Re: Is it the best way to code a Link depending on a condition
cmoulliard wrote: (...) 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. Hi I think the wicket way here may be to create a panel containing this label and then reuse the panel. The panel could also have a model in order to get the label content. Would this solve your repetition issue ? ++ joseph - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Is it the best way to code a Link depending on a condition
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 )); } Joseph Pachod wrote: cmoulliard wrote: (...) 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. Hi I think the wicket way here may be to create a panel containing this label and then reuse the panel. The panel could also have a model in order to get the label content. Would this solve your repetition issue ? ++ joseph - 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-tp25488603p25504977.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
Re: Is it the best way to code a Link depending on a condition
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 IModelString 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
Is it the best way to code a Link depending on a condition
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 a href=# wicket:id=linkRequestspan wicket:id=linkRequestTxt//a 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
Re: Is it the best way to code a Link depending on a condition
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 a href=# wicket:id=linkRequestspan wicket:id=linkRequestTxt//a 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 nsmart people - smart software smime.p7s Description: S/MIME Cryptographic Signature
Re: Is it the best way to code a Link depending on a condition
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 nsmart people - smart software - 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-tp25488603p25488926.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
Re: Is it the best way to code a Link depending on a condition
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 nsmart people - smart software -
Re: Is it the best way to code a Link depending on a condition
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)); } };
Re: Is it the best way to code a Link depending on a condition
Hi Yes there'll always be some repetition. Depending on your logic you might also stuff the first block in the onClick event to generate the model if it's not used anywhere else. That could eliminate the first block. The problem about reusable components in wicket is that probably you might end up with some problems when using the same component instance on two pages unless you re-instantiate your Commons class in every call which might in turn produce a lot of overhead if you only need a few of those labels and stuff. What you could also du is writing a kind of factory method in a base page class (I always have a BasePage class all my pages extend from with some common functionality). There you could have a method like protected Label getIdLabel(String value) { return new Label(id, value); } Matt cmoulliard 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 smime.p7s Description: S/MIME Cryptographic Signature