001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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-2013, 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 * 03-Jul-2013 : Use ParamChecks (DG); 044 * 045 */ 046 047package org.jfree.data.gantt; 048 049import java.util.Collections; 050import java.util.List; 051import org.jfree.chart.util.ParamChecks; 052 053import org.jfree.data.general.Series; 054import org.jfree.util.ObjectUtilities; 055 056/** 057 * A series that contains zero, one or many {@link Task} objects. 058 * <P> 059 * This class is used as a building block for the {@link TaskSeriesCollection} 060 * class that can be used to construct basic Gantt charts. 061 */ 062public class TaskSeries extends Series { 063 064 /** Storage for the tasks in the series. */ 065 private List tasks; 066 067 /** 068 * Constructs a new series with the specified name. 069 * 070 * @param name the series name (<code>null</code> not permitted). 071 */ 072 public TaskSeries(String name) { 073 super(name); 074 this.tasks = new java.util.ArrayList(); 075 } 076 077 /** 078 * Adds a task to the series and sends a 079 * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 080 * listeners. 081 * 082 * @param task the task (<code>null</code> not permitted). 083 */ 084 public void add(Task task) { 085 ParamChecks.nullNotPermitted(task, "task"); 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 @Override 118 public int getItemCount() { 119 return this.tasks.size(); 120 } 121 122 /** 123 * Returns a task from the series. 124 * 125 * @param index the task index (zero-based). 126 * 127 * @return The task. 128 */ 129 public Task get(int index) { 130 return (Task) this.tasks.get(index); 131 } 132 133 /** 134 * Returns the task in the series that has the specified description. 135 * 136 * @param description the name (<code>null</code> not permitted). 137 * 138 * @return The task (possibly <code>null</code>). 139 */ 140 public Task get(String description) { 141 Task result = null; 142 int count = this.tasks.size(); 143 for (int i = 0; i < count; i++) { 144 Task t = (Task) this.tasks.get(i); 145 if (t.getDescription().equals(description)) { 146 result = t; 147 break; 148 } 149 } 150 return result; 151 } 152 153 /** 154 * Returns an unmodifialble list of the tasks in the series. 155 * 156 * @return The tasks. 157 */ 158 public List getTasks() { 159 return Collections.unmodifiableList(this.tasks); 160 } 161 162 /** 163 * Tests this object for equality with an arbitrary object. 164 * 165 * @param obj the object to test against (<code>null</code> permitted). 166 * 167 * @return A boolean. 168 */ 169 @Override 170 public boolean equals(Object obj) { 171 if (obj == this) { 172 return true; 173 } 174 if (!(obj instanceof TaskSeries)) { 175 return false; 176 } 177 if (!super.equals(obj)) { 178 return false; 179 } 180 TaskSeries that = (TaskSeries) obj; 181 if (!this.tasks.equals(that.tasks)) { 182 return false; 183 } 184 return true; 185 } 186 187 /** 188 * Returns an independent copy of this series. 189 * 190 * @return A clone of the series. 191 * 192 * @throws CloneNotSupportedException if there is some problem cloning 193 * the dataset. 194 */ 195 @Override 196 public Object clone() throws CloneNotSupportedException { 197 TaskSeries clone = (TaskSeries) super.clone(); 198 clone.tasks = (List) ObjectUtilities.deepClone(this.tasks); 199 return clone; 200 } 201 202}