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 = { N, N + N + E, N + E,
068: E + N + E, E, E + S + E, S + E, S + S + E, S, S + S + W,
069: S + W, W + S + W, W, W + N + W, N + W, N + N + W, N };
070:
071: /**
072: * Creates a new formatter.
073: */
074: public CompassFormat() {
075: super ();
076: }
077:
078: /**
079: * Returns a string representing the direction.
080: *
081: * @param direction the direction.
082: *
083: * @return A string.
084: */
085: public String getDirectionCode(double direction) {
086:
087: direction = direction % 360;
088: if (direction < 0.0) {
089: direction = direction + 360.0;
090: }
091: int index = ((int) Math.floor(direction / 11.25) + 1) / 2;
092: return DIRECTIONS[index];
093:
094: }
095:
096: /* (non-Javadoc)
097: * @see java.text.NumberFormat#format(double, java.lang.StringBuffer,
098: * java.text.FieldPosition)
099: */
100: public StringBuffer format(double number, StringBuffer toAppendTo,
101: FieldPosition pos) {
102: return toAppendTo.append(getDirectionCode(number));
103: }
104:
105: /* (non-Javadoc)
106: * @see java.text.NumberFormat#format(long, java.lang.StringBuffer,
107: * java.text.FieldPosition)
108: */
109: public StringBuffer format(long number, StringBuffer toAppendTo,
110: FieldPosition pos) {
111: return toAppendTo.append(getDirectionCode(number));
112: }
113:
114: /**
115: * This method returns <code>null</code> for all inputs. This class cannot
116: * be used for parsing.
117: *
118: * @param source the source string.
119: * @param parsePosition the parse position.
120: *
121: * @return <code>null</code>.
122: */
123: public Number parse(String source, ParsePosition parsePosition) {
124: return null;
125: }
126:
127: }
|