001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.navigation.paging;
018
019import org.apache.wicket.Page;
020import org.apache.wicket.markup.html.link.DisabledAttributeLinkBehavior;
021import org.apache.wicket.markup.html.link.Link;
022
023/**
024 * An incremental link to a page of a PageableListView. Assuming your list view navigation looks
025 * like
026 * 
027 * <pre>
028 * 
029 *       [first / &lt;&lt; / &lt;] 1 | 2 | 3 [&gt; / &gt;&gt; /last]
030 * 
031 * </pre>
032 * 
033 * <p>
034 * and "&lt;" meaning the previous and "&lt;&lt;" goto the "current page - 5", than it is this kind
035 * of incremental page links which can easily be created.
036 * 
037 * @author Juergen Donnerstag
038 * @author Martijn Dashorst
039 * @param <T>
040 *            type of model object
041 */
042public class PagingNavigationIncrementLink<T> extends Link<T>
043{
044        private static final long serialVersionUID = 1L;
045
046        /** The increment. */
047        private final int increment;
048
049        /** The PageableListView the page links are referring to. */
050        protected final IPageable pageable;
051
052        /**
053         * Constructor.
054         * 
055         * @param id
056         *            See Component
057         * @param pageable
058         *            The pageable component the page links are referring to
059         * @param increment
060         *            increment by
061         */
062        public PagingNavigationIncrementLink(final String id, final IPageable pageable,
063                final int increment)
064        {
065                super(id);
066                setAutoEnable(true);
067                this.increment = increment;
068                this.pageable = pageable;
069                
070                add(new DisabledAttributeLinkBehavior());
071        }
072
073        /**
074         * @see org.apache.wicket.markup.html.link.Link#onClick()
075         */
076        @Override
077        public void onClick()
078        {
079                // Tell the PageableListView which page to print next
080                pageable.setCurrentPage(getPageNumber());
081
082                // Return the current page.
083                setResponsePage(getPage());
084        }
085
086        /**
087         * Determines the next page number for the pageable component.
088         * 
089         * @return the new page number
090         */
091        public final long getPageNumber()
092        {
093                // Determine the page number based on the current
094                // PageableListView page and the increment
095                long idx = pageable.getCurrentPage() + increment;
096
097                // make sure the index lies between 0 and the last page
098                return Math.max(0, Math.min(pageable.getPageCount() - 1, idx));
099        }
100
101        /**
102         * @return True if it is referring to the first page of the underlying PageableListView.
103         */
104        public boolean isFirst()
105        {
106                return pageable.getCurrentPage() <= 0;
107        }
108
109        /**
110         * @return True if it is referring to the last page of the underlying PageableListView.
111         */
112        public boolean isLast()
113        {
114                return pageable.getCurrentPage() >= (pageable.getPageCount() - 1);
115        }
116
117        /**
118         * Returns true if the page link links to the given page.
119         * 
120         * @param page
121         *            ignored
122         * @return True if this link links to the given page
123         * @see org.apache.wicket.markup.html.link.BookmarkablePageLink#linksTo(org.apache.wicket.Page)
124         */
125        @Override
126        public boolean linksTo(final Page page)
127        {
128                pageable.getCurrentPage();
129                return ((increment < 0) && isFirst()) || ((increment > 0) && isLast());
130        }
131}