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: * TableDataFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report;
030:
031: import java.io.Serializable;
032: import java.util.HashMap;
033: import javax.swing.table.TableModel;
034:
035: /**
036: * The TableDataFactory provides keyed access to predefined tablemodels. The factory does not accept parameters and
037: * therefore cannot be used for parametrized queries. The queryname is used to lookup the table by its previously
038: * registered name.
039: *
040: * @author Thomas Morgner
041: */
042: public class TableDataFactory implements DataFactory, Cloneable,
043: Serializable {
044: /**
045: * A unique identifier for long term persistance.
046: */
047: private static final long serialVersionUID = -238954878318943053L;
048:
049: /**
050: * The tables for this factory.
051: */
052: private HashMap tables;
053:
054: /**
055: * Default Constructor.
056: */
057: public TableDataFactory() {
058: this .tables = new HashMap();
059: }
060:
061: /**
062: * Creates a new TableDataFactory and registers the tablemodel with the given name.
063: *
064: * @param name the name of the table.
065: * @param tableModel the tablemodel.
066: */
067: public TableDataFactory(final String name,
068: final TableModel tableModel) {
069: this ();
070: addTable(name, tableModel);
071: }
072:
073: /**
074: * Registers a tablemodel with the given name. If a different tablemodel has been previously registered with the same
075: * name, this table will replace the existing one.
076: *
077: * @param name the name of the table.
078: * @param tableModel the tablemodel that should be registered.
079: */
080: public void addTable(final String name, final TableModel tableModel) {
081: if (tableModel == null) {
082: throw new NullPointerException();
083: }
084: if (name == null) {
085: throw new NullPointerException();
086: }
087: tables.put(name, tableModel);
088: }
089:
090: /**
091: * Removes the table that has been registered by the given name.
092: *
093: * @param name the name of the table to be removed.
094: */
095: public void removeTable(final String name) {
096: tables.remove(name);
097: }
098:
099: /**
100: * Queries a datasource. The string 'query' defines the name of the query. The Parameterset given here may contain
101: * more data than actually needed.
102: * <p/>
103: * The dataset may change between two calls, do not assume anything!
104: *
105: * @param query the name of the table.
106: * @param parameters are ignored for this factory.
107: * @return the report data or null.
108: */
109: public TableModel queryData(final String query,
110: final DataRow parameters) {
111: return (TableModel) tables.get(query);
112: }
113:
114: /**
115: * This does nothing.
116: */
117: public void open() {
118: }
119:
120: /**
121: * Closes the data factory. Actually, this one does nothing at all.
122: */
123: public void close() {
124: }
125:
126: /**
127: * Derives a freshly initialized report data factory, which is independend of the original data factory. Opening or
128: * Closing one data factory must not affect the other factories.
129: *
130: * @return a copy of this factory.
131: * @throws ReportDataFactoryException if deriving the factory failed.
132: */
133: public DataFactory derive() throws ReportDataFactoryException {
134: try {
135: return (DataFactory) clone();
136: } catch (CloneNotSupportedException e) {
137: throw new ReportDataFactoryException(
138: "Clone should not fail.");
139: }
140: }
141:
142: /**
143: * Creates a copy of this data-factory.
144: *
145: * @return the copy of the data-factory, never null.
146: * @throws CloneNotSupportedException if the clone-operation failed for some reason.
147: */
148: public Object clone() throws CloneNotSupportedException {
149: final TableDataFactory dataFactory = (TableDataFactory) super
150: .clone();
151: dataFactory.tables = (HashMap) tables.clone();
152: return dataFactory;
153: }
154: }
|