package com.onaro.util.jfc.grouping; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JTable; import javax.swing.UIManager; import com.onaro.util.jfc.Resources; import com.onaro.util.jfc.tables.filter.ColumnIndexConvertor; import com.onaro.util.jfc.tables.filter.FilterHeaderRenderer; import com.onaro.util.jfc.tables.filter.IFilterModel; class GroupingHeaderRenderer extends FilterHeaderRenderer { private static final long serialVersionUID = 1L; /** * A non negative number means that the current painted column is used for sorting. It also * indicates the precedence of this column over other column. Zero means this is the first compared * column. negative value means this is a none sorting column. */ private int sortingPrecendence; private boolean ascending; private int nextPrecedence; private GroupingTable groupingTable; private static Font precedenceFont; public GroupingHeaderRenderer(GroupingTable groupingTable, IFilterModel filterModel, ColumnIndexConvertor columnIndexConvertor) { super(filterModel, columnIndexConvertor); this.groupingTable = groupingTable; if (precedenceFont == null) { precedenceFont = UIManager.getFont("TableHeader.font").deriveFont(9f); //$NON-NLS-1$ } } 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); int columnInModel = table.convertColumnIndexToModel(column); sortingPrecendence = groupingTable.getGroupingModel().getSortingPrecedence(columnInModel); if (sortingPrecendence >= 0) { ascending = groupingTable.getGroupingModel().isAscendingSorting(columnInModel); } nextPrecedence = groupingTable.getGroupingModel().getNextAppendColumnPosition(); return this; } protected void setValue(Object value) { if (value instanceof Icon) { setIcon((Icon)value); setText(null); } else { setText((value == null) ? null : value.toString()); setIcon(null); } } public void paint(Graphics g) { super.paint(g); if (sortingPrecendence >= 0) { Icon icon = ascending ? Resources.INSTANCE.getAscendingSortIcon() : Resources.INSTANCE.getDescendingSortIcon(); icon.paintIcon(this, g, getWidth() - icon.getIconWidth() - 2, getHeight() - icon.getIconHeight() - 2); if (nextPrecedence > 1) { Font font = g.getFont(); g.setFont(precedenceFont); g.drawString(String.valueOf(sortingPrecendence + 1),getWidth() - 9, 7); g.setFont(font); } } } }