1 40 package org.jfree.data; 41 42 import java.util.ArrayList ; 43 import java.util.List ; 44 45 import org.jfree.util.ObjectUtils; 46 47 53 public class DefaultTableXYDataset extends AbstractSeriesDataset implements TableXYDataset { 54 55 56 private List data = null; 57 58 59 private List xPoints = null; 60 61 62 private boolean propagateEvents = true; 63 64 67 public DefaultTableXYDataset() { 68 this.data = new ArrayList (); 69 this.xPoints = new ArrayList (); 70 } 71 72 77 public DefaultTableXYDataset(XYSeries series) { 78 79 this.data = new ArrayList (); 80 this.xPoints = new ArrayList (); 81 if (series != null) { 82 if (series.getAllowDuplicateXValues()) { 83 throw new IllegalArgumentException ("Cannot accept XY Series that allow duplicate " 84 + "values. Use XYSeries(seriesName, false) constructor."); 85 } 86 updateXPoints(series); 87 data.add(series); 88 series.addChangeListener(this); 89 } 90 91 } 92 93 100 public void addSeries(XYSeries series) { 101 102 if (series == null) { 104 throw new IllegalArgumentException ( 105 "XYSeriesCollection.addSeries(...): cannot add null series."); 106 } 107 if (series.getAllowDuplicateXValues()) { 108 throw new IllegalArgumentException ("Cannot accept XY Series that allow duplicate " 109 + "values. Use XYSeries(seriesName, false) constructor."); 110 } 111 112 this.updateXPoints(series); 114 data.add(series); 115 series.addChangeListener(this); 116 fireDatasetChanged(); 117 118 } 119 120 125 private void updateXPoints(XYSeries series) { 126 ArrayList seriesXPoints = new ArrayList (); 127 boolean savedState = this.propagateEvents; 128 this.propagateEvents = false; 129 for (int itemNo = 0; itemNo < series.getItemCount(); itemNo++) { 130 Number xValue = series.getXValue(itemNo); 131 seriesXPoints.add(xValue); 132 if (!xPoints.contains(xValue)) { 133 xPoints.add(xValue); 134 for (int seriesNo = 0; seriesNo < this.data.size(); seriesNo++) { 135 XYSeries dataSeries = (XYSeries) data.get(seriesNo); 136 if (!dataSeries.equals(series)) { 137 dataSeries.add(xValue, null); 138 } 139 } 140 } 141 } 142 for (int itemNo = 0; itemNo < xPoints.size(); itemNo++) { 143 Number xPoint = (Number ) this.xPoints.get(itemNo); 144 if (!seriesXPoints.contains(xPoint)) { 145 series.add(xPoint, null); 146 } 147 } 148 this.propagateEvents = savedState; 149 } 150 151 154 public void updateXPoints() { 155 this.propagateEvents = false; 156 for (int seriesNo = 0; seriesNo < data.size(); seriesNo++) { 157 updateXPoints((XYSeries) data.get(seriesNo)); 158 } 159 this.propagateEvents = true; 160 } 161 162 167 public int getSeriesCount() { 168 return this.data.size(); 169 } 170 171 176 public int getItemCount() { 177 if (this.xPoints == null) { 178 return 0; 179 } 180 else { 181 return this.xPoints.size(); 182 } 183 } 184 185 192 public XYSeries getSeries(int series) { 193 194 if ((series < 0) || (series > getSeriesCount())) { 196 throw new IllegalArgumentException ( 197 "XYSeriesCollection.getSeries(...): index outside valid range."); 198 } 199 200 XYSeries ts = (XYSeries) data.get(series); 202 return ts; 203 204 } 205 206 213 public String getSeriesName(int series) { 214 215 217 return getSeries(series).getName(); 219 220 } 221 222 229 public int getItemCount(int series) { 230 231 233 return getSeries(series).getItemCount(); 235 236 } 237 238 246 public Number getXValue(int series, int item) { 247 248 XYSeries ts = (XYSeries) data.get(series); 249 XYDataItem dataItem = ts.getDataItem(item); 250 return dataItem.getX(); 251 252 } 253 254 262 public Number getYValue(int series, int index) { 263 264 XYSeries ts = (XYSeries) data.get(series); 265 XYDataItem dataItem = ts.getDataItem(index); 266 return dataItem.getY(); 267 268 } 269 270 275 public void removeAllSeries() { 276 277 for (int i = 0; i < this.data.size(); i++) { 279 XYSeries series = (XYSeries) this.data.get(i); 280 series.removeChangeListener(this); 281 } 282 283 data.clear(); 285 xPoints.clear(); 286 fireDatasetChanged(); 287 288 } 289 290 297 public void removeSeries(XYSeries series) { 298 299 if (series == null) { 301 throw new IllegalArgumentException ( 302 "XYSeriesCollection.removeSeries(...): cannot remove null series."); 303 } 304 305 if (data.contains(series)) { 307 series.removeChangeListener(this); 308 data.remove(series); 309 if (data.size() == 0) xPoints.clear(); 310 fireDatasetChanged(); 311 } 312 313 } 314 315 322 public void removeSeries(int series) { 323 324 if ((series < 0) || (series > getSeriesCount())) { 326 throw new IllegalArgumentException ( 327 "XYSeriesCollection.removeSeries(...): index outside valid range."); 328 } 329 330 XYSeries ts = (XYSeries) data.get(series); 332 ts.removeChangeListener(this); 333 data.remove(series); 334 if (data.size() == 0) xPoints.clear(); 335 fireDatasetChanged(); 336 337 } 338 339 344 public void seriesChanged(SeriesChangeEvent event) { 345 if (this.propagateEvents) { 346 updateXPoints(); 347 fireDatasetChanged(); 348 } 349 } 350 351 358 public boolean equals(Object obj) { 359 360 if (obj == null) { 361 return false; 362 } 363 364 if (obj == this) { 365 return true; 366 } 367 368 if (obj instanceof DefaultTableXYDataset) { 369 DefaultTableXYDataset c = (DefaultTableXYDataset) obj; 370 return ObjectUtils.equal(this.data, c.data); 371 } 372 373 return false; 374 375 } 376 377 } 378 | Popular Tags |