author | michaelm |
Fri, 16 Feb 2018 10:34:17 +0000 | |
branch | http-client-branch |
changeset 56137 | dd867826d55b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
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 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8966
diff
changeset
|
40 |
|
2 | 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; |
|
8966 | 52 |
import javax.swing.UIManager; |
2 | 53 |
|
8966 | 54 |
|
55 |
@SuppressWarnings("serial") |
|
56 |
public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer { |
|
57 |
||
2 | 58 |
/** Font used if the string to be displayed isn't a font. */ |
8966 | 59 |
protected static Font defaultFont; |
2 | 60 |
/** Icon to use when the item is collapsed. */ |
8966 | 61 |
protected static ImageIcon collapsedIcon; |
2 | 62 |
/** Icon to use when the item is expanded. */ |
8966 | 63 |
protected static ImageIcon expandedIcon; |
2 | 64 |
/** Color to use for the background when selected. */ |
8966 | 65 |
protected static final Color SELECTED_BACKGROUND_COLOR; |
2 | 66 |
|
8966 | 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 |
} |
|
2 | 74 |
try { |
75 |
defaultFont = new Font("SansSerif", 0, 12); |
|
8966 | 76 |
} catch (Exception e) { |
77 |
} |
|
2 | 78 |
try { |
8966 | 79 |
collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class. |
80 |
getResource("/resources/images/collapsed.gif")); |
|
81 |
expandedIcon = new ImageIcon(SampleTreeCellRenderer.class. |
|
82 |
getResource("/resources/images/expanded.gif")); |
|
2 | 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. */ |
|
8966 | 88 |
protected boolean selected; |
2 | 89 |
|
90 |
/** |
|
8966 | 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 |
*/ |
|
2 | 96 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
8966 | 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); |
|
2 | 102 |
|
103 |
/* Set the text. */ |
|
104 |
setText(stringValue); |
|
105 |
/* Tooltips used by the tree. */ |
|
106 |
setToolTipText(stringValue); |
|
107 |
||
108 |
/* Set the image. */ |
|
8966 | 109 |
if (expanded) { |
2 | 110 |
setIcon(expandedIcon); |
8966 | 111 |
} else if (!leaf) { |
2 | 112 |
setIcon(collapsedIcon); |
8966 | 113 |
} else { |
2 | 114 |
setIcon(null); |
8966 | 115 |
} |
2 | 116 |
|
117 |
/* Set the color and the font based on the SampleData userObject. */ |
|
8966 | 118 |
SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value). |
119 |
getUserObject(); |
|
120 |
if (hasFocus) { |
|
121 |
setForeground(UIManager.getColor("Tree.selectionForeground")); |
|
122 |
} else { |
|
2 | 123 |
setForeground(userObject.getColor()); |
8966 | 124 |
} |
125 |
if (userObject.getFont() == null) { |
|
2 | 126 |
setFont(defaultFont); |
8966 | 127 |
} else { |
2 | 128 |
setFont(userObject.getFont()); |
8966 | 129 |
} |
2 | 130 |
|
131 |
/* Update the selected flag for the next paint. */ |
|
132 |
this.selected = selected; |
|
133 |
||
134 |
return this; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
8966 | 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 |
|
2 | 143 |
public void paint(Graphics g) { |
8966 | 144 |
Color bColor; |
145 |
Icon currentI = getIcon(); |
|
2 | 146 |
|
8966 | 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). */ { |
|
2 | 151 |
bColor = getParent().getBackground(); |
8966 | 152 |
} else { |
2 | 153 |
bColor = getBackground(); |
8966 | 154 |
} |
2 | 155 |
g.setColor(bColor); |
8966 | 156 |
if (currentI != null && getText() != null) { |
157 |
int offset = (currentI.getIconWidth() + getIconTextGap()); |
|
2 | 158 |
|
159 |
if (getComponentOrientation().isLeftToRight()) { |
|
160 |
g.fillRect(offset, 0, getWidth() - 1 - offset, |
|
8966 | 161 |
getHeight() - 1); |
162 |
} else { |
|
2 | 163 |
g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1); |
164 |
} |
|
8966 | 165 |
} else { |
166 |
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); |
|
2 | 167 |
} |
168 |
super.paint(g); |
|
169 |
} |
|
170 |
} |