001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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;
024
025import javax.swing.JTable;
026import javax.swing.JTree;
027import javax.swing.UIManager;
028import javax.swing.table.TableCellRenderer;
029import javax.swing.tree.DefaultTreeCellRenderer;
030import javax.swing.tree.TreeCellRenderer;
031import javax.swing.tree.TreeModel;
032
033/**
034 * A TreeCellRenderer that displays a JTree.
035 */
036class TreeTableCellRenderer extends JTree implements
037        TableCellRenderer {
038
039    /**
040     * Serial ID.
041     */
042    private static final long serialVersionUID = 4324031590789321581L;
043
044    /** Tree table to render. */
045    private final TreeTable treeTable;
046
047    /** Last table/tree row asked to renderer. */
048    private int visibleRow;
049
050    /**
051     * Creates a new instance.
052     *
053     * @param treeTable tree table to render.
054     * @param model Tree model.
055     */
056    /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
057        super(model);
058        this.treeTable = treeTable;
059    }
060
061    /**
062     * UpdateUI is overridden to set the colors of the Tree's renderer
063     * to match that of the table.
064     */
065    @Override
066    public void updateUI() {
067        super.updateUI();
068        // Make the tree's cell renderer use the table's cell selection
069        // colors.
070        final TreeCellRenderer tcr = getCellRenderer();
071        if (tcr instanceof DefaultTreeCellRenderer) {
072            final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
073            // For 1.1 uncomment this, 1.2 has a bug that will cause an
074            // exception to be thrown if the border selection color is
075            // null.
076            // renderer.setBorderSelectionColor(null);
077            renderer.setTextSelectionColor(UIManager.getColor("Table.selectionForeground"));
078            renderer.setBackgroundSelectionColor(UIManager.getColor("Table.selectionBackground"));
079        }
080    }
081
082    /**
083     * Sets the row height of the tree, and forwards the row height to
084     * the table.
085     */
086    @Override
087    public void setRowHeight(int newRowHeight) {
088        if (newRowHeight > 0) {
089            super.setRowHeight(newRowHeight);
090            if (treeTable != null
091                    && treeTable.getRowHeight() != newRowHeight) {
092                treeTable.setRowHeight(getRowHeight());
093            }
094        }
095    }
096
097    /**
098     * This is overridden to set the height to match that of the JTable.
099     */
100    @Override
101    public void setBounds(int x, int y, int w, int h) {
102        super.setBounds(x, 0, w, treeTable.getHeight());
103    }
104
105    /**
106     * Subclassed to translate the graphics such that the last visible
107     * row will be drawn at 0,0.
108     */
109    @Override
110    public void paint(Graphics graph) {
111        graph.translate(0, -visibleRow * getRowHeight());
112        super.paint(graph);
113    }
114
115    /**
116     * TreeCellRenderer method. Overridden to update the visible row.
117     *
118     * @see TableCellRenderer
119     */
120    @Override
121    public Component getTableCellRendererComponent(JTable table,
122            Object value,
123            boolean isSelected,
124            boolean hasFocus,
125            int row, int column) {
126        if (isSelected) {
127            setBackground(table.getSelectionBackground());
128        }
129        else {
130            setBackground(table.getBackground());
131        }
132
133        visibleRow = row;
134        return this;
135    }
136
137}