Comparable -> Comparator. private -> protected.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2720 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -252,7 +252,34 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isSorting()) {
|
if (isSorting()) {
|
||||||
Arrays.sort(viewToModel);
|
Arrays.sort(viewToModel, new Comparator<Row>() {
|
||||||
|
public int compare (Row one, Row two) {
|
||||||
|
int row1 = one.modelIndex;
|
||||||
|
int row2 = two.modelIndex;
|
||||||
|
|
||||||
|
for (Directive directive : sortingColumns) {
|
||||||
|
int column = directive.column;
|
||||||
|
Object o1 = tableModel.getValueAt(row1, column);
|
||||||
|
Object o2 = tableModel.getValueAt(row2, column);
|
||||||
|
|
||||||
|
int comparison = 0;
|
||||||
|
// Define null less than everything, except null.
|
||||||
|
if (o1 == null && o2 == null) {
|
||||||
|
comparison = 0;
|
||||||
|
} else if (o1 == null) {
|
||||||
|
comparison = -1;
|
||||||
|
} else if (o2 == null) {
|
||||||
|
comparison = 1;
|
||||||
|
} else {
|
||||||
|
comparison = getComparator(column).compare(o1, o2);
|
||||||
|
}
|
||||||
|
if (comparison != 0) {
|
||||||
|
return directive.direction == DESCENDING ? -comparison : comparison;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return viewToModel;
|
return viewToModel;
|
||||||
@@ -309,42 +336,14 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
|
|
||||||
// Helper classes
|
// Helper classes
|
||||||
|
|
||||||
private class Row implements Comparable<Row> {
|
protected class Row {
|
||||||
private int modelIndex;
|
public final int modelIndex;
|
||||||
|
public Row (int index) {
|
||||||
public Row(int index) {
|
|
||||||
this.modelIndex = index;
|
this.modelIndex = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compareTo(Row orow) {
|
|
||||||
int row1 = modelIndex;
|
|
||||||
int row2 = orow.modelIndex;
|
|
||||||
|
|
||||||
for (Directive directive : sortingColumns) {
|
|
||||||
int column = directive.column;
|
|
||||||
Object o1 = tableModel.getValueAt(row1, column);
|
|
||||||
Object o2 = tableModel.getValueAt(row2, column);
|
|
||||||
|
|
||||||
int comparison = 0;
|
|
||||||
// Define null less than everything, except null.
|
|
||||||
if (o1 == null && o2 == null) {
|
|
||||||
comparison = 0;
|
|
||||||
} else if (o1 == null) {
|
|
||||||
comparison = -1;
|
|
||||||
} else if (o2 == null) {
|
|
||||||
comparison = 1;
|
|
||||||
} else {
|
|
||||||
comparison = getComparator(column).compare(o1, o2);
|
|
||||||
}
|
|
||||||
if (comparison != 0) {
|
|
||||||
return directive.direction == DESCENDING ? -comparison : comparison;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TableModelHandler implements TableModelListener {
|
protected class TableModelHandler implements TableModelListener {
|
||||||
public void tableChanged(TableModelEvent e) {
|
public void tableChanged(TableModelEvent e) {
|
||||||
// If we're not sorting by anything, just pass the event along.
|
// If we're not sorting by anything, just pass the event along.
|
||||||
if (!isSorting()) {
|
if (!isSorting()) {
|
||||||
@@ -399,7 +398,7 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MouseHandler extends MouseAdapter {
|
protected class MouseHandler extends MouseAdapter {
|
||||||
@Override public void mouseClicked(MouseEvent e) {
|
@Override public void mouseClicked(MouseEvent e) {
|
||||||
JTableHeader h = (JTableHeader) e.getSource();
|
JTableHeader h = (JTableHeader) e.getSource();
|
||||||
TableColumnModel columnModel = h.getColumnModel();
|
TableColumnModel columnModel = h.getColumnModel();
|
||||||
@@ -419,10 +418,10 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Arrow implements Icon {
|
protected static class Arrow implements Icon {
|
||||||
private boolean descending;
|
protected boolean descending;
|
||||||
private int size;
|
protected int size;
|
||||||
private int priority;
|
protected int priority;
|
||||||
|
|
||||||
public Arrow(boolean descending, int size, int priority) {
|
public Arrow(boolean descending, int size, int priority) {
|
||||||
this.descending = descending;
|
this.descending = descending;
|
||||||
@@ -472,8 +471,8 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SortableHeaderRenderer implements TableCellRenderer {
|
protected class SortableHeaderRenderer implements TableCellRenderer {
|
||||||
private TableCellRenderer tableCellRenderer;
|
protected TableCellRenderer tableCellRenderer;
|
||||||
|
|
||||||
public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) {
|
public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) {
|
||||||
this.tableCellRenderer = tableCellRenderer;
|
this.tableCellRenderer = tableCellRenderer;
|
||||||
@@ -497,9 +496,9 @@ public class TableSorter extends AbstractTableModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Directive {
|
protected static class Directive {
|
||||||
private int column;
|
protected int column;
|
||||||
private int direction;
|
protected int direction;
|
||||||
|
|
||||||
public Directive(int column, int direction) {
|
public Directive(int column, int direction) {
|
||||||
this.column = column;
|
this.column = column;
|
||||||
|
|||||||
Reference in New Issue
Block a user