001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------------------------------
028 * StandardCrosshairLabelGenerator.java
029 * ------------------------------------
030 * (C) Copyright 2009, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 13-Feb-2009 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.chart.labels;
042
043import java.io.Serializable;
044import java.text.MessageFormat;
045import java.text.NumberFormat;
046import org.jfree.chart.plot.Crosshair;
047
048/**
049 * A default label generator.
050 *
051 * @since 1.0.13
052 */
053public class StandardCrosshairLabelGenerator implements CrosshairLabelGenerator,
054        Serializable {
055
056    /** The label format string. */
057    private String labelTemplate;
058
059    /** A number formatter for the value. */
060    private NumberFormat numberFormat;
061
062    /**
063     * Creates a new instance with default attributes.
064     */
065    public StandardCrosshairLabelGenerator() {
066        this("{0}", NumberFormat.getNumberInstance());
067    }
068
069    /**
070     * Creates a new instance with the specified attributes.
071     *
072     * @param labelTemplate  the label template (<code>null</code> not
073     *     permitted).
074     * @param numberFormat  the number formatter (<code>null</code> not
075     *     permitted).
076     */
077    public StandardCrosshairLabelGenerator(String labelTemplate,
078            NumberFormat numberFormat) {
079        super();
080        if (labelTemplate == null) {
081            throw new IllegalArgumentException(
082                    "Null 'labelTemplate' argument.");
083        }
084        if (numberFormat == null) {
085            throw new IllegalArgumentException(
086                    "Null 'numberFormat' argument.");
087        }
088        this.labelTemplate = labelTemplate;
089        this.numberFormat = numberFormat;
090    }
091
092    /**
093     * Returns the label template string.
094     *
095     * @return The label template string (never <code>null</code>).
096     */
097    public String getLabelTemplate() {
098        return this.labelTemplate;
099    }
100
101    /**
102     * Returns the number formatter.
103     *
104     * @return The formatter (never <code>null</code>).
105     */
106    public NumberFormat getNumberFormat() {
107        return this.numberFormat;
108    }
109
110    /**
111     * Returns a string that can be used as the label for a crosshair.
112     *
113     * @param crosshair  the crosshair (<code>null</code> not permitted).
114     *
115     * @return The label (possibly <code>null</code>).
116     */
117    @Override
118    public String generateLabel(Crosshair crosshair) {
119        Object[] v = new Object[] {this.numberFormat.format(
120                crosshair.getValue())};
121        String result = MessageFormat.format(this.labelTemplate, v);
122        return result;
123    }
124
125    /**
126     * Tests this generator for equality with an arbitrary object.
127     * 
128     * @param obj  the object (<code>null</code> permitted).
129     * 
130     * @return A boolean.
131     */
132    @Override
133    public boolean equals(Object obj) {
134        if (obj == this) {
135            return true;
136        }
137        if (!(obj instanceof StandardCrosshairLabelGenerator)) {
138            return false;
139        }
140        StandardCrosshairLabelGenerator that
141                = (StandardCrosshairLabelGenerator) obj;
142        if (!this.labelTemplate.equals(that.labelTemplate)) {
143            return false;
144        }
145        if (!this.numberFormat.equals(that.numberFormat)) {
146            return false;
147        }
148        return true;
149    }
150
151    /**
152     * Returns a hash code for this instance.
153     *
154     * @return A hash code for this instance.
155     */
156    @Override
157    public int hashCode() {
158        return this.labelTemplate.hashCode();
159    }
160
161}