Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 14-Nov-2001 : Version 1 (DG); 38: * 05-Apr-2002 : Removed redundant first column (DG); 39: * 24-Jun-2002 : Removed unnecessary local variable (DG); 40: * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG); 41: * 42: */ 43: 44: package org.jfree.data.time; 45: 46: import javax.swing.table.AbstractTableModel; 47: 48: import org.jfree.data.general.SeriesChangeEvent; 49: import org.jfree.data.general.SeriesChangeListener; 50: 51: /** 52: * Wrapper around a time series to convert it to a table model for use in 53: * a <code>JTable</code>. 54: */ 55: public class TimeSeriesTableModel extends AbstractTableModel 56: implements SeriesChangeListener { 57: 58: /** The series. */ 59: private TimeSeries series; 60: 61: /** A flag that controls whether the series is editable. */ 62: private boolean editable; 63: 64: /** The new time period. */ 65: private RegularTimePeriod newTimePeriod; 66: 67: /** The new value. */ 68: private Number newValue; 69: 70: /** 71: * Default constructor. 72: */ 73: public TimeSeriesTableModel() { 74: this(new TimeSeries("Untitled")); 75: } 76: 77: /** 78: * Constructs a table model for a time series. 79: * 80: * @param series the time series. 81: */ 82: public TimeSeriesTableModel(TimeSeries series) { 83: this(series, false); 84: } 85: 86: /** 87: * Creates a table model based on a time series. 88: * 89: * @param series the time series. 90: * @param editable if <ocde>true</code>, the table is editable. 91: */ 92: public TimeSeriesTableModel(TimeSeries series, boolean editable) { 93: this.series = series; 94: this.series.addChangeListener(this); 95: this.editable = editable; 96: } 97: 98: /** 99: * 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: public int getColumnCount() { 105: return 2; 106: } 107: 108: /** 109: * Returns the column class in the table model. 110: * 111: * @param column The column index. 112: * 113: * @return The column class in the table model. 114: */ 115: public Class getColumnClass(int column) { 116: if (column == 0) { 117: return String.class; 118: } 119: else { 120: if (column == 1) { 121: return Double.class; 122: } 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: } 141: else { 142: if (column == 1) { 143: return "Value:"; 144: } 145: else { 146: return null; 147: } 148: } 149: 150: } 151: 152: /** 153: * Returns the number of rows in the table model. 154: * 155: * @return The row count. 156: */ 157: public int getRowCount() { 158: return this.series.getItemCount(); 159: } 160: 161: /** 162: * Returns the data value for a cell in the table model. 163: * 164: * @param row the row number. 165: * @param column the column number. 166: * 167: * @return The data value for a cell in the table model. 168: */ 169: public Object getValueAt(int row, int column) { 170: 171: if (row < this.series.getItemCount()) { 172: if (column == 0) { 173: return this.series.getTimePeriod(row); 174: } 175: else { 176: if (column == 1) { 177: return this.series.getValue(row); 178: } 179: else { 180: return null; 181: } 182: } 183: } 184: else { 185: if (column == 0) { 186: return this.newTimePeriod; 187: } 188: else { 189: if (column == 1) { 190: return this.newValue; 191: } 192: else { 193: return null; 194: } 195: } 196: } 197: 198: } 199: 200: /** 201: * Returns a flag indicating whether or not the specified cell is editable. 202: * 203: * @param row the row number. 204: * @param column the column number. 205: * 206: * @return <code>true</code> if the specified cell is editable. 207: */ 208: public boolean isCellEditable(int row, int column) { 209: if (this.editable) { 210: if ((column == 0) || (column == 1)) { 211: return true; 212: } 213: else { 214: return false; 215: } 216: } 217: else { 218: return false; 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 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 v = Double.valueOf(value.toString()); 237: this.series.update(row, v); 238: 239: } 240: catch (NumberFormatException 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: this.newTimePeriod = null; 249: } 250: else if (column == 1) { 251: this.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: }