001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: CSVTableModel.java 2725 2007-04-01 18:49:29Z taqua $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    package org.jfree.report.modules.misc.tablemodel;
032    
033    import javax.swing.table.AbstractTableModel;
034    
035    /**
036     * <code>TableModel</code> used by the <code>CSVTableModelProducer</code> class. It has a
037     * feature which generates the column name if it is not know.
038     *
039     * @author Mimil
040     * @see this.getColumnName()
041     */
042    public class CSVTableModel extends AbstractTableModel
043    {
044    
045      private String[] columnNames = null;
046      private int rowCount = 0;
047      private int maxColumnCount = 0;
048      private Object[][] data;
049    
050      public CSVTableModel ()
051      {
052      }
053    
054      public Object[][] getData ()
055      {
056        return data;
057      }
058    
059      public void setData (final Object[][] data)
060      {
061        this.data = data;
062      }
063    
064      public String[] getColumnNames ()
065      {
066        return columnNames;
067      }
068    
069      public void setColumnNames (final String[] columnNames)
070      {
071        this.columnNames = columnNames;
072      }
073    
074      /**
075       * Counts columns of this <code>TableModel</code>.
076       *
077       * @return the column count
078       */
079      public int getColumnCount ()
080      {
081        if (this.columnNames != null)
082        {
083          return columnNames.length;
084        }
085    
086        return this.maxColumnCount;
087      }
088    
089      /**
090       * Counts rows of this <code>TableModel</code>.
091       *
092       * @return the row count
093       */
094      public int getRowCount ()
095      {
096        return this.rowCount;
097      }
098    
099      /**
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    }