    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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
    041    package org.jfree.chart.labels;
    042
    043    import java.io.Serializable;
    044    import java.text.MessageFormat;
    045    import java.text.NumberFormat;
    046    import org.jfree.chart.plot.Crosshair;
    047
    048    /**
    049     * A default label generator.
    050     *
    051     * @since 1.0.13
    052     */
    053    public 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        public String generateLabel(Crosshair crosshair) {
    118            Object[] v = new Object[] {this.numberFormat.format(
    119                    crosshair.getValue())};
    120            String result = MessageFormat.format(this.labelTemplate, v);
    121            return result;
    122        }
    123
    124        /**
    125         * Tests this generator for equality with an arbitrary object.
    126         *
    127         * @param obj  the object (<code>null</code> permitted).
    128         *
    129         * @return A boolean.
    130         */
    131        public boolean equals(Object obj) {
    132            if (obj == this) {
    133                return true;
    134            }
    135            if (!(obj instanceof StandardCrosshairLabelGenerator)) {
    136                return false;
    137            }
    138            StandardCrosshairLabelGenerator that
    139                    = (StandardCrosshairLabelGenerator) obj;
    140            if (!this.labelTemplate.equals(that.labelTemplate)) {
    141                return false;
    142            }
    143            if (!this.numberFormat.equals(that.numberFormat)) {
    144                return false;
    145            }
    146            return true;
    147        }
    148
    149        /**
    150         * Returns a hash code for this instance.
    151         *
    152         * @return A hash code for this instance.
    153         */
    154        public int hashCode() {
    155            return this.labelTemplate.hashCode();
    156        }
    157
    158    }