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: */
017: package org.apache.wicket.markup.html.navigation.paging;
018:
019: import org.apache.wicket.Page;
020: import org.apache.wicket.markup.html.link.Link;
021:
022: /**
023: * A link to a page of a PageableListView.
024: *
025: * @author Jonathan Locke
026: * @author Eelco Hillenius
027: * @author Martijn Dashorst
028: */
029: public class PagingNavigationLink extends Link {
030: private static final long serialVersionUID = 1L;
031:
032: /** The pageable list view. */
033: protected final IPageable pageable;
034:
035: /** The page of the PageableListView this link is for. */
036: private final int pageNumber;
037:
038: /**
039: * Constructor.
040: *
041: * @param id
042: * See Component
043: * @param pageable
044: * The pageable component for this page link
045: * @param pageNumber
046: * The page number in the PageableListView that this link links
047: * to. Negative pageNumbers are relative to the end of the list.
048: */
049: public PagingNavigationLink(final String id,
050: final IPageable pageable, final int pageNumber) {
051: super (id);
052: setAutoEnable(true);
053: this .pageNumber = pageNumber;
054: this .pageable = pageable;
055: }
056:
057: /**
058: * @see org.apache.wicket.markup.html.link.Link#onClick()
059: */
060: public void onClick() {
061: pageable.setCurrentPage(getPageNumber());
062: }
063:
064: /**
065: * Get pageNumber.
066: *
067: * @return pageNumber.
068: */
069: public final int getPageNumber() {
070: int idx = pageNumber;
071: if (idx < 0) {
072: idx = pageable.getPageCount() + idx;
073: }
074:
075: if (idx > (pageable.getPageCount() - 1)) {
076: idx = pageable.getPageCount() - 1;
077: }
078:
079: if (idx < 0) {
080: idx = 0;
081: }
082:
083: return idx;
084: }
085:
086: /**
087: * @return True if this page is the first page of the containing
088: * PageableListView
089: */
090: public final boolean isFirst() {
091: return getPageNumber() == 0;
092: }
093:
094: /**
095: * @return True if this page is the last page of the containing
096: * PageableListView
097: */
098: public final boolean isLast() {
099: return getPageNumber() == (pageable.getPageCount() - 1);
100: }
101:
102: /**
103: * Returns true if this PageableListView navigation link links to the given
104: * page.
105: *
106: * @param page
107: * The page
108: * @return True if this link links to the given page
109: * @see org.apache.wicket.markup.html.link.PageLink#linksTo(org.apache.wicket.Page)
110: */
111: public final boolean linksTo(final Page page) {
112: return getPageNumber() == pageable.getCurrentPage();
113: }
114: }
|