001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020: import com.google.gwt.user.client.Event;
021:
022: /**
023: * A simple panel that wraps its contents in a scrollable area.
024: */
025: public class ScrollPanel extends SimplePanel implements
026: SourcesScrollEvents {
027:
028: private ScrollListenerCollection scrollListeners;
029:
030: /**
031: * Creates an empty scroll panel.
032: */
033: public ScrollPanel() {
034: setAlwaysShowScrollBars(false);
035: sinkEvents(Event.ONSCROLL);
036:
037: // Prevent IE standard mode bug when a AbsolutePanel is contained.
038: DOM.setStyleAttribute(getElement(), "position", "relative");
039: }
040:
041: /**
042: * Creates a new scroll panel with the given child widget.
043: *
044: * @param child the widget to be wrapped by the scroll panel
045: */
046: public ScrollPanel(Widget child) {
047: this ();
048: setWidget(child);
049: }
050:
051: public void addScrollListener(ScrollListener listener) {
052: if (scrollListeners == null) {
053: scrollListeners = new ScrollListenerCollection();
054: }
055: scrollListeners.add(listener);
056: }
057:
058: /**
059: * Ensures that the specified item is visible, by adjusting the panel's scroll
060: * position.
061: *
062: * @param item the item whose visibility is to be ensured
063: */
064: public void ensureVisible(UIObject item) {
065: Element scroll = getElement();
066: Element element = item.getElement();
067: ensureVisibleImpl(scroll, element);
068: }
069:
070: /**
071: * Gets the horizontal scroll position.
072: *
073: * @return the horizontal scroll position, in pixels
074: */
075: public int getHorizontalScrollPosition() {
076: return DOM.getElementPropertyInt(getElement(), "scrollLeft");
077: }
078:
079: /**
080: * Gets the vertical scroll position.
081: *
082: * @return the vertical scroll position, in pixels
083: */
084: public int getScrollPosition() {
085: return DOM.getElementPropertyInt(getElement(), "scrollTop");
086: }
087:
088: @Override
089: public void onBrowserEvent(Event event) {
090: if (DOM.eventGetType(event) == Event.ONSCROLL) {
091: if (scrollListeners != null) {
092: scrollListeners.fireScroll(this ,
093: getHorizontalScrollPosition(),
094: getScrollPosition());
095: }
096: }
097: }
098:
099: public void removeScrollListener(ScrollListener listener) {
100: if (scrollListeners != null) {
101: scrollListeners.remove(listener);
102: }
103: }
104:
105: /**
106: * Sets whether this panel always shows its scroll bars, or only when
107: * necessary.
108: *
109: * @param alwaysShow <code>true</code> to show scroll bars at all times
110: */
111: public void setAlwaysShowScrollBars(boolean alwaysShow) {
112: DOM.setStyleAttribute(getElement(), "overflow",
113: alwaysShow ? "scroll" : "auto");
114: }
115:
116: /**
117: * Sets the object's height. This height does not include decorations such as
118: * border, margin, and padding.
119: *
120: * @param height the object's new height, in absolute CSS units (e.g. "10px",
121: * "1em" but not "50%")
122: */
123: @Override
124: public void setHeight(String height) {
125: super .setHeight(height);
126: }
127:
128: /**
129: * Sets the horizontal scroll position.
130: *
131: * @param position the new horizontal scroll position, in pixels
132: */
133: public void setHorizontalScrollPosition(int position) {
134: DOM.setElementPropertyInt(getElement(), "scrollLeft", position);
135: }
136:
137: /**
138: * Sets the vertical scroll position.
139: *
140: * @param position the new vertical scroll position, in pixels
141: */
142: public void setScrollPosition(int position) {
143: DOM.setElementPropertyInt(getElement(), "scrollTop", position);
144: }
145:
146: /**
147: * Sets the object's size. This size does not include decorations such as
148: * border, margin, and padding.
149: *
150: * @param width the object's new width, in absolute CSS units (e.g. "10px",
151: * "1em", but not "50%")
152: * @param height the object's new height, in absolute CSS units (e.g. "10px",
153: * "1em", but not "50%")
154: */
155: @Override
156: public void setSize(String width, String height) {
157: super .setSize(width, height);
158: }
159:
160: /**
161: * Sets the object's width. This width does not include decorations such as
162: * border, margin, and padding.
163: *
164: * @param width the object's new width, in absolute CSS units (e.g. "10px",
165: * "1em", but not "50%")
166: */
167: @Override
168: public void setWidth(String width) {
169: super .setWidth(width);
170: }
171:
172: private native void ensureVisibleImpl(Element scroll, Element e) /*-{
173: if (!e)
174: return;
175:
176: var item = e;
177: var realOffset = 0;
178: while (item && (item != scroll)) {
179: realOffset += item.offsetTop;
180: item = item.offsetParent;
181: }
182:
183: scroll.scrollTop = realOffset - scroll.offsetHeight / 2;
184: }-*/;
185:
186: }
|