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/


Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to