2
|
1 |
/*
|
8966
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
5506
|
15 |
* - Neither the name of Oracle nor the names of its
|
2
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
|
|
33 |
import javax.swing.Icon;
|
|
34 |
import javax.swing.ImageIcon;
|
|
35 |
import javax.swing.JLabel;
|
|
36 |
import javax.swing.JTree;
|
|
37 |
import javax.swing.tree.TreeCellRenderer;
|
|
38 |
import javax.swing.tree.DefaultMutableTreeNode;
|
|
39 |
import java.awt.Component;
|
|
40 |
import java.awt.Color;
|
|
41 |
import java.awt.Font;
|
|
42 |
import java.awt.Graphics;
|
8966
|
43 |
import javax.swing.UIManager;
|
2
|
44 |
|
8966
|
45 |
|
|
46 |
@SuppressWarnings("serial")
|
|
47 |
public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer {
|
|
48 |
|
2
|
49 |
/** Font used if the string to be displayed isn't a font. */
|
8966
|
50 |
protected static Font defaultFont;
|
2
|
51 |
/** Icon to use when the item is collapsed. */
|
8966
|
52 |
protected static ImageIcon collapsedIcon;
|
2
|
53 |
/** Icon to use when the item is expanded. */
|
8966
|
54 |
protected static ImageIcon expandedIcon;
|
2
|
55 |
/** Color to use for the background when selected. */
|
8966
|
56 |
protected static final Color SELECTED_BACKGROUND_COLOR;
|
2
|
57 |
|
8966
|
58 |
static {
|
|
59 |
if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
|
|
60 |
SELECTED_BACKGROUND_COLOR = new Color(0, 0,
|
|
61 |
0, 0);
|
|
62 |
} else {
|
|
63 |
SELECTED_BACKGROUND_COLOR = Color.YELLOW;
|
|
64 |
}
|
2
|
65 |
try {
|
|
66 |
defaultFont = new Font("SansSerif", 0, 12);
|
8966
|
67 |
} catch (Exception e) {
|
|
68 |
}
|
2
|
69 |
try {
|
8966
|
70 |
collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class.
|
|
71 |
getResource("/resources/images/collapsed.gif"));
|
|
72 |
expandedIcon = new ImageIcon(SampleTreeCellRenderer.class.
|
|
73 |
getResource("/resources/images/expanded.gif"));
|
2
|
74 |
} catch (Exception e) {
|
|
75 |
System.out.println("Couldn't load images: " + e);
|
|
76 |
}
|
|
77 |
}
|
|
78 |
/** Whether or not the item that was last configured is selected. */
|
8966
|
79 |
protected boolean selected;
|
2
|
80 |
|
|
81 |
/**
|
8966
|
82 |
* This is messaged from JTree whenever it needs to get the size
|
|
83 |
* of the component or it wants to draw it.
|
|
84 |
* This attempts to set the font based on value, which will be
|
|
85 |
* a TreeNode.
|
|
86 |
*/
|
2
|
87 |
public Component getTreeCellRendererComponent(JTree tree, Object value,
|
8966
|
88 |
boolean selected, boolean expanded,
|
|
89 |
boolean leaf, int row,
|
|
90 |
boolean hasFocus) {
|
|
91 |
String stringValue = tree.convertValueToText(value, selected,
|
|
92 |
expanded, leaf, row, hasFocus);
|
2
|
93 |
|
|
94 |
/* Set the text. */
|
|
95 |
setText(stringValue);
|
|
96 |
/* Tooltips used by the tree. */
|
|
97 |
setToolTipText(stringValue);
|
|
98 |
|
|
99 |
/* Set the image. */
|
8966
|
100 |
if (expanded) {
|
2
|
101 |
setIcon(expandedIcon);
|
8966
|
102 |
} else if (!leaf) {
|
2
|
103 |
setIcon(collapsedIcon);
|
8966
|
104 |
} else {
|
2
|
105 |
setIcon(null);
|
8966
|
106 |
}
|
2
|
107 |
|
|
108 |
/* Set the color and the font based on the SampleData userObject. */
|
8966
|
109 |
SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value).
|
|
110 |
getUserObject();
|
|
111 |
if (hasFocus) {
|
|
112 |
setForeground(UIManager.getColor("Tree.selectionForeground"));
|
|
113 |
} else {
|
2
|
114 |
setForeground(userObject.getColor());
|
8966
|
115 |
}
|
|
116 |
if (userObject.getFont() == null) {
|
2
|
117 |
setFont(defaultFont);
|
8966
|
118 |
} else {
|
2
|
119 |
setFont(userObject.getFont());
|
8966
|
120 |
}
|
2
|
121 |
|
|
122 |
/* Update the selected flag for the next paint. */
|
|
123 |
this.selected = selected;
|
|
124 |
|
|
125 |
return this;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
8966
|
129 |
* paint is subclassed to draw the background correctly. JLabel
|
|
130 |
* currently does not allow backgrounds other than white, and it
|
|
131 |
* will also fill behind the icon. Something that isn't desirable.
|
|
132 |
*/
|
|
133 |
@Override
|
2
|
134 |
public void paint(Graphics g) {
|
8966
|
135 |
Color bColor;
|
|
136 |
Icon currentI = getIcon();
|
2
|
137 |
|
8966
|
138 |
if (selected) {
|
|
139 |
bColor = SELECTED_BACKGROUND_COLOR;
|
|
140 |
} else if (getParent() != null) /* Pick background color up from parent (which will come from
|
|
141 |
the JTree we're contained in). */ {
|
2
|
142 |
bColor = getParent().getBackground();
|
8966
|
143 |
} else {
|
2
|
144 |
bColor = getBackground();
|
8966
|
145 |
}
|
2
|
146 |
g.setColor(bColor);
|
8966
|
147 |
if (currentI != null && getText() != null) {
|
|
148 |
int offset = (currentI.getIconWidth() + getIconTextGap());
|
2
|
149 |
|
|
150 |
if (getComponentOrientation().isLeftToRight()) {
|
|
151 |
g.fillRect(offset, 0, getWidth() - 1 - offset,
|
8966
|
152 |
getHeight() - 1);
|
|
153 |
} else {
|
2
|
154 |
g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
|
|
155 |
}
|
8966
|
156 |
} else {
|
|
157 |
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
|
2
|
158 |
}
|
|
159 |
super.paint(g);
|
|
160 |
}
|
|
161 |
}
|