    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     * CompassFormat.java
    029     * ------------------
    030     * (C) Copyright 2003-2008, 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        /**
    096         * Formats a number into the specified string buffer.
    097         *
    098         * @param number  the number to format.
    099         * @param toAppendTo  the string buffer.
    100         * @param pos  the field position (ignored here).
    101         *
    102         * @return The string buffer.
    103         */
    104        public StringBuffer format(double number, StringBuffer toAppendTo,
    105                                   FieldPosition pos) {
    106            return toAppendTo.append(getDirectionCode(number));
    107        }
    108
    109        /**
    110         * Formats a number into the specified string buffer.
    111         *
    112         * @param number  the number to format.
    113         * @param toAppendTo  the string buffer.
    114         * @param pos  the field position (ignored here).
    115         *
    116         * @return The string buffer.
    117         */
    118        public StringBuffer format(long number, StringBuffer toAppendTo,
    119                                   FieldPosition pos) {
    120            return toAppendTo.append(getDirectionCode(number));
    121        }
    122
    123        /**
    124         * This method returns <code>null</code> for all inputs.  This class cannot
    125         * be used for parsing.
    126         *
    127         * @param source  the source string.
    128         * @param parsePosition  the parse position.
    129         *
    130         * @return <code>null</code>.
    131         */
    132        public Number parse(String source, ParsePosition parsePosition) {
    133            return null;
    134        }
    135
    136    }