Source for org.jfree.chart.urls.CustomPieURLGenerator

   1: /* ======================================
   2:  * JFreeChart : a free Java chart library
   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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * --------------------------
  28:  * CustomPieURLGenerator.java
  29:  * --------------------------
  30:  * (C) Copyright 2004-2005, by David Basten and Contributors.
  31:  *
  32:  * Original Author:  David Basten;
  33:  * Contributors:     -;
  34:  *
  35:  * $Id: CustomPieURLGenerator.java,v 1.3.2.1 2005/10/25 20:59:31 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 04-Feb-2004 : Version 1, contributed by David Basten based on 
  40:  *               CustomXYURLGenerator by Richard Atkinson (added to main source
  41:  *               tree on 25-May-2004);
  42:  *
  43:  */
  44: package org.jfree.chart.urls;
  45: 
  46: import java.io.Serializable;
  47: import java.util.ArrayList;
  48: import java.util.HashMap;
  49: import java.util.Iterator;
  50: import java.util.Map;
  51: import java.util.Set;
  52: 
  53: import org.jfree.data.general.PieDataset;
  54: import org.jfree.util.PublicCloneable;
  55: 
  56: /**
  57:  * A custom URL generator for pie charts.
  58:  */
  59: public class CustomPieURLGenerator implements PieURLGenerator, 
  60:                                               Cloneable, 
  61:                                               PublicCloneable, 
  62:                                               Serializable {
  63: 
  64:     /** For serialization. */
  65:     private static final long serialVersionUID = 7100607670144900503L;
  66: 
  67:     /** Storage for the URLs. */
  68:     private ArrayList urls;
  69: 
  70:     /**
  71:      * Default constructor.
  72:      */
  73:     public CustomPieURLGenerator() {
  74:         this.urls = new ArrayList();
  75:     }
  76: 
  77:     /**
  78:      * Generates a URL.
  79:      *
  80:      * @param dataset  the dataset.
  81:      * @param key  the item key.
  82:      * @param pieIndex  the pie index (ignored).
  83:      *
  84:      * @return A string containing the generated URL.
  85:      */
  86:     public String generateURL(PieDataset dataset, Comparable key, 
  87:                               int pieIndex) {
  88:         return getURL(key, pieIndex);
  89:     }
  90: 
  91:     /**
  92:      * Returns the number of URL lists stored by the renderer.
  93:      * 
  94:      * @return The list count.
  95:      */
  96:     public int getListCount() {
  97:         return this.urls.size();
  98:     }
  99:     
 100:     /**
 101:      * Returns the number of URLs in a given list.
 102:      * 
 103:      * @param list  the list index (zero based).
 104:      * 
 105:      * @return The URL count.
 106:      */
 107:     public int getURLCount(int list) {
 108:         
 109:         int result = 0;
 110:         Map urlMap = (Map) this.urls.get(list);
 111:         if (urlMap != null) {
 112:             result = urlMap.size();
 113:         }
 114:         return result;
 115:     }
 116: 
 117:     /**
 118:      * Returns the URL for an item.
 119:      * 
 120:      * @param key  the key.
 121:      * @param pieItem  the item index.
 122:      * 
 123:      * @return The URL.
 124:      */    
 125:     public String getURL(Comparable key, int pieItem) {
 126: 
 127:         String result = null;
 128:         
 129:         if (pieItem < getListCount()) {
 130:             Map urlMap = (Map) this.urls.get(pieItem);
 131:             if (urlMap != null) {
 132:                 result = (String) urlMap.get(key);
 133:             }
 134:         }
 135:         
 136:         return result;
 137:     }
 138: 
 139:     /**
 140:      * Adds a map of URLs.
 141:      *
 142:      * @param urlMap  the URLs.
 143:      */
 144:     public void addURLs(Map urlMap) {
 145:         this.urls.add(urlMap);
 146:     }
 147:     
 148:     /**
 149:      * Tests if this object is equal to another.
 150:      * 
 151:      * @param o  the other object.
 152:      * 
 153:      * @return A boolean.
 154:      */
 155:     public boolean equals(Object o) {
 156:     
 157:         if (o == this) {
 158:             return true;
 159:         }
 160:         
 161:         if (o instanceof CustomPieURLGenerator) {
 162:             CustomPieURLGenerator generator = (CustomPieURLGenerator) o;
 163:             if (getListCount() != generator.getListCount()) {
 164:                 return false;
 165:             }
 166:             Set keySet;
 167:             for (int pieItem = 0; pieItem < getListCount(); pieItem++) {
 168:                 if (getURLCount(pieItem) != generator.getURLCount(pieItem)) {
 169:                     return false;
 170:                 }
 171:                 keySet = ((HashMap) this.urls.get(pieItem)).keySet();
 172:                 String key;
 173:                 for (Iterator i = keySet.iterator(); i.hasNext();) {
 174:                 key = (String) i.next();
 175:                     if (!getURL(key, pieItem).equals(
 176:                             generator.getURL(key, pieItem))) {
 177:                         return false;
 178:                     }
 179:                 }
 180:             }
 181:             return true;
 182:         }
 183:         return false;
 184:     }
 185: 
 186:     /**
 187:      * Returns a clone of the generator.
 188:      * 
 189:      * @return A clone.
 190:      * 
 191:      * @throws CloneNotSupportedException if cloning is not supported.
 192:      */
 193:     public Object clone() throws CloneNotSupportedException {
 194:         CustomPieURLGenerator urlGen = new CustomPieURLGenerator();
 195:         Map map;
 196:         Map newMap;
 197:         String key;
 198: 
 199:         for (Iterator i = this.urls.iterator(); i.hasNext();) {
 200:             map = (Map) i.next();
 201: 
 202:             newMap = new HashMap();
 203:             for (Iterator j = map.keySet().iterator(); j.hasNext();) {
 204:                 key = (String) j.next();
 205:                 newMap.put(key, map.get(key));
 206:             }
 207: 
 208:             urlGen.addURLs(newMap);
 209:             newMap = null;
 210:         }
 211: 
 212:         return urlGen;
 213:     }
 214: 
 215: }