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
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
35
36 class TreeTableCellRenderer extends JTree implements
37 TableCellRenderer {
38
39
40
41
42 private static final long serialVersionUID = 4324031590789321581L;
43
44
45 private static final String COLOR_KEY_TABLE_SELECTION_FOREGROUND = "Table.selectionForeground";
46
47
48 private static final String COLOR_KEY_TABLE_SELECTION_BACKGROUND = "Table.selectionBackground";
49
50
51 private static final String COLOR_KEY_TABLE_BACKGROUND = "Table.background";
52
53
54 private final TreeTable treeTable;
55
56
57 private int visibleRow;
58
59
60
61
62
63
64
65 TreeTableCellRenderer(TreeTable treeTable, TreeModel model) {
66 super(model);
67 this.treeTable = treeTable;
68 }
69
70
71
72
73
74 @Override
75 public void updateUI() {
76 super.updateUI();
77
78
79 final TreeCellRenderer tcr = getCellRenderer();
80 if (tcr instanceof DefaultTreeCellRenderer) {
81 final DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tcr;
82 renderer.setBorderSelectionColor(null);
83 renderer.setTextSelectionColor(
84 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_FOREGROUND));
85 renderer.setBackgroundSelectionColor(
86 UIManager.getColor(COLOR_KEY_TABLE_SELECTION_BACKGROUND));
87 }
88 }
89
90
91
92
93
94 @Override
95 public void setRowHeight(int newRowHeight) {
96 if (newRowHeight > 0) {
97 super.setRowHeight(newRowHeight);
98 if (treeTable != null
99 && treeTable.getRowHeight() != newRowHeight) {
100 treeTable.setRowHeight(getRowHeight());
101 }
102 }
103 }
104
105
106
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
115
116
117 @Override
118 public void paint(Graphics graph) {
119 graph.translate(0, -visibleRow * getRowHeight());
120 super.paint(graph);
121 }
122
123
124
125
126
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 }