package com.onaro.util.jfc.tables; import java.awt.Component; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.table.JTableHeader; import com.onaro.util.jfc.Resources; /** * A cell renderer for painting column headers with an optional arrow that * indicated the sorting order. It extends the {@link ResourceCellRenderer} * class to support rendering of {@link com.onaro.util.jfc.ResourceCommonProperties} objects. */ public class SortingHeaderRenderer extends ResourceCellRenderer { private static final long serialVersionUID = 1L; /** * Tells which column is th sorting column and in what order. */ private SortingInfo sortingInfo; /** * Holds the arrow icon for the current cell. If null than now arrow will * be painted. */ Icon icon; /** * Sets center alignment. * * @param sortable tells which column is th sorting column and in what order */ public SortingHeaderRenderer(SortingInfo sortable) { this.sortingInfo = sortable; setHorizontalAlignment(JLabel.CENTER); } public SortingInfo getSortingInfo() { return sortingInfo; } public void setSortingInfo(SortingInfo sortingInfo) { this.sortingInfo = sortingInfo; } /** * Paint the label and than the icon (if needed to). * * @param g the graphic context */ public void paint(Graphics g) { super.paint(g); if (icon != null) icon.paintIcon(this, g, getWidth() - icon.getIconWidth() - 2, getHeight() - icon.getIconHeight() - 2); } /** * When preparing the renderer, according the rendered column and the current * sorting, decide which arrow icon to use. * * @param table the table * @param value the value to render * @param isSelected true if the cell is selected * @param hasFocus true if the cell has focus * @param row the row number * @param column the column number * @return this renderer */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value == null) value = " "; //$NON-NLS-1$ super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); /** * The following section was copied from JTableHeader */ JTableHeader header = table.getTableHeader(); if (header != null) { setForeground(header.getForeground()); setBackground(header.getBackground()); setFont(header.getFont()); } if (table.convertColumnIndexToModel(column) == sortingInfo.getSortingColumn()) { if (sortingInfo.isAscendingSorting(table.convertColumnIndexToModel(column))) { icon = Resources.INSTANCE.getDescendingSortIcon(); } else { icon = Resources.INSTANCE.getAscendingSortIcon(); } } else { icon = null; } setBorder(UIManager.getBorder("TableHeader.cellBorder")); //$NON-NLS-1$ /** * Use the following in order to use the background decoration provided * by the L&F. * super.setOpaque(false); * super.setBackground(null); */ setHorizontalAlignment(JLabel.CENTER); setForeground(UIManager.getColor("TableHeader.foreground")); //$NON-NLS-1$ setBackground(UIManager.getColor("TableHeader.background")); //$NON-NLS-1$ return this; } }