001: /*
002: * $Id: DefaultListRenderer.java,v 1.10 2007/01/24 14:47:07 kleopatra Exp $
003: *
004: * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: *
021: */
022: package org.jdesktop.swingx.renderer;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.io.Serializable;
027:
028: import javax.swing.JComponent;
029: import javax.swing.JLabel;
030: import javax.swing.JList;
031: import javax.swing.ListCellRenderer;
032:
033: import org.jdesktop.swingx.RolloverRenderer;
034:
035: /**
036: * Adapter to glue SwingX renderer support to core api.
037: * <p>
038: *
039: * @author Jeanette Winzenburg
040: *
041: *
042: */
043: public class DefaultListRenderer implements ListCellRenderer,
044: RolloverRenderer, Serializable {
045:
046: protected ComponentProvider componentController;
047:
048: protected CellContext<JList> cellContext;
049:
050: /**
051: * Instantiates a default list renderer with the default component
052: * controller.
053: *
054: */
055: public DefaultListRenderer() {
056: this ((ComponentProvider) null);
057: }
058:
059: /**
060: * Instantiates a ListCellRenderer with the given componentController.
061: * If the controller is null, creates and uses a default.
062: *
063: * @param componentController the provider of the configured component to
064: * use for cell rendering
065: */
066: public DefaultListRenderer(ComponentProvider componentController) {
067: if (componentController == null) {
068: componentController = new LabelProvider();
069: }
070: this .componentController = componentController;
071: this .cellContext = new ListCellContext();
072: }
073:
074: /**
075: * Instantiates a default table renderer with a default component
076: * controller using the given converter.
077: *
078: * @param converter the converter to use for mapping the
079: * content value to a String representation.
080: *
081: */
082: public DefaultListRenderer(StringValue converter) {
083: this (new LabelProvider(converter));
084: }
085:
086: // -------------- implements javax.swing.table.ListCellRenderer
087: /**
088: *
089: * Returns a configured component, appropriate to render the given
090: * list cell.
091: *
092: * @param list the <code>JList</code> to render on
093: * @param value the value to assign to the cell
094: * @param isSelected true if cell is selected
095: * @param cellHasFocus true if cell has focus
096: * @param index the row index (in view coordinates) of the cell to render
097: * @return a component to render the given list cell.
098: */
099: public Component getListCellRendererComponent(JList list,
100: Object value, int index, boolean isSelected,
101: boolean cellHasFocus) {
102: cellContext.installContext(list, value, index, 0, isSelected,
103: cellHasFocus, true, true);
104: return componentController.getRendererComponent(cellContext);
105: }
106:
107: /**
108: * @param background
109: */
110: public void setBackground(Color background) {
111: componentController.getRendererController().setBackground(
112: background);
113:
114: }
115:
116: /**
117: * @param foreground
118: */
119: public void setForeground(Color foreground) {
120: componentController.getRendererController().setForeground(
121: foreground);
122: }
123:
124: //----------------- RolloverRenderer
125:
126: /**
127: * {@inheritDoc}
128: */
129: public void doClick() {
130: if (isEnabled()) {
131: ((RolloverRenderer) componentController).doClick();
132: }
133: }
134:
135: /**
136: * {@inheritDoc}
137: */
138: public boolean isEnabled() {
139: return (componentController instanceof RolloverRenderer)
140: && ((RolloverRenderer) componentController).isEnabled();
141: }
142:
143: }
|