001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.gui; 021 022import java.awt.Component; 023import java.awt.Graphics; 024import java.io.Serial; 025 026import javax.swing.JTable; 027import javax.swing.JTree; 028import javax.swing.UIManager; 029import javax.swing.table.TableCellRenderer; 030import javax.swing.tree.DefaultTreeCellRenderer; 031import javax.swing.tree.TreeCellRenderer; 032import javax.swing.tree.TreeModel; 033 034/** 035 * A TreeCellRenderer that displays a JTree. 036 */ 037class TreeTableCellRenderer extends JTree implements 038 TableCellRenderer { 039 040 /** 041 * Serial ID. 042 */ 043 @Serial 044 private static final long serialVersionUID = 4324031590789321581L; 045 046 /** The text color for selected cells. */ 047 private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground"; 048 049 /** The background color for selected cells. */ 050 private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground"; 051 052 /** The background color for table. */ 053 private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background"; 054 055 /** Tree table to render. */ 056 private final TreeTable treeTable; 057 058 /** Last table/tree row asked to render. */ 059 private int visibleRow; 060 061 /** 062 * Creates a new instance. 063 * 064 * @param treeTable tree table to render. 065 * @param model Tree model. 066 */ 067 /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) { 068 super(model); 069 this.treeTable = treeTable; 070 } 071 072 /** 073 * UpdateUI is overridden to set the colors of the Tree's renderer 074 * to match that of the table. 075 */ 076 @Override 077 public void updateUI() { 078 super.updateUI(); 079 // Make the tree's cell renderer use the table's cell selection 080 // colors. 081 final TreeCellRenderer tcr = getCellRenderer(); 082 if (tcr instanceof DefaultTreeCellRenderer) { 083 final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr; 084 renderer.setBorderSelectionColor(null); 085 renderer.setTextSelectionColor( 086 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND)); 087 renderer.setBackgroundSelectionColor( 088 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND)); 089 } 090 } 091 092 /** 093 * Sets the row height of the tree, and forwards the row height to 094 * the table. 095 */ 096 @Override 097 public void setRowHeight(int newRowHeight) { 098 if (newRowHeight > 0) { 099 super.setRowHeight(newRowHeight); 100 if (treeTable != null 101 && treeTable.getRowHeight() != newRowHeight) { 102 treeTable.setRowHeight(getRowHeight()); 103 } 104 } 105 } 106 107 /** 108 * This is overridden to set the height to match that of the JTable. 109 */ 110 @Override 111 public void setBounds(int x, int y, int w, int h) { 112 super.setBounds(x, 0, w, treeTable.getHeight()); 113 } 114 115 /** 116 * Subclassed to translate the graphics such that the last visible 117 * row will be drawn at 0,0. 118 */ 119 @Override 120 public void paint(Graphics graph) { 121 graph.translate(0, -visibleRow * getRowHeight()); 122 super.paint(graph); 123 } 124 125 /** 126 * TreeCellRenderer method. Overridden to update the visible row. 127 * 128 * @see TableCellRenderer 129 */ 130 @Override 131 public Component getTableCellRendererComponent(JTable table, 132 Object value, 133 boolean isSelected, 134 boolean hasFocus, 135 int row, int column) { 136 final String colorKey; 137 if (isSelected) { 138 colorKey = COLOR_KEY_TABLE_SELECTION_BACKGROUND; 139 } 140 else { 141 colorKey = COLOR_KEY_TABLE_BACKGROUND; 142 } 143 144 setBackground(UIManager.getColor(colorKey)); 145 visibleRow = row; 146 return this; 147 } 148 149}