001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------
028 * TimeSeriesTableModel.java
029 * -------------------------
030 * (C) Copyright 2001-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.time;
038
039import javax.swing.table.AbstractTableModel;
040
041import org.jfree.data.general.SeriesChangeEvent;
042import org.jfree.data.general.SeriesChangeListener;
043
044/**
045 * Wrapper around a time series to convert it to a table model for use in
046 * a {@code JTable}.
047 */
048public class TimeSeriesTableModel extends AbstractTableModel
049        implements SeriesChangeListener {
050
051    /** The series. */
052    private TimeSeries series;
053
054    /** A flag that controls whether the series is editable. */
055    private boolean editable;
056
057    /** The new time period. */
058    private RegularTimePeriod newTimePeriod;
059
060    /** The new value. */
061    private Number newValue;
062
063    /**
064     * Default constructor.
065     */
066    public TimeSeriesTableModel() {
067        this(new TimeSeries("Untitled"));
068    }
069
070    /**
071     * Constructs a table model for a time series.
072     *
073     * @param series  the time series.
074     */
075    public TimeSeriesTableModel(TimeSeries series) {
076        this(series, false);
077    }
078
079    /**
080     * Creates a table model based on a time series.
081     *
082     * @param series  the time series.
083     * @param editable  if {@code true}, the table is editable.
084     */
085    public TimeSeriesTableModel(TimeSeries series, boolean editable) {
086        this.series = series;
087        this.series.addChangeListener(this);
088        this.editable = editable;
089    }
090
091    /**
092     * Returns the number of columns in the table model.  For this particular
093     * model, the column count is fixed at 2.
094     *
095     * @return The column count.
096     */
097    @Override
098    public int getColumnCount() {
099        return 2;
100    }
101
102    /**
103     * Returns the column class in the table model.
104     *
105     * @param column  the column index.
106     *
107     * @return The column class in the table model.
108     */
109    @Override
110    public Class getColumnClass(int column) {
111        if (column == 0) {
112            return String.class;
113        }
114        else {
115            if (column == 1) {
116                return Double.class;
117            }
118            else {
119                return null;
120            }
121        }
122    }
123
124    /**
125     * Returns the name of a column
126     *
127     * @param column  the column index.
128     *
129     * @return The name of a column.
130     */
131    @Override
132    public String getColumnName(int column) {
133
134        if (column == 0) {
135            return "Period:";
136        }
137        else {
138            if (column == 1) {
139                return "Value:";
140            }
141            else {
142                return null;
143            }
144        }
145
146    }
147
148    /**
149     * Returns the number of rows in the table model.
150     *
151     * @return The row count.
152     */
153    @Override
154    public int getRowCount() {
155        return this.series.getItemCount();
156    }
157
158    /**
159     * Returns the data value for a cell in the table model.
160     *
161     * @param row  the row number.
162     * @param column  the column number.
163     *
164     * @return The data value for a cell in the table model.
165     */
166    @Override
167    public Object getValueAt(int row, int column) {
168
169        if (row < this.series.getItemCount()) {
170            if (column == 0) {
171                return this.series.getTimePeriod(row);
172            }
173            else {
174                if (column == 1) {
175                    return this.series.getValue(row);
176                }
177                else {
178                    return null;
179                }
180            }
181        }
182        else {
183            if (column == 0) {
184                return this.newTimePeriod;
185            }
186            else {
187                if (column == 1) {
188                    return this.newValue;
189                }
190                else {
191                    return null;
192                }
193            }
194        }
195
196    }
197
198    /**
199     * Returns a flag indicating whether or not the specified cell is editable.
200     *
201     * @param row  the row number.
202     * @param column  the column number.
203     *
204     * @return {@code true} if the specified cell is editable.
205     */
206    @Override
207    public boolean isCellEditable(int row, int column) {
208        if (this.editable) {
209            if ((column == 0) || (column == 1)) {
210                return true;
211            }
212            else {
213                return false;
214            }
215        }
216        else {
217            return false;
218        }
219    }
220
221    /**
222     * Updates the time series.
223     *
224     * @param value  the new value.
225     * @param row  the row.
226     * @param column  the column.
227     */
228    @Override
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    @Override
263    public void seriesChanged(SeriesChangeEvent event) {
264        fireTableDataChanged();
265    }
266
267}