forked from checkstyle/checkstyle
/
TreeTableCellRenderer.java
133 lines (121 loc) · 4.33 KB
/
TreeTableCellRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2016 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.gui;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
/**
* A TreeCellRenderer that displays a JTree.
* @author Lars Kühne
*/
class TreeTableCellRenderer extends JTree implements
TableCellRenderer {
/**
* Serial ID.
*/
private static final long serialVersionUID = 4324031590789321581L;
/** Tree table to render. */
private final JTreeTable treeTable;
/** Last table/tree row asked to renderer. */
private int visibleRow;
/**
* Creates a new instance.
* @param treeTable tree table to render.
* @param model Tree model.
*/
TreeTableCellRenderer(JTreeTable treeTable, TreeModel model) {
super(model);
this.treeTable = treeTable;
}
/**
* UpdateUI is overridden to set the colors of the Tree's renderer
* to match that of the table.
*/
@Override
public void updateUI() {
super.updateUI();
// Make the tree's cell renderer use the table's cell selection
// colors.
final TreeCellRenderer tcr = getCellRenderer();
if (tcr instanceof DefaultTreeCellRenderer) {
final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
// For 1.1 uncomment this, 1.2 has a bug that will cause an
// exception to be thrown if the border selection color is
// null.
// renderer.setBorderSelectionColor(null);
renderer.setTextSelectionColor(UIManager.getColor("Table.selectionForeground"));
renderer.setBackgroundSelectionColor(UIManager.getColor("Table.selectionBackground"));
}
}
/**
* Sets the row height of the tree, and forwards the row height to
* the table.
*/
@Override
public void setRowHeight(int newRowHeight) {
if (newRowHeight > 0) {
super.setRowHeight(newRowHeight);
if (treeTable != null
&& treeTable.getRowHeight() != newRowHeight) {
treeTable.setRowHeight(getRowHeight());
}
}
}
/**
* This is overridden to set the height to match that of the JTable.
*/
@Override
public void setBounds(int x, int y, int w, int h) {
super.setBounds(x, 0, w, treeTable.getHeight());
}
/**
* Subclassed to translate the graphics such that the last visible
* row will be drawn at 0,0.
*/
@Override
public void paint(Graphics graph) {
graph.translate(0, -visibleRow * getRowHeight());
super.paint(graph);
}
/**
* TreeCellRenderer method. Overridden to update the visible row.
* @see TableCellRenderer
*/
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if (isSelected) {
setBackground(table.getSelectionBackground());
}
else {
setBackground(table.getBackground());
}
visibleRow = row;
return this;
}
}