KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > gantt > TaskSeriesCollection


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------------
27  * TaskSeriesCollection.java
28  * -------------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Thomas Schuster;
33  *
34  * $Id: TaskSeriesCollection.java,v 1.9 2005/05/20 08:20:04 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 06-Jun-2002 : Version 1 (DG);
39  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and
41  * CategoryToolTipGenerator interface (DG);
42  * 10-Jan-2003 : Renamed GanttSeriesCollection --> TaskSeriesCollection (DG);
43  * 04-Sep-2003 : Fixed bug 800324 (DG);
44  * 16-Sep-2003 : Implemented GanttCategoryDataset (DG);
45  * 12-Jan-2005 : Fixed bug 1099331 (DG);
46  *
47  */

48
49 package org.jfree.data.gantt;
50
51 import java.io.Serializable JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54
55 import org.jfree.data.general.AbstractSeriesDataset;
56 import org.jfree.data.general.SeriesChangeEvent;
57 import org.jfree.data.time.TimePeriod;
58 import org.jfree.util.ObjectUtilities;
59 import org.jfree.util.PublicCloneable;
60
61 /**
62  * A collection of {@link TaskSeries} objects. This class provides one
63  * implementation of the {@link GanttCategoryDataset} interface.
64  */

65 public class TaskSeriesCollection extends AbstractSeriesDataset
66                                   implements GanttCategoryDataset,
67                                              Cloneable JavaDoc, PublicCloneable,
68                                              Serializable JavaDoc {
69
70     /** For serialization. */
71     private static final long serialVersionUID = -2065799050738449903L;
72
73     /**
74      * Storage for aggregate task keys (the task description is used as the
75      * key).
76      */

77     private List JavaDoc keys;
78
79     /** Storage for the series. */
80     private List JavaDoc data;
81
82     /**
83      * Default constructor.
84      */

85     public TaskSeriesCollection() {
86         this.keys = new java.util.ArrayList JavaDoc();
87         this.data = new java.util.ArrayList JavaDoc();
88     }
89
90     /**
91      * Returns the number of series in the collection.
92      *
93      * @return The series count.
94      */

95     public int getSeriesCount() {
96         return getRowCount();
97     }
98
99     /**
100      * Returns the name of a series.
101      *
102      * @param series the series index (zero-based).
103      *
104      * @return The name of a series.
105      */

106     public Comparable JavaDoc getSeriesKey(int series) {
107         TaskSeries ts = (TaskSeries) this.data.get(series);
108         return ts.getKey();
109     }
110
111     /**
112      * Returns the number of rows (series) in the collection.
113      *
114      * @return The series count.
115      */

116     public int getRowCount() {
117         return this.data.size();
118     }
119
120     /**
121      * Returns the row keys. In this case, each series is a key.
122      *
123      * @return The row keys.
124      */

125     public List JavaDoc getRowKeys() {
126         return this.data;
127     }
128
129     /**
130      * Returns the number of column in the dataset.
131      *
132      * @return The column count.
133      */

134     public int getColumnCount() {
135         return this.keys.size();
136     }
137
138     /**
139      * Returns a list of the column keys in the dataset.
140      *
141      * @return The category list.
142      */

143     public List JavaDoc getColumnKeys() {
144         return this.keys;
145     }
146
147     /**
148      * Returns a column key.
149      *
150      * @param index the column index.
151      *
152      * @return The column key.
153      */

154     public Comparable JavaDoc getColumnKey(int index) {
155         return (Comparable JavaDoc) this.keys.get(index);
156     }
157
158     /**
159      * Returns the column index for a column key.
160      *
161      * @param columnKey the columnKey.
162      *
163      * @return The column index.
164      */

165     public int getColumnIndex(Comparable JavaDoc columnKey) {
166         return this.keys.indexOf(columnKey);
167     }
168
169     /**
170      * Returns the row index for the given row key.
171      *
172      * @param rowKey the row key.
173      *
174      * @return The index.
175      */

176     public int getRowIndex(Comparable JavaDoc rowKey) {
177         int result = -1;
178         int count = this.data.size();
179         for (int i = 0; i < count; i++) {
180             TaskSeries s = (TaskSeries) this.data.get(i);
181             if (s.getKey().equals(rowKey)) {
182                 result = i;
183                 break;
184             }
185         }
186         return result;
187     }
188
189     /**
190      * Returns the key for a row.
191      *
192      * @param index the row index (zero-based).
193      *
194      * @return The key.
195      */

196     public Comparable JavaDoc getRowKey(int index) {
197         TaskSeries series = (TaskSeries) this.data.get(index);
198         return series.getKey();
199     }
200
201     /**
202      * Adds a series to the dataset and sends a
203      * {@link org.jfree.data.general.DatasetChangeEvent} to all registered
204      * listeners.
205      *
206      * @param series the series (<code>null</code> not permitted).
207      */

208     public void add(TaskSeries series) {
209         if (series == null) {
210             throw new IllegalArgumentException JavaDoc("Null 'series' argument.");
211         }
212         this.data.add(series);
213         series.addChangeListener(this);
214
215         // look for any keys that we don't already know about...
216
Iterator JavaDoc iterator = series.getTasks().iterator();
217         while (iterator.hasNext()) {
218             Task task = (Task) iterator.next();
219             String JavaDoc key = task.getDescription();
220             int index = this.keys.indexOf(key);
221             if (index < 0) {
222                 this.keys.add(key);
223             }
224         }
225         fireDatasetChanged();
226     }
227
228     /**
229      * Removes a series from the collection and sends
230      * a {@link org.jfree.data.general.DatasetChangeEvent}
231      * to all registered listeners.
232      *
233      * @param series the series.
234      */

235     public void remove(TaskSeries series) {
236         if (series == null) {
237             throw new IllegalArgumentException JavaDoc("Null 'series' argument.");
238         }
239         if (this.data.contains(series)) {
240             series.removeChangeListener(this);
241             this.data.remove(series);
242             fireDatasetChanged();
243         }
244     }
245
246     /**
247      * Removes a series from the collection and sends
248      * a {@link org.jfree.data.general.DatasetChangeEvent}
249      * to all registered listeners.
250      *
251      * @param series the series (zero based index).
252      */

253     public void remove(int series) {
254         if ((series < 0) || (series > getSeriesCount())) {
255             throw new IllegalArgumentException JavaDoc(
256                 "TaskSeriesCollection.remove(): index outside valid range.");
257         }
258
259         // fetch the series, remove the change listener, then remove the series.
260
TaskSeries ts = (TaskSeries) this.data.get(series);
261         ts.removeChangeListener(this);
262         this.data.remove(series);
263         fireDatasetChanged();
264
265     }
266
267     /**
268      * Removes all the series from the collection and sends
269      * a {@link org.jfree.data.general.DatasetChangeEvent}
270      * to all registered listeners.
271      */

272     public void removeAll() {
273
274         // deregister the collection as a change listener to each series in
275
// the collection.
276
Iterator JavaDoc iterator = this.data.iterator();
277         while (iterator.hasNext()) {
278             TaskSeries series = (TaskSeries) iterator.next();
279             series.removeChangeListener(this);
280         }
281
282         // remove all the series from the collection and notify listeners.
283
this.data.clear();
284         fireDatasetChanged();
285
286     }
287
288     /**
289      * Returns the value for an item.
290      *
291      * @param rowKey the row key.
292      * @param columnKey the column key.
293      *
294      * @return The item value.
295      */

296     public Number JavaDoc getValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
297         return getStartValue(rowKey, columnKey);
298     }
299
300     /**
301      * Returns the value for a task.
302      *
303      * @param row the row index (zero-based).
304      * @param column the column index (zero-based).
305      *
306      * @return The start value.
307      */

