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: * Contributers:
017: * Darell Meyer <darrell@mygwt.net> - Derived implementation
018: */
019: package net.mygwt.ui.client.widget;
020:
021: import net.mygwt.ui.client.Events;
022: import net.mygwt.ui.client.MyDOM;
023: import net.mygwt.ui.client.Style;
024: import net.mygwt.ui.client.event.ScrollListener;
025: import net.mygwt.ui.client.event.TypedListener;
026:
027: import com.google.gwt.user.client.DOM;
028: import com.google.gwt.user.client.Element;
029: import com.google.gwt.user.client.Event;
030: import com.google.gwt.user.client.ui.Widget;
031:
032: /**
033: * A <code>Container</code> that scrolls its contents. The container is
034: * created with scrolling disabled.
035: */
036: public class ScrollContainer extends Container {
037:
038: private boolean scroll, alwaysScroll;
039: private int scrollLeft = Style.DEFAULT;
040: private int scrollTop = Style.DEFAULT;
041:
042: /**
043: * Adds a listener to receive scroll events.
044: *
045: * @param listener the listener to be added
046: */
047: public void addScrollListener(ScrollListener listener) {
048: addListener(Events.Scroll, new TypedListener(listener));
049: }
050:
051: /**
052: * Ensures the child widget is visible.
053: *
054: * @param child the child widget
055: */
056: public void ensureVisible(Widget child) {
057: MyDOM.scrollIntoView(child.getElement(), getLayoutTarget(),
058: true);
059: }
060:
061: /**
062: * Returns the horizontal scroll position.
063: *
064: * @return the horizontal scroll position, in pixels
065: */
066: public int getHScrollPosition() {
067: return rendered ? MyDOM.getScrollLeft(getElement()) : 0;
068: }
069:
070: /**
071: * Returns the container's scroll state.
072: *
073: * @return the scroll state
074: */
075: public boolean getScrollEnabled() {
076: return scroll;
077: }
078:
079: /**
080: * Returns the vertical scroll position.
081: *
082: * @return the horizontal scroll position, in pixels
083: */
084: public int getVScrollPosition() {
085: return rendered ? MyDOM.getScrollTop(getElement()) : 0;
086: }
087:
088: /**
089: * Removes a previously added listener.
090: *
091: * @param listener the listener to be removed
092: */
093: public void removeScrollListener(ScrollListener listener) {
094: unhook(Events.Scroll, listener);
095: }
096:
097: /**
098: * Sets whether this panel always shows its scroll bars, or only when
099: * necessary.
100: *
101: * @param alwaysShow <code>true</code> to show scroll bars at all times
102: */
103: public void setAlwaysShowScrollBars(boolean alwaysShow) {
104: if (rendered) {
105: Element ct = getLayoutTarget();
106: DOM.setStyleAttribute(ct, "overflow", alwaysShow ? "scroll"
107: : "auto");
108: }
109: }
110:
111: /**
112: * Sets the horizontal scroll position.
113: *
114: * @param position the new horizontal scroll position, in pixels
115: */
116: public void setHScrollPosition(int position) {
117: scrollLeft = position;
118: if (rendered) {
119: MyDOM.setScrollLeft(getLayoutTarget(), position);
120: }
121: }
122:
123: /**
124: * Enables scrolling if <code>true</code>, otherwise disables.
125: *
126: * @param scroll the scroll enabled state
127: */
128: public void setScrollEnabled(boolean scroll) {
129: this .scroll = scroll;
130: if (rendered) {
131: Element ct = getLayoutTarget();
132: String style = scroll ? "auto" : "hidden";
133: DOM.setStyleAttribute(ct, "overflow", style);
134: }
135: }
136:
137: protected Element getLayoutTarget() {
138: return getElement();
139: }
140:
141: /**
142: * Sets the vertical scroll position.
143: *
144: * @param position the new horizontal scroll position, in pixels
145: */
146: public void setVScrollPosition(int position) {
147: scrollTop = position;
148: if (rendered) {
149: MyDOM.setScrollTop(getLayoutTarget(), position);
150: }
151: }
152:
153: protected void afterRender() {
154: super.afterRender();
155: setScrollEnabled(scroll);
156: if (scrollLeft != Style.DEFAULT) {
157: setHScrollPosition(scrollLeft);
158: }
159: if (scrollTop != Style.DEFAULT) {
160: setVScrollPosition(scrollLeft);
161: }
162: if (alwaysScroll) {
163: setAlwaysShowScrollBars(alwaysScroll);
164: }
165: MyDOM.addEventsSunk(getLayoutTarget(), Event.ONSCROLL);
166: }
167:
168: }
|