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 implements
058: 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: } else {
121: if (column == 1) {
122: return Double.class;
123: } else {
124: return null;
125: }
126: }
127: }
128:
129: /**
130: * Returns the name of a column
131: *
132: * @param column the column index.
133: *
134: * @return The name of a column.
135: */
136: public String getColumnName(int column) {
137:
138: if (column == 0) {
139: return "Period:";
140: } else {
141: if (column == 1) {
142: return "Value:";
143: } else {
144: return null;
145: }
146: }
147:
148: }
149:
150: /**
151: * Returns the number of rows in the table model.
152: *
153: * @return The row count.
154: */
155: public int getRowCount() {
156: return this .series.getItemCount();
157: }
158:
159: /**
160: * Returns the data value for a cell in the table model.
161: *
162: * @param row the row number.
163: * @param column the column number.
164: *
165: * @return The data value for a cell in the table model.
166: */
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: } else {
173: if (column == 1) {
174: return this .series.getValue(row);
175: } else {
176: return null;
177: }
178: }
179: } else {
180: if (column == 0) {
181: return this .newTimePeriod;
182: } else {
183: if (column == 1) {
184: return this .newValue;
185: } else {
186: return null;
187: }
188: }
189: }
190:
191: }
192:
193: /**
194: * Returns a flag indicating whether or not the specified cell is editable.
195: *
196: * @param row the row number.
197: * @param column the column number.
198: *
199: * @return <code>true</code> if the specified cell is editable.
200: */
201: public boolean isCellEditable(int row, int column) {
202: if (this .editable) {
203: if ((column == 0) || (column == 1)) {
204: return true;
205: } else {
206: return false;
207: }
208: } else {
209: return false;
210: }
211: }
212:
213: /**
214: * Updates the time series.
215: *
216: * @param value the new value.
217: * @param row the row.
218: * @param column the column.
219: */
220: public void setValueAt(Object value, int row, int column) {
221:
222: if (row < this .series.getItemCount()) {
223:
224: // update the time series appropriately
225: if (column == 1) {
226: try {
227: Double v = Double.valueOf(value.toString());
228: this .series.update(row, v);
229:
230: } catch (NumberFormatException nfe) {
231: System.err.println("Number format exception");
232: }
233: }
234: } else {
235: if (column == 0) {
236: // this.series.getClass().valueOf(value.toString());
237: this .newTimePeriod = null;
238: } else if (column == 1) {
239: this .newValue = Double.valueOf(value.toString());
240: }
241: }
242: }
243:
244: /**
245: * Receives notification that the time series has been changed. Responds
246: * by firing a table data change event.
247: *
248: * @param event the event.
249: */
250: public void seriesChanged(SeriesChangeEvent event) {
251: fireTableDataChanged();
252: }
253:
254: }
|