KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > TimeSeriesTableModel


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------------
28  * TimeSeriesTableModel.java
29  * -------------------------
30  * (C) Copyright 2001-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: TimeSeriesTableModel.java,v 1.3.2.1 2005/10/25 21:35:24 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 14-Nov-2001 : Version 1 (DG);
40  * 05-Apr-2002 : Removed redundant first column (DG);
41  * 24-Jun-2002 : Removed unnecessary local variable (DG);
42  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
43  *
44  */

45
46 package org.jfree.data.time;
47
48 import javax.swing.table.AbstractTableModel JavaDoc;
49
50 import org.jfree.data.general.SeriesChangeEvent;
51 import org.jfree.data.general.SeriesChangeListener;
52
53 /**
54  * Wrapper around a time series to convert it to a table model for use in
55  * a <code>JTable</code>.
56  */

57 public class TimeSeriesTableModel extends AbstractTableModel JavaDoc
58                                   implements SeriesChangeListener {
59
60     /** The series. */
61     private TimeSeries series;
62
63     /** A flag that controls whether the series is editable. */
64     private boolean editable;
65
66     /** The new time period. */
67     private RegularTimePeriod newTimePeriod;
68
69     /** The new value. */
70     private Number JavaDoc newValue;
71
72     /**
73      * Default constructor.
74      */

75     public TimeSeriesTableModel() {
76         this(new TimeSeries("Untitled"));
77     }
78
79     /**
80      * Constructs a table model for a time series.
81      *
82      * @param series the time series.
83      */

84     public TimeSeriesTableModel(TimeSeries series) {
85         this(series, false);
86     }
87
88     /**
89      * Creates a table model based on a time series.
90      *
91      * @param series the time series.
92      * @param editable if <ocde>true</code>, the table is editable.
93      */

94     public TimeSeriesTableModel(TimeSeries series, boolean editable) {
95         this.series = series;
96         this.series.addChangeListener(this);
97         this.editable = editable;
98     }
99
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 JavaDoc getColumnClass(int column) {
118         if (column == 0) {
119             return String JavaDoc.class;
120         }
121         else {
122             if (column == 1) {
123                 return Double JavaDoc.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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc v = Double.valueOf(value.toString());
239                     this.series.update(row, v);
240
241                 }
242                 catch (NumberFormatException JavaDoc 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 }
269
Popular Tags