KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: GenericTableColumn.java,v 1.7 2005/08/02 23:45:21 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 javax.swing.table.TableCellEditor JavaDoc;
35 import javax.swing.table.TableCellRenderer JavaDoc;
36 import javax.swing.table.TableColumn JavaDoc;
37 import net.sf.jga.fn.BinaryFunctor;
38 import net.sf.jga.fn.UnaryFunctor;
39
40 /**
41  * Column class used in conjuction with GenericTableModel.
42  * <p>
43  * Copyright &copy; 2003-2005 David A. Hall
44  *
45  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
46  */

47
48 public class GenericTableColumn<R,C> extends TableColumn JavaDoc {
49     
50     static final long serialVersionUID = 1960293231258498520L;
51     
52     private Class JavaDoc<C> _coltype;
53     private UnaryFunctor<R,C> _getFn;
54     private BinaryFunctor<R,C,C> _setFn;
55
56     /**
57      * Builds a read-only column that will apply the given functor to objects of
58      * the given class to get the value of specific cells. A default table
59      * renderer will be used, as defined by the Table that uses this column.
60      */

61     public GenericTableColumn(Class JavaDoc<C> coltype, UnaryFunctor<R,C> getFn) {
62         this(coltype, getFn, null);
63     }
64
65     /**
66      * Builds a potentially editable column that will apply the given functors to
67      * objects of the given class to get and set the value of specific cells.
68      * The setFn can be null: if it is, then this column will not be editable.
69      * A default table renderer and editor will be used, as defined by the Table
70      * that uses this column.
71      *
72      */

73     public GenericTableColumn(Class JavaDoc<C> coltype, UnaryFunctor<R,C> getFn,
74                               BinaryFunctor<R,C,C> setFn)
75     {
76         _coltype = coltype;
77         _getFn = getFn;
78         _setFn = setFn;
79     }
80     
81     /**
82      * Builds a potentially editable column that will apply the given functors
83      * to objects of the given class to get and set the value of specific cells.
84      * The setFn can be null: if it is, then this column will not be editable.
85      * The formatter will be used to build a GenericTableCellRenderer for the
86      * column if it is non-null. If formatter, parser, and setFn are all
87      * non-null, then a GenericCellEditor will be built for this column. If any
88      * of these fields are null, the the Table that uses this column will supply
89      * the default renderer/editor.
90      */

91     public GenericTableColumn(Class JavaDoc<C> coltype, UnaryFunctor<R,C> getFn,
92                               BinaryFunctor<R,C,C> setFn,
93                               UnaryFunctor<C,String JavaDoc> formatter,
94                               UnaryFunctor<String JavaDoc,C> parser)
95     {
96         this(coltype, getFn, setFn);
97      
98         if (formatter != null) {
99             setCellRenderer(new GenericTableCellRenderer<C>(formatter));
100
101             if (parser != null && setFn != null) {
102                 setCellEditor(new GenericCellEditor<C>(formatter, parser));
103             }
104         }
105     }
106     
107
108     /**
109      * Builds a potentially editable column that will apply the given functors
110      * to objects of the given class to get and set the value of specific cells.
111      * The setFn can be null: if it is, then this column will not be editable.
112      * Either the renderer or editor may be null: the Table will supply the
113      * default renderer/editor in this case. Note that if the setFn is null,
114      * there will be no editor configured (as it would never be called anyway).
115      */

116     public GenericTableColumn(Class JavaDoc<C> coltype, UnaryFunctor<R,C> getFn,
117                               BinaryFunctor<R,C,C> setFn,
118                               TableCellRenderer JavaDoc renderer, TableCellEditor JavaDoc editor)
119     {
120         this(coltype, getFn, setFn);
121         setCellRenderer(renderer);
122         if (setFn != null)
123             setCellEditor(editor);
124     }
125     
126
127     /*
128      *
129      */

130     public C getValueAt (R rowvalue) {
131         return _getFn.fn(rowvalue);
132     }
133
134     /*
135      *
136      */

137     public void setValueAt (R rowvalue, Object JavaDoc obj) {
138         if (_setFn != null) {
139             // @SuppressWarnings
140
//
141
_setFn.fn(rowvalue, (C) obj);
142         }
143     }
144     
145     /*
146      *
147      */

148     public boolean isEditable() {
149         return _setFn != null;
150     }
151
152     /*
153      *
154      */

155     public Class JavaDoc<C> getColumnClass() {
156         return _coltype;
157     }
158     
159     /*
160      *
161      */

162     public String JavaDoc toString() {
163         return super.toString() +"["+_getFn+","+_setFn+"]";
164     }
165 }
166
Popular Tags