下面列出了javax.swing.RowSorter#SortKey ( ) 实例代码,或者点击链接到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;
}
/**
* Overridden to return an icon suitable to a sorted column, or null if the column is unsorted.
* The icon for the primary sorted column is fully opaque, and the opacity is reduced by a
* factor of <code>alpha</code> for each subsequent sort index.
*
* @param table the <code>JTable</code>.
* @param column the column index.
* @return the sort icon with appropriate opacity, or null if the column is unsorted.
*/
public Icon getIcon(JTable table, int column) {
float computedAlpha = 1.0F;
for (RowSorter.SortKey sortKey : table.getRowSorter().getSortKeys()) {
if (table.convertColumnIndexToView(sortKey.getColumn()) == column) {
switch (sortKey.getSortOrder()) {
case ASCENDING:
return new AlphaIcon(UIManager.getIcon("Table.ascendingSortIcon"), computedAlpha);
case DESCENDING:
return new AlphaIcon(UIManager.getIcon("Table.descendingSortIcon"), computedAlpha);
}
}
computedAlpha *= alpha;
}
return null;
}
/**
* Adjust possible sort keys to new column order.
*/
private List<? extends RowSorter.SortKey> adjustSortKeys(List<? extends RowSorter.SortKey> sortKeys) {
List<RowSorter.SortKey> currentKeys = new ArrayList<>(sortKeys);
List<RowSorter.SortKey> newKeys = new ArrayList<>(currentKeys.size());
for (RowSorter.SortKey sortKey : currentKeys) {
int column = sortKey.getColumn();
RowSorter.SortKey newSortKey;
if (column == 1) {
newSortKey = new RowSorter.SortKey(3, sortKey.getSortOrder());
} else if (column == 3) {
newSortKey = new RowSorter.SortKey(1, sortKey.getSortOrder());
} else {
newSortKey = sortKey;
}
newKeys.add(newSortKey);
}
return newKeys;
}
public void setSortKeys(List newKeys) {
if (newKeys == null || newKeys.isEmpty()) {
setSortKeysImpl(newKeys);
return;
}
RowSorter.SortKey oldKey = getSortKey();
RowSorter.SortKey newKey = (RowSorter.SortKey)newKeys.get(0);
if (oldKey == null || oldKey.getColumn() != newKey.getColumn()) {
// Use defined initial SortOrder for a newly sorted column
setSortColumn(newKey.getColumn());
} else {
setSortKeysImpl(newKeys);
}
}
public static SortOrder getColumnSortOrder(JTable table, int column)
{
SortOrder rv = null;
if (table == null || table.getRowSorter() == null)
{
return rv;
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table
.getRowSorter().getSortKeys();
if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table
.convertColumnIndexToModel(column))
{
rv = sortKeys.get(0).getSortOrder();
}
return rv;
}
void setSortKey(RowSorter.SortKey key) {
RowSorter.SortKey secondaryKey = secondarySortColumn == -1 ||
secondarySortColumn == key.getColumn() ? null :
new RowSorter.SortKey(secondarySortColumn,
getDefaultSortOrder(secondarySortColumn));
setSortKeysImpl(secondaryKey == null ? Arrays.asList(key) :
Arrays.asList(key, secondaryKey));
}
/**
* Reverses the sort order from ascending to descending (or descending
* to ascending) if the specified column is already the primary sorted
* column; otherwise, makes the specified column the primary sorted
* column, with an ascending sort order. If the specified column is not
* sortable, this method has no effect.
*
* @param column index of the column to make the primary sorted column,
* in terms of the underlying model
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public void toggleSortOrder(int column)
{
List<RowSorter.SortKey> keys = new ArrayList<>(getSortKeys());
RowSorter.SortKey sortKey;
int sortIndex;
for (sortIndex = keys.size() - 1; sortIndex >= 0; sortIndex--)
{
if (keys.get(sortIndex).getColumn() == column)
{
break;
}
}
if (sortIndex == -1)
{
// Key doesn't exist
sortKey = new RowSorter.SortKey(column, SortOrder.ASCENDING);
keys.add(0, sortKey);
}
else if (sortIndex == 0)
{
// It's the primary sorting key, toggle it
keys.set(0, toggle(keys.get(0)));
}
else
{
// It's not the first, but was sorted on, remove old
// entry, insert as first with ascending.
keys.remove(sortIndex);
keys.add(0, new RowSorter.SortKey(column, SortOrder.ASCENDING));
}
if (keys.size() > 2)
{
keys = keys.subList(0, 2);
}
setSortKeys(keys);
}
private RowSorter.SortKey toggle(RowSorter.SortKey key)
{
if (key.getSortOrder() == SortOrder.ASCENDING)
{
return new RowSorter.SortKey(key.getColumn(), SortOrder.DESCENDING);
}
return new RowSorter.SortKey(key.getColumn(), SortOrder.ASCENDING);
}
private RowSorter.SortKey toggle(RowSorter.SortKey key)
{
if (key.getSortOrder() == SortOrder.ASCENDING)
{
return new RowSorter.SortKey(key.getColumn(), SortOrder.DESCENDING);
}
return new RowSorter.SortKey(key.getColumn(), SortOrder.ASCENDING);
}
/**
* Convert the Order from SortKey to Page.Order
* @param key the SortKey
* @return the Page order
*/
private Page.Order converSortOrder(RowSorter.SortKey key) {
Page.Order order = Order.ASC;
if (key.getSortOrder() == SortOrder.DESCENDING) {
order = Order.DESC;
}
return order;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
SupportTableModel model = (SupportTableModel) table.getModel();
ImageIcon icon = model.getColumnIcon((String) value);
BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
Graphics2D g2d = i.createGraphics();
// setIcon(sortIcon);
if (icon != null) {
icon.paintIcon(this, g2d, 0, 0);
setText("");
if (sortIcon != null) {
g2d.setColor(getBackground());
g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
}
setIcon(new ImageIcon(i));
} else {
setIcon(sortIcon);
setText(value == null ? "" : value.toString());
}
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
int getSortColumn() {
RowSorter.SortKey key = getSortKey();
return key == null ? -1 : key.getColumn();
}
public LoggerViewPanel() {
model = new LogTableModel();
cboChooseLevel = UITools.createCombobox(new Level[] {null, Level.INFO, Level.ERROR, Level.DEBUG, Level.TRACE });
JPanel panel = new JPanel();
table = new JXTable(model);
btnRefresh = new JButton(MTGConstants.ICON_REFRESH);
t = new Timer(1000, e -> model.fireTableDataChanged());
chckbxAutorefresh = new JCheckBox("Auto-refresh");
setLayout(new BorderLayout(0, 0));
add(new JScrollPane(table), BorderLayout.CENTER);
add(panel, BorderLayout.NORTH);
panel.add(chckbxAutorefresh);
panel.add(btnRefresh);
panel.add(cboChooseLevel);
datesorter = new TableRowSorter<>(table.getModel());
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
int columnIndexToSort = 1;
sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.DESCENDING));
datesorter.setSortKeys(sortKeys);
table.setRowSorter(datesorter);
btnRefresh.addActionListener(ae -> model.fireTableDataChanged());
chckbxAutorefresh.addItemListener(ie -> {
if (chckbxAutorefresh.isSelected()) {
t.start();
btnRefresh.setEnabled(false);
} else {
t.stop();
btnRefresh.setEnabled(true);
}
});
cboChooseLevel.addActionListener(ae->{
if(cboChooseLevel.getSelectedItem()!=null)
{
TableRowSorter<LogTableModel> sorter = new TableRowSorter<>(model);
sorter.setRowFilter(RowFilter.regexFilter(cboChooseLevel.getSelectedItem().toString()));
table.setRowSorter(sorter);
}
else
{
table.setRowSorter(datesorter);
}
});
table.packAll();
}
@Override
public void setSortKeys(List<? extends RowSorter.SortKey> keys)
{
sortKeys = keys;
sort();
}
public List<? extends RowSorter.SortKey> getSortKeys() {
return sortKeys;
}
HeapViewerTreeTable(ProfilerTreeTableModel model, List<? extends RowSorter.SortKey> sortKeys) {
super(model, true, true, new int[] { 0 });
setRootVisible(false);
setShowsRootHandles(true);
setShadeUnfocusedSelection(true);
setForgetPreviouslyExpanded(true);
setAllowsThreeStateColumns(true);
getRowSorter().setSortKeys(sortKeys);
getSelectionModel().addListSelectionListener(new ListSelectionListener() {
private HeapViewerNode currentSelected;
private boolean currentAdjusting;
private boolean adjustingNull;
public void valueChanged(ListSelectionEvent e) {
// System.err.println(">>> selected " + getSelectedNode() + " adjusting " + e.getValueIsAdjusting());
// Ignore changes during sorting, will be handled separately in willBeSorted()
if (sorting) return;
HeapViewerNode node = getSelectedNode();
boolean adjusting = e.getValueIsAdjusting();
// workaround for noise created by restoring selection on internal model updates
// leading nulls which are adjusting mean that following non-adjusting null should be skipped
if (node == null) {
if (adjusting) {
adjustingNull = true;
return;
} else if (adjustingNull) {
adjustingNull = false;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// No node selected after leading adjusting nulls, selection not restored
if (getSelectedNode() == null) nodeSelected(null, false);
}
});
return;
}
} else {
adjustingNull = false;
}
// workaround for noise created by restoring selection on internal model updates
// ignore the same selection which is a result of clearing noise nulls
if (Objects.equals(currentSelected, node) && currentAdjusting == adjusting) return;
currentSelected = node;
currentAdjusting = adjusting;
nodeSelected(node, adjusting);
}
});
initializing = false;
}
@Override
public int compare(Row o1, Row o2)
{
for (RowSorter.SortKey key : keys)
{
if (key.getSortOrder() == SortOrder.UNSORTED)
{
continue;
}
int column = key.getColumn();
Comparator comparator = comparators[column];
Object obj1 = o1.getValueAt(column);
Object obj2 = o2.getValueAt(column);
int ret;
if (obj1 == null)
{
if (obj2 == null)
{
ret = 0;
}
else
{
ret = -1;
}
}
else if (obj2 == null)
{
ret = 1;
}
else
{
ret = comparator.compare(obj1, obj2);
}
if (key.getSortOrder() == SortOrder.DESCENDING)
{
ret *= -1;
}
if (ret != 0)
{
return ret;
}
}
return 0;
}
/**
* @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;
}
public void setSorting(int column, SortOrder sortOrder) {
if (isSortable()) {
RowSorter.SortKey sortKey = new RowSorter.SortKey(column, sortOrder);
_getRowSorter().setSortKeysImpl(Collections.singletonList(sortKey));
}
}
protected void willBeSorted(List<? extends RowSorter.SortKey> sortKeys) {}