Source for org.jfree.chart.axis.CompassFormat

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------
  28:  * CompassFormat.java
  29:  * ------------------
  30:  * (C) Copyright 2003, 2004, by Sylvain Vieujot and Contributors.
  31:  *
  32:  * Original Author:  Sylvain Vieujot;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: CompassFormat.java,v 1.3.2.1 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 18-Feb-2004 : Version 1 contributed by Sylvain Vieujot (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.axis;
  44: 
  45: import java.text.FieldPosition;
  46: import java.text.NumberFormat;
  47: import java.text.ParsePosition;
  48: 
  49: /**
  50:  * A formatter that displays numbers as directions.
  51:  */
  52: public class CompassFormat extends NumberFormat {
  53:     
  54:     /** North. */
  55:     private static final String N = "N";
  56:     
  57:     /** East. */
  58:     private static final String E = "E";
  59:     
  60:     /** South. */
  61:     private static final String S = "S";
  62:     
  63:     /** West. */
  64:     private static final String W = "W";
  65:     
  66:     /** The directions. */
  67:     public static final String[] DIRECTIONS = {
  68:         N, N + N + E, N + E, E + N + E, E, E + S + E, S + E, S + S + E, S,
  69:         S + S + W, S + W, W + S + W, W, W + N + W, N + W, N + N + W, N
  70:     };
  71:     
  72:     /**
  73:      * Creates a new formatter.
  74:      */
  75:     public CompassFormat() {
  76:         super();
  77:     }
  78:     
  79:     /**
  80:      * Returns a string representing the direction.
  81:      * 
  82:      * @param direction  the direction.
  83:      * 
  84:      * @return A string.
  85:      */
  86:     public String getDirectionCode(double direction) {
  87:         
  88:         direction = direction % 360;
  89:         if (direction < 0.0) {
  90:             direction = direction + 360.0;
  91:         }
  92:         int index = ((int) Math.floor(direction / 11.25) + 1) / 2; 
  93:         return DIRECTIONS[index];
  94:         
  95:     }
  96: 
  97:     /* (non-Javadoc)
  98:      * @see java.text.NumberFormat#format(double, java.lang.StringBuffer, 
  99:      *      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: }