308     public Number JavaDoc getValue(int row, int column) {
309         return getStartValue(row, column);
310     }
311
312     /**
313      * Returns the start value for a task.
314      *
315      * @param rowKey the series.
316      * @param columnKey the category.
317      *
318      * @return The start value.
319      */

320     public Number JavaDoc getStartValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
321         Number JavaDoc result = null;
322         int row = getRowIndex(rowKey);
323         TaskSeries series = (TaskSeries) this.data.get(row);
324         Task task = series.get(columnKey.toString());
325         if (task != null) {
326             TimePeriod duration = task.getDuration();
327             result = new Long JavaDoc(duration.getStart().getTime());
328         }
329         return result;
330     }
331
332     /**
333      * Returns the start value for a task.
334      *
335      * @param row the row index (zero-based).
336      * @param column the column index (zero-based).
337      *
338      * @return The start value.
339      */

340     public Number JavaDoc getStartValue(int row, int column) {
341         Comparable JavaDoc rowKey = getRowKey(row);
342         Comparable JavaDoc columnKey = getColumnKey(column);
343         return getStartValue(rowKey, columnKey);
344     }
345
346     /**
347      * Returns the end value for a task.
348      *
349      * @param rowKey the series.
350      * @param columnKey the category.
351      *
352      * @return The end value.
353      */

