001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gui.swing.table;
017:
018: // J2SE dependencies
019: import javax.swing.table.TableModel;
020: import javax.swing.table.AbstractTableModel;
021:
022: // Geotools dependencies
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureType;
026:
027: /**
028: * An implementation of Swing's table model which allows feature tables to be displayed.
029: *
030: * @since 2.2
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/widgets-swing/src/main/java/org/geotools/gui/swing/table/FeatureTableModel.java $
032: * @version $Id: FeatureTableModel.java 22482 2006-10-31 02:58:00Z desruisseaux $
033: * @author James Macgill, CCG
034: *
035: * @todo It would be excellent if there were custom cell renderers available for Geometry types.
036: */
037: public class FeatureTableModel extends AbstractTableModel implements
038: TableModel {
039: /**
040: * Holds the feature table that will be represented by this model.
041: */
042: private FeatureCollection featureTable;
043:
044: /**
045: * {@link #featureTable} as an array. Will be created only when first needed.
046: */
047: private transient Feature[] featureArray;
048:
049: /**
050: * Creates a new instance of feature table model.
051: */
052: public FeatureTableModel() {
053: }
054:
055: /**
056: * Creates a new instance of FeatureTableModel based on the feature collection provided.
057: */
058: public FeatureTableModel(final FeatureCollection features) {
059: setFeatureCollection(features);
060: }
061:
062: /**
063: * Sets which featureTable to represent
064: *
065: * @param features The featureTable to represent. This could fire
066: * a Table Structure Changed event.
067: */
068: public void setFeatureCollection(final FeatureCollection features) {
069: featureArray = null;
070: featureTable = features;
071: fireTableStructureChanged();
072: }
073:
074: /**
075: * The number of columns in the feature table. Note: for the moment, this is
076: * determined by the first feature.
077: *
078: * @return the number of columns in this feature table.
079: *
080: * @todo Just gets first feature type - should use typed feature
081: * collection. Revisit when we have FeatureDocument.
082: */
083: public int getColumnCount() {
084: if (featureTable == null || featureTable.isEmpty()) {
085: return 0;
086: }
087: return featureTable.features().next().getNumberOfAttributes();
088: }
089:
090: /**
091: * Gets the row count for the featureTable.
092: *
093: * @return the number of features in feature table.
094: */
095: public int getRowCount() {
096: if (featureTable == null) {
097: return 0;
098: }
099: return featureTable.size();
100: }
101:
102: /**
103: * Gets the name of a specified column.
104: *
105: * @param col the index of the column to get the name of.
106: * @return the name of {@code col}.
107: *
108: * @todo Just gets first feature type - should use typed feature
109: * collection. Revisit when we have FeatureDocument.
110: */
111: public String getColumnName(int col) {
112: if (featureTable == null || featureTable.isEmpty()) {
113: return null;
114: }
115: Feature firstFeature = featureTable.features().next();
116: FeatureType firstType = firstFeature.getFeatureType();
117: return firstType.getAttributeType(col).getName();
118: }
119:
120: /**
121: * Gets the value stored in a specified cell. In this case, {@code row}={@code Feature}
122: * and {@code col}={@code Attribute}.
123: *
124: * @param row the row number.
125: * @param col the column number.
126: * @return the value in the specified cell.
127: */
128: public Object getValueAt(final int row, final int col) {
129: if (featureArray == null) {
130: featureArray = (Feature[]) featureTable
131: .toArray(new Feature[featureTable.size()]);
132: }
133: return featureArray[row].getAttribute(col);
134: }
135: }
|