001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.galleries.client.ui;
029
030import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
031import org.opencms.ade.galleries.shared.CmsResultItemBean;
032import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.SortParams;
033
034import java.util.List;
035
036import com.google.gwt.core.client.Scheduler;
037import com.google.gwt.core.client.Scheduler.ScheduledCommand;
038import com.google.gwt.event.dom.client.ScrollEvent;
039import com.google.gwt.event.dom.client.ScrollHandler;
040import com.google.gwt.user.client.ui.ScrollPanel;
041
042/**
043 * Scroll handler class which loads more items in the front of the search result list when the user scrolls to the
044 * top.<p>
045 */
046public class CmsResultsBackwardsScrollHandler implements ScrollHandler {
047
048    /** The scroll threshold in pixels. */
049    public static final int SCROLL_THRESHOLD = 50;
050
051    /** Flag used to temporarily disable the scroll handler. */
052    protected boolean m_enabled = true;
053
054    /** The index of the first shown page. */
055    protected int m_firstShownPage;
056
057    /** The number of results per page. */
058    protected int m_pageSize;
059
060    /** The list of search result beans. */
061    protected List<CmsResultItemBean> m_resultBeans;
062
063    /** The search results tab. */
064    protected CmsResultsTab m_resultsTab;
065
066    /** The search bean. */
067    protected CmsGallerySearchBean m_searchBean;
068
069    /**
070     * Creates a new handler instance for a given results tab.<p>
071     *
072     * @param resultsTab the results tab for which to create the handler
073     */
074    public CmsResultsBackwardsScrollHandler(CmsResultsTab resultsTab) {
075
076        m_resultsTab = resultsTab;
077    }
078
079    /**
080     * Checks whether more items can be loaded at the front of the list.<p>
081     *
082     * @return true if more items can be loaded at the front of the list
083     */
084    public boolean hasMore() {
085
086        return m_firstShownPage > 1;
087    }
088
089    /**
090     * @see com.google.gwt.event.dom.client.ScrollHandler#onScroll(com.google.gwt.event.dom.client.ScrollEvent)
091     */
092    public void onScroll(ScrollEvent event) {
093
094        if (m_searchBean == null) {
095            return;
096        }
097        if (m_resultBeans == null) {
098            return;
099        }
100        if (!m_enabled) {
101            return;
102        }
103        final ScrollPanel scrollPanel = (ScrollPanel)event.getSource();
104        final int scrollPos = scrollPanel.getVerticalScrollPosition();
105        if ((scrollPos <= SCROLL_THRESHOLD) && hasMore()) {
106            m_enabled = false;
107            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
108
109                /**
110                 * @see com.google.gwt.core.client.Scheduler.ScheduledCommand#execute()
111                 */
112                public void execute() {
113
114                    m_enabled = true;
115                    m_resultsTab.getList().setVerticalScrollPosition(50);
116                }
117            });
118            loadPreviousPage();
119        }
120
121    }
122
123    /**
124     * Updates the handler with a new search bean.<p>
125     *
126     * @param searchBean the search bean
127     */
128    public void updateSearchBean(CmsGallerySearchBean searchBean) {
129
130        m_searchBean = searchBean;
131        if (searchBean != null) {
132            m_pageSize = searchBean.getMatchesPerPage();
133            int lastPage = searchBean.getLastPage();
134            // we don't just store the search bean because it gets reused for multiple searches
135            // and so the result list may change.
136            m_resultBeans = searchBean.getResults();
137            if (lastPage != -1) {
138                loadPage(lastPage);
139                m_firstShownPage = lastPage;
140                if (lastPage > 1) {
141                    loadPage(lastPage - 1);
142                    m_firstShownPage = lastPage - 1;
143                }
144            }
145        } else {
146            m_resultBeans = null;
147        }
148    }
149
150    /**
151     * Loads a page with a given index.<p>
152     *
153     * @param pageNum the index of the page to load
154     */
155    protected void loadPage(int pageNum) {
156
157        int start = (pageNum - 1) * m_pageSize;
158        List<CmsResultItemBean> results = m_resultBeans;
159        int end = start + m_pageSize;
160        if (end > results.size()) {
161            end = results.size();
162        }
163        List<CmsResultItemBean> page = results.subList(start, end);
164        boolean showPath = SortParams.path_asc.name().equals(m_searchBean.getSortOrder())
165            || SortParams.path_desc.name().equals(m_searchBean.getSortOrder());
166        m_resultsTab.addContentItems(page, true, showPath);
167    }
168
169    /**
170     * Loads the page before the first shown page.<p>
171     */
172    protected void loadPreviousPage() {
173
174        loadPage(m_firstShownPage - 1);
175        m_firstShownPage -= 1;
176    }
177
178}