1 40 41 package org.jfree.data; 42 43 import javax.swing.table.AbstractTableModel ; 44 45 import org.jfree.data.time.RegularTimePeriod; 46 import org.jfree.data.time.TimeSeries; 47 48 53 public class TimeSeriesTableModel extends AbstractTableModel implements SeriesChangeListener { 54 55 56 private TimeSeries series; 57 58 59 private boolean editable; 60 61 62 private RegularTimePeriod newTimePeriod; 63 64 65 private Number newValue; 66 67 70 public TimeSeriesTableModel() { 71 this(new TimeSeries("Untitled")); 72 } 73 74 79 public TimeSeriesTableModel(TimeSeries series) { 80 this(series, false); 81 } 82 83 89 public TimeSeriesTableModel(TimeSeries series, boolean editable) { 90 91 this.series = series; 92 this.series.addChangeListener(this); 93 this.editable = editable; 94 } 95 96 102 public int getColumnCount() { 103 return 2; 104 } 105 106 112 public Class getColumnClass(int column) { 113 114 if (column == 0) { 115 return String .class; 116 } 117 else { 118 if (column == 1) { 119 return Double .class; 120 } 121 else { 122 return null; 123 } 124 } 125 126 } 127 128 135 public String 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 156 public int getRowCount() { 157 return this.series.getItemCount(); 158 } 159 160 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 } 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 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 229 public void setValueAt(Object value, int row, int column) { 230 231 if (row < this.series.getItemCount()) { 232 233 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 newTimePeriod = null; 249 } 250 else if (column == 1) { 251 newValue = Double.valueOf(value.toString()); 252 } 253 } 254 } 255 256 262 public void seriesChanged(SeriesChangeEvent event) { 263 fireTableDataChanged(); 264 } 265 266 } 267 | Popular Tags |