📄 tablesorter.java
字号:
} } return modelToView; } // TableModel interface methods public int getRowCount() { return (tableModel == null) ? 0 : tableModel.getRowCount(); } public int getColumnCount() { return (tableModel == null) ? 0 : tableModel.getColumnCount(); } public String getColumnName(int column) { return tableModel.getColumnName(column); } public Class getColumnClass(int column) { return tableModel.getColumnClass(column); } public boolean isCellEditable(int row, int column) { return tableModel.isCellEditable(modelIndex(row), column); } public Object getValueAt(int row, int column) { if (row >= getRowCount() || column >= getColumnCount()) { return null; } return tableModel.getValueAt(modelIndex(row), column); } public void setValueAt(Object aValue, int row, int column) { tableModel.setValueAt(aValue, modelIndex(row), column); } // Helper classes private class Row implements Comparable { private int modelIndex; public Row(int index) { this.modelIndex = index; } public int compareTo(Object o) { int row1 = modelIndex; int row2 = ((Row) o).modelIndex; for (Iterator it = sortingColumns.iterator(); it.hasNext();) { Directive directive = (Directive) it.next(); 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 { Class type = tableModel.getColumnClass(column); if (columnComparators.containsKey(type)) comparison = ((Comparator)columnComparators.get(type)).compare(o1, o2); else comparison = compareByColumn(type, o1, o2); // comparison = getComparator(column).compare(o1, o2); } if (comparison != 0) return directive.direction == DESCENDING ? -comparison : comparison; } return 0; } private int compareByColumn(Class type, Object o1, Object o2) { if (type.getSuperclass() == java.lang.Number.class) { Number n1 = (Number)o1; double d1 = n1.doubleValue(); Number n2 = (Number)o2; double d2 = n2.doubleValue(); if (d1 < d2) return -1; else if (d1 > d2) return 1; else return 0; } else if (type == java.util.Date.class) { Date d1 = (Date)o1; long n1 = d1.getTime(); Date d2 = (Date)o2; long n2 = d2.getTime(); if (n1 < n2) return -1; else if (n1 > n2) return 1; else return 0; } else if (type == String.class) { String s1 = (String)o1; String s2 = (String)o2; int result = s1.compareTo(s2); if (result < 0) return -1; else if (result > 0) return 1; else return 0; } else if (type == Boolean.class) { Boolean bool1 = (Boolean)o1; boolean b1 = bool1.booleanValue(); Boolean bool2 = (Boolean)o2; boolean b2 = bool2.booleanValue(); if (b1 == b2) return 0; else if (b1) // Define false < true return 1; else return -1; } else { try { Number n1 = (Number)o1; double d1 = n1.doubleValue(); Number n2 = (Number)o2; double d2 = n2.doubleValue(); if (d1 < d2) return -1; else if (d1 > d2) return 1; else return 0; } catch (ClassCastException cExc) { Object v1 = o1; String s1 = v1.toString(); Object v2 = o2; String s2 = v2.toString(); int result = s1.compareTo(s2); if (result < 0) return -1; else if (result > 0) return 1; else return 0; } } } } // class Row private class TableModelHandler implements TableModelListener { public void tableChanged(TableModelEvent e) { // If we're not sorting by anything, just pass the event along. if (!isSorting()) { clearSortingState(); fireTableChanged(e); return; } // If the table structure has changed, cancel the sorting; the // sorting columns may have been either moved or deleted from // the model. if (e.getFirstRow() == TableModelEvent.HEADER_ROW) { cancelSorting(); fireTableChanged(e); return; } // We can map a cell event through to the view without widening // when the following conditions apply: // // a) all the changes are on one row (e.getFirstRow() == e.getLastRow()) and, // b) all the changes are in one column (column != TableModelEvent.ALL_COLUMNS) and, // c) we are not sorting on that column (getSortingStatus(column) == NOT_SORTED) and, // d) a reverse lookup will not trigger a sort (modelToView != null) // // Note: INSERT and DELETE events fail this test as they have column == ALL_COLUMNS. // // The last check, for (modelToView != null) is to see if modelToView // is already allocated. If we don't do this check; sorting can become // a performance bottleneck for applications where cells // change rapidly in different parts of the table. If cells // change alternately in the sorting column and then outside of // it this class can end up re-sorting on alternate cell updates - // which can be a performance problem for large tables. The last // clause avoids this problem. int column = e.getColumn(); if (e.getFirstRow() == e.getLastRow() && column != TableModelEvent.ALL_COLUMNS && getSortingStatus(column) == NOT_SORTED && modelToView != null) { int viewIndex = getModelToView()[e.getFirstRow()]; fireTableChanged(new TableModelEvent(TableSorter.this, viewIndex, viewIndex, column, e.getType())); return; } // Something has happened to the data that may have invalidated the row order. clearSortingState(); fireTableDataChanged(); return; } } private class MouseHandler extends MouseAdapter { int column, status; public void mouseClicked(MouseEvent e) { JTableHeader h = (JTableHeader)e.getSource(); TableColumnModel columnModel = h.getColumnModel(); int viewColumn = columnModel.getColumnIndexAtX(e.getX()); column = columnModel.getColumn(viewColumn).getModelIndex(); if (column != -1) { status = getSortingStatus(column); if (!e.isControlDown()) cancelSorting(); // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. status = status + (e.isShiftDown() ? -1 : 1); status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1} SwingWorker worker = new SwingWorker() { public Object construct() { setSortingStatus(column, status); return "done"; } }; worker.start(); } } } // class MouseHandler /* private class SortableHeaderRenderer extends JButton implements TableCellRenderer { public SortableHeaderRenderer() { setMargin(btnMargin); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { String label = value.toString(); setText(label); setToolTipText(label); setHorizontalTextPosition(SwingConstants.LEFT); int modelColumn = table.convertColumnIndexToModel(column); setIcon(getHeaderRendererIcon(modelColumn, getFont().getSize())); return this; } } // class SortableHeaderRenderer */ private static class Directive { private int column; private int direction; public Directive(int column, int direction) { this.column = column; this.direction = direction; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -