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 * HMSNumberFormat.java
029 * --------------------
030 * (C) Copyright 2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 28-Sep-2013 : Version 1 (RW);
038 *
039 */
040
041package org.jfree.chart.util;
042
043import java.text.DecimalFormat;
044import java.text.FieldPosition;
045import java.text.NumberFormat;
046import java.text.ParsePosition;
047
048/**
049 * A custom number formatter that formats numbers (in seconds) as HH:MM:SS.
050 * Created in response to:
051 * 
052 * http://stackoverflow.com/questions/19028908/jfreechart-need-to-customize-y-axis-just-for-printing
053 * 
054 * @since 1.0.17
055 */
056public class HMSNumberFormat extends NumberFormat {
057
058    private NumberFormat formatter = new DecimalFormat("00");
059    
060    /**
061     * Creates a new instance.
062     */
063    public HMSNumberFormat() {
064        // nothing to do
065    }
066
067    /**
068     * Formats the specified number as a string of the form HH:MM:SS.  The 
069     * decimal fraction is ignored.
070     *
071     * @param number  the number to format.
072     * @param toAppendTo  the buffer to append to (ignored here).
073     * @param pos  the field position (ignored here).
074     *
075     * @return The string buffer.
076     */
077    @Override
078    public StringBuffer format(double number, StringBuffer toAppendTo,
079            FieldPosition pos) {
080        return format((long) number, toAppendTo, pos);
081    }
082
083    /**
084     * Formats the specified number as a string of the form HH:MM:SS.
085     *
086     * @param number  the number to format.
087     * @param toAppendTo  the buffer to append to (ignored here).
088     * @param pos  the field position (ignored here).
089     *
090     * @return The string buffer.
091     */
092    @Override
093    public StringBuffer format(long number, StringBuffer toAppendTo,
094            FieldPosition pos) {
095        StringBuffer sb = new StringBuffer();
096        long hours = number / 3600;
097        sb.append(this.formatter.format(hours)).append(":");
098        long remaining = number - (hours * 3600);
099        long minutes = remaining / 60;
100        sb.append(this.formatter.format(minutes)).append(":");
101        long seconds = remaining - (minutes * 60);
102        sb.append(this.formatter.format(seconds));
103        return sb;
104    }
105
106    /**
107     * Parsing is not implemented, so this method always returns
108     * <code>null</code>.
109     *
110     * @param source  ignored.
111     * @param parsePosition  ignored.
112     *
113     * @return Always <code>null</code>.
114     */
115    @Override
116    public Number parse (String source, ParsePosition parsePosition) {
117        return null; // don't bother with parsing
118    }
119
120}