87a14ec9498bc06c0e60514f73f8b914e3e0786b
[privoxy.git] / src / java / org / privoxy / activityconsole / SortableTableModel.java
1 /*********************************************************************
2  *
3  * File        :  $Source$
4  *
5  * Purpose     :  Sorting JTable model.
6  *
7  * Copyright   :  Written by and Copyright (C) 2003 the SourceForge
8  *                Privoxy team. http://www.privoxy.org/
9  *
10  *                Based on the Internet Junkbuster originally written
11  *                by and Copyright (C) 1997 Anonymous Coders and
12  *                Junkbusters Corporation.  http://www.junkbusters.com
13  *
14  *                This program is free software; you can redistribute it
15  *                and/or modify it under the terms of the GNU General
16  *                Public License as published by the Free Software
17  *                Foundation; either version 2 of the License, or (at
18  *                your option) any later version.
19  *
20  *                This program is distributed in the hope that it will
21  *                be useful, but WITHOUT ANY WARRANTY; without even the
22  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
23  *                PARTICULAR PURPOSE.  See the GNU General Public
24  *                License for more details.
25  *
26  *                The GNU General Public License should be included with
27  *                this file.  If not, you can view it at
28  *                http://www.gnu.org/copyleft/gpl.html
29  *                or write to the Free Software Foundation, Inc., 59
30  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  *
32  * Revisions   :
33  *    $Log$
34  *********************************************************************/
35
36 package org.privoxy.activityconsole;
37
38 import java.util.*;
39 import java.awt.*;
40 import javax.swing.*;
41 import javax.swing.table.*;
42
43 /** 
44  * Sorting JTable table model.
45  * @author Last Modified By: $Author$
46  * @version $Rev$-$Date$$State$
47  */
48 public class SortableTableModel extends DefaultTableModel
49 {
50
51   private static final String
52   COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
53
54   int[] indexes;
55   TableSorter sorter;
56
57   public SortableTableModel()
58   {
59   }
60
61   public SortableTableModel(Vector data, Vector columnNames)
62   {
63     super(data, columnNames);
64   }
65   public Object getValueAt(int row, int col)
66   {
67     int rowIndex = row;
68     if (indexes != null)
69     {
70       rowIndex = indexes[row];
71     }
72     return super.getValueAt(rowIndex, col);
73   }
74
75   public void setValueAt(Object value, int row, int col)
76   {
77     int rowIndex = row;
78     if (indexes != null)
79     {
80       rowIndex = indexes[row];
81     }
82     super.setValueAt(value, rowIndex, col);
83   }
84
85   public void sortByColumn(int column, boolean isAscent)
86   {
87     if (sorter == null)
88     {
89       sorter = new TableSorter(this);
90     }
91     sorter.sort(column, isAscent);   
92     fireTableDataChanged();
93   }
94
95   public int[] getIndexes()
96   {
97     int n = getRowCount();
98     if (indexes != null)
99     {
100       if (indexes.length == n)
101       {
102         return indexes;
103       }
104     }
105     indexes = new int[n];
106     for (int i=0; i<n; i++)
107     {
108       indexes[i] = i;
109     }
110     return indexes;
111   }
112 }