Class PagingTableModel<T>

  • Type Parameters:
    T - the type of elements in this table model
    All Implemented Interfaces:
    java.io.Serializable, javax.swing.table.TableModel

    public abstract class PagingTableModel<T>
    extends javax.swing.table.AbstractTableModel
    A paginated TableModel. The model will have at most, by default, 50 rows loaded in memory at any given time. The advertised row count will be of all the entries (as if they were all loaded in memory).

    If a JTable, using this model, is wrapped in a JScrollPane the vertical scroll bar will be shown as if all the entries were loaded.

    Rows (page segments) will be loaded in a separate thread, on demand.

    Implementation based on PagingTableModel located in http://www.coderanch.com/t/345383/GUI/java/JTable-Paging, with permission from the author, Brian Cole ([email protected], http://bitguru.com).
    Contains the following changes:

    • Removed simulation code;
    • Added type parameter;
    • Added abstract methods.
    See Also:
    loadPage(int, int), setMaxPageSize(int), TableModel, JTable, JScrollPane, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_MAX_PAGE_SIZE
      Default maximum page size.
      static java.lang.String DEFAULT_SEGMENT_LOADER_THREAD_NAME
      Default segment loader thread name.
      • Fields inherited from class javax.swing.table.AbstractTableModel

        listenerList
    • Constructor Summary

      Constructors 
      Constructor Description
      PagingTableModel()
      Constructs a PagingTableModel with default default segment loader thread name ( "ZAP-PagingTableModel-SegmentLoaderThread") and default maximum page size (50).
      PagingTableModel​(int maxPageSize)
      Constructs a PagingTableModel with the given maximum page size and default segment loader thread name ( "ZAP-PagingTableModel-SegmentLoaderThread").
      PagingTableModel​(java.lang.String segmentLoaderThreadName)
      Constructs a PagingTableModel with the given segment loader thread name and default maximum page size (50).
      PagingTableModel​(java.lang.String segmentLoaderThreadName, int maxPageSize)
      Constructs a PagingTableModel with the given segment loader thread name and given maximum page size.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void clear()  
      void fireTableDataChanged()  
      int getMaxPageSize()
      Returns the maximum page size.
      protected abstract java.lang.Object getPlaceholderValueAt​(int columnIndex)
      Gets the placeholder value that should be shown for the given column, until the actual values are ready to be shown.
      protected abstract java.lang.Object getRealValueAt​(T rowObject, int columnIndex)
      Called by getValueAt(int, int) when requested row is already loaded.
      abstract int getRowCount()
      Returns the number of all items.
      protected T getRowObject​(int rowIndex)
      Gets the object at the given row.
      java.lang.Object getValueAt​(int rowIndex, int columnIndex)  
      protected abstract java.util.List<T> loadPage​(int offset, int length)
      Called when a new page is required.
      void setMaxPageSize​(int maxPageSize)
      Sets the maximum size of the page.
      void setMaxPageSizeWithoutPageChanges​(int maxPageSize)
      Sets the maximum size of the page.
      • Methods inherited from class javax.swing.table.AbstractTableModel

        addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.swing.table.TableModel

        getColumnCount
    • Field Detail

      • DEFAULT_SEGMENT_LOADER_THREAD_NAME

        public static final java.lang.String DEFAULT_SEGMENT_LOADER_THREAD_NAME
        Default segment loader thread name.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_PAGE_SIZE

        public static final int DEFAULT_MAX_PAGE_SIZE
        Default maximum page size.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PagingTableModel

        public PagingTableModel​(java.lang.String segmentLoaderThreadName)
        Constructs a PagingTableModel with the given segment loader thread name and default maximum page size (50).
        Parameters:
        segmentLoaderThreadName - the name for segment loader thread
        Throws:
        java.lang.IllegalArgumentException - if maxPageSize is negative or zero.
      • PagingTableModel

        public PagingTableModel​(int maxPageSize)
        Constructs a PagingTableModel with the given maximum page size and default segment loader thread name ( "ZAP-PagingTableModel-SegmentLoaderThread").
        Parameters:
        maxPageSize - the maximum page size
        Throws:
        java.lang.IllegalArgumentException - if maxPageSize is negative or zero.
      • PagingTableModel

        public PagingTableModel​(java.lang.String segmentLoaderThreadName,
                                int maxPageSize)
        Constructs a PagingTableModel with the given segment loader thread name and given maximum page size.
        Parameters:
        segmentLoaderThreadName - the name for segment loader thread
        maxPageSize - the maximum page size
        Throws:
        java.lang.IllegalArgumentException - if maxPageSize is negative or zero.
    • Method Detail

      • getMaxPageSize

        public int getMaxPageSize()
        Returns the maximum page size.
        Returns:
        the maximum page size.
      • setMaxPageSize

        public void setMaxPageSize​(int maxPageSize)
        Sets the maximum size of the page.

        If the given maximum size is greater than the current maximum size a new page will be loaded, otherwise the current page will be shrunk to meet the given maximum size. In both cases the TableModelListener will be notified of the change.

        The call to this method has no effect if the given maximum size is equal to the current maximum size.

        Parameters:
        maxPageSize - the new maximum page size
        Throws:
        java.lang.IllegalArgumentException - if maxPageSize is negative or zero.
        See Also:
        setMaxPageSizeWithoutPageChanges(int), TableModelListener
      • setMaxPageSizeWithoutPageChanges

        public void setMaxPageSizeWithoutPageChanges​(int maxPageSize)
        Sets the maximum size of the page.

        As opposed to method #setMaxPageSize(int) no changes will be made to the current page.

        Parameters:
        maxPageSize - the new maximum page size
        Throws:
        java.lang.IllegalArgumentException - if maxPageSize is negative or zero.
        See Also:
        setMaxPageSize(int)
      • fireTableDataChanged

        public void fireTableDataChanged()
        Overrides:
        fireTableDataChanged in class javax.swing.table.AbstractTableModel
      • getRowCount

        public abstract int getRowCount()
        Returns the number of all items.
        Returns:
        number of items
      • getRealValueAt

        protected abstract java.lang.Object getRealValueAt​(T rowObject,
                                                           int columnIndex)
        Called by getValueAt(int, int) when requested row is already loaded.
        Parameters:
        rowObject - the row object
        columnIndex - the column index
        Returns:
        value from requested column
      • getPlaceholderValueAt

        protected abstract java.lang.Object getPlaceholderValueAt​(int columnIndex)
        Gets the placeholder value that should be shown for the given column, until the actual values are ready to be shown.
        Parameters:
        columnIndex - the column index
        Returns:
        Value is used to display while loading entry
      • loadPage

        protected abstract java.util.List<T> loadPage​(int offset,
                                                      int length)
        Called when a new page is required.

        The returned List should support fast (preferably constant time) random access.

        Parameters:
        offset - the start offset of the page
        length - the length of the page
        Returns:
        an excerpt of whole list
        See Also:
        List.get(int)
      • getValueAt

        public final java.lang.Object getValueAt​(int rowIndex,
                                                 int columnIndex)
      • getRowObject

        protected T getRowObject​(int rowIndex)
        Gets the object at the given row.
        Parameters:
        rowIndex - the index of the row
        Returns:
        null if object is not in the current page
      • clear

        protected void clear()