From 6763e76e85933f20fa3969d97add31573f4559af Mon Sep 17 00:00:00 2001 From: Akarshit Wal Date: Mon, 13 Jul 2015 14:56:12 +0530 Subject: [PATCH] Added sorting to table --- .../app/contrib/ContributionListPanel.java | 99 ++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/app/src/processing/app/contrib/ContributionListPanel.java b/app/src/processing/app/contrib/ContributionListPanel.java index b8b509230..1d7303364 100644 --- a/app/src/processing/app/contrib/ContributionListPanel.java +++ b/app/src/processing/app/contrib/ContributionListPanel.java @@ -27,6 +27,7 @@ import java.util.*; import java.util.Map.Entry; import javax.swing.*; +import javax.swing.RowSorter.SortKey; import javax.swing.border.Border; import javax.swing.event.*; import javax.swing.table.*; @@ -115,6 +116,7 @@ public class ContributionListPanel extends JPanel implements Scrollable, Contrib table.setColumnSelectionAllowed(false); table.setCellSelectionEnabled(false); table.setAutoCreateColumnsFromModel(true); + table.setAutoCreateRowSorter(false); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel() .addListSelectionListener(new ListSelectionListener() { @@ -131,7 +133,10 @@ public class ContributionListPanel extends JPanel implements Scrollable, Contrib TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter); sorter.setComparator(1, contribListing.getComparator()); - + sorter.setSortable(0, false); + sorter.setSortable(2, false); + table.getTableHeader().setDefaultRenderer(new MyColumnHeaderRenderer()); + GroupLayout layout = new GroupLayout(this); layout.setHorizontalGroup(layout.createParallelGroup().addComponent(scrollPane)); layout.setVerticalGroup(layout.createSequentialGroup().addComponent(scrollPane)); @@ -140,6 +145,98 @@ public class ContributionListPanel extends JPanel implements Scrollable, Contrib table.setVisible(true); } + + class MyColumnHeaderRenderer extends DefaultTableCellRenderer { + + /** + * Constructs a DefaultTableHeaderCellRenderer. + *

+ * The horizontal alignment and text position are set as appropriate to a + * table header cell, and the opaque property is set to false. + */ + public MyColumnHeaderRenderer() { +// setHorizontalAlignment(CENTER); + setHorizontalTextPosition(LEFT); +// setVerticalAlignment(BOTTOM); + setOpaque(true); + } + + /** + * Returns the default table header cell renderer. + *

+ * If the column is sorted, the appropriate icon is retrieved from the + * current Look and Feel, and a border appropriate to a table header cell + * is applied. + *

+ * Subclasses may override this method to provide custom content or + * formatting. + * + * @param table the JTable. + * @param value the value to assign to the header cell + * @param isSelected This parameter is ignored. + * @param hasFocus This parameter is ignored. + * @param row This parameter is ignored. + * @param column the column of the header cell to render + * @return the default table header cell renderer + */ + @Override + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, + isSelected, hasFocus, row, column); + JTableHeader tableHeader = table.getTableHeader(); + if (tableHeader != null) { + setForeground(tableHeader.getForeground()); + } + setIcon(getIcon(table, column)); + setBackground(Color.WHITE); + setBorder(BorderFactory.createCompoundBorder(BorderFactory + .createMatteBorder(2, 0, 2, 0, Color.BLACK), BorderFactory + .createEmptyBorder(0, (column == 2 ? 17 : 0), 0, 0))); + return this; + } + + /** + * Overloaded to return an icon suitable to the primary sorted column, or null if + * the column is not the primary sort key. + * + * @param table the JTable. + * @param column the column index. + * @return the sort icon, or null if the column is unsorted. + */ + protected Icon getIcon(JTable table, int column) { + SortKey sortKey = getSortKey(table, column); + if (sortKey != null && table.convertColumnIndexToView(sortKey.getColumn()) == column) { + switch (sortKey.getSortOrder()) { + case ASCENDING: + return UIManager.getIcon("Table.ascendingSortIcon"); + case DESCENDING: + return UIManager.getIcon("Table.descendingSortIcon"); + } + } + 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; + } +} class StatusRendere extends DefaultTableCellRenderer {