001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.util; 029 030import com.google.gwt.core.client.Scheduler; 031import com.google.gwt.core.client.Scheduler.ScheduledCommand; 032import com.google.gwt.event.dom.client.ScrollEvent; 033import com.google.gwt.event.dom.client.ScrollHandler; 034import com.google.gwt.user.client.ui.ScrollPanel; 035import com.google.gwt.user.client.ui.Widget; 036 037/** 038 * Scroll handler which executes an action when the user has scrolled to the bottom.<p> 039 * 040 * @since 8.0.0 041 */ 042public class CmsScrollToBottomHandler implements ScrollHandler { 043 044 /** 045 * If the lower edge of the content being scrolled is at most this many pixels below the lower 046 * edge of the scrolling viewport, the action is triggered. 047 */ 048 public static final int DEFAULT_SCROLL_THRESHOLD = 30; 049 050 /** 051 * An internal flag that, if false, prevents the scroll action from being executed.<p> 052 */ 053 protected boolean m_enabled = true; 054 055 /** 056 * The action which is triggered when the user scrolls to the bottom.<p> 057 */ 058 private Runnable m_callback; 059 060 /** 061 * If the lower edge of the content being scrolled is at most this many pixels below the lower 062 * edge of the scrolling viewport, the action is triggered. 063 */ 064 private int m_scrollThreshold = DEFAULT_SCROLL_THRESHOLD; 065 066 /** 067 * Constructs a new scroll handler.<p> 068 * 069 * @param callback the action which should be executed when the user scrolls to the bottom. 070 */ 071 public CmsScrollToBottomHandler(Runnable callback) { 072 073 m_callback = callback; 074 } 075 076 /** 077 * Constructs a new scroll handler with a custom scroll threshold. 078 * 079 * The scroll threshold is the distance from the bottom edge of the scrolled content 080 * such that when the distance from the bottom edge of the scroll viewport to the bottom 081 * edge of the scrolled content becomes lower than the distance, the scroll action is triggered. 082 * 083 * @param callback the action which should be executed when the user scrolls to the bottom. 084 * @param scrollThreshold the scroll threshold 085 */ 086 public CmsScrollToBottomHandler(Runnable callback, int scrollThreshold) { 087 088 m_callback = callback; 089 m_scrollThreshold = scrollThreshold; 090 } 091 092 /** 093 * @see com.google.gwt.event.dom.client.ScrollHandler#onScroll(com.google.gwt.event.dom.client.ScrollEvent) 094 */ 095 public void onScroll(ScrollEvent event) { 096 097 if (!m_enabled) { 098 return; 099 } 100 final ScrollPanel scrollPanel = (ScrollPanel)event.getSource(); 101 final int scrollPos = scrollPanel.getVerticalScrollPosition(); 102 Widget child = scrollPanel.getWidget(); 103 int childHeight = child.getOffsetHeight(); 104 int ownHeight = scrollPanel.getOffsetHeight(); 105 boolean isBottom = (scrollPos + ownHeight) >= (childHeight - m_scrollThreshold); 106 if (isBottom) { 107 m_callback.run(); 108 m_enabled = false; 109 scrollPanel.setVerticalScrollPosition(scrollPos); 110 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 111 112 /** 113 * @see com.google.gwt.core.client.Scheduler.ScheduledCommand#execute() 114 */ 115 public void execute() { 116 117 scrollPanel.setVerticalScrollPosition(scrollPos); 118 m_enabled = true; 119 } 120 }); 121 } 122 } 123}