PagingUtilImpl.java

  1. /*
  2.  * Copyright 2010-2024 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.common.paging;

  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;

  22. import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.api.PageLinkFactory;
  23. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  24. import com.vaadin.ui.AbstractOrderedLayout;
  25. import com.vaadin.ui.HorizontalLayout;
  26. import com.vaadin.ui.Label;
  27. import com.vaadin.ui.Link;

  28. /**
  29.  * The Class PagingUtil.
  30.  */
  31. @Component
  32. public final class PagingUtilImpl implements PagingUtil {

  33.     /** The first page. */
  34.     private static final String FIRST_PAGE = "first page";

  35.     /** The last page. */
  36.     private static final String LAST_PAGE = "last page";

  37.     /** The limit for displaying start end links. */
  38.     private static final int LIMIT_FOR_DISPLAYING_START_END_LINKS = 5;

  39.     /** The next page. */
  40.     private static final String NEXT_PAGE = "next page";

  41.     /** The page header. */
  42.     private static final String PAGE_HEADER = "Page: ";

  43.     /** The page one. */
  44.     private static final int PAGE_ONE = 1;

  45.     /** The page separator. */
  46.     private static final char PAGE_SEPARATOR = '/';

  47.     /** The pages total results. */
  48.     private static final String PAGES_TOTAL_RESULTS = " pages. Total results:";

  49.     /** The previous page. */
  50.     private static final String PREVIOUS_PAGE = "previous page";

  51.     /** The results per page. */
  52.     private static final String RESULTS_PER_PAGE = " results per page:";

  53.     /** The show. */
  54.     private static final String SHOW = " :: Show ";

  55.     /** The page link factory. */
  56.     @Autowired
  57.     private PageLinkFactory pageLinkFactory;

  58.     /**
  59.      * Default constructor for PagingUtilImpl.
  60.      */
  61.     public PagingUtilImpl() {
  62.         // Default constructor
  63.     }

  64.     /**
  65.      * Adds the paging link.
  66.      *
  67.      * @param label          the label
  68.      * @param name           the name
  69.      * @param pageId         the page id
  70.      * @param maxPages       the max pages
  71.      * @param pagingControls the paging controls
  72.      */
  73.     private void addPagingLink(final String label, final String name, final String pageId, final long maxPages, final HorizontalLayout pagingControls) {
  74.         final Link previousPageLink = pageLinkFactory.createAdminPagingLink(label,name, pageId, String.valueOf(maxPages));
  75.         pagingControls.addComponent(previousPageLink);
  76.         pagingControls.setExpandRatio(previousPageLink, ContentRatio.SMALL);
  77.     }

  78.     /**
  79.      * Creates the paging controls.
  80.      *
  81.      * @param content       the content
  82.      * @param name          the name
  83.      * @param pageId        the page id
  84.      * @param size          the size
  85.      * @param pageNr        the page nr
  86.      * @param resultPerPage the result per page
  87.      */
  88.     @Override
  89.     public void createPagingControls(final AbstractOrderedLayout content, final String name, final String pageId, final Long size, final int pageNr,
  90.             final int resultPerPage) {
  91.                 final HorizontalLayout pagingControls = new HorizontalLayout();
  92.                 pagingControls.setSpacing(true);
  93.                 pagingControls.setMargin(true);

  94.                 final long maxPages = (size +resultPerPage-1) / resultPerPage;

  95.                 final StringBuilder stringBuilder = new StringBuilder();
  96.                 stringBuilder.append(PAGE_HEADER)
  97.                 .append(pageNr)
  98.                 .append(PAGE_SEPARATOR)
  99.                 .append(maxPages)
  100.                 .append(PAGES_TOTAL_RESULTS)
  101.                 .append(size)
  102.                 .append(RESULTS_PER_PAGE)
  103.                 .append(resultPerPage)
  104.                 .append(SHOW);
  105.                 final Label pageInfo = new Label(stringBuilder.toString());
  106.                 pagingControls.addComponent(pageInfo);
  107.                 pagingControls.setExpandRatio(pageInfo, ContentRatio.SMALL);


  108.                 if (pageNr > PAGE_ONE) {
  109.                     addPagingLink(PREVIOUS_PAGE,name, pageId, pageNr -1L,pagingControls);
  110.                 }

  111.                 if (maxPages > PAGE_ONE && pageNr < maxPages) {
  112.                     addPagingLink(NEXT_PAGE,name, pageId, pageNr +1L,pagingControls);
  113.                 }

  114.                 if (maxPages > LIMIT_FOR_DISPLAYING_START_END_LINKS && pageNr > PAGE_ONE) {
  115.                     addPagingLink(FIRST_PAGE,name, pageId, 1,pagingControls);
  116.                 }

  117.                 if (maxPages > LIMIT_FOR_DISPLAYING_START_END_LINKS && pageNr < maxPages) {
  118.                     addPagingLink(LAST_PAGE,name, pageId, maxPages,pagingControls);
  119.                 }

  120.                 content.addComponent(pagingControls);
  121.                 content.setExpandRatio(pagingControls, ContentRatio.SMALL2);

  122.             }

  123. }