| /* |
| * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| * |
| * This code is free software; you can redistribute it and/or modify it |
| * under the terms of the GNU General Public License version 2 only, as |
| * published by the Free Software Foundation. Sun designates this |
| * particular file as subject to the "Classpath" exception as provided |
| * by Sun in the LICENSE file that accompanied this code. |
| * |
| * This code is distributed in the hope that it will be useful, but WITHOUT |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| * version 2 for more details (a copy is included in the LICENSE file that |
| * accompanied this code). |
| * |
| * You should have received a copy of the GNU General Public License version |
| * 2 along with this work; if not, write to the Free Software Foundation, |
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| * |
| * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| * CA 95054 USA or visit www.sun.com if you need additional information or |
| * have any questions. |
| */ |
| package sun.swing.table; |
| |
| import java.awt.Component; |
| import java.awt.Color; |
| import javax.swing.*; |
| import javax.swing.plaf.UIResource; |
| import javax.swing.border.Border; |
| import javax.swing.table.*; |
| |
| /** |
| */ |
| public class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer |
| implements UIResource { |
| private boolean horizontalTextPositionSet; |
| |
| public DefaultTableCellHeaderRenderer() { |
| setHorizontalAlignment(JLabel.CENTER); |
| } |
| |
| public void setHorizontalTextPosition(int textPosition) { |
| horizontalTextPositionSet = true; |
| super.setHorizontalTextPosition(textPosition); |
| } |
| |
| 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); |
| setBackground(bgColor); |
| |
| 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); |
| } |
| SortOrder sortOrder = getColumnSortOrder(table, column); |
| if (sortOrder != null) { |
| switch(sortOrder) { |
| case ASCENDING: |
| sortIcon = UIManager.getIcon( |
| "Table.ascendingSortIcon"); |
| break; |
| case DESCENDING: |
| sortIcon = UIManager.getIcon( |
| "Table.descendingSortIcon"); |
| break; |
| case UNSORTED: |
| sortIcon = UIManager.getIcon( |
| "Table.naturalSortIcon"); |
| break; |
| } |
| } |
| } |
| } |
| |
| setText(value == null ? "" : value.toString()); |
| setIcon(sortIcon); |
| |
| Border border = null; |
| if (hasFocus) { |
| border = UIManager.getBorder("TableHeader.focusCellBorder"); |
| } |
| if (border == null) { |
| border = UIManager.getBorder("TableHeader.cellBorder"); |
| } |
| setBorder(border); |
| |
| return this; |
| } |
| |
| public static SortOrder getColumnSortOrder(JTable table, int column) { |
| SortOrder rv = null; |
| if (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; |
| } |
| } |