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 renderer) {
083 renderer.setBorderSelectionColor(null);
084 renderer.setTextSelectionColor(
085 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
086 renderer.setBackgroundSelectionColor(
087 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
088 }
089 }
090
091 /**
092 * Sets the row height of the tree, and forwards the row height to
093 * the table.
094 */
095 @Override
096 public void setRowHeight(int newRowHeight) {
097 if (newRowHeight > 0) {
098 super.setRowHeight(newRowHeight);
099 if (treeTable != null
100 && treeTable.getRowHeight() != newRowHeight) {
101 treeTable.setRowHeight(getRowHeight());
102 }
103 }
104 }
105
106 /**
107 * This is overridden to set the height to match that of the JTable.
108 */
109 @Override
110 public void setBounds(int x, int y, int w, int h) {
111 super.setBounds(x, 0, w, treeTable.getHeight());
112 }
113
114 /**
115 * Subclassed to translate the graphics such that the last visible
116 * row will be drawn at 0,0.
117 */
118 @Override
119 public void paint(Graphics graph) {
120 graph.translate(0, -visibleRow * getRowHeight());
121 super.paint(graph);
122 }
123
124 /**
125 * TreeCellRenderer method. Overridden to update the visible row.
126 *
127 * @see TableCellRenderer
128 */
129 @Override
130 public Component getTableCellRendererComponent(JTable table,
131 Object value,
132 boolean isSelected,
133 boolean hasFocus,
134 int row, int column) {
135 final String colorKey;
136 if (isSelected) {
137 colorKey = COLOR_KEY_TABLE_SELECTION_BACKGROUND;
138 }
139 else {
140 colorKey = COLOR_KEY_TABLE_BACKGROUND;
141 }
142
143 setBackground(UIManager.getColor(colorKey));
144 visibleRow = row;
145 return this;
146 }
147
148}