To get a hyperlink text cell, you need to have two things, 1) something that renders the text as a hyperlink (or in our case, something that LOOKS like a hyperlink but isn't really because we're in a web application there's no where to navigate in the traditional hyperlink sense), and 2) either a callback mechanism or a SelectHandler.
We went with the second because it allows for more than one handler to be added if need be. A good rule of thumb is that if it's a one off and will only be used once, use a callback but if it's a completely reusable widget, use a handler.
Note that our design such that ONLY the hyperlink has the pointer cursor and NOT the entire cell and we only care about the three events listed in the constructor, if you need more, you'll need to add them and add additional code in onBrowserEvent(...). Additionally, onBrowserEvent(...) could potentially be called a lot so you do not want any heavy logic in there.
Here's what we're doing:
Code:
public class AnchorTemplates
{
public static final TextTemplates TEMPLATE = GWT.create(TextTemplates.class);
public static interface TextTemplates extends XTemplates
{
@XTemplate("<a class='anchor' style='cursor: pointer;'>{anchorText}</a>")
public SafeHtml anchor(String anchorText);
}
}
public class ClickableAnchorCell extends ResizeCell<String> implements HasSelectHandlers
{
public ClickableAnchorCell()
{
super("click", "mouseover", "mouseout");
}
@Override
public HandlerRegistration addSelectHandler(SelectEvent.SelectHandler handler)
{
return addHandler(handler, SelectEvent.getType());
}
@Override
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
ValueUpdater<String> valueUpdater)
{
if (!isDisableEvents())
{
String eventType = event.getType();
if ("click".equals(eventType))
{
XElement target = event.getEventTarget().cast();
if (target.hasClassName("anchor"))
{
onClick(context);
}
}
}
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb)
{
if (value != null)
{
sb.append(AnchorTemplates.TEMPLATE.anchor(value));
}
}
protected void onClick(Context context)
{
if (!isDisableEvents() && fireCancellableEvent(context, new BeforeSelectEvent(context)))
{
fireEvent(context, new SelectEvent(context));
}
}
}