Path: blob/master/src/java.desktop/share/classes/sun/swing/table/DefaultTableCellHeaderRenderer.java
41155 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing.table;2526import sun.swing.DefaultLookup;2728import java.awt.Component;29import java.awt.Color;30import java.awt.FontMetrics;31import java.awt.Graphics;32import java.awt.Insets;33import java.awt.Point;34import java.awt.Rectangle;35import java.io.Serializable;36import javax.swing.*;37import javax.swing.plaf.UIResource;38import javax.swing.border.Border;39import javax.swing.table.*;4041@SuppressWarnings("serial") // JDK-implementation class42public class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer43implements UIResource {44private boolean horizontalTextPositionSet;45private Icon sortArrow;46private EmptyIcon emptyIcon = new EmptyIcon();4748public DefaultTableCellHeaderRenderer() {49setHorizontalAlignment(JLabel.CENTER);50}5152public void setHorizontalTextPosition(int textPosition) {53horizontalTextPositionSet = true;54super.setHorizontalTextPosition(textPosition);55}5657public Component getTableCellRendererComponent(JTable table, Object value,58boolean isSelected, boolean hasFocus, int row, int column) {59Icon sortIcon = null;6061boolean isPaintingForPrint = false;6263if (table != null) {64JTableHeader header = table.getTableHeader();65if (header != null) {66Color fgColor = null;67Color bgColor = null;68if (hasFocus) {69fgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellForeground");70bgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellBackground");71}72if (fgColor == null) {73fgColor = header.getForeground();74}75if (bgColor == null) {76bgColor = header.getBackground();77}78setForeground(fgColor);79setBackground(bgColor);8081setFont(header.getFont());8283isPaintingForPrint = header.isPaintingForPrint();84}8586if (!isPaintingForPrint && table.getRowSorter() != null) {87if (!horizontalTextPositionSet) {88// There is a row sorter, and the developer hasn't89// set a text position, change to leading.90setHorizontalTextPosition(JLabel.LEADING);91}92SortOrder sortOrder = getColumnSortOrder(table, column);93if (sortOrder != null) {94switch(sortOrder) {95case ASCENDING:96sortIcon = DefaultLookup.getIcon(97this, ui, "Table.ascendingSortIcon");98break;99case DESCENDING:100sortIcon = DefaultLookup.getIcon(101this, ui, "Table.descendingSortIcon");102break;103case UNSORTED:104sortIcon = DefaultLookup.getIcon(105this, ui, "Table.naturalSortIcon");106break;107}108}109}110}111112setText(value == null ? "" : value.toString());113setIcon(sortIcon);114sortArrow = sortIcon;115116Border border = null;117if (hasFocus) {118border = DefaultLookup.getBorder(this, ui, "TableHeader.focusCellBorder");119}120if (border == null) {121border = DefaultLookup.getBorder(this, ui, "TableHeader.cellBorder");122}123setBorder(border);124125return this;126}127128public static SortOrder getColumnSortOrder(JTable table, int column) {129SortOrder rv = null;130if (table == null || table.getRowSorter() == null) {131return rv;132}133java.util.List<? extends RowSorter.SortKey> sortKeys =134table.getRowSorter().getSortKeys();135if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() ==136table.convertColumnIndexToModel(column)) {137rv = sortKeys.get(0).getSortOrder();138}139return rv;140}141142@Override143public void paintComponent(Graphics g) {144boolean b = DefaultLookup.getBoolean(this, ui,145"TableHeader.rightAlignSortArrow", false);146if (b && sortArrow != null) {147//emptyIcon is used so that if the text in the header is right148//aligned, or if the column is too narrow, then the text will149//be sized appropriately to make room for the icon that is about150//to be painted manually here.151emptyIcon.width = sortArrow.getIconWidth();152emptyIcon.height = sortArrow.getIconHeight();153setIcon(emptyIcon);154super.paintComponent(g);155Point position = computeIconPosition(g);156sortArrow.paintIcon(this, g, position.x, position.y);157} else {158super.paintComponent(g);159}160}161162private Point computeIconPosition(Graphics g) {163FontMetrics fontMetrics = g.getFontMetrics();164Rectangle viewR = new Rectangle();165Rectangle textR = new Rectangle();166Rectangle iconR = new Rectangle();167Insets i = getInsets();168viewR.x = i.left;169viewR.y = i.top;170viewR.width = getWidth() - (i.left + i.right);171viewR.height = getHeight() - (i.top + i.bottom);172SwingUtilities.layoutCompoundLabel(173this,174fontMetrics,175getText(),176sortArrow,177getVerticalAlignment(),178getHorizontalAlignment(),179getVerticalTextPosition(),180getHorizontalTextPosition(),181viewR,182iconR,183textR,184getIconTextGap());185int x = getWidth() - i.right - sortArrow.getIconWidth();186int y = iconR.y;187return new Point(x, y);188}189190@SuppressWarnings("serial") // JDK-implementation class191private class EmptyIcon implements Icon, Serializable {192int width = 0;193int height = 0;194public void paintIcon(Component c, Graphics g, int x, int y) {}195public int getIconWidth() { return width; }196public int getIconHeight() { return height; }197}198}199200201