⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagedlistholder.java

📁 该源码包括了基于J2EE的数据持久层设计,设计中使用了DAO,Service,等模式,并在Struts下进行了测试.
💻 JAVA
字号:
/*****************************************************************************
 * Copyright (C) JavaWing Organization. All rights reserved.                 *
 * ------------------------------------------------------------------------- *
 * The software in this package is reserved by JavaWing Organization.        *
 * FrameServer is a MVC framework which implements multiform terminal        *
 * support , includes WEB,J2ME,BREW,WAP,SMS and etc.                         *
 * FrameServer is based on Ioc container and Aop framework.                  *
 * ------------------------------------------------------------------------- *
 * @version 4.0                                                              *
 *****************************************************************************/

package org.conference.pagination;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p><strong>PagedListHolder</strong> ,
 * PagedListHolder is a simple state holder for handling lists of objects,
 * separating them into pages. Page numbering starts with 0.
 *
 * <p>This is mainly targetted at usage in web UIs. Typically, an instance will be
 * instantiated with a list of beans, put into the session, and exported as model.
 * The properties are all set/get programmatically. The getters
 * will mainly be used by the view.
 *
 * <p>Supports sorting the underlying list via a SortDefinition implementation,
 * available as property "sort". By default, a MutableSortDefinition instance
 * that toggles the ascending value on setting the same property again is used.
 *
 * <p>This class just provides support for an unmodifiable List of beans.
 * If you need on-demand refresh because of Locale or filter changes,
 * consider RefreshablePagedListHolder.
 *
 * @see #getPageList
 * @see org.javawing.component.paging.RefreshablePagedListHolder
 * @see org.javawing.component.paging.MutableSortDefinition
 *
 * @author   Yellowicq
 * @version  $Id: PagedListHolder.java,v 2.0 2005/10/12 16.34.08 yellowicq Exp $
 * @package  org.javawing.component.paging
 * @since 4.0
 */
