Here is my use case. A search returns a list of Document objects. A GWT CellTable displays the list. One of the table columns shows an icon based on document type, i.e., pdf, word etc. When a user double clicks the icon, it retrieves the content of the document.
GWT ImageCell does the icon part nicely. However, there is no built-in behavior to support clicking. To do that, I had to create a subclass of ImageCell and override a couple of methods. The credit goes to a post on stackoverflow.
The code below is the solution. One thing to note is that the Context object provides access to the actual object (Document, in my case) associated with the column. Here I am merely creating an alert with the document name. There are certainly a lot more exciting stuff you can do with it.
Column<Document, String> iconColumn = new Column<Document, String>(
new ClickableIconCell()) {
@Override
public String getValue(Document object) {
return "images/pdf-16.png";
}
};
private class ClickableIconCell extends ImageCell {
@Override
public Set<String> getConsumedEvents() {
Set<String> consumedEvents = new HashSet<String>();
consumedEvents.add("dblclick");
return consumedEvents;
}
@Override
public void onBrowserEvent(Context context, Element parent,
String value, NativeEvent event,
ValueUpdater<String> valueUpdater) {
switch (DOM.eventGetType((Event) event)) {
case Event.ONDBLCLICK:
Document doc = (Document) context.getKey();
Window.alert(doc.name);
break;
default:
break;
}
}
}
No comments:
Post a Comment