001    /* ======================================
002     * JFreeChart : a free Java chart library
003     * ======================================
004     *
005     * (C) Copyright 2000-2008, 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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025     * in the United States and other countries.]
026     *
027     * -------------------------------
028     * CustomCategoryURLGenerator.java
029     * -------------------------------
030     * (C) Copyright 2008, by Diego Pierangeli and Contributors.
031     *
032     * Original Author:  Diego Pierangeli;
033     * Contributors:     David Gilbert (for Object Refinery Limited);
034     *
035     * Changes:
036     * --------
037     * 23-Apr-2008 : Version 1, contributed by Diego Pierangeli based on
038     *               CustomXYURLGenerator by Richard Atkinson, with some
039     *               modifications by David Gilbert(DG);
040     *
041     */
042    package org.jfree.chart.urls;
043    
044    import java.io.Serializable;
045    import java.util.ArrayList;
046    import java.util.List;
047    
048    import org.jfree.data.category.CategoryDataset;
049    import org.jfree.util.PublicCloneable;
050    
051    /**
052     * A custom URL generator.
053     */
054    public class CustomCategoryURLGenerator implements CategoryURLGenerator,
055            Cloneable, PublicCloneable, Serializable {
056    
057        /** Storage for the URLs. */
058        private ArrayList urlSeries = new ArrayList();
059    
060        /**
061         * Default constructor.
062         */
063        public CustomCategoryURLGenerator() {
064            super();
065        }
066    
067        /**
068         * Returns the number of URL lists stored by the renderer.
069         *
070         * @return The list count.
071         */
072        public int getListCount() {
073            return this.urlSeries.size();
074        }
075    
076        /**
077         * Returns the number of URLs in a given list.
078         *
079         * @param list  the list index (zero based).
080         *
081         * @return The URL count.
082         */
083        public int getURLCount(int list) {
084            int result = 0;
085            List urls = (List) this.urlSeries.get(list);
086            if (urls != null) {
087                result = urls.size();
088            }
089            return result;
090        }
091    
092        /**
093         * Returns the URL for an item.
094         *
095         * @param series  the series index.
096         * @param item  the item index.
097         *
098         * @return The URL (possibly <code>null</code>).
099         */
100        public String getURL(int series, int item) {
101            String result = null;
102            if (series < getListCount()) {
103                List urls = (List) this.urlSeries.get(series);
104                if (urls != null) {
105                    if (item < urls.size()) {
106                        result = (String) urls.get(item);
107                    }
108                }
109            }
110            return result;
111        }
112    
113        /**
114         * Generates a URL.
115         *
116         * @param dataset  the dataset (ignored in this implementation).
117         * @param series  the series (zero-based index).
118         * @param item  the item (zero-based index).
119         *
120         * @return A string containing the URL (possibly <code>null</code>).
121         */
122        public String generateURL(CategoryDataset dataset, int series, int item) {
123            return getURL(series, item);
124        }
125    
126        /**
127         * Adds a list of URLs.
128         *
129         * @param urls  the list of URLs (<code>null</code> permitted).
130         */
131        public void addURLSeries(List urls) {
132            List listToAdd = null;
133            if (urls != null) {
134                listToAdd = new java.util.ArrayList(urls);
135            }
136            this.urlSeries.add(listToAdd);
137        }
138    
139        /**
140         * Tests if this object is equal to another.
141         *
142         * @param obj  the other object.
143         *
144         * @return A boolean.
145         */
146        public boolean equals(Object obj) {
147            if (obj == this) {
148                return true;
149            }
150            if (!(obj instanceof CustomCategoryURLGenerator)) {
151                return false;
152            }
153            CustomCategoryURLGenerator generator = (CustomCategoryURLGenerator) obj;
154            int listCount = getListCount();
155            if (listCount != generator.getListCount()) {
156                return false;
157            }
158    
159            for (int series = 0; series < listCount; series++) {
160                int urlCount = getURLCount(series);
161                if (urlCount != generator.getURLCount(series)) {
162                    return false;
163                }
164    
165                for (int item = 0; item < urlCount; item++) {
166                    String u1 = getURL(series, item);
167                    String u2 = generator.getURL(series, item);
168                    if (u1 != null) {
169                        if (!u1.equals(u2)) {
170                            return false;
171                        }
172                    } else {
173                        if (u2 != null) {
174                            return false;
175                        }
176                    }
177                }
178            }
179            return true;
180        }
181    
182        /**
183         * Returns a new generator that is a copy of, and independent from, this
184         * generator.
185         *
186         * @return A clone.
187         *
188         * @throws CloneNotSupportedException if there is a problem with cloning.
189         */
190        public Object clone() throws CloneNotSupportedException {
191            CustomCategoryURLGenerator clone
192                    = (CustomCategoryURLGenerator) super.clone();
193            clone.urlSeries = new java.util.ArrayList(this.urlSeries);
194            return clone;
195        }
196    
197    }