001    package de.mospace.swing.table;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: SortableTableModel.java 12 2007-07-27 09:48:53Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import javax.swing.table.TableModel;
023    
024        /** A row-sortable TableModel where sorting is managed by
025         * translating between two sets of indices, an
026         * index set identifying the table entries (entries)
027         * and another index set representing the current position of
028         * an entry in the table (rows).<p>
029         * All TableModel methods accept and return
030         * rows because this is what the associated JTables need. If one wants
031         * to access the same entry in two SortableTableModels the row numbers
032         * have to be transformed accordingly. E. g.
033         * <code>
034         * this.setValueAt(anotherSTM.getValueAt(anotherSTM.getRows(getEntries(row)),col), row, col)
035         * </code>
036         * @author Moritz Ringler
037         * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
038         */
039    public interface SortableTableModel extends TableModel{
040        /** Constant for ascending sort order */
041        final static int ASCENDING = 1;
042        /** Constant for descending sort order */
043        final static int DESCENDING = -1;
044    
045        /** Sorts the entries in this TableModel according to the criteria
046         *  set by {@link #setSortBy}.
047         **/
048        public void sort();
049    
050        /** Shuffles the entries in this TableModel.**/
051        public void randomize();
052    
053        /** Sets the column with top sorting precedence
054        * and the way it is to be sorted. Implementations
055        * can but do not need to remember previous calls to this method to
056        * determine what to do when two entries have equal fields
057        * in this column.
058        * @param col the table column
059        * @param asc true for ascending order
060        */
061        public void setSortBy(int col, boolean asc);
062    
063        /** Sets the column with top sorting precedence. If <code>col</code> is
064        * already the column with top sorting precedence the sort order will
065        * be reversed, otherwise the entries in this model will be sorted in
066        * ascending order.<p>
067        * Implementations can but do not need to remember previous calls
068        * to this method to determine what to do when two entries have equal fields
069        * in this column.
070        *
071        * @param col the table column
072        *
073        * @see #setSortBy(int, boolean)
074        */
075        public void setSortBy(int col);
076    
077        /** Gets the column with top sorting precedence.
078        *
079        * @return the column by which this table model is sorted.
080        */
081        public int getSortingColumn();
082    
083        /** Returns whether the model is currently sorted or not. **/
084        public boolean isSorted();
085    
086        /** Gets the columns by which this table is sorted. The returned array
087        * holds the information used for sorting the table.
088        * The array holds n pairs of integers in the order of decreasing sorting
089        * precedence. The first integer in each pair is a column number, if it is
090        * -1 this element and all following elements must be ignored. The second integer
091        * is either <code>ASCENDING</code> or <code>DESCENDING</code>.
092        * @return an array of {column, order} pairs with decreasing sorting precedence
093        */
094        public int[][] getSortOrder();
095    
096        /** Gets the current row number of an entry.*/
097        public int getRows(int entry);
098    
099        /** Gets the current row numbers for a list of entries.*/
100        public int[] getRows(int[] entries);
101    
102        /** Gets the entry represented by the specified row in the current sorting.*/
103        public int getEntries(int row);
104    
105        /** Gets the entries represented by the specified rows in the current sorting.*/
106        public int[] getEntries(int[] rows);
107    }