    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025     * Other names may be trademarks of their respective owners.]
    026     *
    027     * ---------------
    028     * TaskSeries.java
    029     * ---------------
    030     * (C) Copyright 2002-2008, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * Changes
    036     * -------
    037     * 06-Jun-2002 : Version 1 (DG);
    038     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    039     * 24-Oct-2002 : Added methods to get TimeAllocation by task index (DG);
    040     * 10-Jan-2003 : Renamed GanttSeries --> TaskSeries (DG);
    041     * 30-Jul-2004 : Added equals() method (DG);
    042     * 09-May-2008 : Fixed cloning bug (DG);
    043     *
    044     */
    045
    046    package org.jfree.data.gantt;
    047
    048    import java.util.Collections;
    049    import java.util.List;
    050
    051    import org.jfree.data.general.Series;
    052    import org.jfree.util.ObjectUtilities;
    053
    054    /**
    055     * A series that contains zero, one or many {@link Task} objects.
    056     * <P>
    057     * This class is used as a building block for the {@link TaskSeriesCollection}
    058     * class that can be used to construct basic Gantt charts.
    059     */
    060    public class TaskSeries extends Series {
    061
    062        /** Storage for the tasks in the series. */
    063        private List tasks;
    064
    065        /**
    066         * Constructs a new series with the specified name.
    067         *
    068         * @param name  the series name (<code>null</code> not permitted).
    069         */
    070        public TaskSeries(String name) {
    071            super(name);
    072            this.tasks = new java.util.ArrayList();
    073        }
    074
    075        /**
    076         * Adds a task to the series and sends a
    077         * {@link org.jfree.data.general.SeriesChangeEvent} to all registered
    078         * listeners.
    079         *
    080         * @param task  the task (<code>null</code> not permitted).
    081         */
    082        public void add(Task task) {
    083            if (task == null) {
    084                throw new IllegalArgumentException("Null 'task' argument.");
    085            }
    086            this.tasks.add(task);
    087            fireSeriesChanged();
    088        }
    089
    090        /**
    091         * Removes a task from the series and sends
    092         * a {@link org.jfree.data.general.SeriesChangeEvent}
    093         * to all registered listeners.
    094         *
    095         * @param task  the task.
    096         */
    097        public void remove(Task task) {
    098            this.tasks.remove(task);
    099            fireSeriesChanged();
    100        }
    101
    102        /**
    103         * Removes all tasks from the series and sends
    104         * a {@link org.jfree.data.general.SeriesChangeEvent}
    105         * to all registered listeners.
    106         */
    107        public void removeAll() {
    108            this.tasks.clear();
    109            fireSeriesChanged();
    110        }
    111
    112        /**
    113         * Returns the number of items in the series.
    114         *
    115         * @return The item count.
    116         */
    117        public int getItemCount() {
    118            return this.tasks.size();
    119        }
    120
    121        /**
    122         * Returns a task from the series.
    123         *
    124         * @param index  the task index (zero-based).
    125         *
    126         * @return The task.
    127         */
    128        public Task get(int index) {
    129            return (Task) this.tasks.get(index);
    130        }
    131
    132        /**
    133         * Returns the task in the series that has the specified description.
    134         *
    135         * @param description  the name (<code>null</code> not permitted).
    136         *
    137         * @return The task (possibly <code>null</code>).
    138         */
    139        public Task get(String description) {
    140            Task result = null;
    141            int count = this.tasks.size();
    142            for (int i = 0; i < count; i++) {
    143                Task t = (Task) this.tasks.get(i);
    144                if (t.getDescription().equals(description)) {
    145                    result = t;
    146                    break;
    147                }
    148            }
    149            return result;
    150        }
    151
    152        /**
    153         * Returns an unmodifialble list of the tasks in the series.
    154         *
    155         * @return The tasks.
    156         */
    157        public List getTasks() {
    158            return Collections.unmodifiableList(this.tasks);
    159        }
    160
    161        /**
    162         * Tests this object for equality with an arbitrary object.
    163         *
    164         * @param obj  the object to test against (<code>null</code> permitted).
    165         *
    166         * @return A boolean.
    167         */
    168        public boolean equals(Object obj) {
    169            if (obj == this) {
    170                return true;
    171            }
    172            if (!(obj instanceof TaskSeries)) {
    173                return false;
    174            }
    175            if (!super.equals(obj)) {
    176                return false;
    177            }
    178            TaskSeries that = (TaskSeries) obj;
    179            if (!this.tasks.equals(that.tasks)) {
    180                return false;
    181            }
    182            return true;
    183        }
    184
    185        /**
    186         * Returns an independent copy of this series.
    187         *
    188         * @return A clone of the series.
    189         *
    190         * @throws CloneNotSupportedException if there is some problem cloning
    191         *     the dataset.
    192         */
    193        public Object clone() throws CloneNotSupportedException {
    194            TaskSeries clone = (TaskSeries) super.clone();
    195            clone.tasks = (List) ObjectUtilities.deepClone(this.tasks);
    196            return clone;
    197        }
    198
    199    }