Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/SampleTree/SampleTreeCellRenderer.java
41149 views
1
/*
2
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
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
*
15
* - Neither the name of Oracle nor the names of its
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
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
42
import javax.swing.Icon;
43
import javax.swing.ImageIcon;
44
import javax.swing.JLabel;
45
import javax.swing.JTree;
46
import javax.swing.tree.TreeCellRenderer;
47
import javax.swing.tree.DefaultMutableTreeNode;
48
import java.awt.Component;
49
import java.awt.Color;
50
import java.awt.Font;
51
import java.awt.Graphics;
52
import javax.swing.UIManager;
53
54
55
@SuppressWarnings("serial")
56
public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer {
57
58
/** Font used if the string to be displayed isn't a font. */
59
protected static Font defaultFont;
60
/** Icon to use when the item is collapsed. */
61
protected static ImageIcon collapsedIcon;
62
/** Icon to use when the item is expanded. */
63
protected static ImageIcon expandedIcon;
64
/** Color to use for the background when selected. */
65
protected static final Color SELECTED_BACKGROUND_COLOR;
66
67
static {
68
if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
69
SELECTED_BACKGROUND_COLOR = new Color(0, 0,
70
0, 0);
71
} else {
72
SELECTED_BACKGROUND_COLOR = Color.YELLOW;
73
}
74
try {
75
defaultFont = new Font("SansSerif", 0, 12);
76
} catch (Exception e) {
77
}
78
try {
79
collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class.
80
getResource("/resources/images/collapsed.gif"));
81
expandedIcon = new ImageIcon(SampleTreeCellRenderer.class.
82
getResource("/resources/images/expanded.gif"));
83
} catch (Exception e) {
84
System.out.println("Couldn't load images: " + e);
85
}
86
}
87
/** Whether or not the item that was last configured is selected. */
88
protected boolean selected;
89
90
/**
91
* This is messaged from JTree whenever it needs to get the size
92
* of the component or it wants to draw it.
93
* This attempts to set the font based on value, which will be
94
* a TreeNode.
95
*/
96
public Component getTreeCellRendererComponent(JTree tree, Object value,
97
boolean selected, boolean expanded,
98
boolean leaf, int row,
99
boolean hasFocus) {
100
String stringValue = tree.convertValueToText(value, selected,
101
expanded, leaf, row, hasFocus);
102
103
/* Set the text. */
104
setText(stringValue);
105
/* Tooltips used by the tree. */
106
setToolTipText(stringValue);
107
108
/* Set the image. */
109
if (expanded) {
110
setIcon(expandedIcon);
111
} else if (!leaf) {
112
setIcon(collapsedIcon);
113
} else {
114
setIcon(null);
115
}
116
117
/* Set the color and the font based on the SampleData userObject. */
118
SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value).
119
getUserObject();
120
if (hasFocus) {
121
setForeground(UIManager.getColor("Tree.selectionForeground"));
122
} else {
123
setForeground(userObject.getColor());
124
}
125
if (userObject.getFont() == null) {
126
setFont(defaultFont);
127
} else {
128
setFont(userObject.getFont());
129
}
130
131
/* Update the selected flag for the next paint. */
132
this.selected = selected;
133
134
return this;
135
}
136
137
/**
138
* paint is subclassed to draw the background correctly. JLabel
139
* currently does not allow backgrounds other than white, and it
140
* will also fill behind the icon. Something that isn't desirable.
141
*/
142
@Override
143
public void paint(Graphics g) {
144
Color bColor;
145
Icon currentI = getIcon();
146
147
if (selected) {
148
bColor = SELECTED_BACKGROUND_COLOR;
149
} else if (getParent() != null) /* Pick background color up from parent (which will come from
150
the JTree we're contained in). */ {
151
bColor = getParent().getBackground();
152
} else {
153
bColor = getBackground();
154
}
155
g.setColor(bColor);
156
if (currentI != null && getText() != null) {
157
int offset = (currentI.getIconWidth() + getIconTextGap());
158
159
if (getComponentOrientation().isLeftToRight()) {
160
g.fillRect(offset, 0, getWidth() - 1 - offset,
161
getHeight() - 1);
162
} else {
163
g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
164
}
165
} else {
166
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
167
}
168
super.paint(g);
169
}
170
}
171
172