001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.event.shared.HandlerRegistration; 031import com.google.gwt.user.client.Event; 032import com.google.gwt.user.client.Event.NativePreviewEvent; 033import com.google.gwt.user.client.Event.NativePreviewHandler; 034import com.google.gwt.user.client.ui.ScrollPanel; 035import com.google.gwt.user.client.ui.Widget; 036 037/** 038 * Native preview handler that focuses the on scroll wheel mouse event on the given scroll panel.<p> 039 */ 040public final class CmsFocusedScrollingHandler implements NativePreviewHandler { 041 042 /** The scroll panel. */ 043 private ScrollPanel m_scrollPanel; 044 045 /** The scroll handler registration. */ 046 private HandlerRegistration m_handlerRegistration; 047 048 /** 049 * Constructor.<p> 050 * 051 * @param scrollPanel the scroll panel to focus on 052 */ 053 private CmsFocusedScrollingHandler(ScrollPanel scrollPanel) { 054 055 m_scrollPanel = scrollPanel; 056 } 057 058 /** 059 * Installs a focused scrolling handler on the given widget.<p> 060 * 061 * @param scrollPanel the scroll panel 062 * 063 * @return the focused scrolling handler 064 */ 065 public static CmsFocusedScrollingHandler installFocusedScrollingHandler(ScrollPanel scrollPanel) { 066 067 if (scrollPanel == null) { 068 throw new UnsupportedOperationException("No scroll panel given"); 069 } 070 CmsFocusedScrollingHandler handler = new CmsFocusedScrollingHandler(scrollPanel); 071 handler.register(); 072 return handler; 073 } 074 075 /** 076 * Returns if the handler is currently registered.<p> 077 * 078 * @return <code>true</code> if the handler is currently registered and active 079 */ 080 public boolean isRegistered() { 081 082 return m_handlerRegistration != null; 083 } 084 085 /** 086 * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent) 087 */ 088 public void onPreviewNativeEvent(NativePreviewEvent event) { 089 090 if ((Event.ONMOUSEWHEEL == event.getTypeInt()) && (m_handlerRegistration != null)) { 091 CmsPositionBean position = CmsPositionBean.generatePositionInfo(m_scrollPanel); 092 if (position.isOverElement(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY())) { 093 boolean cancelEvent = false; 094 int velocity = event.getNativeEvent().getMouseWheelVelocityY(); 095 if (velocity > 0) { 096 097 Widget child = m_scrollPanel.getWidget(); 098 int scrollSpace = child.getOffsetHeight() 099 - m_scrollPanel.getVerticalScrollPosition() 100 - m_scrollPanel.getOffsetHeight(); 101 CmsDebugLog.getInstance().printLine("Scrolling down: " + scrollSpace); 102 cancelEvent = scrollSpace <= 0; 103 } else { 104 CmsDebugLog.getInstance().printLine("Scrolling up: " + m_scrollPanel.getVerticalScrollPosition()); 105 cancelEvent = m_scrollPanel.getVerticalScrollPosition() == 0; 106 } 107 if (cancelEvent) { 108 event.cancel(); 109 } 110 } else { 111 removeHandler(); 112 } 113 114 } 115 116 } 117 118 /** 119 * Registers the handler.<p> 120 */ 121 public void register() { 122 123 removeHandler(); 124 m_handlerRegistration = Event.addNativePreviewHandler(this); 125 } 126 127 /** 128 * Removes the handler and deactivates it.<p> 129 * Call {@link #register()} the register again.<p> 130 */ 131 public void removeHandler() { 132 133 if (m_handlerRegistration != null) { 134 m_handlerRegistration.removeHandler(); 135 m_handlerRegistration = null; 136 } 137 } 138}