KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > data > JRTableModelDataSource


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 /*
30  * Contributors:
31  * Artur Biesiadowski - abies@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.data;
34
35 import java.util.HashMap JavaDoc;
36
37 import javax.swing.table.TableModel JavaDoc;
38
39 import net.sf.jasperreports.engine.JRException;
40 import net.sf.jasperreports.engine.JRField;
41 import net.sf.jasperreports.engine.JRRewindableDataSource;
42
43
44 /**
45  * @author Teodor Danciu (teodord@users.sourceforge.net)
46  * @version $Id: JRTableModelDataSource.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
47  */

48 public class JRTableModelDataSource implements JRRewindableDataSource
49 {
50     
51
52     /**
53      *
54      */

55     private TableModel JavaDoc tableModel = null;
56     private int index = -1;
57     private HashMap JavaDoc columnNames = new HashMap JavaDoc();
58     
59
60     /**
61      *
62      */

63     public JRTableModelDataSource(TableModel JavaDoc model)
64     {
65         this.tableModel = model;
66         
67         if (this.tableModel != null)
68         {
69             for(int i = 0; i < tableModel.getColumnCount(); i++)
70             {
71                 this.columnNames.put(tableModel.getColumnName(i), new Integer JavaDoc(i));
72             }
73         }
74     }
75     
76
77     /**
78      *
79      */

80     public boolean next()
81     {
82         this.index++;
83
84         if (this.tableModel != null)
85         {
86             return (this.index < this.tableModel.getRowCount());
87         }
88
89         return false;
90     }
91     
92     
93     /**
94      *
95      */

96     public Object JavaDoc getFieldValue(JRField jrField) throws JRException
97     {
98         String JavaDoc fieldName = jrField.getName();
99         
100         Integer JavaDoc columnIndex = (Integer JavaDoc)this.columnNames.get(fieldName);
101         
102         if (columnIndex != null)
103         {
104             return this.tableModel.getValueAt(index, columnIndex.intValue());
105         }
106         else if (fieldName.startsWith("COLUMN_"))
107         {
108             return this.tableModel.getValueAt(index, Integer.parseInt(fieldName.substring(7)));
109         }
110         else
111         {
112             throw new JRException("Unknown column name : " + fieldName);
113         }
114     }
115
116     
117     /**
118      *
119      */

120     public void moveFirst()
121     {
122         this.index = -1;
123     }
124
125
126 }
127
Popular Tags