001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * Artur Biesiadowski - abies@users.sourceforge.net
032: */
033: package net.sf.jasperreports.engine.data;
034:
035: import java.util.HashMap;
036:
037: import javax.swing.table.TableModel;
038:
039: import net.sf.jasperreports.engine.JRException;
040: import net.sf.jasperreports.engine.JRField;
041: import net.sf.jasperreports.engine.JRRewindableDataSource;
042:
043: /**
044: * @author Teodor Danciu (teodord@users.sourceforge.net)
045: * @version $Id: JRTableModelDataSource.java 1229 2006-04-19 10:27:35Z teodord $
046: */
047: public class JRTableModelDataSource implements JRRewindableDataSource {
048:
049: /**
050: *
051: */
052: private TableModel tableModel = null;
053: private int index = -1;
054: private HashMap columnNames = new HashMap();
055:
056: /**
057: *
058: */
059: public JRTableModelDataSource(TableModel model) {
060: this .tableModel = model;
061:
062: if (this .tableModel != null) {
063: for (int i = 0; i < tableModel.getColumnCount(); i++) {
064: this .columnNames.put(tableModel.getColumnName(i),
065: new Integer(i));
066: }
067: }
068: }
069:
070: /**
071: *
072: */
073: public boolean next() {
074: this .index++;
075:
076: if (this .tableModel != null) {
077: return (this .index < this .tableModel.getRowCount());
078: }
079:
080: return false;
081: }
082:
083: /**
084: *
085: */
086: public Object getFieldValue(JRField jrField) throws JRException {
087: String fieldName = jrField.getName();
088:
089: Integer columnIndex = (Integer) this .columnNames.get(fieldName);
090:
091: if (columnIndex != null) {
092: return this .tableModel.getValueAt(index, columnIndex
093: .intValue());
094: } else if (fieldName.startsWith("COLUMN_")) {
095: return this .tableModel.getValueAt(index, Integer
096: .parseInt(fieldName.substring(7)));
097: } else {
098: throw new JRException("Unknown column name : " + fieldName);
099: }
100: }
101:
102: /**
103: *
104: */
105: public void moveFirst() {
106: this .index = -1;
107: }
108:
109: }
|