下面列出了javax.swing.RowSorter#getSortKeys ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static TableState createMetaState(final JTable table, final boolean selectedOnly) {
final TableState state = new TableState(table, selectedOnly);
final GraphTableModel tm = (GraphTableModel) table.getModel();
for (int i = 0; i < table.getColumnCount(); i++) {
final TableColumn tc = table.getColumnModel().getColumn(i);
final int modelIndex = tc.getModelIndex();
final Attribute attr = tm.getAttribute(modelIndex);
final String label = attr.getName();
final ColumnState cs = new ColumnState(label, tm.getSegment(modelIndex), tc.getWidth());
state.columns.add(cs);
}
final RowSorter<? extends TableModel> sorter = table.getRowSorter();
for (final RowSorter.SortKey sk : sorter.getSortKeys()) {
// TODO: should really store the column label + segment here.
state.sortOrder.add(String.format("%d,%s", sk.getColumn(), sk.getSortOrder()));
}
return state;
}
/**
* Tries to return the sort key for the given column.
*
* @param sorter
* @param column
* @return the sort key or {@code null}
*/
private SortKey getSortKey(RowSorter<? extends TableModel> sorter, int column) {
if (sorter == null) {
return null;
}
for (Object sortObj : sorter.getSortKeys()) {
SortKey key = (SortKey) sortObj;
if (key.getColumn() == column) {
return key;
}
}
return null;
}
/**
* Returns the current sort key, or null if the column is unsorted.
*
* @param table the table
* @param column the column index
* @return the SortKey, or null if the column is unsorted
*/
protected SortKey getSortKey(JTable table, int column) {
RowSorter<?> rowSorter = table.getRowSorter();
if (rowSorter == null) {
return null;
}
List<?> sortedColumns = rowSorter.getSortKeys();
if (sortedColumns.size() > 0) {
return (SortKey) sortedColumns.get(0);
}
return null;
}
/**
* @see com.seaglasslookandfeel.ui.SeaGlassTableHeaderUI$DefaultTableCellHeaderRenderer#getTableCellRendererComponent(javax.swing.JTable,
* java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
boolean hasRollover = false; // (column == getRolloverColumn());
if (isSelected || hasRollover || hasFocus) {
SeaGlassLookAndFeel.setSelectedUI((SeaGlassLabelUI) SeaGlassLookAndFeel.getUIOfType(getUI(), SeaGlassLabelUI.class),
isSelected, hasFocus, table.isEnabled(), hasRollover);
} else {
SeaGlassLookAndFeel.resetSelectedUI();
}
// Stuff a variable into the client property of this renderer
// indicating the sort order, so that different rendering can be
// done for the header based on sorted state.
RowSorter rs = table == null ? null : table.getRowSorter();
java.util.List<? extends RowSorter.SortKey> sortKeys = rs == null ? null : rs.getSortKeys();
if (sortKeys != null && sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
putClientProperty("Table.sortOrder", "ASCENDING");
break;
case DESCENDING:
putClientProperty("Table.sortOrder", "DESCENDING");
break;
case UNSORTED:
putClientProperty("Table.sortOrder", "UNSORTED");
break;
default:
throw new AssertionError("Cannot happen");
}
} else {
putClientProperty("Table.sortOrder", "UNSORTED");
}
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return this;
}