Source for org.jfree.report.modules.misc.tablemodel.CSVTableModel

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: CSVTableModel.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.misc.tablemodel;
  32: 
  33: import javax.swing.table.AbstractTableModel;
  34: 
  35: /**
  36:  * <code>TableModel</code> used by the <code>CSVTableModelProducer</code> class. It has a
  37:  * feature which generates the column name if it is not know.
  38:  *
  39:  * @author Mimil
  40:  * @see this.getColumnName()
  41:  */
  42: public class CSVTableModel extends AbstractTableModel
  43: {
  44: 
  45:   private String[] columnNames = null;
  46:   private int rowCount = 0;
  47:   private int maxColumnCount = 0;
  48:   private Object[][] data;
  49: 
  50:   public CSVTableModel ()
  51:   {
  52:   }
  53: 
  54:   public Object[][] getData ()
  55:   {
  56:     return data;
  57:   }
  58: 
  59:   public void setData (final Object[][] data)
  60:   {
  61:     this.data = data;
  62:   }
  63: 
  64:   public String[] getColumnNames ()
  65:   {
  66:     return columnNames;
  67:   }
  68: 
  69:   public void setColumnNames (final String[] columnNames)
  70:   {
  71:     this.columnNames = columnNames;
  72:   }
  73: 
  74:   /**
  75:    * Counts columns of this <code>TableModel</code>.
  76:    *
  77:    * @return the column count
  78:    */
  79:   public int getColumnCount ()
  80:   {
  81:     if (this.columnNames != null)
  82:     {
  83:       return columnNames.length;
  84:     }
  85: 
  86:     return this.maxColumnCount;
  87:   }
  88: 
  89:   /**
  90:    * Counts rows of this <code>TableModel</code>.
  91:    *
  92:    * @return the row count
  93:    */
  94:   public int getRowCount ()
  95:   {
  96:     return this.rowCount;
  97:   }
  98: 
  99:   /**
 100:    * Gets the Object at specified row and column positions.
 101:    *
 102:    * @param rowIndex    row index
 103:    * @param columnIndex column index
 104:    * @return The requested Object
 105:    */
 106:   public Object getValueAt (final int rowIndex, final int columnIndex)
 107:   {
 108:     final Object[] line = this.data[rowIndex];
 109: 
 110:     if (line.length < columnIndex)
 111:     {
 112:       return null;
 113:     }
 114:     else
 115:     {
 116:       return line[columnIndex];
 117:     }
 118:   }
 119: 
 120:   /**
 121:    * Sets the maximum column count if it is bigger than the current one.
 122:    *
 123:    * @param maxColumnCount
 124:    */
 125:   public void setMaxColumnCount (final int maxColumnCount)
 126:   {
 127:     if (this.maxColumnCount < maxColumnCount)
 128:     {
 129:       this.maxColumnCount = maxColumnCount;
 130:     }
 131:   }
 132: 
 133:   public int getMaxColumnCount()
 134:   {
 135:     return maxColumnCount;
 136:   }
 137: 
 138:   /**
 139:    * Return the column name at a specified position.
 140:    *
 141:    * @param column column index
 142:    * @return the column name
 143:    */
 144:   public String getColumnName (final int column)
 145:   {
 146:     if (this.columnNames != null)
 147:     {
 148:       return this.columnNames[column];
 149:     }
 150:     else
 151:     {
 152:       if (column >= this.maxColumnCount)
 153:       {
 154:         throw new IllegalArgumentException("Column (" + column + ") does not exist");
 155:       }
 156:       else
 157:       {
 158:         return "COLUMN_" + column;
 159:       }
 160:     }
 161:   }
 162: }