001: // ============================================================================
002: // $Id: GenericListCellRenderer.java,v 1.11 2005/08/31 14:15:27 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.swing;
033:
034: import java.awt.Color;
035: import java.awt.Component;
036: import javax.swing.DefaultListCellRenderer;
037: import javax.swing.Icon;
038: import javax.swing.JLabel;
039: import javax.swing.JList;
040: import net.sf.jga.fn.UnaryFunctor;
041: import net.sf.jga.fn.string.DefaultFormat;
042: import net.sf.jga.util.Formattable;
043:
044: /**
045: * ListCellRenderer that passes the contents through a functor to render the
046: * contents, instead of calling toString() on the contents. The functor must
047: * take an argument of some type T and return a string.
048: * <p>
049: * Copyright © 2002-2005 David A. Hall
050: *
051: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
052: */
053:
054: public class GenericListCellRenderer<T> extends DefaultListCellRenderer
055: implements Formattable<T> {
056:
057: static final long serialVersionUID = -8781266653605837205L;
058:
059: private UnaryFunctor<T, String> _uf;
060:
061: public GenericListCellRenderer() {
062: this (new DefaultFormat<T>());
063: }
064:
065: public GenericListCellRenderer(UnaryFunctor<T, String> uf) {
066: if (uf == null) {
067: throw new IllegalArgumentException(
068: "Non-null functor required");
069: }
070:
071: _uf = uf;
072: }
073:
074: public Component getListCellRendererComponent(JList list,
075: Object value, int idx, boolean sel, boolean focus) {
076: Component comp = super .getListCellRendererComponent(list,
077: value, idx, sel, focus);
078: if (comp instanceof JLabel && !(value instanceof Icon)) {
079: // @SuppressWarnings
080: //
081: ((JLabel) comp).setText(_uf.fn((T) value));
082: }
083:
084: return comp;
085: }
086:
087: // ---------------------
088: // Formattable interface
089: // ---------------------
090:
091: /**
092: * Changes the format of an existing renderer
093: */
094: public void setFormat(UnaryFunctor<T, String> formatter) {
095: if (formatter == null) {
096: throw new IllegalArgumentException(
097: "Non-null functor required");
098: }
099:
100: _uf = formatter;
101: }
102: }
|