    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * CompassFormat.java
    029     * ------------------
    030     * (C) Copyright 2003, 2004, by Sylvain Vieujot and Contributors.
    031     *
    032     * Original Author:  Sylvain Vieujot;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * $Id: CompassFormat.java,v 1.3.2.1 2005/10/25 20:37:34 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 18-Feb-2004 : Version 1 contributed by Sylvain Vieujot (DG);
    040     *
    041     */
    042
    043    package org.jfree.chart.axis;
    044
    045    import java.text.FieldPosition;
    046    import java.text.NumberFormat;
    047    import java.text.ParsePosition;
    048
    049    /**
    050     * A formatter that displays numbers as directions.
    051     */
    052    public class CompassFormat extends NumberFormat {
    053
    054        /** North. */
    055        private static final String N = "N";
    056
    057        /** East. */
    058        private static final String E = "E";
    059
    060        /** South. */
    061        private static final String S = "S";
    062
    063        /** West. */
    064        private static final String W = "W";
    065
    066        /** The directions. */
    067        public static final String[] DIRECTIONS = {
    068            N, N + N + E, N + E, E + N + E, E, E + S + E, S + E, S + S + E, S,
    069            S + S + W, S + W, W + S + W, W, W + N + W, N + W, N + N + W, N
    070        };
    071
    072        /**
    073         * Creates a new formatter.
    074         */
    075        public CompassFormat() {
    076            super();
    077        }
    078
    079        /**
    080         * Returns a string representing the direction.
    081         *
    082         * @param direction  the direction.
    083         *
    084         * @return A string.
    085         */
    086        public String getDirectionCode(double direction) {
    087
    088            direction = direction % 360;
    089            if (direction < 0.0) {
    090                direction = direction + 360.0;
    091            }
    092            int index = ((int) Math.floor(direction / 11.25) + 1) / 2;
    093            return DIRECTIONS[index];
    094
    095        }
    096
    097        /* (non-Javadoc)
    098         * @see java.text.NumberFormat#format(double, java.lang.StringBuffer,
    099         *      java.text.FieldPosition)
    100         */
    101        public StringBuffer format(double number, StringBuffer toAppendTo,
    102                                   FieldPosition pos) {
    103            return toAppendTo.append(getDirectionCode(number));
    104        }
    105
    106        /* (non-Javadoc)
    107         * @see java.text.NumberFormat#format(long, java.lang.StringBuffer,
    108         *      java.text.FieldPosition)
    109         */
    110        public StringBuffer format(long number, StringBuffer toAppendTo,
    111                                   FieldPosition pos) {
    112            return toAppendTo.append(getDirectionCode(number));
    113        }
    114
    115        /**
    116         * This method returns <code>null</code> for all inputs.  This class cannot
    117         * be used for parsing.
    118         *
    119         * @param source  the source string.
    120         * @param parsePosition  the parse position.
    121         *
    122         * @return <code>null</code>.
    123         */
    124        public Number parse(String source, ParsePosition parsePosition) {
    125            return null;
    126        }
    127
    128    }