public class PagedListHolder
    implements Serializable {

  public static final int DEFAULT_PAGE_SIZE = 10;

  public static final int DEFAULT_MAX_LINKED_PAGES = 10;

  private List source;

  private Date refreshDate;

  //private SortDefinition sort;

 // private SortDefinition sortUsed;

  private int pageSize = DEFAULT_PAGE_SIZE;

  private int page = 0;

  private boolean newPageSet;

  private int maxLinkedPages = DEFAULT_MAX_LINKED_PAGES;

  /**
   * Create a new holder instance.
   * You'll need to set a source list to be able to use the holder.
   * @see #setSource
   */
  public PagedListHolder() {
   this(new ArrayList (0));
  }

  /**
   * Create a new holder instance with the given source list, starting with
   * a default sort definition (with "toggleAscendingOnProperty" activated).
   * @param source the source List
   * @see MutableSortDefinition#setToggleAscendingOnProperty
   */
 // public PagedListHolder(List source) {
  //  this(source, new MutableSortDefinition(true));
 // }
  /**
   * Create a new holder instance with the given source list.
   * @param source the source List
   * @param sort the SortDefinition to start with
   */
 // public PagedListHolder(List source, SortDefinition sort) {
 //   setSource(source);
 //   setSort(sort);
 // }  
  
  public PagedListHolder(List source) {
     setSource(source);
   //setSort(sort);
  }  
  /**
   * Set the source list for this holder.
   *
   * @param source List
   */
  public void setSource(List source) {
    this.source = source;
    this.refreshDate = new Date();
   // this.sortUsed = null;
  }

  /**
   * Return the source list for this holder.
   *
   * @return List
   */
  public List getSource() {
    return source;
  }

  /**
   * Return the last time the list has been fetched from the source provider.
   *
   * @return Date
   */
  public Date getRefreshDate() {
    return refreshDate;
  }

  /**
   * Set the sort definition for this holder. Typically an instance of
   * MutableSortDefinition.
   *
   * @see org.javawing.component.paging.MutableSortDefinition
   * @param sort SortDefinition
   */
 // public void setSort(SortDefinition sort) {
 //   this.sort = sort;
 // }

  /**
   * Return the sort definition for this holder.
   *
   * @return SortDefinition
   */
 // public SortDefinition getSort() {
  //  return sort;
  //}

  /**
   * Set the current page size. Resets the current page number if changed.
   *
   * <p>Default value is 10.
   *
   * @param pageSize int
   */
  public void setPageSize(int pageSize) {
    if (pageSize != this.pageSize) {
      this.pageSize = pageSize;
      if (!this.newPageSet) {
        this.page = 0;
      }
    }
  }

  /**
   * Return the current page size.
   *
   * @return int
   */
  public int getPageSize() {
    return pageSize;
  }

  /**
   * Set the current page number. Page numbering starts with 0.
   *
   * @param page int
   */
  public void setPage(int page) {
    this.page = page;
    this.newPageSet = true;
  }

  /**
   * Return the current page number. Page numbering starts with 0.
   *
   * @return int
   */
  public int getPage() {
    this.newPageSet = false;
    if (this.page >= getPageCount()) {
      this.page = getPageCount() - 1;
    }
    return this.page;
  }

  /**
   * Set the maximum number of page links to a few pages around the current one.
   *
   * @param maxLinkedPages int
   */
  public void setMaxLinkedPages(int maxLinkedPages) {
    this.maxLinkedPages = maxLinkedPages;
  }

  /**
   * Return the maximum number of page links to a few pages around the current
   * one.
   *
   * @return int
   */
  public int getMaxLinkedPages() {
    return maxLinkedPages;
  }

  /**
   * Return the number of pages for the current source list.
   *
   * @return int
   */
  public int getPageCount() {
    float nrOfPages = (float) getSource().size() / getPageSize();
    return (int) ( (nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ?
                  nrOfPages + 1 : nrOfPages);
  }

  /**
   * Return if the current page is the first one.
   *
   * @return boolean
   */
  public boolean isFirstPage() {
    return getPage() == 0;
  }

  /**
   * Return if the current page is the last one.
   *
   * @return boolean
   */
  public boolean isLastPage() {
    return getPage() == getPageCount() - 1;
  }

  /**
   * Switch to previous page.
   * Will stay on first page if already on first page.
   */
  public void previousPage() {
    if (!isFirstPage()) {
      this.page--;
    }
  }

  /**
   * Switch to next page.
   * Will stay on last page if already on last page.
   */
  public void nextPage() {
    if (!isLastPage()) {
      this.page++;
     // System.out.println(page);
    }
  }

  /**
   * Return the total number of elements in the source list.
   *
   * @return int
   */
  public int getNrOfElements() {
    return getSource().size();
  }

  /**
   * Return the element index of the first element on the current page. Element
   * numbering starts with 0.
   *
   * @return int
   */
  public int getFirstElementOnPage() {
    return (getPageSize() * getPage());
  }

  /**
   * Return the element index of the last element on the current page. Element
   * numbering starts with 0.
   *
   * @return int
   */
  public int getLastElementOnPage() {
    int endIndex = getPageSize() * (getPage() + 1);
    return (endIndex > getSource().size() ? getSource().size() : endIndex) - 1;
  }

  /**
   * Return a sub-list representing the current page.
   *
   * @return List
   */
  public List getPageList() {
    return getSource().subList(getFirstElementOnPage(),
                               getLastElementOnPage() + 1);
  }

  /**
   * Return the first page to which create a link around the current page.
   *
   * @return int
   */
  public int getFirstLinkedPage() {
    return Math.max(0, getPage() - (getMaxLinkedPages() / 2));
  }

  /**
   * Return the last page to which create a link around the current page.
   *
   * @return int
   */
  public int getLastLinkedPage() {
    return Math.min(getFirstLinkedPage() + getMaxLinkedPages() - 1,
                    getPageCount() - 1);
  }

  /**
   * Resort the list if necessary, i.e. if the current <code>sort</code> instance
   * isn't equal to the backed-up <code>sortUsed</code> instance.
   * <p>Calls <code>doSort</code> to trigger actual sorting.
   * @see #doSort
   */
 // public void resort() {
  //  SortDefinition sort = getSort();
  //  if (sort != null && !sort.equals(this.sortUsed)) {
  //    this.sortUsed = copySortDefinition(sort);
  //    doSort(getSource(), sort);
  //    setPage(0);
  //  }
 // }

  /**
   * Create a deep copy of the given sort definition,
   * for use as state holder to compare a modified sort definition against.
   * <p>Default implementation creates a MutableSortDefinition instance.
   * Can be overridden in subclasses, in particular in case of custom
   * extensions to the SortDefinition interface. Is allowed to return
   * null, which means that no sort state will be held, triggering
   * actual sorting for each <code>resort</code> call.
   * @param sort the current SortDefinition object
   * @return a deep copy of the SortDefinition object
   * @see MutableSortDefinition#MutableSortDefinition(SortDefinition)
   */
//  protected SortDefinition copySortDefinition(SortDefinition sort) {
 //   return new MutableSortDefinition(sort);
 // }

  /**
   * Actually perform sorting of the given source list, according to the given
   * sort definition.
   *
   * <p>The default implementation uses FrameServer's PropertyComparator. Can be
   * overridden in subclasses.
   *
   * @see PropertyComparator#sort(java.util.List, SortDefinition)
   * @param source List
   * @param sort SortDefinition
   */
 /// protected void doSort(List<E> source, SortDefinition sort) {
  //  PropertyComparator.sort(source, sort);
 // }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -