001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 TableCellRenderer { 038 039 /** 040 * Serial ID. 041 */ 042 @Serial 043 private static final long serialVersionUID = 4324031590789321581L; 044 045 /** The text color for selected cells. */ 046 private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground"; 047 048 /** The background color for selected cells. */ 049 private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground"; 050 051 /** The background color for table. */ 052 private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background"; 053 054 /** Tree table to render. */ 055 private final TreeTable treeTable; 056 057 /** Last table/tree row asked to render. */ 058 private int visibleRow; 059 060 /** 061 * Creates a new instance. 062 * 063 * @param treeTable tree table to render. 064 * @param model Tree model. 065 */ 066 /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) { 067 super(model); 068 this.treeTable = treeTable; 069 } 070 071 /** 072 * UpdateUI is overridden to set the colors of the Tree's renderer to match that of the table. 073 */ 074 @Override 075 public void updateUI() { 076 super.updateUI(); 077 // Make the tree's cell renderer use the table's cell selection 078 // colors. 079 final TreeCellRenderer tcr = getCellRenderer(); 080 if (tcr instanceof final DefaultTreeCellRenderer renderer) { 081 renderer.setBorderSelectionColor(null); 082 renderer.setTextSelectionColor( 083 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND)); 084 renderer.setBackgroundSelectionColor( 085 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND)); 086 } 087 } 088 089 /** 090 * Sets the row height of the tree, and forwards the row height to the table. 091 */ 092 @Override 093 public void setRowHeight(int newRowHeight) { 094 if (newRowHeight > 0) { 095 super.setRowHeight(newRowHeight); 096 if (treeTable != null 097 && treeTable.getRowHeight() != newRowHeight) { 098 treeTable.setRowHeight(getRowHeight()); 099 } 100 } 101 } 102 103 /** 104 * This is overridden to set the height to match that of the JTable. 105 */ 106 @Override 107 public void setBounds(int x, int y, int w, int h) { 108 super.setBounds(x, 0, w, treeTable.getHeight()); 109 } 110 111 /** 112 * Subclassed to translate the graphics such that the last visible row will be drawn at 0,0. 113 */ 114 @Override 115 public void paint(Graphics graph) { 116 graph.translate(0, -visibleRow * getRowHeight()); 117 super.paint(graph); 118 } 119 120 /** 121 * TreeCellRenderer method. Overridden to update the visible row. 122 * 123 * @see TableCellRenderer 124 */ 125 @Override 126 public Component getTableCellRendererComponent(JTable table, 127 Object value, 128 boolean isSelected, 129 boolean hasFocus, 130 int row, int column) { 131 final String colorKey; 132 if (isSelected) { 133 colorKey = COLOR_KEY_TABLE_SELECTION_BACKGROUND; 134 } 135 else { 136 colorKey = COLOR_KEY_TABLE_BACKGROUND; 137 } 138 139 setBackground(UIManager.getColor(colorKey)); 140 visibleRow = row; 141 return this; 142 } 143 144}