View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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  
25  import javax.swing.JTable;
26  import javax.swing.JTree;
27  import javax.swing.UIManager;
28  import javax.swing.table.TableCellRenderer;
29  import javax.swing.tree.DefaultTreeCellRenderer;
30  import javax.swing.tree.TreeCellRenderer;
31  import javax.swing.tree.TreeModel;
32  
33  /**
34   * A TreeCellRenderer that displays a JTree.
35   */
36  class TreeTableCellRenderer extends JTree implements
37          TableCellRenderer {
38  
39      /**
40       * Serial ID.
41       */
42      private static final long serialVersionUID = 4324031590789321581L;
43  
44      /** The text color for selected cells. */
45      private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
46  
47      /** The background color for selected cells. */
48      private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
49  
50      /** The background color for table. */
51      private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
52  
53      /** Tree table to render. */
54      private final TreeTable treeTable;
55  
56      /** Last table/tree row asked to render. */
57      private int visibleRow;
58  
59      /**
60       * Creates a new instance.
61       *
62       * @param treeTable tree table to render.
63       * @param model Tree model.
64       */
65      /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
66          super(model);
67          this.treeTable = treeTable;
68      }
69  
70      /**
71       * UpdateUI is overridden to set the colors of the Tree's renderer
72       * 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 DefaultTreeCellRenderer) {
81              final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
82              renderer.setBorderSelectionColor(null);
83              renderer.setTextSelectionColor(
84                      UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
85              renderer.setBackgroundSelectionColor(
86                      UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
87          }
88      }
89  
90      /**
91       * Sets the row height of the tree, and forwards the row height to
92       * the table.
93       */
94      @Override
95      public void setRowHeight(int newRowHeight) {
96          if (newRowHeight > 0) {
97              super.setRowHeight(newRowHeight);
98              if (treeTable != null
99                      && treeTable.getRowHeight() != newRowHeight) {
100                 treeTable.setRowHeight(getRowHeight());
101             }
102         }
103     }
104 
105     /**
106      * This is overridden to set the height to match that of the JTable.
107      */
108     @Override
109     public void setBounds(int x, int y, int w, int h) {
110         super.setBounds(x, 0, w, treeTable.getHeight());
111     }
112 
113     /**
114      * Subclassed to translate the graphics such that the last visible
115      * row will be drawn at 0,0.
116      */
117     @Override
118     public void paint(Graphics graph) {
119         graph.translate(0, -visibleRow * getRowHeight());
120         super.paint(graph);
121     }
122 
123     /**
124      * TreeCellRenderer method. Overridden to update the visible row.
125      *
126      * @see TableCellRenderer
127      */
128     @Override
129     public Component getTableCellRendererComponent(JTable table,
130             Object value,
131             boolean isSelected,
132             boolean hasFocus,
133             int row, int column) {
134         final String colorKey;
135         if (isSelected) {
136             colorKey = COLOR_KEY_TABLE_SELECTION_BACKGROUND;
137         }
138         else {
139             colorKey = COLOR_KEY_TABLE_BACKGROUND;
140         }
141 
142         setBackground(UIManager.getColor(colorKey));
143         visibleRow = row;
144         return this;
145     }
146 
147 }