-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathComponentTreeCellRenderer.java
More file actions
211 lines (194 loc) · 7.34 KB
/
ComponentTreeCellRenderer.java
File metadata and controls
211 lines (194 loc) · 7.34 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* @(#)ComponentTreeCellRenderer.java 1.0 11/16/08
*/
package darrylbu.renderer;
import darrylbu.model.ComponentTreeModel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
/**
* A tree cell renderer that displays scaled replicas of Swing components
* at the nodes of the tree.
* <P>
* Extends {@link DefaultTreeCellRenderer}.
*
* @author Darryl
*/
public class ComponentTreeCellRenderer extends DefaultTreeCellRenderer {
private JTree tree;
private double scale;
private Map<JComponent, DefaultTreeCellRenderer> renderers;
private final Color selColor;
private final Color nonSelColor;
/**
* Returns a <code>ComponentTreeCellRenderer</code> suitable for rendering scaled
* snapshots of Swing components. This class is intended for use with
* a <code>JTree</code> that has a <code>ComponentTreeModel</code>.
*
* @param tree the tree of components
* @param scale the scale at which to render the components
* @see ComponentTreeModel
* @see DefaultTreeCellRenderer
*/
public ComponentTreeCellRenderer(JTree tree, double scale) {
this.tree = tree;
this.scale = scale;
setHorizontalTextPosition(LEFT);
setOpaque(true);
setBorder(new EmptyBorder(2, 0, 2, 0));
setFont(tree.getFont());
renderers = new HashMap<JComponent, DefaultTreeCellRenderer>();
selColor = backgroundSelectionColor == null ?
Color.LIGHT_GRAY :
new Color(backgroundSelectionColor.getRGB());
nonSelColor = backgroundNonSelectionColor == null ?
tree.getBackground() :
backgroundNonSelectionColor;
}
/**
* Configures the renderer based on the passed in component.
* The text and icon are set from the <code>JComponent</code> located
* as the userObject of the node and the scale, or the String returned
* by invoking <code>String.valueOf</code> on <code>value</code> if
* the object at the node is not a <code>JComponent</code>.
* <P>
* The background color is set based on the selection status and the
* icon is a scaled replica of the <code>JComponent</code>.
* <P>
* For the sake of performance, a renderer for each component is cached.
* The cache can be synchronized with the current state of the component
* hierarchy by invoking <code>refresh()</code>
*
* @see #refresh()
* @see DefaultTreeCellRenderer#getTreeCellRendererComponent(JTree,
* Object, boolean, boolean, boolean, int, boolean)
*/
@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object userObject = node.getUserObject();
if (userObject instanceof JComponent) {
DefaultTreeCellRenderer renderer = null;
JComponent component = (JComponent) node.getUserObject();
if (!renderers.containsKey(component)) {
renderer = rendererForComponent(component, tree);
renderers.put(component, renderer);
} else {
renderer = renderers.get(component);
}
setIcon(renderer.getIcon());
setText(renderer.getText());
} else {
setIcon(null);
setText(String.valueOf(userObject));
}
setBackground(selected ? selColor : nonSelColor);
return this;
}
/**
* Creates a renderer with a scaled image of the component
*
* @param component the component
* @param tree the tree
* @return a renderer for the component
*/
private DefaultTreeCellRenderer rendererForComponent(JComponent component,
JTree tree) {
Rectangle visibleRect = component.getVisibleRect();
String className = component.getClass().getName();
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setHorizontalTextPosition(LEFT);
if (component == tree) {
renderer.setText(className + " (this tree)");
return renderer;
}
if (!component.isShowing()) {
renderer.setText(className + " (not visible)");
return renderer;
}
if (visibleRect.width * scale < 1.0 || visibleRect.height * scale < 1.0) {
renderer.setText(className + " (too small to show)");
return renderer;
}
BufferedImage bi = new BufferedImage(
(int) (visibleRect.width * scale) + 2,
(int) (visibleRect.height * scale) + 2,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
g2.setClip(visibleRect);
g2.scale(scale, scale);
Method method;
try {
method = JComponent.class.getDeclaredMethod("paintComponent",
Graphics.class);
method.setAccessible(true);
method.invoke(component, g2);
method = JComponent.class.getDeclaredMethod("paintBorder",
Graphics.class);
method.setAccessible(true);
method.invoke(component, g2);
// attempting to paint this tree on any of its ancestors causes the
// tree to repaint and leads to infinite recursion
if (SwingUtilities.isDescendingFrom(tree, component)) {
g2.setColor(textNonSelectionColor);
g2.scale(1.0 / scale, 1.0 / scale);
g2.drawString("(Children not shown)", 2, 15);
} else {
method = JComponent.class.getDeclaredMethod("paintChildren",
Graphics.class);
method.setAccessible(true);
method.invoke(component, g2);
}
// ignore checked exceptions
} catch (IllegalAccessException ignore) {
} catch (IllegalArgumentException ignore) {
} catch (InvocationTargetException ignore) {
} catch (NoSuchMethodException ignore) {
} catch (SecurityException ignore) {
}
g2.dispose();
renderer.setText(className);
renderer.setIcon(new ImageIcon(bi));
return renderer;
}
/**
* Reloads the renderer cache using the current state of the components.
*/
public void refresh() {
renderers.clear();
tree.updateUI();
}
/**
* Sets the scale at which components will be rendered. The recommended
* range of vales is 0.1 to 1.0. Setting a scale much in excess of 1.0
* may result in slow rendering of the tree.
* <P>
* Setting the scale will also refresh the tree using the current state
* of the components
*
* @param scale the scale at which to render the components
* @see #refresh
*/
public void setScale(double scale) {
if (this.scale != scale) {
this.scale = scale;
refresh();
}
}
}