001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, 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 * MultipleXYSeriesLabelGenerator.java 029 * ----------------------------------- 030 * (C) Copyright 2004, 2005, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: MultipleXYSeriesLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 19-Nov-2004 : Version 1 (DG); 040 * 18-Apr-2005 : Use StringBuffer (DG); 041 * 042 */ 043 044 package org.jfree.chart.labels; 045 046 import java.io.Serializable; 047 import java.text.MessageFormat; 048 import java.util.HashMap; 049 import java.util.List; 050 import java.util.Map; 051 052 import org.jfree.data.xy.XYDataset; 053 import org.jfree.util.PublicCloneable; 054 055 /** 056 * A series label generator for plots that use data from 057 * an {@link org.jfree.data.xy.XYDataset}. 058 */ 059 public class MultipleXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 060 Cloneable, 061 PublicCloneable, 062 Serializable { 063 064 /** For serialization. */ 065 private static final long serialVersionUID = 138976236941898560L; 066 067 /** The default item label format. */ 068 public static final String DEFAULT_LABEL_FORMAT = "{0}"; 069 070 /** The format pattern for the initial part of the label. */ 071 private String formatPattern; 072 073 /** The format pattern for additional labels. */ 074 private String additionalFormatPattern; 075 076 /** Storage for the additional series labels. */ 077 private Map seriesLabelLists; 078 079 /** 080 * Creates an item label generator using default number formatters. 081 */ 082 public MultipleXYSeriesLabelGenerator() { 083 this(DEFAULT_LABEL_FORMAT); 084 } 085 086 /** 087 * Creates a new series label generator. 088 * 089 * @param format the format pattern (<code>null</code> not permitted). 090 */ 091 public MultipleXYSeriesLabelGenerator(String format) { 092 if (format == null) { 093 throw new IllegalArgumentException("Null 'format' argument."); 094 } 095 this.formatPattern = format; 096 this.additionalFormatPattern = "\n{0}"; 097 this.seriesLabelLists = new HashMap(); 098 } 099 100 /** 101 * Adds an extra label for the specified series. 102 * 103 * @param series the series index. 104 * @param label the label. 105 */ 106 public void addSeriesLabel(int series, String label) { 107 Integer key = new Integer(series); 108 List labelList = (List) this.seriesLabelLists.get(key); 109 if (labelList == null) { 110 labelList = new java.util.ArrayList(); 111 this.seriesLabelLists.put(key, labelList); 112 } 113 labelList.add(label); 114 } 115 116 /** 117 * Clears the extra labels for the specified series. 118 * 119 * @param series the series index. 120 */ 121 public void clearSeriesLabels(int series) { 122 Integer key = new Integer(series); 123 this.seriesLabelLists.put(key, null); 124 } 125 126 /** 127 * Generates a label for the specified series. This label will be 128 * used for the chart legend. 129 * 130 * @param dataset the dataset (<code>null</code> not permitted). 131 * @param series the series. 132 * 133 * @return A series label. 134 */ 135 public String generateLabel(XYDataset dataset, int series) { 136 if (dataset == null) { 137 throw new IllegalArgumentException("Null 'dataset' argument."); 138 } 139 StringBuffer label = new StringBuffer(); 140 label.append( 141 MessageFormat.format( 142 this.formatPattern, createItemArray(dataset, series) 143 ) 144 ); 145 Integer key = new Integer(series); 146 List extraLabels = (List) this.seriesLabelLists.get(key); 147 if (extraLabels != null) { 148 Object[] temp = new Object[1]; 149 for (int i = 0; i < extraLabels.size(); i++) { 150 temp[0] = extraLabels.get(i); 151 String labelAddition = MessageFormat.format( 152 this.additionalFormatPattern, temp 153 ); 154 label.append(labelAddition); 155 } 156 } 157 return label.toString(); 158 } 159 160 /** 161 * Creates the array of items that can be passed to the 162 * {@link MessageFormat} class for creating labels. 163 * 164 * @param dataset the dataset (<code>null</code> not permitted). 165 * @param series the series (zero-based index). 166 * 167 * @return The items (never <code>null</code>). 168 */ 169 protected Object[] createItemArray(XYDataset dataset, int series) { 170 Object[] result = new Object[1]; 171 result[0] = dataset.getSeriesKey(series).toString(); 172 return result; 173 } 174 175 /** 176 * Returns an independent copy of the generator. 177 * 178 * @return A clone. 179 * 180 * @throws CloneNotSupportedException if cloning is not supported. 181 */ 182 public Object clone() throws CloneNotSupportedException { 183 return super.clone(); 184 } 185 186 /** 187 * Tests this object for equality with an arbitrary object. 188 * 189 * @param obj the other object (<code>null</code> permitted). 190 * 191 * @return A boolean. 192 */ 193 public boolean equals(Object obj) { 194 if (obj == this) { 195 return true; 196 } 197 if (!(obj instanceof StandardXYSeriesLabelGenerator)) { 198 return false; 199 } 200 if (!super.equals(obj)) { 201 return false; 202 } 203 return true; 204 } 205 206 }