    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, 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     * HighLowItemLabelGenerator.java
    029     * ------------------------------
    030     * (C) Copyright 2001-2007, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   David Basten;
    034     *
    035     * Changes
    036     * -------
    037     * 13-Dec-2001 : Version 1 (DG);
    038     * 16-Jan-2002 : Completed Javadocs (DG);
    039     * 23-Apr-2002 : Added date to the tooltip string (DG);
    040     * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    041     * 21-Mar-2003 : Implemented Serializable (DG);
    042     * 13-Aug-2003 : Implemented Cloneable (DG);
    043     * 17-Nov-2003 : Implemented PublicCloneable (DG);
    044     * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
    045     * 25-May-2004 : Added number formatter (see patch 890496) (DG);
    046     * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
    047     *               getYValue() (DG);
    048     * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
    049     *
    050     */
    051
    052    package org.jfree.chart.labels;
    053
    054    import java.io.Serializable;
    055    import java.text.DateFormat;
    056    import java.text.NumberFormat;
    057    import java.util.Date;
    058
    059    import org.jfree.data.xy.OHLCDataset;
    060    import org.jfree.data.xy.XYDataset;
    061    import org.jfree.util.PublicCloneable;
    062
    063    /**
    064     * A standard item label generator for plots that use data from a
    065     * {@link OHLCDataset}.
    066     */
    067    public class HighLowItemLabelGenerator implements XYItemLabelGenerator,
    068                                                      XYToolTipGenerator,
    069                                                      Cloneable,
    070                                                      PublicCloneable,
    071                                                      Serializable {
    072
    073        /** For serialization. */
    074        private static final long serialVersionUID = 5617111754832211830L;
    075
    076        /** The date formatter. */
    077        private DateFormat dateFormatter;
    078
    079        /** The number formatter. */
    080        private NumberFormat numberFormatter;
    081
    082        /**
    083         * Creates an item label generator using the default date and number
    084         * formats.
    085         */
    086        public HighLowItemLabelGenerator() {
    087            this(DateFormat.getInstance(), NumberFormat.getInstance());
    088        }
    089
    090        /**
    091         * Creates a tool tip generator using the supplied date formatter.
    092         *
    093         * @param dateFormatter  the date formatter (<code>null</code> not
    094         *                       permitted).
    095         * @param numberFormatter  the number formatter (<code>null</code> not
    096         *                         permitted).
    097         */
    098        public HighLowItemLabelGenerator(DateFormat dateFormatter,
    099                                         NumberFormat numberFormatter) {
    100            if (dateFormatter == null) {
    101                throw new IllegalArgumentException(
    102                        "Null 'dateFormatter' argument.");
    103            }
    104            if (numberFormatter == null) {
    105                throw new IllegalArgumentException(
    106                        "Null 'numberFormatter' argument.");
    107            }
    108            this.dateFormatter = dateFormatter;
    109            this.numberFormatter = numberFormatter;
    110        }
    111
    112        /**
    113         * Generates a tooltip text item for a particular item within a series.
    114         *
    115         * @param dataset  the dataset.
    116         * @param series  the series (zero-based index).
    117         * @param item  the item (zero-based index).
    118         *
    119         * @return The tooltip text.
    120         */
    121        public String generateToolTip(XYDataset dataset, int series, int item) {
    122
    123            String result = null;
    124
    125            if (dataset instanceof OHLCDataset) {
    126                OHLCDataset d = (OHLCDataset) dataset;
    127                Number high = d.getHigh(series, item);
    128                Number low = d.getLow(series, item);
    129                Number open = d.getOpen(series, item);
    130                Number close = d.getClose(series, item);
    131                Number x = d.getX(series, item);
    132
    133                result = d.getSeriesKey(series).toString();
    134
    135                if (x != null) {
    136                    Date date = new Date(x.longValue());
    137                    result = result + "--> Date=" + this.dateFormatter.format(date);
    138                    if (high != null) {
    139                        result = result + " High="
    140                                 + this.numberFormatter.format(high.doubleValue());
    141                    }
    142                    if (low != null) {
    143                        result = result + " Low="
    144                                 + this.numberFormatter.format(low.doubleValue());
    145                    }
    146                    if (open != null) {
    147                        result = result + " Open="
    148                                 + this.numberFormatter.format(open.doubleValue());
    149                    }
    150                    if (close != null) {
    151                        result = result + " Close="
    152                                 + this.numberFormatter.format(close.doubleValue());
    153                    }
    154                }
    155
    156            }
    157
    158            return result;
    159
    160        }
    161
    162        /**
    163         * Generates a label for the specified item. The label is typically a
    164         * formatted version of the data value, but any text can be used.
    165         *
    166         * @param dataset  the dataset (<code>null</code> not permitted).
    167         * @param series  the series index (zero-based).
    168         * @param category  the category index (zero-based).
    169         *
    170         * @return The label (possibly <code>null</code>).
    171         */
    172        public String generateLabel(XYDataset dataset, int series, int category) {
    173            return null;  //TODO: implement this method properly
    174        }
    175
    176        /**
    177         * Returns an independent copy of the generator.
    178         *
    179         * @return A clone.
    180         *
    181         * @throws CloneNotSupportedException if cloning is not supported.
    182         */
    183        public Object clone() throws CloneNotSupportedException {
    184
    185            HighLowItemLabelGenerator clone
    186                = (HighLowItemLabelGenerator) super.clone();
    187
    188            if (this.dateFormatter != null) {
    189                clone.dateFormatter = (DateFormat) this.dateFormatter.clone();
    190            }
    191            if (this.numberFormatter != null) {
    192                clone.numberFormatter = (NumberFormat) this.numberFormatter.clone();
    193            }
    194
    195            return clone;
    196
    197        }
    198
    199        /**
    200         * Tests if this object is equal to another.
    201         *
    202         * @param obj  the other object.
    203         *
    204         * @return A boolean.
    205         */
    206        public boolean equals(Object obj) {
    207            if (obj == this) {
    208                return true;
    209            }
    210            if (!(obj instanceof HighLowItemLabelGenerator)) {
    211                return false;
    212            }
    213            HighLowItemLabelGenerator generator = (HighLowItemLabelGenerator) obj;
    214            if (!this.dateFormatter.equals(generator.dateFormatter)) {
    215                return false;
    216            }
    217            if (!this.numberFormatter.equals(generator.numberFormatter)) {
    218                return false;
    219            }
    220            return true;
    221        }
    222
    223    }