Source for org.jfree.ui.SortableTableModel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------------
  28:  * SortableTableModel.java
  29:  * -----------------------
  30:  * (C) Copyright 2000-2005, by Object Refinery Limited;
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SortableTableModel.java,v 1.5 2005/11/16 15:58:41 taqua Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 26-Oct-2001 : Changed package to com.jrefinery.ui.* (DG);
  40:  * 20-Nov-2001 : Made constructor protected (DG);
  41:  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.ui;
  46: 
  47: import javax.swing.table.AbstractTableModel;
  48: 
  49: /**
  50:  * The base class for a sortable table model.
  51:  *
  52:  * @author David Gilbert
  53:  */
  54: public abstract class SortableTableModel extends AbstractTableModel {
  55: 
  56:     /** The column on which the data is sorted (-1 for no sorting). */
  57:     private int sortingColumn;
  58: 
  59:     /** Indicates ascending (true) or descending (false) order. */
  60:     private boolean ascending;
  61: 
  62:     /**
  63:      * Constructs a sortable table model.
  64:      */
  65:     public SortableTableModel() {
  66:         this.sortingColumn = -1;
  67:         this.ascending = true;
  68:     }
  69: 
  70:     /**
  71:      * Returns the index of the sorting column, or -1 if the data is not sorted
  72:      * on any column.
  73:      *
  74:      * @return the column used for sorting.
  75:      */
  76:     public int getSortingColumn() {
  77:         return this.sortingColumn;
  78:     }
  79: 
  80:     /**
  81:      * Returns <code>true</code> if the data is sorted in ascending order, and 
  82:      * <code>false</code> otherwise.
  83:      *
  84:      * @return <code>true</code> if the data is sorted in ascending order, and 
  85:      *         <code>false</code> otherwise.
  86:      */
  87:     public boolean isAscending() {
  88:         return ascending;
  89:     }
  90: 
  91:     /**
  92:      * Sets the flag that determines whether the sort order is ascending or 
  93:      * descending.
  94:      *
  95:      * @param flag  the flag.
  96:      */
  97:     public void setAscending(final boolean flag) {
  98:         this.ascending = flag;
  99:     }
 100: 
 101:     /**
 102:      * Sorts the table.
 103:      *
 104:      * @param column  the column to sort on (zero-based index).
 105:      * @param ascending  a flag to indicate ascending order or descending order.
 106:      */
 107:     public void sortByColumn(final int column, final boolean ascending) {
 108:         if (isSortable(column)) {
 109:             this.sortingColumn = column;
 110:         }
 111:     }
 112: 
 113:     /**
 114:      * Returns a flag indicating whether or not a column is sortable.
 115:      *
 116:      * @param column  the column (zero-based index).
 117:      *
 118:      * @return boolean.
 119:      */
 120:     public boolean isSortable(final int column) {
 121:         return false;
 122:     }
 123: 
 124: }