Thursday, November 29th, 2007

Rolling your own GWT Hyperlink class

Category: GWT, JavaScript

From reading his post, Ivan Moscoso definitely has too much time on his hands as he decided to see how easy it was to set the status bar text when hovering over a link in GWT:

It’s Wednesday, and you have had it up to here with the weather. There’s nothing on TV and you’ve gone out for so many walks, you can’t even get your dog to look you in the eye without him hiding under the kitchen table. You have time on your hand, so you decide you want to see how easy it is to set the status bar text when hovering over a link in GWT (why? don’t ask why, let’s just pretend for a second).

So he tried three different solutions including extending GWT’s HyperLink class and wrapping the Hyperlink class within a GWT FocusPanel. Those two really didn’t pan out as he had hoped:

The problem with this option is that it doesn’t exactly work as intended. ‘setStatusText()’ gets invoked at the right time, but if you look at the status bar in Firefox, the status bar text doesn’t update. I’m not sure why exactly- it might just be my quick & dirty JavaScript, but even if everything worked as intended I’m still not happy with this solution. I mean, look at the code– I have to pull in many ingredients just to complete a simple task– DOM.eventGetType()? Writing a JSNI method definition? Registering the right Event type, overriding default behavior, etc.? Not the most readable code and not the kind of code I would lightly hand off to someone just getting their feet wet with GWT.

Finally, Ivan decided to roll his own Hyperlink class:

class MyHyperlink extends HTML {
  public MyHyperlink(String name) {
        setHTML(
          "<a onmouseover=\"status='';return true;\" href=\"#x\">" + name + "</a>"
        );

        addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                // Whatever you would have done in Hyperlink's clickListener...
                foo.submitSomething();
            }
        });
    }
}

You can check out all three solutions via Ivan’s post and see why he decided to build his own GWT class.

Posted by Rey Bango at 9:33 am
Comment here

+++--
3.3 rating from 29 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.