001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: TimeSeriesTableModel.java,v 1.3.2.1 2005/10/25 21:35:24 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 14-Nov-2001 : Version 1 (DG);
040     * 05-Apr-2002 : Removed redundant first column (DG);
041     * 24-Jun-2002 : Removed unnecessary local variable (DG);
042     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
043     *
044     */
045    
046    package org.jfree.data.time;
047    
048    import javax.swing.table.AbstractTableModel;
049    
050    import org.jfree.data.general.SeriesChangeEvent;
051    import org.jfree.data.general.SeriesChangeListener;
052    
053    /**
054     * Wrapper around a time series to convert it to a table model for use in 
055     * a <code>JTable</code>.
056     */
057    public class TimeSeriesTableModel extends AbstractTableModel 
058                                      implements SeriesChangeListener {
059    
060        /** The series. */
061        private TimeSeries series;
062    
063        /** A flag that controls whether the series is editable. */
064        private boolean editable;
065    
066        /** The new time period. */
067        private RegularTimePeriod newTimePeriod;
068    
069        /** The new value. */
070        private Number newValue;
071    
072        /**
073         * Default constructor.
074         */
075        public TimeSeriesTableModel() {
076            this(new TimeSeries("Untitled"));
077        }
078    
079        /**
080         * Constructs a table model for a time series.
081         *
082         * @param series  the time series.
083         */
084        public TimeSeriesTableModel(TimeSeries series) {
085            this(series, false);
086        }
087    
088        /**
089         * Creates a table model based on a time series.
090         *
091         * @param series  the time series.
092         * @param editable  if <ocde>true</code>, the table is editable.
093         */
094        public TimeSeriesTableModel(TimeSeries series, boolean editable) {
095            this.series = series;
096            this.series.addChangeListener(this);
097            this.editable = editable;
098        }
099    
100        /**
101         * Returns the number of columns in the table model.  For this particular
102         * model, the column count is fixed at 2.
103         *
104         * @return The column count.
105         */
106        public int getColumnCount() {
107            return 2;
108        }
109    
110        /**
111         * Returns the column class in the table model.
112         *
113         * @param column    The column index.
114         * 
115         * @return The column class in the table model.
116         */
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        public String getColumnName(int column) {
139    
140            if (column == 0) {
141                return "Period:";
142            }
143            else {
144                if (column == 1) {
145                    return "Value:";
146                }
147                else {
148                    return null;
149                }
150            }
151    
152        }
153    
154        /**
155         * Returns the number of rows in the table model.
156         *
157         * @return The row count.
158         */
159        public int getRowCount() {
160            return this.series.getItemCount();
161        }
162    
163        /**
164         * Returns the data value for a cell in the table model.
165         *
166         * @param row  the row number.
167         * @param column  the column number.
168         * 
169         * @return The data value for a cell in the table model.
170         */
171        public Object getValueAt(int row, int column) {
172    
173            if (row < this.series.getItemCount()) {
174                if (column == 0) {
175                    return this.series.getTimePeriod(row);
176                }
177                else {
178                    if (column == 1) {
179                        return this.series.getValue(row);
180                    }
181                    else {
182                        return null;
183                    }
184                }
185            }
186            else {
187                if (column == 0) {
188                    return this.newTimePeriod;
189                }
190                else {
191                    if (column == 1) {
192                        return this.newValue;
193                    }
194                    else {
195                        return null;
196                    }
197                }
198            }
199    
200        }
201    
202        /**
203         * Returns a flag indicating whether or not the specified cell is editable.
204         *
205         * @param row  the row number.
206         * @param column  the column number.
207         *
208         * @return <code>true</code> if the specified cell is editable.
209         */
210        public boolean isCellEditable(int row, int column) {
211            if (this.editable) {
212                if ((column == 0) || (column == 1)) {
213                    return true;
214                }
215                else {
216                    return false;
217                }
218            }
219            else {
220                return false;
221            }
222        }
223    
224        /**
225         * Updates the time series.
226         *
227         * @param value  the new value.
228         * @param row  the row.
229         * @param column  the column.
230         */
231        public void setValueAt(Object value, int row, int column) {
232    
233            if (row < this.series.getItemCount()) {
234    
235                // update the time series appropriately
236                if (column == 1) {
237                    try {
238                        Double v = Double.valueOf(value.toString());
239                        this.series.update(row, v);
240    
241                    }
242                    catch (NumberFormatException nfe) {
243                        System.err.println("Number format exception");
244                    }
245                }
246            }
247            else {
248                if (column == 0) {
249                    // this.series.getClass().valueOf(value.toString());
250                    this.newTimePeriod = null;
251                }
252                else if (column == 1) {
253                    this.newValue = Double.valueOf(value.toString());
254                }
255            }
256        }
257    
258        /**
259         * Receives notification that the time series has been changed.  Responds
260         * by firing a table data change event.
261         *
262         * @param event  the event.
263         */
264        public void seriesChanged(SeriesChangeEvent event) {
265            fireTableDataChanged();
266        }
267    
268    }