Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/swing/table/DefaultTableCellHeaderRenderer.java
38830 views
/*1* Copyright (c) 2005, 2010, 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.*;4041public class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer42implements UIResource {43private boolean horizontalTextPositionSet;44private Icon sortArrow;45private EmptyIcon emptyIcon = new EmptyIcon();4647public DefaultTableCellHeaderRenderer() {48setHorizontalAlignment(JLabel.CENTER);49}5051public void setHorizontalTextPosition(int textPosition) {52horizontalTextPositionSet = true;53super.setHorizontalTextPosition(textPosition);54}5556public Component getTableCellRendererComponent(JTable table, Object value,57boolean isSelected, boolean hasFocus, int row, int column) {58Icon sortIcon = null;5960boolean isPaintingForPrint = false;6162if (table != null) {63JTableHeader header = table.getTableHeader();64if (header != null) {65Color fgColor = null;66Color bgColor = null;67if (hasFocus) {68fgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellForeground");69bgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellBackground");70}71if (fgColor == null) {72fgColor = header.getForeground();73}74if (bgColor == null) {75bgColor = header.getBackground();76}77setForeground(fgColor);78setBackground(bgColor);7980setFont(header.getFont());8182isPaintingForPrint = header.isPaintingForPrint();83}8485if (!isPaintingForPrint && table.getRowSorter() != null) {86if (!horizontalTextPositionSet) {87// There is a row sorter, and the developer hasn't88// set a text position, change to leading.89setHorizontalTextPosition(JLabel.LEADING);90}91SortOrder sortOrder = getColumnSortOrder(table, column);92if (sortOrder != null) {93switch(sortOrder) {94case ASCENDING:95sortIcon = DefaultLookup.getIcon(96this, ui, "Table.ascendingSortIcon");97break;98case DESCENDING:99sortIcon = DefaultLookup.getIcon(100this, ui, "Table.descendingSortIcon");101break;102case UNSORTED:103sortIcon = DefaultLookup.getIcon(104this, ui, "Table.naturalSortIcon");105break;106}107}108}109}110111setText(value == null ? "" : value.toString());112setIcon(sortIcon);113sortArrow = sortIcon;114115Border border = null;116if (hasFocus) {117border = DefaultLookup.getBorder(this, ui, "TableHeader.focusCellBorder");118}119if (border == null) {120border = DefaultLookup.getBorder(this, ui, "TableHeader.cellBorder");121}122setBorder(border);123124return this;125}126127public static SortOrder getColumnSortOrder(JTable table, int column) {128SortOrder rv = null;129if (table == null || table.getRowSorter() == null) {130return rv;131}132java.util.List<? extends RowSorter.SortKey> sortKeys =133table.getRowSorter().getSortKeys();134if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() ==135table.convertColumnIndexToModel(column)) {136rv = sortKeys.get(0).getSortOrder();137}138return rv;139}140141@Override142public void paintComponent(Graphics g) {143boolean b = DefaultLookup.getBoolean(this, ui,144"TableHeader.rightAlignSortArrow", false);145if (b && sortArrow != null) {146//emptyIcon is used so that if the text in the header is right147//aligned, or if the column is too narrow, then the text will148//be sized appropriately to make room for the icon that is about149//to be painted manually here.150emptyIcon.width = sortArrow.getIconWidth();151emptyIcon.height = sortArrow.getIconHeight();152setIcon(emptyIcon);153super.paintComponent(g);154Point position = computeIconPosition(g);155sortArrow.paintIcon(this, g, position.x, position.y);156} else {157super.paintComponent(g);158}159}160161private Point computeIconPosition(Graphics g) {162FontMetrics fontMetrics = g.getFontMetrics();163Rectangle viewR = new Rectangle();164Rectangle textR = new Rectangle();165Rectangle iconR = new Rectangle();166Insets i = getInsets();167viewR.x = i.left;168viewR.y = i.top;169viewR.width = getWidth() - (i.left + i.right);170viewR.height = getHeight() - (i.top + i.bottom);171SwingUtilities.layoutCompoundLabel(172this,173fontMetrics,174getText(),175sortArrow,176getVerticalAlignment(),177getHorizontalAlignment(),178getVerticalTextPosition(),179getHorizontalTextPosition(),180viewR,181iconR,182textR,183getIconTextGap());184int x = getWidth() - i.right - sortArrow.getIconWidth();185int y = iconR.y;186return new Point(x, y);187}188189private class EmptyIcon implements Icon, Serializable {190int width = 0;191int height = 0;192public void paintIcon(Component c, Graphics g, int x, int y) {}193public int getIconWidth() { return width; }194public int getIconHeight() { return height; }195}196}197198199