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 * StandardXYSeriesLabelGenerator.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: StandardXYSeriesLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 16-Nov-2004 : Version 1 (DG); 040 * 041 */ 042 043 package org.jfree.chart.labels; 044 045 import java.io.Serializable; 046 import java.text.MessageFormat; 047 048 import org.jfree.data.xy.XYDataset; 049 import org.jfree.util.PublicCloneable; 050 051 /** 052 * A standard series label generator for plots that use data from 053 * an {@link org.jfree.data.xy.XYDataset}. 054 */ 055 public class StandardXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 056 Cloneable, 057 PublicCloneable, 058 Serializable { 059 060 /** For serialization. */ 061 private static final long serialVersionUID = 1916017081848400024L; 062 063 /** The default item label format. */ 064 public static final String DEFAULT_LABEL_FORMAT = "{0}"; 065 066 /** The format pattern. */ 067 private String formatPattern; 068 069 /** 070 * Creates a default series label generator (uses 071 * {@link #DEFAULT_LABEL_FORMAT}). 072 */ 073 public StandardXYSeriesLabelGenerator() { 074 this(DEFAULT_LABEL_FORMAT); 075 } 076 077 /** 078 * Creates a new series label generator. 079 * 080 * @param format the format pattern (<code>null</code> not permitted). 081 */ 082 public StandardXYSeriesLabelGenerator(String format) { 083 if (format == null) { 084 throw new IllegalArgumentException("Null 'format' argument."); 085 } 086 this.formatPattern = format; 087 } 088 089 /** 090 * Generates a label for the specified series. This label will be 091 * used for the chart legend. 092 * 093 * @param dataset the dataset (<code>null</code> not permitted). 094 * @param series the series. 095 * 096 * @return A series label. 097 */ 098 public String generateLabel(XYDataset dataset, int series) { 099 if (dataset == null) { 100 throw new IllegalArgumentException("Null 'dataset' argument."); 101 } 102 String label = MessageFormat.format( 103 this.formatPattern, createItemArray(dataset, series) 104 ); 105 return label; 106 } 107 108 /** 109 * Creates the array of items that can be passed to the 110 * {@link MessageFormat} class for creating labels. 111 * 112 * @param dataset the dataset (<code>null</code> not permitted). 113 * @param series the series (zero-based index). 114 * 115 * @return The items (never <code>null</code>). 116 */ 117 protected Object[] createItemArray(XYDataset dataset, int series) { 118 Object[] result = new Object[1]; 119 result[0] = dataset.getSeriesKey(series).toString(); 120 return result; 121 } 122 123 /** 124 * Returns an independent copy of the generator. 125 * 126 * @return A clone. 127 * 128 * @throws CloneNotSupportedException if cloning is not supported. 129 */ 130 public Object clone() throws CloneNotSupportedException { 131 return super.clone(); 132 } 133 134 /** 135 * Tests this object for equality with an arbitrary object. 136 * 137 * @param obj the other object (<code>null</code> permitted). 138 * 139 * @return A boolean. 140 */ 141 public boolean equals(Object obj) { 142 if (obj == this) { 143 return true; 144 } 145 if (!(obj instanceof StandardXYSeriesLabelGenerator)) { 146 return false; 147 } 148 if (!super.equals(obj)) { 149 return false; 150 } 151 return true; 152 } 153 154 }