KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedHashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.jfree.data.general.PieDataset;
24
25 import org.jfree.data.general.DatasetGroup;
26 import org.jfree.data.general.DatasetChangeListener;
27 import org.jfree.data.general.DatasetChangeEvent;
28
29 import org.josql.Query;
30 import org.josql.QueryResults;
31 import org.josql.QueryExecutionException;
32 import org.josql.QueryParseException;
33
34 import org.josql.internal.Utilities;
35
36 import org.josql.expressions.SelectItemExpression;
37
38 public class JoSQLFreeChartPieDataset extends Query implements PieDataset
39 {
40
41     private Map JavaDoc values = new LinkedHashMap JavaDoc ();
42     private int key = 0;
43     private int value = 0;
44     private List JavaDoc listeners = new ArrayList JavaDoc ();
45     private DatasetGroup group = null;
46
47     public JoSQLFreeChartPieDataset ()
48     {
49
50     }
51
52     public void addChangeListener (DatasetChangeListener l)
53     {
54
55     this.listeners.add (l);
56
57     }
58
59     public void removeChangeListener (DatasetChangeListener l)
60     {
61
62     this.listeners.remove (l);
63
64     }
65
66     public DatasetGroup getGroup ()
67     {
68
69     return this.group;
70
71     }
72
73     public void setGroup (DatasetGroup g)
74     {
75
76     this.group = g;
77
78     }
79
80     public void setKeyValue (int keyCol,
81                  int valueCol)
82                          throws IllegalArgumentException JavaDoc,
83                                 IllegalStateException JavaDoc,
84                                 QueryParseException
85     {
86
87     if (!this.parsed ())
88     {
89
90         throw new IllegalStateException JavaDoc ("Cannot specify the key and value columns until a query has been specified and parsed.");
91
92     }
93
94     if (keyCol < 1)
95     {
96
97         throw new IllegalArgumentException JavaDoc ("Key column index must be a minimum of 1.");
98
99     }
100
101     if (valueCol < 1)
102     {
103
104         throw new IllegalArgumentException JavaDoc ("Value column index must be a minimum of 1.");
105
106     }
107
108     List JavaDoc cols = this.getColumns ();
109
110     if (keyCol > cols.size ())
111     {
112
113         throw new IllegalArgumentException JavaDoc ("Key column index must be a minimum of " +
114                         cols.size () +
115                         ".");
116
117     }
118
119     if (valueCol > cols.size ())
120     {
121
122         throw new IllegalArgumentException JavaDoc ("Value column index must be a minimum of " +
123                         cols.size () +
124                         ".");
125
126     }
127
128     SelectItemExpression vexp = (SelectItemExpression) cols.get (valueCol - 1);
129
130     Class JavaDoc vc = vexp.getExpectedReturnType (this);
131
132     if (!Utilities.isNumber (vc))
133     {
134
135         throw new IllegalArgumentException JavaDoc ("Value column: " +
136                         valueCol +
137                         " will evaluate to an instance of type: " +
138                         vc.getName () +
139                         ", but only columns that return numbers are allowed.");
140         
141     }
142
143     this.key = keyCol;
144     this.value = valueCol;
145
146     }
147
148     /**
149      * Exectute the query and return the results. A reference to the results is also held to
150      * allow them to be iterated over. If you plan on re-using this data source then
151      * you should call: {@link #clearResults()} to free up the references to the results.
152      *
153      * @param l The List of objects to execute the query on.
154      * @return The results.
155      * @throws QueryExecutionException If the query cannot be executed, or if the query
156      * is set to return objects rather than "columns".
157      */

158     public QueryResults executeQuery (List JavaDoc l)
159                                   throws QueryExecutionException
160     {
161
162     if (this.isWantObjects ())
163     {
164
165         throw new QueryExecutionException ("Only SQL statements that return columns (not the objects passed in) can be used.");
166
167     }
168
169     if ((this.key == 0)
170         ||
171         (this.value == 0)
172        )
173     {
174
175         throw new IllegalStateException JavaDoc ("Key and/or value columns not specified.");
176
177     }
178
179     QueryResults qr = super.execute (l);
180
181     List JavaDoc res = qr.getResults ();
182
183     Map JavaDoc nValues = new LinkedHashMap JavaDoc ();
184
185     for (int i = 0; i < res.size (); i++)
186     {
187
188         List JavaDoc resR = (List JavaDoc) res.get (i);
189
190         // Get the key.
191
Object JavaDoc k = resR.get (this.key - 1);
192         
193         // Get the value.
194
Object JavaDoc v = resR.get (this.value - 1);
195
196         String JavaDoc kv = null + "";
197
198         if (k == null)
199         {
200
201         nValues.put (kv,
202                  v);
203
204         }
205
206         // See if the key is "comparable".
207
if (k instanceof Comparable JavaDoc)
208         {
209
210         nValues.put (k,
211                  v);
212
213         } else {
214
215         kv = k.toString ();
216
217         nValues.put (kv,
218                  v);
219
220         }
221
222     }
223
224     // Just switch them, this way enables any client of the data to
225
// still iterate over them without fear of a ConcurrentModificationException
226
// occuring.
227
this.values = nValues;
228
229     // Notify our listeners.
230
DatasetChangeEvent dce = new DatasetChangeEvent (this,
231                              this);
232
233     for (int i = 0; i < this.listeners.size (); i++)
234     {
235
236         DatasetChangeListener d = (DatasetChangeListener) this.listeners.get (i);
237
238         d.datasetChanged (dce);
239
240     }
241
242     return qr;
243
244     }
245
246     public int getItemCount ()
247     {
248
249     return this.values.size ();
250
251     }
252
253     public Number JavaDoc getValue (int index)
254     {
255
256     int c = 0;
257
258     Iterator JavaDoc iter = this.values.keySet ().iterator ();
259
260     while (iter.hasNext ())
261     {
262
263         Object JavaDoc k = iter.next ();
264
265         if (index == c)
266         {
267
268         return (Number JavaDoc) this.values.get (k);
269
270         }
271
272         c++;
273
274     }
275
276     return new Double JavaDoc (-1);
277
278     }
279
280     public int getIndex (Comparable JavaDoc k)
281     {
282
283     int c = 0;
284
285     Iterator JavaDoc iter = this.values.keySet ().iterator ();
286
287     while (iter.hasNext ())
288     {
289
290         Comparable JavaDoc ko = (Comparable JavaDoc) iter.next ();
291
292         if (ko.compareTo (k) == 0)
293         {
294
295         return c;
296
297         }
298
299         c++;
300
301     }
302
303     return -1;
304
305     }
306
307     public Comparable JavaDoc getKey (int index)
308     {
309
310     int c = 0;
311
312     Iterator JavaDoc iter = this.values.keySet ().iterator ();
313
314     while (iter.hasNext ())
315     {
316
317         Object JavaDoc k = iter.next ();
318
319         if (index == c)
320         {
321
322         return (Comparable JavaDoc) k;
323
324         }
325
326         c++;
327
328     }
329
330     return null;
331
332     }
333
334     public Number JavaDoc getValue (Comparable JavaDoc key)
335     {
336
337     return (Number JavaDoc) this.values.get (key);
338
339     }
340
341     public List JavaDoc getKeys ()
342     {
343
344     return new ArrayList JavaDoc (this.values.keySet ());
345
346     }
347
348 }
349
Popular Tags