1 /*********************************************************************
5 * Purpose : Sorts JTable rows.
7 * Copyright : Written by and Copyright (C) 2003 the SourceForge
8 * Privoxy team. http://www.privoxy.org/
10 * Based on the Internet Junkbuster originally written
11 * by and Copyright (C) 1997 Anonymous Coders and
12 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
34 *********************************************************************/
36 package org.privoxy.activityconsole;
41 import javax.swing.table.*;
46 * @author Last Modified By: $Author$
47 * @version $Rev$-$Date$$State$
49 public class TableSorter
51 private static final String
52 COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
54 SortableTableModel model;
56 public TableSorter(SortableTableModel model)
63 public void sort(int column, boolean isAscent)
65 int n = model.getRowCount();
66 int[] indexes = model.getIndexes();
68 for (int i=0; i<n-1; i++)
71 for (int j=i+1; j<n; j++)
75 if (compare(column, j, k) < 0)
82 if (compare(column, j, k) > 0)
89 indexes[i] = indexes[k];
96 public int compare(int column, int row1, int row2)
98 Object o1 = model.getValueAt(row1, column);
99 Object o2 = model.getValueAt(row2, column);
100 if (o1 == null && o2 == null)
114 Class type = model.getColumnClass(column);
115 if (type.getSuperclass() == Number.class)
117 return compare((Number)o1, (Number)o2);
119 else if (type == String.class)
121 return((String)o1).compareTo((String)o2);
123 else if (type == Date.class)
125 return compare((Date)o1, (Date)o2);
127 else if (type == Boolean.class)
129 return compare((Boolean)o1, (Boolean)o2);
131 else if (o1 instanceof StatWidget)
133 return((StatWidget)o1).compareTo((StatWidget)o2);
137 return((String)o1).compareTo((String)o2);
142 public int compare(Number o1, Number o2)
144 double n1 = o1.doubleValue();
145 double n2 = o2.doubleValue();
160 public int compare(Date o1, Date o2)
162 long n1 = o1.getTime();
163 long n2 = o2.getTime();
178 public int compare(Boolean o1, Boolean o2)
180 boolean b1 = o1.booleanValue();
181 boolean b2 = o2.booleanValue();