001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * ReportDataRow.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.states.datarow;
030:
031: import javax.swing.table.TableModel;
032:
033: import org.jfree.report.DataFactory;
034: import org.jfree.report.DataRow;
035: import org.jfree.report.ReportDataFactoryException;
036:
037: public final class ReportDataRow {
038: //private Map nameCache;
039: private String[] names;
040: private final TableModel reportData;
041: private int cursor;
042:
043: private ReportDataRow(final TableModel reportData) {
044: if (reportData == null) {
045: throw new NullPointerException();
046: }
047: this .reportData = reportData;
048: this .cursor = 0;
049:
050: final int columnCount = reportData.getColumnCount();
051: this .names = new String[columnCount];
052:
053: for (int i = 0; i < columnCount; i++) {
054: this .names[i] = reportData.getColumnName(i);
055: }
056: }
057:
058: private ReportDataRow(final TableModel reportData,
059: final ReportDataRow reportDataRow) {
060: if (reportData == null) {
061: throw new NullPointerException();
062: }
063:
064: if (reportDataRow == null) {
065: throw new NullPointerException();
066: }
067:
068: this .reportData = reportData;
069: this .cursor = 0;
070:
071: this .cursor = reportDataRow.cursor + 1;
072: this .names = reportDataRow.names;
073: }
074:
075: public static ReportDataRow createDataRow(
076: final DataFactory dataFactory, final String query,
077: final DataRow parameters) throws ReportDataFactoryException {
078: final TableModel reportData = dataFactory.queryData(query,
079: parameters);
080: return new ReportDataRow(reportData);
081: }
082:
083: /**
084: * Returns the value of the expression or column in the tablemodel using the
085: * given column number as index. For functions and expressions, the
086: * <code>getValue()</code> method is called and for columns from the
087: * tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets
088: * called.
089: *
090: * @param col the item index.
091: * @return the value.
092: * @throws IllegalStateException if the datarow detected a deadlock.
093: */
094: public Object get(final int col) {
095: return reportData.getValueAt(cursor, col);
096: }
097:
098: /**
099: * Returns the name of the column, expression or function. For columns from
100: * the tablemodel, the tablemodels <code>getColumnName</code> method is
101: * called. For functions, expressions and report properties the assigned name
102: * is returned.
103: *
104: * @param col the item index.
105: * @return the name.
106: */
107: public String getColumnName(final int col) {
108: return names[col];
109: }
110:
111: /**
112: * Returns the number of columns, expressions and functions and marked
113: * ReportProperties in the report.
114: *
115: * @return the item count.
116: */
117: public int getColumnCount() {
118: return names.length;
119: }
120:
121: /**
122: * Advances to the next row and attaches the given master row to the objects
123: * contained in that client data row.
124: *
125: * @return
126: */
127: public ReportDataRow advance() {
128: return new ReportDataRow(reportData, this );
129: }
130:
131: public boolean isAdvanceable() {
132: return cursor < (reportData.getRowCount() - 1);
133: }
134:
135: public boolean isReadable() {
136: return cursor >= 0 && cursor < reportData.getRowCount();
137: }
138:
139: public TableModel getReportData() {
140: return reportData;
141: }
142:
143: public int getCursor() {
144: return cursor;
145: }
146: }
|