1 15 package org.josql.contrib; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Map ; 20 import java.util.HashMap ; 21 22 import org.jfree.data.xy.XYDataset; 23 24 import org.jfree.data.DomainOrder; 25 26 import org.jfree.data.general.DatasetGroup; 27 import org.jfree.data.general.DatasetChangeListener; 28 import org.jfree.data.general.DatasetChangeEvent; 29 30 import org.josql.Query; 31 import org.josql.QueryResults; 32 import org.josql.QueryExecutionException; 33 import org.josql.QueryParseException; 34 35 import org.josql.internal.Utilities; 36 37 import org.josql.expressions.SelectItemExpression; 38 39 public class JoSQLFreeChartXYDataset extends Query implements XYDataset 40 { 41 42 private int row = 0; 43 private List results = null; 44 private Map series = new HashMap (); 45 private List listeners = new ArrayList (); 46 private DatasetGroup group = null; 47 48 public JoSQLFreeChartXYDataset () 49 { 50 51 } 52 53 public void addChangeListener (DatasetChangeListener l) 54 { 55 56 this.listeners.add (l); 57 58 } 59 60 public void removeChangeListener (DatasetChangeListener l) 61 { 62 63 this.listeners.remove (l); 64 65 } 66 67 public DatasetGroup getGroup () 68 { 69 70 return this.group; 71 72 } 73 74 public void setGroup (DatasetGroup g) 75 { 76 77 this.group = g; 78 79 } 80 81 public int indexOf (Comparable c) 82 { 83 84 return ((Integer ) c).intValue (); 86 87 } 88 89 public Comparable getSeriesKey (int series) 90 { 91 92 return new Integer (series); 93 94 } 95 96 public int getSeriesCount () 97 { 98 99 return this.series.size (); 100 101 } 102 103 public void removeSeries (int series) 104 { 105 106 this.series.remove (new Integer (series)); 107 108 } 109 110 public void addSeries (int series, 111 int xCol, 112 int yCol) 113 throws IllegalArgumentException , 114 IllegalStateException , 115 QueryParseException 116 { 117 118 if (!this.parsed ()) 119 { 120 121 throw new IllegalStateException ("Cannot add a series until a query has been specified and parsed."); 122 123 } 124 125 if (xCol < 1) 126 { 127 128 throw new IllegalArgumentException ("X column index must be a minimum of 1."); 129 130 } 131 132 if (yCol < 1) 133 { 134 135 throw new IllegalArgumentException ("Y column index must be a minimum of 1."); 136 137 } 138 139 List cols = this.getColumns (); 140 141 if (xCol > cols.size ()) 142 { 143 144 throw new IllegalArgumentException ("X column index must be a minimum of " + 145 cols.size () + 146 "."); 147 148 } 149 150 if (yCol > cols.size ()) 151 { 152 153 throw new IllegalArgumentException ("Y column index must be a minimum of " + 154 cols.size () + 155 "."); 156 157 } 158 159 SelectItemExpression xexp = (SelectItemExpression) cols.get (xCol - 1); 160 161 Class xc = xexp.getExpectedReturnType (this); 162 163 if (!Utilities.isNumber (xc)) 164 { 165 166 throw new IllegalArgumentException ("X column: " + 167 xexp + 168 " will evaluate to an instance of type: " + 169 xc.getName () + 170 ", but only columns that return numbers are allowed."); 171 172 } 173 174 SelectItemExpression yexp = (SelectItemExpression) cols.get (yCol - 1); 175 176 Class yc = yexp.getExpectedReturnType (this); 177 178 if (!Utilities.isNumber (yc)) 179 { 180 181 throw new IllegalArgumentException ("Y column: " + 182 yexp + 183 " will evaluate to an instance of type: " + 184 yc.getName () + 185 ", but only columns that return numbers are allowed."); 186 187 } 188 189 this.series.put (new Integer (series), 190 new Series (xCol, 191 yCol)); 192 193 } 194 195 205 public QueryResults executeQuery (List l) 206 throws QueryExecutionException 207 { 208 209 if (this.isWantObjects ()) 210 { 211 212 throw new QueryExecutionException ("Only SQL statements that return columns (not the objects passed in) can be used."); 213 214 } 215 216 QueryResults qr = super.execute (l); 217 218 this.results = qr.getResults (); 219 220 DatasetChangeEvent dce = new DatasetChangeEvent (this, 222 this); 223 224 for (int i = 0; i < this.listeners.size (); i++) 225 { 226 227 DatasetChangeListener d = (DatasetChangeListener) this.listeners.get (i); 228 229 d.datasetChanged (dce); 230 231 } 232 233 return qr; 234 235 } 236 237 public List getResults () 238 { 239 240 return this.results; 241 242 } 243 244 public void clearResults () 245 { 246 247 this.results = null; 248 249 } 250 251 public int getItemCount (int series) 252 { 253 254 if (this.results == null) 255 { 256 257 return 0; 258 259 } 260 261 return this.results.size (); 262 263 } 264 265 public double getXValue (int series, 266 int item) 267 { 268 269 return ((Double ) this.getX (series, 271 item)).doubleValue (); 272 273 } 274 275 public double getYValue (int series, 276 int item) 277 { 278 279 return ((Double ) this.getY (series, 281 item)).doubleValue (); 282 283 } 284 285 public Number getX (int series, 286 int item) 287 { 288 289 if (this.results == null) 290 { 291 292 return new Double (0); 293 294 } 295 296 List l = (List ) this.results.get (item); 297 298 Series s = (Series) this.series.get (new Integer (series)); 299 300 if (s == null) 301 { 302 303 return new Double (0); 304 305 } 306 307 Number n = (Number ) l.get (s.xCol - 1); 308 309 if (n instanceof Double ) 310 { 311 312 return n; 313 314 } else { 315 316 return new Double (n.doubleValue ()); 317 318 } 319 320 } 321 322 public Number getY (int series, 323 int item) 324 { 325 326 if (this.results == null) 327 { 328 329 return new Double (0); 330 331 } 332 333 List l = (List ) this.results.get (item); 334 335 Series s = (Series) this.series.get (new Integer (series)); 336 337 if (s == null) 338 { 339 340 return new Double (0); 341 342 } 343 344 Number n = (Number ) l.get (s.yCol - 1); 345 346 if (n instanceof Double ) 347 { 348 349 return n; 350 351 } else { 352 353 return new Double (n.doubleValue ()); 354 355 } 356 357 } 358 359 public DomainOrder getDomainOrder () 360 { 361 362 return DomainOrder.ASCENDING; 363 364 } 365 366 private class Series 367 { 368 369 public int xCol = 0; 370 public int yCol = 0; 371 372 public Series (int x, 373 int y) 374 { 375 376 this.xCol = x; 377 this.yCol = y; 378 379 } 380 381 } 382 383 } 384 | Popular Tags |