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.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     * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent)
077     */
078    public void onPreviewNativeEvent(NativePreviewEvent event) {
079
080        if ((Event.ONMOUSEWHEEL == event.getTypeInt()) && (m_handlerRegistration != null)) {
081            CmsPositionBean position = CmsPositionBean.generatePositionInfo(m_scrollPanel);
082            if (position.isOverElement(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY())) {
083                boolean cancelEvent = false;
084                int velocity = event.getNativeEvent().getMouseWheelVelocityY();
085                if (velocity > 0) {
086
087                    Widget child = m_scrollPanel.getWidget();
088                    int scrollSpace = child.getOffsetHeight()
089                        - m_scrollPanel.getVerticalScrollPosition()
090                        - m_scrollPanel.getOffsetHeight();
091                    CmsDebugLog.getInstance().printLine("Scrolling down:  " + scrollSpace);
092                    cancelEvent = scrollSpace <= 0;
093                } else {
094                    CmsDebugLog.getInstance().printLine("Scrolling up:  " + m_scrollPanel.getVerticalScrollPosition());
095                    cancelEvent = m_scrollPanel.getVerticalScrollPosition() == 0;
096                }
097                if (cancelEvent) {
098                    event.cancel();
099                }
100            } else {
101                removeHandler();
102            }
103
104        }
105
106    }
107
108    /**
109     * Returns if the handler is currently registered.<p>
110     *
111     * @return <code>true</code> if the handler is currently registered and active
112     */
113    public boolean isRegistered() {
114
115        return m_handlerRegistration != null;
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}