01: package com.xoetrope.swing;
02:
03: import java.awt.Component;
04: import javax.swing.DefaultListCellRenderer;
05: import javax.swing.ImageIcon;
06: import javax.swing.JList;
07: import net.xoetrope.xui.XProject;
08: import net.xoetrope.xui.XProjectManager;
09: import net.xoetrope.xui.style.XStyle;
10: import net.xoetrope.xui.style.XStyleManager;
11:
12: /**
13: * Renders a combobox list with an image icon
14: */
15: public class XGraphicListCellRenderer extends DefaultListCellRenderer {
16: private String[] severityImageName = { "severity0.gif",
17: "severity1.gif", "severity2.gif", "severity3.gif",
18: "severity4.gif", };
19:
20: private ImageIcon[] severityImages;
21: private XStyleManager styleManager;
22:
23: public XGraphicListCellRenderer() {
24: super ();
25: XProject currentProject = XProjectManager.getCurrentProject();
26: styleManager = currentProject.getStyleManager();
27: severityImages = new ImageIcon[severityImageName.length];
28: for (int i = 0; i < severityImages.length; i++)
29: severityImages[i] = (ImageIcon) currentProject
30: .getIcon(severityImageName[i]);
31: }
32:
33: /*
34: * This method finds the image and text corresponding
35: * to the selected value and returns the label, set up
36: * to display the text and image.
37: */
38: public Component getListCellRendererComponent(JList list,
39: Object value, int index, boolean isSelected,
40: boolean cellHasFocus) {
41: Component renderer = super .getListCellRendererComponent(list,
42: value, index, isSelected, cellHasFocus);
43:
44: renderer.setFont(list.getFont());
45: if (isSelected) {
46: renderer.setBackground(list.getSelectionBackground());
47: renderer.setForeground(list.getSelectionForeground());
48: } else {
49: XStyle comboStyle = styleManager.getStyle("base/edit");
50: renderer.setBackground(comboStyle
51: .getStyleAsColor(comboStyle.COLOR_BACK));
52: renderer.setForeground(comboStyle
53: .getStyleAsColor(comboStyle.COLOR_FORE));
54: }
55:
56: //Set the icon and text. If icon was null, say so.
57: if (index >= 0)
58: setIcon(severityImages[index % severityImageName.length]);
59:
60: return renderer;
61: }
62: }
|