001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.components.list;
031:
032: import java.awt.Color;
033: import java.awt.Component;
034:
035: import javax.swing.JLabel;
036: import javax.swing.JList;
037: import javax.swing.ListCellRenderer;
038: import javax.swing.LookAndFeel;
039: import javax.swing.UIManager;
040:
041: import com.jeta.forms.store.properties.ListItemProperty;
042:
043: /**
044: * Responsible for rendering ListItemProperty objects. This renderer is needed
045: * to support icons for list and combo box items that are specified in the
046: * designer. The runtime will automatically create ListItemProperties for these
047: * items.
048: *
049: * @author Jeff Tassin
050: */
051: public class ListItemRenderer extends JLabel implements
052: ListCellRenderer {
053: /**
054: * The foreground and background colors for a list cell. These can change if
055: * the look and feel changes.
056: */
057: private Color m_sel_bg;
058: private Color m_sel_fg;
059: private Color m_bg;
060: private Color m_fg;
061:
062: /**
063: * Keep a reference to the current look and feel. Check each time we render
064: * in case the look and feel has changed.
065: */
066: private LookAndFeel m_laf;
067:
068: public ListItemRenderer() {
069: /**
070: * This is needed for some look and feels or the background color won't
071: * show.
072: */
073: setOpaque(true);
074: }
075:
076: /**
077: * ListCellRenderer implementation.
078: */
079: public Component getListCellRendererComponent(JList list,
080: Object value, int index, boolean isSelected,
081: boolean cellHasFocus) {
082: LookAndFeel laf = UIManager.getLookAndFeel();
083: if (laf != m_laf) {
084: m_laf = laf;
085: updateColorCache();
086: }
087:
088: if (isSelected) {
089: setBackground(m_sel_bg);
090: setForeground(m_sel_fg);
091: } else {
092: setBackground(m_bg);
093: setForeground(m_fg);
094: }
095:
096: if (value instanceof ListItemProperty) {
097: ListItemProperty prop = (ListItemProperty) value;
098:
099: setIcon(prop.icon());
100: String txt = prop.getLabel();
101: if (txt == null)
102: txt = "";
103: setText(txt);
104: } else {
105: if (value == null)
106: value = "";
107: setText(value.toString());
108: }
109: return this ;
110: }
111:
112: /**
113: * Caches the foreground/background colors for a list box for the current
114: * look and feel.
115: */
116: private void updateColorCache() {
117: m_sel_fg = UIManager.getColor("List.selectionForeground");
118: m_sel_bg = UIManager.getColor("List.selectionBackground");
119: m_bg = UIManager.getColor("List.background");
120: m_fg = UIManager.getColor("List.foreground");
121: setOpaque(true);
122: }
123: }
|