1 16 package com.google.gwt.user.client.ui; 17 18 import com.google.gwt.user.client.DOM; 19 import com.google.gwt.user.client.Element; 20 import com.google.gwt.user.client.Event; 21 22 25 public class ScrollPanel extends SimplePanel implements SourcesScrollEvents { 26 27 private ScrollListenerCollection scrollListeners; 28 29 32 public ScrollPanel() { 33 setAlwaysShowScrollBars(false); 34 sinkEvents(Event.ONSCROLL); 35 } 36 37 42 public ScrollPanel(Widget child) { 43 this(); 44 setWidget(child); 45 } 46 47 public void addScrollListener(ScrollListener listener) { 48 if (scrollListeners == null) { 49 scrollListeners = new ScrollListenerCollection(); 50 } 51 scrollListeners.add(listener); 52 } 53 54 60 public void ensureVisible(UIObject item) { 61 Element scroll = getElement(); 62 Element element = item.getElement(); 63 ensureVisibleImpl(scroll, element); 64 } 65 66 71 public int getHorizontalScrollPosition() { 72 return DOM.getElementPropertyInt(getElement(), "scrollLeft"); 73 } 74 75 80 public int getScrollPosition() { 81 return DOM.getElementPropertyInt(getElement(), "scrollTop"); 82 } 83 84 public void onBrowserEvent(Event event) { 85 if (DOM.eventGetType(event) == Event.ONSCROLL) { 86 if (scrollListeners != null) { 87 scrollListeners.fireScroll(this, getHorizontalScrollPosition(), 88 getScrollPosition()); 89 } 90 } 91 } 92 93 public void removeScrollListener(ScrollListener listener) { 94 if (scrollListeners != null) { 95 scrollListeners.remove(listener); 96 } 97 } 98 99 105 public void setAlwaysShowScrollBars(boolean alwaysShow) { 106 DOM.setStyleAttribute(getElement(), "overflow", alwaysShow ? "scroll" 107 : "auto"); 108 } 109 110 115 public void setHorizontalScrollPosition(int position) { 116 DOM.setElementPropertyInt(getElement(), "scrollLeft", position); 117 } 118 119 124 public void setScrollPosition(int position) { 125 DOM.setElementPropertyInt(getElement(), "scrollTop", position); 126 } 127 128 private native void ensureVisibleImpl(Element scroll, Element e) ; 141 } 142 | Popular Tags |