1 /*
2
3 dsh-identify Lightweight components for identifiable beans.
4 Copyright (c) 2003-2019 held jointly by the individual authors.
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
20 > http://www.fsf.org/licensing/licenses/lgpl.html
21 > http://www.opensource.org/licenses/lgpl-license.php
22
23 */
24 package org.dishevelled.identify;
25
26 import java.awt.Image;
27 import java.awt.Component;
28
29 import javax.swing.JTree;
30 import javax.swing.JLabel;
31 import javax.swing.ImageIcon;
32
33 import javax.swing.tree.DefaultTreeCellRenderer;
34
35 import org.dishevelled.iconbundle.IconSize;
36 import org.dishevelled.iconbundle.IconState;
37 import org.dishevelled.iconbundle.IconBundle;
38 import org.dishevelled.iconbundle.IconTextDirection;
39
40 /**
41 * Tree cell renderer that displays the name property value
42 * and appropriate icon from an icon bundle for a given bean.
43 *
44 * @author Michael Heuer
45 */
46 public class IdTreeCellRenderer
47 extends DefaultTreeCellRenderer
48 {
49 /** Default icon size. */
50 public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
51
52 /** Icon size. */
53 private IconSize iconSize;
54
55 /** ImageIcon wrapper for image from icon bundle. */
56 private transient ImageIcon imageIcon;
57
58
59 /**
60 * Create a new tree cell renderer for identifiable beans with
61 * the default icon size.
62 *
63 * @see #DEFAULT_ICON_SIZE
64 */
65 public IdTreeCellRenderer()
66 {
67 this(DEFAULT_ICON_SIZE);
68 }
69
70 /**
71 * Create a new tree cell renderer for identifiable beans with
72 * the specified icon size.
73 *
74 * @param iconSize icon size, must not be null
75 */
76 public IdTreeCellRenderer(final IconSize iconSize)
77 {
78 super();
79 setIconSize(iconSize);
80 }
81
82
83 /**
84 * Return the icon size for this tree cell renderer.
85 *
86 * @return the icon size for this tree cell renderer
87 */
88 public final IconSize getIconSize()
89 {
90 return iconSize;
91 }
92
93 /**
94 * Set the icon size for this tree cell renderer to <code>iconSize</code>.
95 *
96 * @param iconSize icon size, must not be null
97 */
98 public final void setIconSize(final IconSize iconSize)
99 {
100 if (iconSize == null)
101 {
102 throw new IllegalArgumentException("iconSize must not be null");
103 }
104 this.iconSize = iconSize;
105 }
106
107 @Override
108 public Component getTreeCellRendererComponent(final JTree tree,
109 final Object value,
110 final boolean isSelected,
111 final boolean isExpanded,
112 final boolean isLeaf,
113 final int row,
114 final boolean hasFocus)
115 {
116 JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, isSelected, isExpanded, isLeaf, row, hasFocus);
117
118 String name = IdentifyUtils.getNameFor(value);
119 label.setText(name);
120
121 IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value);
122
123 if (iconBundle == null)
124 {
125 label.setIcon(null);
126 }
127 else
128 {
129 IconTextDirection textDirection = IdentifyUtils.determineTextDirection(label);
130 Image image = iconBundle.getImage(label, textDirection, IconState.NORMAL, iconSize);
131
132 if (imageIcon == null)
133 {
134 imageIcon = new ImageIcon(image);
135 }
136 else
137 {
138 imageIcon.setImage(image);
139 }
140 label.setIcon(imageIcon);
141 }
142
143 return label;
144 }
145 }