public void ensureSelectedTabIsVisible(boolean animate) { if (currentAnimation_ != null) { currentAnimation_.cancel(); currentAnimation_ = null; } Element selectedTab = (Element) DomUtils.findNode( getElement(), true, false, new NodePredicate() { public boolean test(Node n) { if (n.getNodeType() != Node.ELEMENT_NODE) return false; return ((Element) n).getClassName().contains("gwt-TabLayoutPanelTab-selected"); } }); if (selectedTab == null) { return; } selectedTab = selectedTab.getFirstChildElement().getFirstChildElement(); Element tabBar = getTabBarElement(); if (!isVisible() || !isAttached() || tabBar.getOffsetWidth() == 0) return; // not yet loaded final Element tabBarParent = tabBar.getParentElement(); final int start = tabBarParent.getScrollLeft(); int end = DomUtils.ensureVisibleHoriz( tabBarParent, selectedTab, padding_, padding_ + rightMargin_, true); // When tabs are closed, the overall width shrinks, and this can lead // to cases where there's too much empty space on the screen Node lastTab = getLastChildElement(tabBar); if (lastTab == null || lastTab.getNodeType() != Node.ELEMENT_NODE) return; int edge = DomUtils.getRelativePosition(tabBarParent, Element.as(lastTab)).x + Element.as(lastTab).getOffsetWidth(); end = Math.min(end, Math.max(0, edge - (tabBarParent.getOffsetWidth() - rightMargin_))); if (edge <= tabBarParent.getOffsetWidth() - rightMargin_) end = 0; if (start != end) { if (!animate) { tabBarParent.setScrollLeft(end); } else { final int finalEnd = end; currentAnimation_ = new Animation() { @Override protected void onUpdate(double progress) { double delta = (finalEnd - start) * progress; tabBarParent.setScrollLeft((int) (start + delta)); } @Override protected void onComplete() { if (this == currentAnimation_) { tabBarParent.setScrollLeft(finalEnd); currentAnimation_ = null; } } }; currentAnimation_.run(Math.max(200, Math.min(1500, Math.abs(end - start) * 2))); } } }
/** Kindly borrowed from PlayN. * */ protected static float getRelativeX(NativeEvent e, Element target) { return e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft(); }