KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > swing > GenericTableCellRenderer


1 // ============================================================================
2
// $Id: GenericTableCellRenderer.java,v 1.14 2005/08/31 14:00:52 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32
package net.sf.jga.swing;
33
34 import java.text.Format JavaDoc;
35 import java.text.NumberFormat JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
38 import net.sf.jga.fn.UnaryFunctor;
39 import net.sf.jga.fn.adaptor.Constant;
40 import net.sf.jga.fn.comparison.EqualTo;
41 import net.sf.jga.fn.string.DefaultFormat;
42 import net.sf.jga.fn.string.FormatValue;
43 import net.sf.jga.util.Formattable;
44
45 /**
46  * TableCellRenderer that passes the contents through a functor to render the
47  * contents, instead of calling toString() on the contents.
48  * <p>
49  * Copyright &copy; 2003-2005 David A. Hall
50  *
51  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
52  */

53
54 public class GenericTableCellRenderer<T> extends DefaultTableCellRenderer JavaDoc implements Formattable<T> {
55
56     static final long serialVersionUID = -1084070441724877620L;
57
58     private UnaryFunctor<T,String JavaDoc> _formatter;
59     private boolean _formatNulls = false;
60
61     /**
62      * Builds a TableCellRenderer that uses a default formatter
63      */

64     public GenericTableCellRenderer () {
65         this(new DefaultFormat<T>(), false);
66     }
67
68     /**
69      * Builds a TableCellRenderer that uses the given functor to format values.
70      */

71     public GenericTableCellRenderer (UnaryFunctor<T,String JavaDoc> formatter) {
72         this(formatter, false);
73     }
74
75     /**
76      * Builds a TableCellRenderer that uses the given functor to format values,
77      * and the given flag to determine if nulls are formatted.
78      */

79     public GenericTableCellRenderer (UnaryFunctor<T,String JavaDoc> formatter,
80                                      boolean formatsNulls)
81     {
82         if (formatter == null) {
83             throw new IllegalArgumentException JavaDoc("Non-null functor required");
84         }
85         
86         _formatter = formatter;
87         _formatNulls = formatsNulls;
88     }
89
90     /**
91      * Overrides the base class setValue:
92      */

93     protected void setValue(Object JavaDoc value) {
94         if (value == null && !_formatNulls) {
95             setText("");
96             return;
97         }
98         
99         try {
100             // @SuppressWarning
101
// ClassCastException is explicitely caught and sort of dealt with
102
setText(_formatter.fn((T)value));
103         }
104         catch (ClassCastException JavaDoc x) {
105             setText("#" +value +"#");
106         }
107         catch (IllegalArgumentException JavaDoc x) {
108             setText("#" +value +"#");
109         }
110     }
111
112     public String JavaDoc toString() {
113         return "GenericTableCellRenderer["+_formatter+"]";
114     }
115     
116     // ---------------------
117
// Formattable interface
118
// ---------------------
119

120     /**
121      * Changes the format of an existing renderer
122      * @throws IllegalArgumentException
123      */

124     public void setFormat(UnaryFunctor<T,String JavaDoc> formatter) {
125         if (formatter == null) {
126             throw new IllegalArgumentException JavaDoc("Non-null functor required");
127         }
128         
129         _formatter = formatter;
130     }
131 }
132
Popular Tags