I'm trying to have an handler on Text Area(gxt) to get to know when user reach the top of text area.
TextArea logTextArea = new TextArea();
logTextArea.setReadOnly(true);
logTextArea.addDomHandler(new ScrollHandler() {
@Override
public void onScroll(ScrollEvent event) {
InputElement textAreaElement = logTextArea.getCell().getInputElement(logTextArea.getElement());
int scrollTop = textAreaElement.getScrollTop();
}
}, ScrollEvent.getType());
VerticalLayoutContainer dataContainer = new VerticalLayoutContainer();
HorizontalLayoutContainer secondRow = new HorizontalLayoutContainer();
secondRow.add(logTextArea, new HorizontalLayoutData(1, 1, new Margins(5, 10, 5, 10)));
dataContainer.add(secondRow, new VerticalLayoutData(1, 0.5));
add(dataContainer);//this class extends ContentPanel
This handler is never called on scroll, but I also tried with a lot of other events, like mouseover, mousewhell, mouseclick ... and all of these events worked. Can somebody help with any idea?
AFAIK, scroll event will not work in cell based widgets out of the box if the event's target and the widget are not the same elements. And GXT's TextArea is a widget that has such DOM structure.
That's all because scroll event is a "non bubbling" event.
AFAIK, GWT widgets, that uses cells, have a special handling for non bubbling events to be dispatched through GWT events system.
And the list of types of supporting non bubbling events are too short and limited to focus
, blur
, load
and error
events.
See CellBasedWidgetImplStandard
class for details.
First solution, that I may suggest, is to explicitly assign onscroll handler for textarea. For example:
Event.sinkEvents(textAreaElement, Event.getEventsSunk(textAreaElement) | Event.ONSCROLL);
here textAreaElement
- is the textarea DOM element.
But this should be done every time when the cell rerenders its content (e.g. when setValue method called for TextArea widget).
Other solution is a bit hacky and uses private api implementation, but could be applied only once to cell based widget. You can do the same things as done in CellBasedWidgetImplStandard.sinkEvent(Widget, String)
e.g. :
WidgetHelper.sinkEvent(logTextArea.getElement(), BrowserEvents.SCROLL);
where WidgetHelper
may looks like this:
public class WidgetHelper {
public static void sincEvent(Element element, String typeName){
element.setAttribute("__gwtCellBasedWidgetImplDispatching" + typeName, "true");
sinkEvent0(element, typeName);
}
private static native void sinkEvent0(Element element, String typeName) /*-{
element.addEventListener(typeName,
@com.google.gwt.user.cellview.client.CellBasedWidgetImplStandard::dispatchNonBubblingEvent, true);
}-*/;
}
Probably, this is a subject to create an issue in GWT project.