View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * @author Lars K├╝hne
36   */
37  class TreeTableCellRenderer extends JTree implements
38          TableCellRenderer {
39      /**
40       * Serial ID.
41       */
42      private static final long serialVersionUID = 4324031590789321581L;
43  
44      /** Tree table to render. */
45      private final TreeTable treeTable;
46  
47      /** Last table/tree row asked to renderer. */
48      private int visibleRow;
49  
50      /**
51       * Creates a new instance.
52       * @param treeTable tree table to render.
53       * @param model Tree model.
54       */
55      TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
56          super(model);
57          this.treeTable = treeTable;
58      }
59  
60      /**
61       * UpdateUI is overridden to set the colors of the Tree's renderer
62       * to match that of the table.
63       */
64      @Override
65      public void updateUI() {
66          super.updateUI();
67          // Make the tree's cell renderer use the table's cell selection
68          // colors.
69          final TreeCellRenderer tcr = getCellRenderer();
70          if (tcr instanceof DefaultTreeCellRenderer) {
71              final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
72              // For 1.1 uncomment this, 1.2 has a bug that will cause an
73              // exception to be thrown if the border selection color is
74              // null.
75              // renderer.setBorderSelectionColor(null);
76              renderer.setTextSelectionColor(UIManager.getColor("Table.selectionForeground"));
77              renderer.setBackgroundSelectionColor(UIManager.getColor("Table.selectionBackground"));
78          }
79      }
80  
81      /**
82       * Sets the row height of the tree, and forwards the row height to
83       * the table.
84       */
85      @Override
86      public void setRowHeight(int newRowHeight) {
87          if (newRowHeight > 0) {
88              super.setRowHeight(newRowHeight);
89              if (treeTable != null
90                      && treeTable.getRowHeight() != newRowHeight) {
91                  treeTable.setRowHeight(getRowHeight());
92              }
93          }
94      }
95  
96      /**
97       * This is overridden to set the height to match that of the JTable.
98       */
99      @Override
100     public void setBounds(int x, int y, int w, int h) {
101         super.setBounds(x, 0, w, treeTable.getHeight());
102     }
103 
104     /**
105      * Subclassed to translate the graphics such that the last visible
106      * row will be drawn at 0,0.
107      */
108     @Override
109     public void paint(Graphics graph) {
110         graph.translate(0, -visibleRow * getRowHeight());
111         super.paint(graph);
112     }
113 
114     /**
115      * TreeCellRenderer method. Overridden to update the visible row.
116      * @see TableCellRenderer
117      */
118     @Override
119     public Component getTableCellRendererComponent(JTable table,
120             Object value,
121             boolean isSelected,
122             boolean hasFocus,
123             int row, int column) {
124         if (isSelected) {
125             setBackground(table.getSelectionBackground());
126         }
127         else {
128             setBackground(table.getBackground());
129         }
130 
131         visibleRow = row;
132         return this;
133     }
134 }