1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.awt.Component;
23 import java.awt.Graphics;
24 import java.io.Serial;
25
26 import javax.swing.JTable;
27 import javax.swing.JTree;
28 import javax.swing.UIManager;
29 import javax.swing.table.TableCellRenderer;
30 import javax.swing.tree.DefaultTreeCellRenderer;
31 import javax.swing.tree.TreeCellRenderer;
32 import javax.swing.tree.TreeModel;
33
34
35
36
37 class TreeTableCellRenderer extends JTree implements
38 TableCellRenderer {
39
40
41
42
43 @Serial
44 private static final long serialVersionUID = 4324031590789321581L;
45
46
47 private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
48
49
50 private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
51
52
53 private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
54
55
56 private final TreeTable treeTable;
57
58
59 private int visibleRow;
60
61
62
63
64
65
66
67 TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
68 super(model);
69 this.treeTable = treeTable;
70 }
71
72
73
74
75
76 @Override
77 public void updateUI() {
78 super.updateUI();
79
80
81 final TreeCellRenderer tcr = getCellRenderer();
82 if (tcr instanceof DefaultTreeCellRenderer renderer) {
83 renderer.setBorderSelectionColor(null);
84 renderer.setTextSelectionColor(
85 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
86 renderer.setBackgroundSelectionColor(
87 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
88 }
89 }
90
91
92
93
94
95 @Override
96 public void setRowHeight(int newRowHeight) {
97 if (newRowHeight > 0) {
98 super.setRowHeight(newRowHeight);
99 if (treeTable != null
100 && treeTable.getRowHeight() != newRowHeight) {
101 treeTable.setRowHeight(getRowHeight());
102 }
103 }
104 }
105
106
107
108
109 @Override
110 public void setBounds(int x, int y, int w, int h) {
111 super.setBounds(x, 0, w, treeTable.getHeight());
112 }
113
114
115
116
117
118 @Override
119 public void paint(Graphics graph) {
120 graph.translate(0, -visibleRow * getRowHeight());
121 super.paint(graph);
122 }
123
124
125
126
127
128
129 @Override
130 public Component getTableCellRendererComponent(JTable table,
131 Object value,
132 boolean isSelected,
133 boolean hasFocus,
134 int row, int column) {
135 final String colorKey;
136 if (isSelected) {
137 colorKey = COLOR_KEY_TABLE_SELECTION_BACKGROUND;
138 }
139 else {
140 colorKey = COLOR_KEY_TABLE_BACKGROUND;
141 }
142
143 setBackground(UIManager.getColor(colorKey));
144 visibleRow = row;
145 return this;
146 }
147
148 }