Advertisement
Guest User

GWT ScrolledTabLayoutPanel

a guest
Jan 25th, 2012
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. package whatever.you.want;
  2.  
  3. import com.google.gwt.core.client.Scheduler;
  4. import com.google.gwt.dom.client.Style.Unit;
  5. import com.google.gwt.event.dom.client.ClickEvent;
  6. import com.google.gwt.event.dom.client.ClickHandler;
  7. import com.google.gwt.event.logical.shared.ResizeEvent;
  8. import com.google.gwt.event.logical.shared.ResizeHandler;
  9. import com.google.gwt.event.shared.HandlerRegistration;
  10. import com.google.gwt.resources.client.ImageResource;
  11. import com.google.gwt.user.client.Window;
  12. import com.google.gwt.user.client.ui.FlowPanel;
  13. import com.google.gwt.user.client.ui.Image;
  14. import com.google.gwt.user.client.ui.LayoutPanel;
  15. import com.google.gwt.user.client.ui.TabLayoutPanel;
  16. import com.google.gwt.user.client.ui.Widget;
  17.  
  18. /**
  19.  * A {@link TabLayoutPanel} that shows scroll buttons if necessary
  20.  */
  21. public class ScrolledTabLayoutPanel extends TabLayoutPanel {
  22.  
  23.         private static final int IMAGE_PADDING_PIXELS = 4;
  24.  
  25.         private LayoutPanel panel;
  26.         private FlowPanel tabBar;
  27.         private Image scrollLeftButton;
  28.         private Image scrollRightButton;
  29.         private HandlerRegistration windowResizeHandler;
  30.  
  31.         private ImageResource leftArrowImage;
  32.         private ImageResource rightArrowImage;
  33.  
  34.         public ScrolledTabLayoutPanel(double barHeight, Unit barUnit,
  35.             ImageResource leftArrowImage, ImageResource rightArrowImage) {
  36.                 super(barHeight, barUnit);
  37.  
  38.                 this.leftArrowImage = leftArrowImage;
  39.                 this.rightArrowImage = rightArrowImage;
  40.  
  41.                 // The main widget wrapped by this composite, which is a LayoutPanel with the tab bar & the tab content
  42.                 panel = (LayoutPanel) getWidget();
  43.  
  44.                 // Find the tab bar, which is the first flow panel in the LayoutPanel
  45.                 for (int i = 0; i < panel.getWidgetCount(); ++i) {
  46.                         Widget widget = panel.getWidget(i);
  47.                         if (widget instanceof FlowPanel) {
  48.                                 tabBar = (FlowPanel) widget;
  49.                                 break; // tab bar found
  50.                         }
  51.                 }
  52.  
  53.                 initScrollButtons();
  54.         }
  55.  
  56.         @Override
  57.         public void add(Widget child, Widget tab) {
  58.                 super.add(child, tab);
  59.                 checkIfScrollButtonsNecessary();
  60.         }
  61.  
  62.         @Override
  63.         public boolean remove(Widget w) {
  64.                 boolean b = super.remove(w);
  65.                 checkIfScrollButtonsNecessary();
  66.                 return b;
  67.         }
  68.  
  69.         @Override
  70.         protected void onLoad() {
  71.                 super.onLoad();
  72.  
  73.                 if (windowResizeHandler == null) {
  74.                         windowResizeHandler = Window.addResizeHandler(new ResizeHandler() {
  75.                                 @Override
  76.                                 public void onResize(ResizeEvent event) {
  77.                                         checkIfScrollButtonsNecessary();
  78.                                 }
  79.                         });
  80.                 }
  81.         }
  82.  
  83.         @Override
  84.         protected void onUnload() {
  85.                 super.onUnload();
  86.  
  87.                 if (windowResizeHandler != null) {
  88.                         windowResizeHandler.removeHandler();
  89.                         windowResizeHandler = null;
  90.                 }
  91.         }
  92.  
  93.         private ClickHandler createScrollClickHandler(final int diff) {
  94.                 return new ClickHandler() {
  95.                         @Override
  96.                         public void onClick(ClickEvent event) {
  97.                                 Widget lastTab = getLastTab();
  98.                                 if (lastTab == null)
  99.                                         return;
  100.  
  101.                                 int newLeft = parsePosition(tabBar.getElement().getStyle().getLeft()) + diff;
  102.                                 int rightOfLastTab = getRightOfWidget(lastTab);
  103.  
  104.                                 // Prevent scrolling the last tab too far away form the right border,
  105.                                 // or the first tab further than the left border position
  106.                                 if (newLeft <= 0 && (getTabBarWidth() - newLeft < (rightOfLastTab + 20))) {
  107.                                         scrollTo(newLeft);
  108.                                 }
  109.                         }
  110.                 };
  111.         }
  112.  
  113.         /** Create and attach the scroll button images with a click handler */
  114.         private void initScrollButtons() {
  115.                 scrollLeftButton = new Image(leftArrowImage);
  116.                 int leftImageWidth = scrollLeftButton.getWidth();
  117.                 panel.insert(scrollLeftButton, 0);
  118.                 panel.setWidgetLeftWidth(scrollLeftButton, 0, Unit.PX, leftImageWidth, Unit.PX);
  119.                 panel.setWidgetTopHeight(scrollLeftButton, 0, Unit.PX, scrollLeftButton.getWidth(), Unit.PX);
  120.                 scrollLeftButton.addClickHandler(createScrollClickHandler(+20));
  121.                 scrollLeftButton.setVisible(false);
  122.  
  123.                 scrollRightButton = new Image(rightArrowImage);
  124.                 panel.insert(scrollRightButton, 0);
  125.                 panel.setWidgetLeftWidth(scrollRightButton, leftImageWidth + IMAGE_PADDING_PIXELS, Unit.PX, scrollRightButton.getWidth(), Unit.PX);
  126.                 panel.setWidgetTopHeight(scrollRightButton, 0, Unit.PX, scrollRightButton.getHeight(), Unit.PX);
  127.  
  128.                 scrollRightButton.addClickHandler(createScrollClickHandler(-20));
  129.                 scrollRightButton.setVisible(false);
  130.         }
  131.  
  132.         private void checkIfScrollButtonsNecessary() {
  133.                 // Defer size calculations until sizes are available, when calculating immediately after
  134.                 // add(), all size methods return zero
  135.             Scheduler.get().scheduleDeferred( new Scheduler.ScheduledCommand() {
  136.  
  137.                 @Override
  138.                 public void execute() {
  139.                     boolean isScrolling = isScrollingNecessary();
  140.                     // When the scroll buttons are being hidden, reset the scroll position to zero to
  141.                     // make sure no tabs are still out of sight
  142.                     if (scrollRightButton.isVisible() && !isScrolling) {
  143.                             resetScrollPosition();
  144.                     }
  145.                     scrollRightButton.setVisible(isScrolling);
  146.                     scrollLeftButton.setVisible(isScrolling);
  147.                 }
  148.                
  149.             }
  150.             );
  151.  
  152.         }
  153.  
  154.         private void resetScrollPosition() {
  155.                 scrollTo(0);
  156.         }
  157.  
  158.         private void scrollTo(int pos) {
  159.                 tabBar.getElement().getStyle().setLeft(pos, Unit.PX);
  160.         }
  161.  
  162.         private boolean isScrollingNecessary() {
  163.                 Widget lastTab = getLastTab();
  164.                 if (lastTab == null)
  165.                         return false;
  166.  
  167.                 return getRightOfWidget(lastTab) > getTabBarWidth();
  168.         }
  169.  
  170.         private int getRightOfWidget(Widget widget) {
  171.                 return widget.getElement().getOffsetLeft() + widget.getElement().getOffsetWidth();
  172.         }
  173.  
  174.         private int getTabBarWidth() {
  175.                 return tabBar.getElement().getParentElement().getClientWidth();
  176.         }
  177.  
  178.         private Widget getLastTab() {
  179.                 if (tabBar.getWidgetCount() == 0)
  180.                         return null;
  181.  
  182.                 return tabBar.getWidget(tabBar.getWidgetCount() - 1);
  183.         }
  184.  
  185.         private static int parsePosition(String positionString) {
  186.                 int position;
  187.                 try {
  188.                         for (int i = 0; i < positionString.length(); i++) {
  189.                                 char c = positionString.charAt(i);
  190.                                 if (c != '-' && !(c >= '0' && c <= '9')) {
  191.                                         positionString = positionString.substring(0, i);
  192.                                 }
  193.                         }
  194.  
  195.                         position = Integer.parseInt(positionString);
  196.                 } catch (NumberFormatException ex) {
  197.                         position = 0;
  198.                 }
  199.                 return position;
  200.         }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement