KickJava   Java API By Example, From Geeks To Geeks.

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


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

40
41 package org.jfree.data;
42
43 import javax.swing.table.AbstractTableModel JavaDoc;
44
45 import org.jfree.data.time.RegularTimePeriod;
46 import org.jfree.data.time.TimeSeries;
47
48 /**
49  * Wrapper around a time series to convert it to a table model for use in a JTable.
50  *
51  * @author David Gilbert
52  */

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

70     public TimeSeriesTableModel() {
71         this(new TimeSeries("Untitled"));
72     }
73
74     /**
75      * Constructs a table model for a time series.
76      *
77      * @param series the time series.
78      */

79     public TimeSeriesTableModel(TimeSeries series) {
80         this(series, false);
81     }
82
83     /**
84      * Creates a table model based on a time series.
85      *
86      * @param series the time series.
87      * @param editable if <ocde>true</code>, the table is editable.
88      */

89     public TimeSeriesTableModel(TimeSeries series, boolean editable) {
90
91         this.series = series;
92         this.series.addChangeListener(this);
93         this.editable = editable;
94     }
95
96     /**
97      * Returns the number of columns in the table model. For this particular
98      * model, the column count is fixed at 2.
99      *
100      * @return The column count.
101      */

102     public int getColumnCount() {
103         return 2;
104     }
105
106     /**
107      * Returns the column class in the table model.
108      *
109      * @param column The column index.
110      * @return the column class in the table model.
111      */

112     public Class JavaDoc getColumnClass(int column) {
113
114         if (column == 0) {
115             return String JavaDoc.class;
116         }
117         else {
118             if (column == 1) {
119                 return Double JavaDoc.class;
120             }
121             else {
122                 return null;
123             }
124         }
125
126     }
127
128     /**
129      * Returns the name of a column
130      *
131      * @param column the column index.
132      *
133      * @return the name of a column.
134      */

135     public String JavaDoc getColumnName(int column) {
136
137         if (column == 0) {
138             return "Period:";
139         }
140         else {
141             if (column == 1) {
142                 return "Value:";
143             }
144             else {
145                 return null;
146             }
147         }
148
149     }
150
151     /**
152      * Returns the number of rows in the table model.
153      *
154      * @return The row count.
155      */

156     public int getRowCount() {
157         return this.series.getItemCount();
158     }
159
160     /**
161      * Returns the data value for a cell in the table model.
162      *
163      * @param row The row number.
164      * @param column The column number.
165      * @return the data value for a cell in the table model.
166      */

167     public Object JavaDoc 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 newTimePeriod;
185             }
186             else {
187                 if (column == 1) {
188                     return 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</code> if the specified cell is editable.
205      */

206     public boolean isCellEditable(int row, int column) {
207
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     /**
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 JavaDoc 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 JavaDoc v = Double.valueOf(value.toString());
237                     this.series.update(row, v);
238
239                 }
240                 catch (NumberFormatException JavaDoc 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
newTimePeriod = null;
249             }
250             else if (column == 1) {
251                 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 }
267
Popular Tags