001: // ============================================================================
002: // $Id: GenericTableCellRenderer.java,v 1.14 2005/08/31 14:00:52 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.text.Format;
035: import java.text.NumberFormat;
036: import javax.swing.JLabel;
037: import javax.swing.table.DefaultTableCellRenderer;
038: import net.sf.jga.fn.UnaryFunctor;
039: import net.sf.jga.fn.adaptor.Constant;
040: import net.sf.jga.fn.comparison.EqualTo;
041: import net.sf.jga.fn.string.DefaultFormat;
042: import net.sf.jga.fn.string.FormatValue;
043: import net.sf.jga.util.Formattable;
044:
045: /**
046: * TableCellRenderer that passes the contents through a functor to render the
047: * contents, instead of calling toString() on the contents.
048: * <p>
049: * Copyright © 2003-2005 David A. Hall
050: *
051: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
052: */
053:
054: public class GenericTableCellRenderer<T> extends
055: DefaultTableCellRenderer implements Formattable<T> {
056:
057: static final long serialVersionUID = -1084070441724877620L;
058:
059: private UnaryFunctor<T, String> _formatter;
060: private boolean _formatNulls = false;
061:
062: /**
063: * Builds a TableCellRenderer that uses a default formatter
064: */
065: public GenericTableCellRenderer() {
066: this (new DefaultFormat<T>(), false);
067: }
068:
069: /**
070: * Builds a TableCellRenderer that uses the given functor to format values.
071: */
072: public GenericTableCellRenderer(UnaryFunctor<T, String> formatter) {
073: this (formatter, false);
074: }
075:
076: /**
077: * Builds a TableCellRenderer that uses the given functor to format values,
078: * and the given flag to determine if nulls are formatted.
079: */
080: public GenericTableCellRenderer(UnaryFunctor<T, String> formatter,
081: boolean formatsNulls) {
082: if (formatter == null) {
083: throw new IllegalArgumentException(
084: "Non-null functor required");
085: }
086:
087: _formatter = formatter;
088: _formatNulls = formatsNulls;
089: }
090:
091: /**
092: * Overrides the base class setValue:
093: */
094: protected void setValue(Object value) {
095: if (value == null && !_formatNulls) {
096: setText("");
097: return;
098: }
099:
100: try {
101: // @SuppressWarning
102: // ClassCastException is explicitely caught and sort of dealt with
103: setText(_formatter.fn((T) value));
104: } catch (ClassCastException x) {
105: setText("#" + value + "#");
106: } catch (IllegalArgumentException x) {
107: setText("#" + value + "#");
108: }
109: }
110:
111: public String toString() {
112: return "GenericTableCellRenderer[" + _formatter + "]";
113: }
114:
115: // ---------------------
116: // Formattable interface
117: // ---------------------
118:
119: /**
120: * Changes the format of an existing renderer
121: * @throws IllegalArgumentException
122: */
123: public void setFormat(UnaryFunctor<T, String> formatter) {
124: if (formatter == null) {
125: throw new IllegalArgumentException(
126: "Non-null functor required");
127: }
128:
129: _formatter = formatter;
130: }
131: }
|