KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > ScrollPanel


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 /**
23  * A simple panel that wraps its contents in a scrollable area.
24  */

25 public class ScrollPanel extends SimplePanel implements SourcesScrollEvents {
26
27   private ScrollListenerCollection scrollListeners;
28
29   /**
30    * Creates an empty scroll panel.
31    */

32   public ScrollPanel() {
33     setAlwaysShowScrollBars(false);
34     sinkEvents(Event.ONSCROLL);
35   }
36
37   /**
38    * Creates a new scroll panel with the given child widget.
39    *
40    * @param child the widget to be wrapped by the scroll panel
41    */

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   /**
55    * Ensures that the specified item is visible, by adjusting the panel's scroll
56    * position.
57    *
58    * @param item the item whose visibility is to be ensured
59    */

60   public void ensureVisible(UIObject item) {
61     Element scroll = getElement();
62     Element element = item.getElement();
63     ensureVisibleImpl(scroll, element);
64   }
65
66   /**
67    * Gets the horizontal scroll position.
68    *
69    * @return the horizontal scroll position, in pixels
70    */

71   public int getHorizontalScrollPosition() {
72     return DOM.getElementPropertyInt(getElement(), "scrollLeft");
73   }
74
75   /**
76    * Gets the vertical scroll position.
77    *
78    * @return the vertical scroll position, in pixels
79    */

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   /**
100    * Sets whether this panel always shows its scroll bars, or only when
101    * necessary.
102    *
103    * @param alwaysShow <code>true</code> to show scroll bars at all times
104    */

105   public void setAlwaysShowScrollBars(boolean alwaysShow) {
106     DOM.setStyleAttribute(getElement(), "overflow", alwaysShow ? "scroll"
107         : "auto");
108   }
109
110   /**
111    * Sets the horizontal scroll position.
112    *
113    * @param position the new horizontal scroll position, in pixels
114    */

115   public void setHorizontalScrollPosition(int position) {
116     DOM.setElementPropertyInt(getElement(), "scrollLeft", position);
117   }
118
119   /**
120    * Sets the vertical scroll position.
121    *
122    * @param position the new vertical scroll position, in pixels
123    */

124   public void setScrollPosition(int position) {
125     DOM.setElementPropertyInt(getElement(), "scrollTop", position);
126   }
127
128   private native void ensureVisibleImpl(Element scroll, Element e) /*-{
129     if (!e)
130       return;
131
132     var item = e;
133     var realOffset = 0;
134     while (item && (item != scroll)) {
135       realOffset += item.offsetTop;
136       item = item.offsetParent;
137     }
138
139     scroll.scrollTop = realOffset - scroll.offsetHeight / 2;
140   }-*/
;
141 }
142
Popular Tags