1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.awt.Component;
23 import java.awt.Graphics;
24 import java.io.Serial;
25
26 import javax.swing.JTable;
27 import javax.swing.JTree;
28 import javax.swing.UIManager;
29 import javax.swing.table.TableCellRenderer;
30 import javax.swing.tree.DefaultTreeCellRenderer;
31 import javax.swing.tree.TreeCellRenderer;
32 import javax.swing.tree.TreeModel;
33
34 /**
35 * A TreeCellRenderer that displays a JTree.
36 */
37 class TreeTableCellRenderer extends JTree implements
38 TableCellRenderer {
39
40 /**
41 * Serial ID.
42 */
43 @Serial
44 private static final long serialVersionUID = 4324031590789321581L;
45
46 /** The text color for selected cells. */
47 private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
48
49 /** The background color for selected cells. */
50 private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
51
52 /** The background color for table. */
53 private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
54
55 /** Tree table to render. */
56 private final TreeTable treeTable;
57
58 /** Last table/tree row asked to render. */
59 private int visibleRow;
60
61 /**
62 * Creates a new instance.
63 *
64 * @param treeTable tree table to render.
65 * @param model Tree model.
66 */
67 /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
68 super(model);
69 this.treeTable = treeTable;
70 }
71
72 /**
73 * UpdateUI is overridden to set the colors of the Tree's renderer
74 * to match that of the table.
75 */
76 @Override
77 public void updateUI() {
78 super.updateUI();
79 // Make the tree's cell renderer use the table's cell selection
80 // colors.
81 final TreeCellRenderer tcr = getCellRenderer();
82 if (tcr instanceof DefaultTreeCellRenderer renderer) {
83 renderer.setBorderSelectionColor(null);
84 renderer.setTextSelectionColor(
85 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
86 renderer.setBackgroundSelectionColor(
87 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
88 }
89 }
90
91 /**
92 * Sets the row height of the tree, and forwards the row height to
93 * the table.
94 */
95 @Override
96 public void setRowHeight(int newRowHeight) {
97 if (newRowHeight > 0) {
98 super.setRowHeight(newRowHeight);
99 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 }