View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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 TableCellRenderer {
38  
39      /**
40       * Serial ID.
41       */
42      @Serial
43      private static final long serialVersionUID = 4324031590789321581L;
44  
45      /** The text color for selected cells. */
46      private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
47  
48      /** The background color for selected cells. */
49      private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
50  
51      /** The background color for table. */
52      private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
53  
54      /** Tree table to render. */
55      private final TreeTable treeTable;
56  
57      /** Last table/tree row asked to render. */
58      private int visibleRow;
59  
60      /**
61       * Creates a new instance.
62       *
63       * @param treeTable tree table to render.
64       * @param model Tree model.
65       */
66      /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
67          super(model);
68          this.treeTable = treeTable;
69      }
70  
71      /**
72       * UpdateUI is overridden to set the colors of the Tree's renderer to match that of the table.
73       */
74      @Override
75      public void updateUI() {
76          super.updateUI();
77          // Make the tree's cell renderer use the table's cell selection
78          // colors.
79          final TreeCellRenderer tcr = getCellRenderer();
80          if (tcr instanceof final DefaultTreeCellRenderer renderer) {
81              renderer.setBorderSelectionColor(null);
82              renderer.setTextSelectionColor(
83                      UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
84              renderer.setBackgroundSelectionColor(
85                      UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
86          }
87      }
88  
89      /**
90       * Sets the row height of the tree, and forwards the row height to the table.
91       */
92      @Override
93      public void setRowHeight(int newRowHeight) {
94          if (newRowHeight > 0) {
95              super.setRowHeight(newRowHeight);
96              if (treeTable != null
97                      && treeTable.getRowHeight() != newRowHeight) {
98                  treeTable.setRowHeight(getRowHeight());
99              }
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 }