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: TableReportData.java 3048 2007-07-28 18:02:42Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    package org.jfree.report;
032    
033    import javax.swing.table.TableModel;
034    
035    /**
036     * Creation-Date: 19.02.2006, 17:00:10
037     *
038     * @author Thomas Morgner
039     */
040    public class TableReportData implements ReportData
041    {
042      private TableModel tableModel;
043      private int cursor;
044      private int rowMax;
045      private int rowMin;
046      private int cursorMaxPosition;
047    
048      public TableReportData(final TableModel tableModel)
049      {
050        this(tableModel, 0, tableModel.getRowCount());
051      }
052    
053      public TableReportData(final TableModel tableModel, final int start, final int length)
054      {
055        this.tableModel = tableModel;
056        this.rowMax = start + length;
057        this.rowMin = start;
058        this.cursorMaxPosition = length;
059      }
060    
061      public int getColumnCount() throws DataSourceException
062      {
063        return tableModel.getColumnCount();
064      }
065    
066      public boolean isReadable() throws DataSourceException
067      {
068        return cursor > 0 && cursor <= cursorMaxPosition ;
069      }
070    
071      public boolean setCursorPosition(final int row) throws DataSourceException
072      {
073        if (row > cursorMaxPosition)
074        {
075          return false;
076        }
077        else if (row < 0)
078        {
079          return false;
080        }
081        cursor = row;
082        return true;
083      }
084    
085      /**
086       * This operation checks, whether a call to next will be likely to succeed. If
087       * there is a next data row, this should return true.
088       *
089       * @return
090       * @throws org.jfree.report.DataSourceException
091       *
092       */
093      public boolean isAdvanceable() throws DataSourceException
094      {
095        return cursor < cursorMaxPosition;
096      }
097    
098      public String getColumnName(final int column) throws DataSourceException
099      {
100        return tableModel.getColumnName(column);
101      }
102    
103      public Object get(final int column) throws DataSourceException
104      {
105        if (isReadable() == false)
106        {
107          throw new DataSourceException("Unable to read from datasource");
108        }
109        return tableModel.getValueAt(cursor - 1, column);
110      }
111    
112      public boolean next() throws DataSourceException
113      {
114        if (cursor >= cursorMaxPosition)
115        {
116          return false;
117        }
118    
119        cursor += 1;
120        return true;
121      }
122    
123      public void close() throws DataSourceException
124      {
125        // does nothing ...
126      }
127    
128      public int getCursorPosition() throws DataSourceException
129      {
130        return cursor;
131      }
132    }