354     public Number JavaDoc getEndValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
355         Number JavaDoc result = null;
356         int row = getRowIndex(rowKey);
357         TaskSeries series = (TaskSeries) this.data.get(row);
358         Task task = series.get(columnKey.toString());
359         if (task != null) {
360             TimePeriod duration = task.getDuration();
361             result = new Long JavaDoc(duration.getEnd().getTime());
362         }
363         return result;
364     }
365
366     /**
367      * Returns the end value for a task.
368      *
369      * @param row the row index (zero-based).
370      * @param column the column index (zero-based).
371      *
372      * @return The end value.
373      */

374     public Number JavaDoc getEndValue(int row, int column) {
375         Comparable JavaDoc rowKey = getRowKey(row);
376         Comparable JavaDoc columnKey = getColumnKey(column);
377         return getEndValue(rowKey, columnKey);
378     }
379
380     /**
381      * Returns the percent complete for a given item.
382      *
383      * @param row the row index (zero-based).
384      * @param column the column index (zero-based).
385      *
386      * @return The percent complete (possibly <code>null</code>).
387      */

388     public Number JavaDoc getPercentComplete(int row, int column) {
389         Comparable JavaDoc rowKey = getRowKey(row);
390         Comparable JavaDoc columnKey = getColumnKey(column);
391         return getPercentComplete(rowKey, columnKey);
392     }
393
394     /**
395      * Returns the percent complete for a given item.
396      *
397      * @param rowKey the row key.
398      * @param columnKey the column key.
399      *
400      * @return The percent complete.
401      */

402     public Number JavaDoc getPercentComplete(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
403         Number JavaDoc result = null;
404         int row = getRowIndex(rowKey);
405         TaskSeries series = (TaskSeries) this.data.get(row);
406         Task task = series.get(columnKey.toString());
407         if (task != null) {
408             result = task.getPercentComplete();
409         }
410         return result;
411     }
412
413     /**
414      * Returns the number of sub-intervals for a given item.
415      *
416      * @param row the row index (zero-based).
417      * @param column the column index (zero-based).
418      *
419      * @return The sub-interval count.
420      */

421     public int getSubIntervalCount(int row, int column) {
422         Comparable JavaDoc rowKey = getRowKey(row);
423         Comparable JavaDoc columnKey = getColumnKey(column);
424         return getSubIntervalCount(rowKey, columnKey);
425     }
426
427     /**
428      * Returns the number of sub-intervals for a given item.
429      *
430      * @param rowKey the row key.
431      * @param columnKey the column key.
432      *
433      * @return The sub-interval count.
434      */

435     public int getSubIntervalCount(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
436         int result = 0;
437         int row = getRowIndex(rowKey);
438         TaskSeries series = (TaskSeries) this.data.get(row);
439         Task task = series.get(columnKey.toString());
440         if (task != null) {
441             result = task.getSubtaskCount();
442         }
443         return result;
444     }
445
446     /**
447      * Returns the start value of a sub-interval for a given item.
448      *
449      * @param row the row index (zero-based).
450      * @param column the column index (zero-based).
451      * @param subinterval the sub-interval index (zero-based).
452      *
453      * @return The start value (possibly <code>null</code>).
454      */

455     public Number JavaDoc getStartValue(int row, int column, int subinterval) {
456         Comparable JavaDoc rowKey = getRowKey(row);
457         Comparable JavaDoc columnKey = getColumnKey(column);
458         return getStartValue(rowKey, columnKey, subinterval);
459     }
460
461     /**
462      * Returns the start value of a sub-interval for a given item.
463      *
464      * @param rowKey the row key.
465      * @param columnKey the column key.
466      * @param subinterval the subinterval.
467      *
468      * @return The start value (possibly <code>null</code>).
469      */

