J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.swing.table; |
| 26 | |
| 27 | import java.awt.Component; |
| 28 | import java.awt.Color; |
| 29 | import javax.swing.*; |
| 30 | import javax.swing.plaf.UIResource; |
| 31 | import javax.swing.border.Border; |
| 32 | import javax.swing.table.*; |
| 33 | |
| 34 | /** |
| 35 | */ |
| 36 | public class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer |
| 37 | implements UIResource { |
| 38 | private boolean horizontalTextPositionSet; |
| 39 | |
| 40 | public DefaultTableCellHeaderRenderer() { |
| 41 | setHorizontalAlignment(JLabel.CENTER); |
| 42 | } |
| 43 | |
| 44 | public void setHorizontalTextPosition(int textPosition) { |
| 45 | horizontalTextPositionSet = true; |
| 46 | super.setHorizontalTextPosition(textPosition); |
| 47 | } |
| 48 | |
| 49 | public Component getTableCellRendererComponent(JTable table, Object value, |
| 50 | boolean isSelected, boolean hasFocus, int row, int column) { |
| 51 | Icon sortIcon = null; |
| 52 | |
| 53 | boolean isPaintingForPrint = false; |
| 54 | |
| 55 | if (table != null) { |
| 56 | JTableHeader header = table.getTableHeader(); |
| 57 | if (header != null) { |
| 58 | Color fgColor = null; |
| 59 | Color bgColor = null; |
| 60 | if (hasFocus) { |
| 61 | fgColor = UIManager.getColor("TableHeader.focusCellForeground"); |
| 62 | bgColor = UIManager.getColor("TableHeader.focusCellBackground"); |
| 63 | } |
| 64 | if (fgColor == null) { |
| 65 | fgColor = header.getForeground(); |
| 66 | } |
| 67 | if (bgColor == null) { |
| 68 | bgColor = header.getBackground(); |
| 69 | } |
| 70 | setForeground(fgColor); |
| 71 | setBackground(bgColor); |
| 72 | |
| 73 | setFont(header.getFont()); |
| 74 | |
| 75 | isPaintingForPrint = header.isPaintingForPrint(); |
| 76 | } |
| 77 | |
| 78 | if (!isPaintingForPrint && table.getRowSorter() != null) { |
| 79 | if (!horizontalTextPositionSet) { |
| 80 | // There is a row sorter, and the developer hasn't |
| 81 | // set a text position, change to leading. |
| 82 | setHorizontalTextPosition(JLabel.LEADING); |
| 83 | } |
| 84 | SortOrder sortOrder = getColumnSortOrder(table, column); |
| 85 | if (sortOrder != null) { |
| 86 | switch(sortOrder) { |
| 87 | case ASCENDING: |
| 88 | sortIcon = UIManager.getIcon( |
| 89 | "Table.ascendingSortIcon"); |
| 90 | break; |
| 91 | case DESCENDING: |
| 92 | sortIcon = UIManager.getIcon( |
| 93 | "Table.descendingSortIcon"); |
| 94 | break; |
| 95 | case UNSORTED: |
| 96 | sortIcon = UIManager.getIcon( |
| 97 | "Table.naturalSortIcon"); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | setText(value == null ? "" : value.toString()); |
| 105 | setIcon(sortIcon); |
| 106 | |
| 107 | Border border = null; |
| 108 | if (hasFocus) { |
| 109 | border = UIManager.getBorder("TableHeader.focusCellBorder"); |
| 110 | } |
| 111 | if (border == null) { |
| 112 | border = UIManager.getBorder("TableHeader.cellBorder"); |
| 113 | } |
| 114 | setBorder(border); |
| 115 | |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | public static SortOrder getColumnSortOrder(JTable table, int column) { |
| 120 | SortOrder rv = null; |
| 121 | if (table.getRowSorter() == null) { |
| 122 | return rv; |
| 123 | } |
| 124 | java.util.List<? extends RowSorter.SortKey> sortKeys = |
| 125 | table.getRowSorter().getSortKeys(); |
| 126 | if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() == |
| 127 | table.convertColumnIndexToModel(column)) { |
| 128 | rv = sortKeys.get(0).getSortOrder(); |
| 129 | } |
| 130 | return rv; |
| 131 | } |
| 132 | } |