private Element createScrollableElement(BaseResources.Css baseCss) {
final DivElement scrollableElement = Elements.createDivElement(css.scrollable());
scrollableElement.addClassName(baseCss.documentScrollable());
scrollableElement.addEventListener(Event.SCROLL, new EventListener() {
@Override
public void handleEvent(Event evt) {
setScrollTop(scrollableElement.getScrollTop(), false);
}
}, false);
scrollableElement.addEventListener(Event.CONTEXTMENU, new EventListener() {
@Override
public void handleEvent(Event evt) {
/*
* TODO: eventually have our context menu, but for now
* disallow browser's since it's confusing that it does not have copy
* nor paste options
*/
evt.stopPropagation();
evt.preventDefault();
}
}, false);
// TODO: Detach listener in appropriate moment.
MouseGestureListener.createAndAttach(scrollableElement, new MouseGestureListener.Callback() {
@Override
public boolean onClick(int clickCount, MouseEvent event) {
// The buffer area does not include the scrollable's padding
int bufferClientLeft = css.scrollableLeftPadding();
int bufferClientTop = 0;
for (Element element = scrollableElement; element.getOffsetParent() != null;
element = element.getOffsetParent()) {
bufferClientLeft += element.getOffsetLeft();
bufferClientTop += element.getOffsetTop();
}
/*
* This onClick method will get called for horizontal scrollbar interactions. We want to
* exit early for those. It will not get called for vertical scrollbar interactions since
* that is a separate element outside of the scrollable element.
*/
if (scrollableElement == event.getTarget()) {
// Test if the mouse event is on the horizontal scrollbar.
int relativeY = event.getClientY() - bufferClientTop;
if (relativeY > scrollableElement.getClientHeight()) {
// Prevent editor losing focus
event.preventDefault();
return false;
}
}
getDelegate().onMouseClick(clickCount,
event.getClientX(),
event.getClientY(),
bufferClientLeft,
bufferClientTop,
event.isShiftKey());
return true;
}
@Override
public void onDragRelease(MouseEvent event) {
getDelegate().onMouseDragRelease(event.getClientX(), event.getClientY());
}
@Override
public void onDrag(MouseEvent event) {
getDelegate().onMouseDrag(event.getClientX(), event.getClientY());
}
});
/*
* Don't allow tabbing to this -- the input element will be tabbable
* instead
*/
scrollableElement.setTabIndex(-1);
Browser.getWindow().addEventListener(Event.RESIZE, new EventListener() {
@Override
public void handleEvent(Event evt) {
// TODO: also listen for the navigation slider
// this event is being caught multiple times, and sometimes the
// calculated values are all zero. So only respond if we have positive