470     public Number JavaDoc getStartValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey,
471                                 int subinterval) {
472         Number JavaDoc result = null;
473         int row = getRowIndex(rowKey);
474         TaskSeries series = (TaskSeries) this.data.get(row);
475         Task task = series.get(columnKey.toString());
476         if (task != null) {
477             Task sub = task.getSubtask(subinterval);
478             if (sub != null) {
479                 TimePeriod duration = sub.getDuration();
480                 result = new Long JavaDoc(duration.getStart().getTime());
481             }
482         }
483         return result;
484     }
485
486     /**
487      * Returns the end value of a sub-interval for a given item.
488      *
489      * @param row the row index (zero-based).
490      * @param column the column index (zero-based).
491      * @param subinterval the subinterval.
492      *
493      * @return The end value (possibly <code>null</code>).
494      */

495     public Number JavaDoc getEndValue(int row, int column, int subinterval) {
496         Comparable JavaDoc rowKey = getRowKey(row);
497         Comparable JavaDoc columnKey = getColumnKey(column);
498         return getEndValue(rowKey, columnKey, subinterval);
499     }
500
501     /**
502      * Returns the end value of a sub-interval for a given item.
503      *
504      * @param rowKey the row key.
505      * @param columnKey the column key.
506      * @param subinterval the subinterval.
507      *
508      * @return The end value (possibly <code>null</code>).
509      */

510     public Number JavaDoc getEndValue(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey,
511                               int subinterval) {
512         Number JavaDoc result = null;
513         int row = getRowIndex(rowKey);
514         TaskSeries series = (TaskSeries) this.data.get(row);
515         Task task = series.get(columnKey.toString());
516         if (task != null) {
517             Task sub = task.getSubtask(subinterval);
518             if (sub != null) {
519                 TimePeriod duration = sub.getDuration();
520                 result = new Long JavaDoc(duration.getEnd().getTime());
521             }
522         }
523         return result;
524     }
525
526     /**
527      * Returns the percentage complete value of a sub-interval for a given item.
528      *
529      * @param row the row index (zero-based).
530      * @param column the column index (zero-based).
531      * @param subinterval the sub-interval.
532      *
533      * @return The percent complete value (possibly <code>null</code>).
534      */

535     public Number JavaDoc getPercentComplete(int row, int column, int subinterval) {
536         Comparable JavaDoc rowKey = getRowKey(row);
537         Comparable JavaDoc columnKey = getColumnKey(column);
538         return getPercentComplete(rowKey, columnKey, subinterval);
539     }
540
541     /**
542      * Returns the percentage complete value of a sub-interval for a given item.
543      *
544      * @param rowKey the row key.
545      * @param columnKey the column key.
546      * @param subinterval the sub-interval.
547      *
548      * @return The precent complete value (possibly <code>null</code>).
549      */

550     public Number JavaDoc getPercentComplete(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey,
551                                      int subinterval) {
552         Number JavaDoc result = null;
553         int row = getRowIndex(rowKey);
554         TaskSeries series = (TaskSeries) this.data.get(row);
555         Task task = series.get(columnKey.toString());
556         if (task != null) {
557             Task sub = task.getSubtask(subinterval);
558             if (sub != null) {
559                 result = sub.getPercentComplete();
560             }
561         }
562         return result;
563     }
564
565     /**
566      * Called when a series belonging to the dataset changes.
567      *
568      * @param event information about the change.
569      */

570     public void seriesChanged(SeriesChangeEvent event) {
571         refreshKeys();
572         fireDatasetChanged();
573     }
574
575     /**
576      * Refreshes the keys.
577      */

578     private void refreshKeys() {
579
580         this.keys.clear();
581         for (int i = 0; i < getSeriesCount(); i++) {
582             TaskSeries series = (TaskSeries) this.data.get(i);
583             // look for any keys that we don't already know about...
584
Iterator JavaDoc iterator = series.getTasks().iterator();
585             while (iterator.hasNext()) {
586                 Task task = (Task) iterator.next();
587                 String JavaDoc key = task.getDescription();
588                 int index = this.keys.indexOf(key);
589                 if (index < 0) {
590                     this.keys.add(key);
591                 }
592             }
593         }
594
595     }
596     
597     /**
598      * Tests this instance for equality with an arbitrary object.
599      *
600      * @param obj the object (<code>null</code> permitted).
601      *
602      * @return A boolean.
603      */

604     public boolean equals(Object JavaDoc obj) {
605         if (obj == this) {
606             return true;
607         }
608         if (!(obj instanceof TaskSeriesCollection)) {
609             return false;
610         }
611         TaskSeriesCollection that = (TaskSeriesCollection) obj;
612         if (!ObjectUtilities.equal(this.data, that.data)) {
613             return false;
614         }
615         return true;
616     }
617
618 }
619
Popular Tags