KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > contrib > JoSQLFreeChartXYDataset


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.contrib;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
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 JavaDoc results = null;
44     private Map JavaDoc series = new HashMap JavaDoc ();
45     private List JavaDoc listeners = new ArrayList JavaDoc ();
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 JavaDoc c)
82     {
83
84     // Must be an integer.
85
return ((Integer JavaDoc) c).intValue ();
86
87     }
88
89     public Comparable JavaDoc getSeriesKey (int series)
90     {
91
92     return new Integer JavaDoc (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 JavaDoc (series));
107
108     }
109
110     public void addSeries (int series,
111                int xCol,
112                int yCol)
113                        throws IllegalArgumentException JavaDoc,
114                               IllegalStateException JavaDoc,
115                               QueryParseException
116     {
117
118     if (!this.parsed ())
119     {
120
121         throw new IllegalStateException JavaDoc ("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 JavaDoc ("X column index must be a minimum of 1.");
129
130     }
131
132     if (yCol < 1)
133     {
134
135         throw new IllegalArgumentException JavaDoc ("Y column index must be a minimum of 1.");
136
137     }
138
139     List JavaDoc cols = this.getColumns ();
140
141     if (xCol > cols.size ())
142     {
143
144         throw new IllegalArgumentException JavaDoc ("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 JavaDoc ("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 JavaDoc xc = xexp.getExpectedReturnType (this);
162
163     if (!Utilities.isNumber (xc))
164     {
165
166         throw new IllegalArgumentException JavaDoc ("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 JavaDoc yc = yexp.getExpectedReturnType (this);
177
178     if (!Utilities.isNumber (yc))
179     {
180
181         throw new IllegalArgumentException JavaDoc ("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 JavaDoc (series),
190              new Series (xCol,
191                      yCol));
192
193     }
194
195     /**
196      * Exectute the query and return the results. A reference to the results is also held to
197      * allow them to be iterated over. If you plan on re-using this data source then
198      * you should call: {@link #clearResults()} to free up the references to the results.
199      *
200      * @param l The List of objects to execute the query on.
201      * @return The results.
202      * @throws QueryExecutionException If the query cannot be executed, or if the query
203      * is set to return objects rather than "columns".
204      */

205     public QueryResults executeQuery (List JavaDoc 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     // Notify our listeners.
221
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 JavaDoc 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     // Bad man!
270
return ((Double JavaDoc) this.getX (series,
271                     item)).doubleValue ();
272
273     }
274
275     public double getYValue (int series,
276                  int item)
277     {
278
279     // Bad man!
280
return ((Double JavaDoc) this.getY (series,
281                     item)).doubleValue ();
282
283     }
284
285     public Number JavaDoc getX (int series,
286             int item)
287     {
288
289     if (this.results == null)
290     {
291
292         return new Double JavaDoc (0);
293
294     }
295
296     List JavaDoc l = (List JavaDoc) this.results.get (item);
297
298     Series s = (Series) this.series.get (new Integer JavaDoc (series));
299
300     if (s == null)
301     {
302
303         return new Double JavaDoc (0);
304
305     }
306
307     Number JavaDoc n = (Number JavaDoc) l.get (s.xCol - 1);
308
309     if (n instanceof Double JavaDoc)
310     {
311
312         return n;
313
314     } else {
315
316         return new Double JavaDoc (n.doubleValue ());
317
318     }
319
320     }
321
322     public Number JavaDoc getY (int series,
323             int item)
324     {
325
326     if (this.results == null)
327     {
328
329         return new Double JavaDoc (0);
330
331     }
332
333     List JavaDoc l = (List JavaDoc) this.results.get (item);
334
335     Series s = (Series) this.series.get (new Integer JavaDoc (series));
336
337     if (s == null)
338     {
339
340         return new Double JavaDoc (0);
341
342     }
343
344     Number JavaDoc n = (Number JavaDoc) l.get (s.yCol - 1);
345
346     if (n instanceof Double JavaDoc)
347     {
348
349         return n;
350
351     } else {
352
353         return new Double JavaDoc (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