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