    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -------------------------
    028     * TimeSeriesTableModel.java
    029     * -------------------------
    030     * (C) Copyright 2001-2008, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * Changes
    036     * -------
    037     * 14-Nov-2001 : Version 1 (DG);
    038     * 05-Apr-2002 : Removed redundant first column (DG);
    039     * 24-Jun-2002 : Removed unnecessary local variable (DG);
    040     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    041     *
    042     */
    043
    044    package org.jfree.data.time;
    045
    046    import javax.swing.table.AbstractTableModel;
    047
    048    import org.jfree.data.general.SeriesChangeEvent;
    049    import org.jfree.data.general.SeriesChangeListener;
    050
    051    /**
    052     * Wrapper around a time series to convert it to a table model for use in
    053     * a <code>JTable</code>.
    054     */
    055    public class TimeSeriesTableModel extends AbstractTableModel
    056                                      implements SeriesChangeListener {
    057
    058        /** The series. */
    059        private TimeSeries series;
    060
    061        /** A flag that controls whether the series is editable. */
    062        private boolean editable;
    063
    064        /** The new time period. */
    065        private RegularTimePeriod newTimePeriod;
    066
    067        /** The new value. */
    068        private Number newValue;
    069
    070        /**
    071         * Default constructor.
    072         */
    073        public TimeSeriesTableModel() {
    074            this(new TimeSeries("Untitled"));
    075        }
    076
    077        /**
    078         * Constructs a table model for a time series.
    079         *
    080         * @param series  the time series.
    081         */
    082        public TimeSeriesTableModel(TimeSeries series) {
    083            this(series, false);
    084        }
    085
    086        /**
    087         * Creates a table model based on a time series.
    088         *
    089         * @param series  the time series.
    090         * @param editable  if <ocde>true</code>, the table is editable.
    091         */
    092        public TimeSeriesTableModel(TimeSeries series, boolean editable) {
    093            this.series = series;
    094            this.series.addChangeListener(this);
    095            this.editable = editable;
    096        }
    097
    098        /**
    099         * Returns the number of columns in the table model.  For this particular
    100         * model, the column count is fixed at 2.
    101         *
    102         * @return The column count.
    103         */
    104        public int getColumnCount() {
    105            return 2;
    106        }
    107
    108        /**
    109         * Returns the column class in the table model.
    110         *
    111         * @param column    The column index.
    112         *
    113         * @return The column class in the table model.
    114         */
    115        public Class getColumnClass(int column) {
    116            if (column == 0) {
    117                return String.class;
    118            }
    119            else {
    120                if (column == 1) {
    121                    return Double.class;
    122                }
    123                else {
    124                    return null;
    125                }
    126            }
    127        }
    128
    129        /**
    130         * Returns the name of a column
    131         *
    132         * @param column  the column index.
    133         *
    134         * @return The name of a column.
    135         */
    136        public String getColumnName(int column) {
    137
    138            if (column == 0) {
    139                return "Period:";
    140            }
    141            else {
    142                if (column == 1) {
    143                    return "Value:";
    144                }
    145                else {
    146                    return null;
    147                }
    148            }
    149
    150        }
    151
    152        /**
    153         * Returns the number of rows in the table model.
    154         *
    155         * @return The row count.
    156         */
    157        public int getRowCount() {
    158            return this.series.getItemCount();
    159        }
    160
    161        /**
    162         * Returns the data value for a cell in the table model.
    163         *
    164         * @param row  the row number.
    165         * @param column  the column number.
    166         *
    167         * @return The data value for a cell in the table model.
    168         */
    169        public Object getValueAt(int row, int column) {
    170
    171            if (row < this.series.getItemCount()) {
    172                if (column == 0) {
    173                    return this.series.getTimePeriod(row);
    174                }
    175                else {
    176                    if (column == 1) {
    177                        return this.series.getValue(row);
    178                    }
    179                    else {
    180                        return null;
    181                    }
    182                }
    183            }
    184            else {
    185                if (column == 0) {
    186                    return this.newTimePeriod;
    187                }
    188                else {
    189                    if (column == 1) {
    190                        return this.newValue;
    191                    }
    192                    else {
    193                        return null;
    194                    }
    195                }
    196            }
    197
    198        }
    199
    200        /**
    201         * Returns a flag indicating whether or not the specified cell is editable.
    202         *
    203         * @param row  the row number.
    204         * @param column  the column number.
    205         *
    206         * @return <code>true</code> if the specified cell is editable.
    207         */
    208        public boolean isCellEditable(int row, int column) {
    209            if (this.editable) {
    210                if ((column == 0) || (column == 1)) {
    211                    return true;
    212                }
    213                else {
    214                    return false;
    215                }
    216            }
    217            else {
    218                return false;
    219            }
    220        }
    221
    222        /**
    223         * Updates the time series.
    224         *
    225         * @param value  the new value.
    226         * @param row  the row.
    227         * @param column  the column.
    228         */
    229        public void setValueAt(Object value, int row, int column) {
    230
    231            if (row < this.series.getItemCount()) {
    232
    233                // update the time series appropriately
    234                if (column == 1) {
    235                    try {
    236                        Double v = Double.valueOf(value.toString());
    237                        this.series.update(row, v);
    238
    239                    }
    240                    catch (NumberFormatException nfe) {
    241                        System.err.println("Number format exception");
    242                    }
    243                }
    244            }
    245            else {
    246                if (column == 0) {
    247                    // this.series.getClass().valueOf(value.toString());
    248                    this.newTimePeriod = null;
    249                }
    250                else if (column == 1) {
    251                    this.newValue = Double.valueOf(value.toString());
    252                }
    253            }
    254        }
    255
    256        /**
    257         * Receives notification that the time series has been changed.  Responds
    258         * by firing a table data change event.
    259         *
    260         * @param event  the event.
    261         */
    262        public void seriesChanged(SeriesChangeEvent event) {
    263            fireTableDataChanged();
    264        }
    265
    266    }