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 * A link to a page of a PageableListView. 025 * 026 * @author Jonathan Locke 027 * @author Eelco Hillenius 028 * @author Martijn Dashorst 029 * @param <T> 030 * type of model object 031 */ 032public class PagingNavigationLink<T> extends Link<T> 033{ 034 private static final long serialVersionUID = 1L; 035 036 /** The pageable list view. */ 037 protected final IPageable pageable; 038 039 /** The page of the PageableListView this link is for. */ 040 private final long pageNumber; 041 042 /** 043 * Constructor. 044 * 045 * @param id 046 * See Component 047 * @param pageable 048 * The pageable component for this page link 049 * @param pageNumber 050 * The page number in the PageableListView that this link links to. Negative 051 * pageNumbers are relative to the end of the list. 052 */ 053 public PagingNavigationLink(final String id, final IPageable pageable, final long pageNumber) 054 { 055 super(id); 056 setAutoEnable(true); 057 this.pageNumber = pageNumber; 058 this.pageable = pageable; 059 060 add(new DisabledAttributeLinkBehavior()); 061 } 062 063 /** 064 * @see org.apache.wicket.markup.html.link.Link#onClick() 065 */ 066 @Override 067 public void onClick() 068 { 069 pageable.setCurrentPage(getPageNumber()); 070 } 071 072 /** 073 * Get pageNumber. 074 * 075 * @return pageNumber. 076 */ 077 public final long getPageNumber() 078 { 079 return cullPageNumber(pageNumber); 080 } 081 082 /** 083 * Allows the link to cull the page number to the valid range before it is retrieved from the 084 * link 085 * 086 * @param pageNumber 087 * @return culled page number 088 */ 089 protected long cullPageNumber(long pageNumber) 090 { 091 long idx = pageNumber; 092 if (idx < 0) 093 { 094 idx = pageable.getPageCount() + idx; 095 } 096 097 if (idx > (pageable.getPageCount() - 1)) 098 { 099 idx = pageable.getPageCount() - 1; 100 } 101 102 if (idx < 0) 103 { 104 idx = 0; 105 } 106 107 return idx; 108 } 109 110 /** 111 * @return True if this page is the first page of the containing PageableListView 112 */ 113 public final boolean isFirst() 114 { 115 return getPageNumber() == 0; 116 } 117 118 /** 119 * @return True if this page is the last page of the containing PageableListView 120 */ 121 public final boolean isLast() 122 { 123 return getPageNumber() == (pageable.getPageCount() - 1); 124 } 125 126 /** 127 * Returns true if this PageableListView navigation link links to the given page. 128 * 129 * @param page 130 * The page 131 * @return True if this link links to the given page 132 * @see org.apache.wicket.markup.html.link.Link#linksTo(org.apache.wicket.Page) 133 */ 134 @Override 135 public final boolean linksTo(final Page page) 136 { 137 return getPageNumber() == pageable.getCurrentPage(); 138 } 139}