001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.util.Arrays;
021: import java.util.Comparator;
022:
023: import javax.swing.event.TableModelEvent;
024: import javax.swing.event.TableModelListener;
025: import javax.swing.table.AbstractTableModel;
026:
027: public class TableModelSorter extends AbstractTableModel implements
028: TableModelListener {
029:
030: public final static boolean SORT_DESCENDING = true;
031:
032: public final static boolean SORT_ASCENDING = false;
033:
034: protected SortableTableModel model;
035:
036: protected Integer index[];
037:
038: /**
039: * Physically sort table data.
040: *
041: * @param column
042: * the column you want to sort for
043: * @param descending
044: * sort it descending or ascending
045: */
046: public TableModelSorter(SortableTableModel model) {
047: setModel(model);
048: }
049:
050: public SortableTableModel getModel() {
051: return model;
052: }
053:
054: public void setModel(SortableTableModel model) {
055: this .model = model;
056: model.addTableModelListener(this );
057: initIndex();
058: }
059:
060: public void sort(final int column, final boolean descending) {
061: if (getModel().isColumnSortable(column)) {
062: Arrays.sort(index, new Comparator() {
063: public int compare(Object o1, Object o2) {
064: int row1 = ((Integer) o1).intValue();
065: int row2 = ((Integer) o2).intValue();
066: Object c1 = getModel().getValueAt(row1, column);
067: Object c2 = getModel().getValueAt(row2, column);
068: String s1 = getModel().getComparable(c1, column);
069: String s2 = getModel().getComparable(c2, column);
070: return (descending ? s1.compareToIgnoreCase(s2)
071: : s2.compareToIgnoreCase(s1));
072: }
073: });
074: // CAUTION: must not be fireTableStructureChanged() as this resets
075: // the columns widths
076: fireTableDataChanged();
077: }
078: }
079:
080: public void resetSort() {
081: initIndex();
082: }
083:
084: public Object getValueAt(int aRow, int aColumn) {
085: return model.getValueAt(indirectRow(aRow), aColumn);
086: }
087:
088: public void setValueAt(Object aValue, int aRow, int aColumn) {
089: model.setValueAt(aValue, indirectRow(aRow), aColumn);
090: }
091:
092: public int getRowCount() {
093: return (model == null) ? 0 : model.getRowCount();
094: }
095:
096: public int getColumnCount() {
097: return (model == null) ? 0 : model.getColumnCount();
098: }
099:
100: public String getColumnName(int aColumn) {
101: return model.getColumnName(aColumn);
102: }
103:
104: public Class getColumnClass(int aColumn) {
105: return model.getColumnClass(aColumn);
106: }
107:
108: public boolean isCellEditable(int row, int column) {
109: return model.isCellEditable(indirectRow(row), column);
110: }
111:
112: public void tableChanged(TableModelEvent e) {
113: fireTableChanged(e);
114: initIndex();
115: }
116:
117: private void initIndex() {
118: int rowCount = getModel().getRowCount();
119:
120: // Set up a new array of indexes with the right number of elements
121: // for the new data model.
122: index = new Integer[rowCount];
123:
124: // Initialise with the identity mapping.
125: for (int row = 0; row < rowCount; row++) {
126: index[row] = new Integer(row);
127: }
128: }
129:
130: private int indirectRow(int row) {
131: return index[row].intValue();
132: }
133:
134: }
|