001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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    /** The text color for selected cells. */
045    private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
046
047    /** The background color for selected cells. */
048    private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
049
050    /** The background color for table. */
051    private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
052
053    /** Tree table to render. */
054    private final TreeTable treeTable;
055
056    /** Last table/tree row asked to render. */
057    private int visibleRow;
058
059    /**
060     * Creates a new instance.
061     *
062     * @param treeTable tree table to render.
063     * @param model Tree model.
064     */
065    /* package */ TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
066        super(model);
067        this.treeTable = treeTable;
068    }
069
070    /**
071     * UpdateUI is overridden to set the colors of the Tree's renderer
072     * to match that of the table.
073     */
074    @Override
075    public void updateUI() {
076        super.updateUI();
077        // Make the tree's cell renderer use the table's cell selection
078        // colors.
079        final TreeCellRenderer tcr = getCellRenderer();
080        if (tcr instanceof DefaultTreeCellRenderer) {
081            final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
082            renderer.setBorderSelectionColor(null);
083            renderer.setTextSelectionColor(
084                    UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
085            renderer.setBackgroundSelectionColor(
086                    UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
087        }
088    }
089
090    /**
091     * Sets the row height of the tree, and forwards the row height to
092     * the table.
093     */
094    @Override
095    public void setRowHeight(int newRowHeight) {
096        if (newRowHeight > 0) {
097            super.setRowHeight(newRowHeight);
098            if (treeTable != null
099                    && 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}