1 45 46 package org.jfree.data.time; 47 48 import javax.swing.table.AbstractTableModel ; 49 50 import org.jfree.data.general.SeriesChangeEvent; 51 import org.jfree.data.general.SeriesChangeListener; 52 53 57 public class TimeSeriesTableModel extends AbstractTableModel 58 implements SeriesChangeListener { 59 60 61 private TimeSeries series; 62 63 64 private boolean editable; 65 66 67 private RegularTimePeriod newTimePeriod; 68 69 70 private Number newValue; 71 72 75 public TimeSeriesTableModel() { 76 this(new TimeSeries("Untitled")); 77 } 78 79 84 public TimeSeriesTableModel(TimeSeries series) { 85 this(series, false); 86 } 87 88 94 public TimeSeriesTableModel(TimeSeries series, boolean editable) { 95 this.series = series; 96 this.series.addChangeListener(this); 97 this.editable = editable; 98 } 99 100 106 public int getColumnCount() { 107 return 2; 108 } 109 110 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 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 159 public int getRowCount() { 160 return this.series.getItemCount(); 161 } 162 163 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 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 231 public void setValueAt(Object value, int row, int column) { 232 233 if (row < this.series.getItemCount()) { 234 235 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.newTimePeriod = null; 251 } 252 else if (column == 1) { 253 this.newValue = Double.valueOf(value.toString()); 254 } 255 } 256 } 257 258 264 public void seriesChanged(SeriesChangeEvent event) { 265 fireTableDataChanged(); 266 } 267 268 } 269 | Popular Tags |