01: /*
02: * Copyright (C) 2004 NNL Technology AB
03: * Visit www.infonode.net for information about InfoNode(R)
04: * products and how to contact NNL Technology AB.
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19: * MA 02111-1307, USA.
20: */
21:
22: // $Id: TextIconListCellRenderer.java,v 1.11 2006/06/13 20:12:39 johan Exp $
23: package net.infonode.gui;
24:
25: import net.infonode.gui.icon.IconUtil;
26:
27: import javax.swing.*;
28: import java.awt.*;
29:
30: /**
31: * @author johan
32: */
33: public class TextIconListCellRenderer extends DefaultListCellRenderer {
34: private ListCellRenderer renderer;
35: private Icon emptyIcon;
36: private int width;
37: private int gap = -1;
38:
39: public TextIconListCellRenderer(ListCellRenderer renderer) {
40: this .renderer = renderer;
41: }
42:
43: public void calculateMaximumIconWidth(Object[] list) {
44: width = IconUtil.getMaxIconWidth(list);
45: emptyIcon = width == 0 ? null : new Icon() {
46: public int getIconHeight() {
47: return 1;
48: }
49:
50: public int getIconWidth() {
51: return width;
52: }
53:
54: public void paintIcon(Component c, Graphics g, int x, int y) {
55: }
56: };
57: }
58:
59: public void setRenderer(ListCellRenderer renderer) {
60: this .renderer = renderer;
61: }
62:
63: public Component getListCellRendererComponent(JList list,
64: Object value, int index, boolean isSelected,
65: boolean cellHasFocus) {
66: if (index == -1)
67: return null;
68:
69: JLabel label = (JLabel) renderer.getListCellRendererComponent(
70: list, value, index, isSelected, cellHasFocus);
71: if (gap < 0)
72: gap = label.getIconTextGap();
73:
74: Icon icon = IconUtil.getIcon(value);
75:
76: if (icon == null) {
77: label.setIcon(emptyIcon);
78: } else {
79: label.setIcon(icon);
80: label.setIconTextGap(gap + width - icon.getIconWidth());
81: }
82:
83: return label;
84: }
85: }
|