KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > MultipleXYSeriesLabelGenerator


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
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  * MultipleXYSeriesLabelGenerator.java
29  * -----------------------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: MultipleXYSeriesLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 19-Nov-2004 : Version 1 (DG);
40  * 18-Apr-2005 : Use StringBuffer (DG);
41  *
42  */

43
44 package org.jfree.chart.labels;
45
46 import java.io.Serializable JavaDoc;
47 import java.text.MessageFormat JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51
52 import org.jfree.data.xy.XYDataset;
53 import org.jfree.util.PublicCloneable;
54
55 /**
56  * A series label generator for plots that use data from
57  * an {@link org.jfree.data.xy.XYDataset}.
58  */

59 public class MultipleXYSeriesLabelGenerator implements XYSeriesLabelGenerator,
60                                                        Cloneable JavaDoc,
61                                                        PublicCloneable,
62                                                        Serializable JavaDoc {
63
64     /** For serialization. */
65     private static final long serialVersionUID = 138976236941898560L;
66     
67     /** The default item label format. */
68     public static final String JavaDoc DEFAULT_LABEL_FORMAT = "{0}";
69     
70     /** The format pattern for the initial part of the label. */
71     private String JavaDoc formatPattern;
72     
73     /** The format pattern for additional labels. */
74     private String JavaDoc additionalFormatPattern;
75     
76     /** Storage for the additional series labels. */
77     private Map JavaDoc seriesLabelLists;
78
79     /**
80      * Creates an item label generator using default number formatters.
81      */

82     public MultipleXYSeriesLabelGenerator() {
83         this(DEFAULT_LABEL_FORMAT);
84     }
85     
86     /**
87      * Creates a new series label generator.
88      *
89      * @param format the format pattern (<code>null</code> not permitted).
90      */

91     public MultipleXYSeriesLabelGenerator(String JavaDoc format) {
92         if (format == null) {
93             throw new IllegalArgumentException JavaDoc("Null 'format' argument.");
94         }
95         this.formatPattern = format;
96         this.additionalFormatPattern = "\n{0}";
97         this.seriesLabelLists = new HashMap JavaDoc();
98     }
99     
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 JavaDoc label) {
107         Integer JavaDoc key = new Integer JavaDoc(series);
108         List JavaDoc labelList = (List JavaDoc) this.seriesLabelLists.get(key);
109         if (labelList == null) {
110             labelList = new java.util.ArrayList JavaDoc();
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 JavaDoc key = new Integer JavaDoc(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 JavaDoc generateLabel(XYDataset dataset, int series) {
136         if (dataset == null) {
137             throw new IllegalArgumentException JavaDoc("Null 'dataset' argument.");
138         }
139         StringBuffer JavaDoc label = new StringBuffer JavaDoc();
140         label.append(
141             MessageFormat.format(
142                 this.formatPattern, createItemArray(dataset, series)
143             )
144         );
145         Integer JavaDoc key = new Integer JavaDoc(series);
146         List JavaDoc extraLabels = (List JavaDoc) this.seriesLabelLists.get(key);
147         if (extraLabels != null) {
148             Object JavaDoc[] temp = new Object JavaDoc[1];
149             for (int i = 0; i < extraLabels.size(); i++) {
150                 temp[0] = extraLabels.get(i);
151                 String JavaDoc 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 JavaDoc[] createItemArray(XYDataset dataset, int series) {
170         Object JavaDoc[] result = new Object JavaDoc[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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
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 JavaDoc 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 }
207
Popular Tags