Advertisement
Guest User

TomaszDziurko.WicketPagingNavigator

a guest
Dec 31st, 2010
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.83 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.wicket.markup.html.navigation.paging;
  18.  
  19. import org.apache.wicket.Component;
  20. import org.apache.wicket.behavior.AbstractBehavior;
  21. import org.apache.wicket.markup.ComponentTag;
  22. import org.apache.wicket.markup.html.link.AbstractLink;
  23. import org.apache.wicket.markup.html.panel.Panel;
  24.  
  25. /**
  26.  * A Wicket panel component to draw and maintain a complete page navigator, meant to be easily added
  27.  * to any PageableListView. A navigation which contains links to the first and last page, the
  28.  * current page +- some increment and which supports paged navigation bars (@see
  29.  * PageableListViewNavigationWithMargin).
  30.  *
  31.  * @author Juergen Donnerstag
  32.  */
  33. public class PagingNavigator extends Panel
  34. {
  35.     private static final long serialVersionUID = 1L;
  36.  
  37.     public static final String NAVIGATION_ID = "navigation";
  38.  
  39.     /** The navigation bar to be printed, e.g. 1 | 2 | 3 etc. */
  40.     private PagingNavigation pagingNavigation;
  41.     private final IPageable pageable;
  42.     private final IPagingLabelProvider labelProvider;
  43.  
  44.     /**
  45.      * Constructor.
  46.      *
  47.      * @param id
  48.      *            See Component
  49.      * @param pageable
  50.      *            The pageable component the page links are referring to.
  51.      */
  52.     public PagingNavigator(final String id, final IPageable pageable)
  53.     {
  54.         this(id, pageable, null);
  55.     }
  56.  
  57.     /**
  58.      * Constructor.
  59.      *
  60.      * @param id
  61.      *            See Component
  62.      * @param pageable
  63.      *            The pageable component the page links are referring to.
  64.      * @param labelProvider
  65.      *            The label provider for the link text.
  66.      */
  67.     public PagingNavigator(final String id, final IPageable pageable,
  68.         final IPagingLabelProvider labelProvider)
  69.     {
  70.         super(id);
  71.         this.pageable = pageable;
  72.         this.labelProvider = labelProvider;
  73.     }
  74.  
  75.     /**
  76.      * {@link IPageable} this navigator is linked with
  77.      *
  78.      * @return {@link IPageable} instance
  79.      */
  80.     public final IPageable getPageable()
  81.     {
  82.         return pageable;
  83.     }
  84.  
  85.     /**
  86.      * @see org.apache.wicket.Component#onBeforeRender()
  87.      */
  88.     @Override
  89.     protected void onBeforeRender()
  90.     {
  91.         if (get("first") == null)
  92.         {
  93.             // Get the navigation bar and add it to the hierarchy
  94.             pagingNavigation = newNavigation(pageable, labelProvider);
  95.             add(pagingNavigation);
  96.  
  97.             // Add additional page links
  98.             add(newPagingNavigationLink("first", pageable, 0).add(
  99.                 new TitleAppender("PagingNavigator.first")));
  100.             add(newPagingNavigationIncrementLink("prev", pageable, -1).add(
  101.                 new TitleAppender("PagingNavigator.previous")));
  102.             add(newPagingNavigationIncrementLink("next", pageable, 1).add(
  103.                 new TitleAppender("PagingNavigator.next")));
  104.             add(newPagingNavigationLink("last", pageable, -1).add(
  105.                 new TitleAppender("PagingNavigator.last")));
  106.         }
  107.         super.onBeforeRender();
  108.     }
  109.  
  110.     /**
  111.      * Create a new increment link. May be subclassed to make use of specialized links, e.g. Ajaxian
  112.      * links.
  113.      *
  114.      * @param id
  115.      *            the link id
  116.      * @param pageable
  117.      *            the pageable to control
  118.      * @param increment
  119.      *            the increment
  120.      * @return the increment link
  121.      */
  122.     protected AbstractLink newPagingNavigationIncrementLink(String id, IPageable pageable,
  123.         int increment)
  124.     {
  125.         return new PagingNavigationIncrementLink<Void>(id, pageable, increment);
  126.     }
  127.  
  128.     /**
  129.      * Create a new pagenumber link. May be subclassed to make use of specialized links, e.g.
  130.      * Ajaxian links.
  131.      *
  132.      * @param id
  133.      *            the link id
  134.      * @param pageable
  135.      *            the pageable to control
  136.      * @param pageNumber
  137.      *            the page to jump to
  138.      * @return the pagenumber link
  139.      */
  140.     protected AbstractLink newPagingNavigationLink(String id, IPageable pageable, int pageNumber)
  141.     {
  142.         return new PagingNavigationLink<Void>(id, pageable, pageNumber);
  143.     }
  144.  
  145.     /**
  146.      * Create a new PagingNavigation. May be subclassed to make us of specialized PagingNavigation.
  147.      *
  148.      * @param pageable
  149.      *            the pageable component
  150.      * @param labelProvider
  151.      *            The label provider for the link text.
  152.      * @return the navigation object
  153.      */
  154.     protected PagingNavigation newNavigation(final IPageable pageable,
  155.         final IPagingLabelProvider labelProvider)
  156.     {
  157.         return new PagingNavigation(NAVIGATION_ID, pageable, labelProvider);
  158.     }
  159.  
  160.     /**
  161.      * Gets the pageable navigation component for configuration purposes.
  162.      *
  163.      * @return the associated pageable navigation.
  164.      */
  165.     public final PagingNavigation getPagingNavigation()
  166.     {
  167.         return pagingNavigation;
  168.     }
  169.  
  170.     /**
  171.      * Appends title attribute to navigation links
  172.      *
  173.      * @author igor.vaynberg
  174.      */
  175.     private final class TitleAppender extends AbstractBehavior
  176.     {
  177.         private static final long serialVersionUID = 1L;
  178.  
  179.         private final String resourceKey;
  180.  
  181.         /**
  182.          * Constructor
  183.          *
  184.          * @param resourceKey
  185.          *            resource key of the message
  186.          */
  187.         public TitleAppender(String resourceKey)
  188.         {
  189.             this.resourceKey = resourceKey;
  190.         }
  191.  
  192.         /** {@inheritDoc} */
  193.         @Override
  194.         public void onComponentTag(Component component, ComponentTag tag)
  195.         {
  196.             tag.put("title", PagingNavigator.this.getString(resourceKey));
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement