View Javadoc

    1   /*
    2    $Id: DefaultTableModel.java,v 1.2 2003/11/04 12:00:48 jstrachan Exp $
    3
    4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    5
    6    Redistribution and use of this software and associated documentation
    7    ("Software"), with or without modification, are permitted provided
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    44
    45   */
    46  package groovy.model;
    47
    48  import groovy.lang.Closure;
    49
    50  import java.util.Collections;
    51  import java.util.List;
    52
    53  import javax.swing.table.AbstractTableModel;
    54  import javax.swing.table.DefaultTableColumnModel;
    55  import javax.swing.table.TableColumnModel;
    56
    57  import org.codehaus.groovy.runtime.InvokerHelper;
    58
    59  /***
    60   * A default table model made up of PropertyModels on a Value model.
    61   *
    62   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    63   * @version $Revision: 1.2 $
    64   */
    65  public class DefaultTableModel extends AbstractTableModel {
    66
    67      private ValueModel rowModel;
    68      private ValueModel rowsModel;
    69      private MyTableColumnModel columnModel = new MyTableColumnModel();
    70
    71      public DefaultTableModel(ValueModel rowsModel) {
    72          this(rowsModel, new ValueHolder());
    73      }
    74
    75      public DefaultTableModel(ValueModel rowsModel, ValueModel rowModel) {
    76          this.rowModel = rowModel;
    77          this.rowsModel = rowsModel;
    78      }
    79
    80      /***
    81       * @return the column definitions.
    82       */
    83      public List getColumnList() {
    84          return columnModel.getColumnList();
    85      }
    86
    87      public TableColumnModel getColumnModel() {
    88          return columnModel;
    89      }
    90
    91      /***
    92       * Adds a property model column to the table
    93       */
    94      public DefaultTableColumn addPropertyColumn(Object headerValue, String property, Class type) {
    95          return addColumn(headerValue, new PropertyModel(rowModel, property, type));
    96      }
    97
    98      /***
    99       * Adds a closure based column to the table
    100      */
    101     public DefaultTableColumn addClosureColumn(Object headerValue, Closure readClosure, Closure writeClosure, Class type) {
    102         return addColumn(headerValue, new ClosureModel(rowModel, readClosure, writeClosure, type));
    103     }
    104
    105     public DefaultTableColumn addColumn(Object headerValue, ValueModel columnValueModel) {
    106         DefaultTableColumn answer = new DefaultTableColumn(headerValue, columnValueModel);
    107         addColumn(answer);
    108         return answer;
    109     }
    110
    111     /***
    112      * Adds a new column definition to the table
    113      */
    114     public void addColumn(DefaultTableColumn column) {
    115         columnModel.addColumn(column);
    116     }
    117
    118     /***
    119      * Removes a column definition from the table
    120      */
    121     public void removeColumn(DefaultTableColumn column) {
    122         columnModel.removeColumn(column);
    123     }
    124
    125     public int getRowCount() {
    126         return getRows().size();
    127     }
    128
    129     public int getColumnCount() {
    130         return columnModel.getColumnCount();
    131     }
    132
    133     public String getColumnName(int columnIndex) {
    134         String answer = null;
    135         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
    136             return answer;
    137         }
    138         Object value = columnModel.getColumn(columnIndex).getHeaderValue();
    139         if (value != null) {
    140             return value.toString();
    141         }
    142         return answer;
    143     }
    144
    145     public Class getColumnClass(int columnIndex) {
    146         return getColumnModel(columnIndex).getType();
    147     }
    148
    149     public boolean isCellEditable(int rowIndex, int columnIndex) {
    150         return getColumnModel(columnIndex).isEditable();
    151     }
    152
    153     public Object getValueAt(int rowIndex, int columnIndex) {
    154         List rows = getRows();
    155         Object answer = null;
    156         if (rowIndex < 0 || rowIndex >= rows.size()) {
    157             return answer;
    158         }
    159         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
    160             return answer;
    161         }
    162         Object row = getRows().get(rowIndex);
    163         rowModel.setValue(row);
    164         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
    165         if (row == null || column == null) {
    166             return answer;
    167         }
    168         return column.getValue(row, rowIndex, columnIndex);
    169     }
    170
    171     public void setValueAt(Object value, int rowIndex, int columnIndex) {
    172         List rows = getRows();
    173         if (rowIndex < 0 || rowIndex >= rows.size()) {
    174             return;
    175         }
    176         if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) {
    177             return;
    178         }
    179         Object row = getRows().get(rowIndex);
    180         rowModel.setValue(row);
    181         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
    182         if (row == null || column == null) {
    183             return;
    184         }
    185         column.setValue(row, value, rowIndex, columnIndex);
    186     }
    187
    188     protected ValueModel getColumnModel(int columnIndex) {
    189         DefaultTableColumn column = (DefaultTableColumn) columnModel.getColumn(columnIndex);
    190         return column.getValueModel();
    191     }
    192
    193     protected List getRows() {
    194         Object value = rowsModel.getValue();
    195         if (value == null) {
    196             return Collections.EMPTY_LIST;
    197         }
    198         return InvokerHelper.asList(value);
    199     }
    200
    201     protected static class MyTableColumnModel extends DefaultTableColumnModel {
    202         public List getColumnList() {
    203             return tableColumns;
    204         }
    205     }
    206
    207     public ValueModel getRowModel() {
    208         return rowModel;
    209     }
    210
    211     public ValueModel getRowsModel() {
    212         return rowsModel;
    213     }
    214
    215 }