001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * [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-2014, 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
044package org.jfree.data.time;
045
046import javax.swing.table.AbstractTableModel;
047
048import org.jfree.data.general.SeriesChangeEvent;
049import 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 */
055public 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 {@code true}, 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    @Override
105    public int getColumnCount() {
106        return 2;
107    }
108
109    /**
110     * Returns the column class in the table model.
111     *
112     * @param column  the column index.
113     *
114     * @return The column class in the table model.
115     */
116    @Override
117    public Class getColumnClass(int column) {
118        if (column == 0) {
119            return String.class;
120        }
121        else {
122            if (column == 1) {
123                return Double.class;
124            }
125            else {
126                return null;
127            }
128        }
129    }
130
131    /**
132     * Returns the name of a column
133     *
134     * @param column  the column index.
135     *
136     * @return The name of a column.
137     */
138    @Override
139    public String getColumnName(int column) {
140
141        if (column == 0) {
142            return "Period:";
143        }
144        else {
145            if (column == 1) {
146                return "Value:";
147            }
148            else {
149                return null;
150            }
151        }
152
153    }
154
155    /**
156     * Returns the number of rows in the table model.
157     *
158     * @return The row count.
159     */
160    @Override
161    public int getRowCount() {
162        return this.series.getItemCount();
163    }
164
165    /**
166     * Returns the data value for a cell in the table model.
167     *
168     * @param row  the row number.
169     * @param column  the column number.
170     *
171     * @return The data value for a cell in the table model.
172     */
173    @Override
174    public Object getValueAt(int row, int column) {
175
176        if (row < this.series.getItemCount()) {
177            if (column == 0) {
178                return this.series.getTimePeriod(row);
179            }
180            else {
181                if (column == 1) {
182                    return this.series.getValue(row);
183                }
184                else {
185                    return null;
186                }
187            }
188        }
189        else {
190            if (column == 0) {
191                return this.newTimePeriod;
192            }
193            else {
194                if (column == 1) {
195                    return this.newValue;
196                }
197                else {
198                    return null;
199                }
200            }
201        }
202
203    }
204
205    /**
206     * Returns a flag indicating whether or not the specified cell is editable.
207     *
208     * @param row  the row number.
209     * @param column  the column number.
210     *
211     * @return <code>true</code> if the specified cell is editable.
212     */
213    @Override
214    public boolean isCellEditable(int row, int column) {
215        if (this.editable) {
216            if ((column == 0) || (column == 1)) {
217                return true;
218            }
219            else {
220                return false;
221            }
222        }
223        else {
224            return false;
225        }
226    }
227
228    /**
229     * Updates the time series.
230     *
231     * @param value  the new value.
232     * @param row  the row.
233     * @param column  the column.
234     */
235    @Override
236    public void setValueAt(Object value, int row, int column) {
237
238        if (row < this.series.getItemCount()) {
239
240            // update the time series appropriately
241            if (column == 1) {
242                try {
243                    Double v = Double.valueOf(value.toString());
244                    this.series.update(row, v);
245
246                }
247                catch (NumberFormatException nfe) {
248                    System.err.println("Number format exception");
249                }
250            }
251        }
252        else {
253            if (column == 0) {
254                // this.series.getClass().valueOf(value.toString());
255                this.newTimePeriod = null;
256            }
257            else if (column == 1) {
258                this.newValue = Double.valueOf(value.toString());
259            }
260        }
261    }
262
263    /**
264     * Receives notification that the time series has been changed.  Responds
265     * by firing a table data change event.
266     *
267     * @param event  the event.
268     */
269    @Override
270    public void seriesChanged(SeriesChangeEvent event) {
271        fireTableDataChanged();
272    }
273
274}