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: * DataSourceReferenceTableModel.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.misc.referencedoc;
030:
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.Iterator;
034: import javax.swing.table.AbstractTableModel;
035:
036: import org.jfree.report.modules.parser.ext.factory.base.ObjectDescription;
037: import org.jfree.report.modules.parser.ext.factory.datasource.DataSourceCollector;
038: import org.jfree.report.modules.parser.ext.factory.datasource.DataSourceFactory;
039:
040: /**
041: * A table model for the style key reference generator.
042: *
043: * @author Thomas Morgner
044: */
045: public class DataSourceReferenceTableModel extends AbstractTableModel {
046: /**
047: * Represents a row in the table model.
048: */
049: private static class DataSourceDescriptionRow {
050: /**
051: * The factory.
052: */
053: private final DataSourceFactory datasourceFactory;
054:
055: /**
056: * The key.
057: */
058: private final String datasourceName;
059:
060: /**
061: * The implementing class for the datasource name.
062: */
063: private final Class implementingClass;
064:
065: /**
066: * Creates a new row.
067: *
068: * @param datasourceFactory the datasource factory
069: * @param name the name of the datasource within the factory.
070: * @param implementingClass the class that implements the named datasource.
071: */
072: public DataSourceDescriptionRow(
073: final DataSourceFactory datasourceFactory,
074: final String name, final Class implementingClass) {
075: this .datasourceFactory = datasourceFactory;
076: this .datasourceName = name;
077: this .implementingClass = implementingClass;
078: }
079:
080: /**
081: * Returns the factory.
082: *
083: * @return The factory.
084: */
085: public DataSourceFactory getFactory() {
086: return datasourceFactory;
087: }
088:
089: /**
090: * Returns the datasource name.
091: *
092: * @return The datasource name.
093: */
094: public String getName() {
095: return datasourceName;
096: }
097:
098: /**
099: * Returns the class object for the datasource.
100: *
101: * @return the datasource class.
102: */
103: public Class getImplementingClass() {
104: return implementingClass;
105: }
106: }
107:
108: /**
109: * The column names.
110: */
111: private static final String[] COLUMN_NAMES = {
112: "datasource-factory", //$NON-NLS-1$
113: "datasource-name", //$NON-NLS-1$
114: "datasource-class" //$NON-NLS-1$
115: };
116:
117: /**
118: * Storage for the rows.
119: */
120: private final ArrayList rows;
121:
122: /**
123: * Creates a new table model.
124: *
125: * @param cf the factory collection.
126: */
127: public DataSourceReferenceTableModel(final DataSourceCollector cf) {
128: rows = new ArrayList();
129: addFactoryCollector(cf);
130: }
131:
132: /**
133: * Adds a factory.
134: *
135: * @param cf the factory.
136: */
137: private void addFactoryCollector(final DataSourceCollector cf) {
138: final Iterator it = cf.getFactories();
139: while (it.hasNext()) {
140: final DataSourceFactory cfact = (DataSourceFactory) it
141: .next();
142: if (cfact instanceof DataSourceCollector) {
143: addFactoryCollector((DataSourceCollector) cfact);
144: } else {
145: addDataSourceFactory(cfact);
146: }
147: }
148: }
149:
150: /**
151: * Adds a factory.
152: *
153: * @param cf the factory.
154: */
155: private void addDataSourceFactory(final DataSourceFactory cf) {
156: Iterator it = cf.getRegisteredNames();
157: final ArrayList factories = new ArrayList();
158:
159: while (it.hasNext()) {
160: final String c = (String) it.next();
161: factories.add(c);
162: }
163:
164: Collections.sort(factories);
165: it = factories.iterator();
166:
167: while (it.hasNext()) {
168: final String keyName = (String) it.next();
169: final ObjectDescription od = cf
170: .getDataSourceDescription(keyName);
171: rows.add(new DataSourceDescriptionRow(cf, keyName, od
172: .getObjectClass()));
173: }
174: }
175:
176: /**
177: * Returns the number of rows in the model. A <code>JTable</code> uses this method to
178: * determine how many rows it should display. This method should be quick, as it is
179: * called frequently during rendering.
180: *
181: * @return the number of rows in the model
182: *
183: * @see #getColumnCount
184: */
185: public int getRowCount() {
186: return rows.size();
187: }
188:
189: /**
190: * Returns the number of columns in the model. A <code>JTable</code> uses this method to
191: * determine how many columns it should create and display by default.
192: *
193: * @return the number of columns in the model
194: *
195: * @see #getRowCount
196: */
197: public int getColumnCount() {
198: return COLUMN_NAMES.length;
199: }
200:
201: /**
202: * Returns the column name.
203: *
204: * @param column the column being queried
205: * @return a string containing the default name of <code>column</code>
206: */
207: public String getColumnName(final int column) {
208: return COLUMN_NAMES[column];
209: }
210:
211: /**
212: * Returns <code>String.class</code> regardless of <code>columnIndex</code>.
213: *
214: * @param columnIndex the column being queried
215: * @return the Object.class
216: */
217: public Class getColumnClass(final int columnIndex) {
218: return String.class;
219: }
220:
221: /**
222: * Returns the value for the cell at <code>columnIndex</code> and
223: * <code>rowIndex</code>.
224: *
225: * @param rowIndex the row whose value is to be queried
226: * @param columnIndex the column whose value is to be queried
227: * @return the value Object at the specified cell
228: */
229: public Object getValueAt(final int rowIndex, final int columnIndex) {
230: final DataSourceDescriptionRow or = (DataSourceDescriptionRow) rows
231: .get(rowIndex);
232: switch (columnIndex) {
233: case 0:
234: return String.valueOf(or.getFactory().getClass().getName());
235: case 1:
236: return String.valueOf(or.getName());
237: case 2:
238: return String.valueOf(or.getImplementingClass().getName());
239: default:
240: return null;
241: }
242: }
243: }
|