001: // ============================================================================
002: // $Id: GenericTableColumn.java,v 1.7 2005/08/02 23:45:21 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 javax.swing.table.TableCellEditor;
035: import javax.swing.table.TableCellRenderer;
036: import javax.swing.table.TableColumn;
037: import net.sf.jga.fn.BinaryFunctor;
038: import net.sf.jga.fn.UnaryFunctor;
039:
040: /**
041: * Column class used in conjuction with GenericTableModel.
042: * <p>
043: * Copyright © 2003-2005 David A. Hall
044: *
045: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
046: */
047:
048: public class GenericTableColumn<R, C> extends TableColumn {
049:
050: static final long serialVersionUID = 1960293231258498520L;
051:
052: private Class<C> _coltype;
053: private UnaryFunctor<R, C> _getFn;
054: private BinaryFunctor<R, C, C> _setFn;
055:
056: /**
057: * Builds a read-only column that will apply the given functor to objects of
058: * the given class to get the value of specific cells. A default table
059: * renderer will be used, as defined by the Table that uses this column.
060: */
061: public GenericTableColumn(Class<C> coltype, UnaryFunctor<R, C> getFn) {
062: this (coltype, getFn, null);
063: }
064:
065: /**
066: * Builds a potentially editable column that will apply the given functors to
067: * objects of the given class to get and set the value of specific cells.
068: * The setFn can be null: if it is, then this column will not be editable.
069: * A default table renderer and editor will be used, as defined by the Table
070: * that uses this column.
071: *
072: */
073: public GenericTableColumn(Class<C> coltype,
074: UnaryFunctor<R, C> getFn, BinaryFunctor<R, C, C> setFn) {
075: _coltype = coltype;
076: _getFn = getFn;
077: _setFn = setFn;
078: }
079:
080: /**
081: * Builds a potentially editable column that will apply the given functors
082: * to objects of the given class to get and set the value of specific cells.
083: * The setFn can be null: if it is, then this column will not be editable.
084: * The formatter will be used to build a GenericTableCellRenderer for the
085: * column if it is non-null. If formatter, parser, and setFn are all
086: * non-null, then a GenericCellEditor will be built for this column. If any
087: * of these fields are null, the the Table that uses this column will supply
088: * the default renderer/editor.
089: */
090: public GenericTableColumn(Class<C> coltype,
091: UnaryFunctor<R, C> getFn, BinaryFunctor<R, C, C> setFn,
092: UnaryFunctor<C, String> formatter,
093: UnaryFunctor<String, C> parser) {
094: this (coltype, getFn, setFn);
095:
096: if (formatter != null) {
097: setCellRenderer(new GenericTableCellRenderer<C>(formatter));
098:
099: if (parser != null && setFn != null) {
100: setCellEditor(new GenericCellEditor<C>(formatter,
101: parser));
102: }
103: }
104: }
105:
106: /**
107: * Builds a potentially editable column that will apply the given functors
108: * to objects of the given class to get and set the value of specific cells.
109: * The setFn can be null: if it is, then this column will not be editable.
110: * Either the renderer or editor may be null: the Table will supply the
111: * default renderer/editor in this case. Note that if the setFn is null,
112: * there will be no editor configured (as it would never be called anyway).
113: */
114: public GenericTableColumn(Class<C> coltype,
115: UnaryFunctor<R, C> getFn, BinaryFunctor<R, C, C> setFn,
116: TableCellRenderer renderer, TableCellEditor editor) {
117: this (coltype, getFn, setFn);
118: setCellRenderer(renderer);
119: if (setFn != null)
120: setCellEditor(editor);
121: }
122:
123: /*
124: *
125: */
126: public C getValueAt(R rowvalue) {
127: return _getFn.fn(rowvalue);
128: }
129:
130: /*
131: *
132: */
133: public void setValueAt(R rowvalue, Object obj) {
134: if (_setFn != null) {
135: // @SuppressWarnings
136: //
137: _setFn.fn(rowvalue, (C) obj);
138: }
139: }
140:
141: /*
142: *
143: */
144: public boolean isEditable() {
145: return _setFn != null;
146: }
147:
148: /*
149: *
150: */
151: public Class<C> getColumnClass() {
152: return _coltype;
153: }
154:
155: /*
156: *
157: */
158: public String toString() {
159: return super .toString() + "[" + _getFn + "," + _setFn + "]";
160: }
161: }
|