- There is a standard Comparator for reversing Comparables, so let's use
that everywhere; deprecated Comparators.REVERSE_COMPARABLE. - Added a note that the ReversingComparator is old news, too, but we're not ready to go 1.5 everywhere... - Added Comparators.LEXICAL_CASE_INSENSITIVE which treats all sorted objects as Strings that are sorted case insensitively. - Have TableSorter use standard Comparators. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1733 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -13,6 +13,8 @@ import javax.swing.event.TableModelEvent;
|
|||||||
import javax.swing.event.TableModelListener;
|
import javax.swing.event.TableModelListener;
|
||||||
import javax.swing.table.*;
|
import javax.swing.table.*;
|
||||||
|
|
||||||
|
import com.samskivert.util.Comparators;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TableSorter is a decorator for TableModels; adding sorting
|
* TableSorter is a decorator for TableModels; adding sorting
|
||||||
* functionality to a supplied TableModel. TableSorter does
|
* functionality to a supplied TableModel. TableSorter does
|
||||||
@@ -75,17 +77,6 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
|
|
||||||
private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
|
private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
|
||||||
|
|
||||||
public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() {
|
|
||||||
public int compare(Object o1, Object o2) {
|
|
||||||
return ((Comparable) o1).compareTo(o2);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
public static final Comparator LEXICAL_COMPARATOR = new Comparator() {
|
|
||||||
public int compare(Object o1, Object o2) {
|
|
||||||
return o1.toString().compareTo(o2.toString());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static JTable createSortedTable (TableModel model)
|
public static JTable createSortedTable (TableModel model)
|
||||||
{
|
{
|
||||||
TableSorter sorter = new TableSorter(model);
|
TableSorter sorter = new TableSorter(model);
|
||||||
@@ -227,9 +218,9 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
return comparator;
|
return comparator;
|
||||||
}
|
}
|
||||||
if (Comparable.class.isAssignableFrom(columnType)) {
|
if (Comparable.class.isAssignableFrom(columnType)) {
|
||||||
return COMPARABLE_COMAPRATOR;
|
return Comparators.COMPARABLE;
|
||||||
}
|
}
|
||||||
return LEXICAL_COMPARATOR;
|
return Comparators.LEXICAL_CASE_INSENSITIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Row[] getViewToModel() {
|
private Row[] getViewToModel() {
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ public class Comparators
|
|||||||
/**
|
/**
|
||||||
* A comparator that can be used to reverse the results of another
|
* A comparator that can be used to reverse the results of another
|
||||||
* comparator.
|
* comparator.
|
||||||
|
* TODO: deprecate this when we more globally move to 1.5:
|
||||||
|
* @use java.util.Collections.reverseOrder(Comparator c);
|
||||||
*/
|
*/
|
||||||
public static class ReversingComparator implements Comparator
|
public static class ReversingComparator implements Comparator
|
||||||
{
|
{
|
||||||
@@ -49,24 +51,49 @@ public class Comparators
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A comparator that compares {@link Comparable} instances.
|
* A comparator that compares {@link Comparable} instances.
|
||||||
|
* Can you believe this isn't defined somewhere in the standard
|
||||||
|
* java libraries?
|
||||||
*/
|
*/
|
||||||
public static final Comparator COMPARABLE = new Comparator() {
|
public static final Comparator COMPARABLE = new Comparator() {
|
||||||
public int compare (Object o1, Object o2)
|
public int compare (Object o1, Object o2)
|
||||||
{
|
{
|
||||||
if (o1 == o2) {
|
if (o1 == o2) { // catches null == null
|
||||||
return 0;
|
return 0;
|
||||||
}
|
} else if (o1 == null) {
|
||||||
if (o1 == null) {
|
|
||||||
return 1;
|
return 1;
|
||||||
|
} else if (o2 == null) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return ((Comparable)o1).compareTo(o2);
|
return ((Comparable)o1).compareTo(o2); // null-free
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A comparator that compares the toString() value of all objects
|
||||||
|
* case insensitively.
|
||||||
|
*/
|
||||||
|
public static final Comparator LEXICAL_CASE_INSENSITIVE = new Comparator() {
|
||||||
|
public int compare (Object o1, Object o2)
|
||||||
|
{
|
||||||
|
if (o1 == o2) { // catches null == null
|
||||||
|
return 0;
|
||||||
|
} else if (o1 == null) {
|
||||||
|
return 1;
|
||||||
|
} else if (o2 == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// now that we've filtered all nulls, compare the toString()s
|
||||||
|
return String.CASE_INSENSITIVE_ORDER.compare(
|
||||||
|
o1.toString(), o2.toString());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A comparator that imposes a reverse ordering on {@link Comparable}
|
* A comparator that imposes a reverse ordering on {@link Comparable}
|
||||||
* instances.
|
* instances.
|
||||||
|
*
|
||||||
|
* @deprecated use java.util.Collections.reverseOrder()
|
||||||
*/
|
*/
|
||||||
public static final Comparator REVERSE_COMPARABLE =
|
public static final Comparator REVERSE_COMPARABLE =
|
||||||
new ReversingComparator(COMPARABLE);
|
java.util.Collections.reverseOrder();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -352,7 +352,7 @@ public class QuickSort
|
|||||||
*/
|
*/
|
||||||
public static void rsort (ArrayList a)
|
public static void rsort (ArrayList a)
|
||||||
{
|
{
|
||||||
sort(a, Comparators.REVERSE_COMPARABLE);
|
sort(a, java.util.Collections.reverseOrder());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class SortableArrayList extends AbstractList
|
|||||||
*/
|
*/
|
||||||
public void rsort ()
|
public void rsort ()
|
||||||
{
|
{
|
||||||
sort(Comparators.REVERSE_COMPARABLE);
|
sort(java.util.Collections.reverseOrder());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user