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