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