Source for org.jfree.chart.axis.AxisLocation

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * AxisLocation.java
  29:  * -----------------
  30:  * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Nick Guenther;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 02-May-2003 : Version 1 (DG);
  38:  * 03-Jul-2003 : Added isTopOrBottom() and isLeftOrRight() methods (DG);
  39:  * 13-Aug-2003 : Fixed readResolve() bug (id=788202) (DG);
  40:  * 24-Mar-2004 : Added static getOpposite() method (DG);
  41:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  42:  * 22-Mar-2007 : Added getOpposite() method, suggested by Nick Guenther (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.chart.axis;
  47: 
  48: import java.io.ObjectStreamException;
  49: import java.io.Serializable;
  50: 
  51: /**
  52:  * Used to indicate the location of an axis on a 2D plot, prior to knowing the 
  53:  * orientation of the plot.
  54:  */
  55: public final class AxisLocation implements Serializable {
  56: 
  57:     /** For serialization. */
  58:     private static final long serialVersionUID = -3276922179323563410L;
  59:     
  60:     /** Axis at the top or left. */
  61:     public static final AxisLocation TOP_OR_LEFT = new AxisLocation(
  62:             "AxisLocation.TOP_OR_LEFT");
  63: 
  64:     /** Axis at the top or right. */
  65:     public static final AxisLocation TOP_OR_RIGHT = new AxisLocation(
  66:             "AxisLocation.TOP_OR_RIGHT");
  67:     
  68:     /** Axis at the bottom or left. */
  69:     public static final AxisLocation BOTTOM_OR_LEFT = new AxisLocation(
  70:             "AxisLocation.BOTTOM_OR_LEFT");
  71:         
  72:     /** Axis at the bottom or right. */
  73:     public static final AxisLocation BOTTOM_OR_RIGHT = new AxisLocation(
  74:             "AxisLocation.BOTTOM_OR_RIGHT");
  75:     
  76:     /** The name. */
  77:     private String name;
  78: 
  79:     /**
  80:      * Private constructor.
  81:      *
  82:      * @param name  the name.
  83:      */
  84:     private AxisLocation(String name) {
  85:         this.name = name;
  86:     }
  87: 
  88:     /**
  89:      * Returns the location that is opposite to this location.
  90:      *
  91:      * @return The opposite location.
  92:      * 
  93:      * @since 1.0.5
  94:      */
  95:     public AxisLocation getOpposite() {
  96:         return getOpposite(this);
  97:     }
  98:     
  99:     /**
 100:      * Returns a string representing the object.
 101:      *
 102:      * @return The string.
 103:      */
 104:     public String toString() {
 105:         return this.name;
 106:     }
 107: 
 108:     /**
 109:      * Returns <code>true</code> if this object is equal to the specified 
 110:      * object, and <code>false</code> otherwise.
 111:      *
 112:      * @param obj  the other object (<code>null</code> permitted).
 113:      *
 114:      * @return A boolean.
 115:      */
 116:     public boolean equals(Object obj) {
 117: 
 118:         if (this == obj) {
 119:             return true;
 120:         }
 121:         if (!(obj instanceof AxisLocation)) {
 122:             return false;
 123:         }
 124:         AxisLocation location = (AxisLocation) obj;
 125:         if (!this.name.equals(location.toString())) {
 126:             return false;
 127:         }
 128:         return true;
 129: 
 130:     }
 131:     
 132:     /**
 133:      * Returns the location that is opposite to the supplied location.
 134:      * 
 135:      * @param location  the location (<code>null</code> not permitted).
 136:      * 
 137:      * @return The opposite location.
 138:      */
 139:     public static AxisLocation getOpposite(AxisLocation location) {
 140:         if (location == null) {
 141:             throw new IllegalArgumentException("Null 'location' argument.");   
 142:         }
 143:         AxisLocation result = null;
 144:         if (location == AxisLocation.TOP_OR_LEFT) {
 145:             result = AxisLocation.BOTTOM_OR_RIGHT;   
 146:         }
 147:         else if (location == AxisLocation.TOP_OR_RIGHT) {
 148:             result = AxisLocation.BOTTOM_OR_LEFT;   
 149:         }
 150:         else if (location == AxisLocation.BOTTOM_OR_LEFT) {
 151:             result = AxisLocation.TOP_OR_RIGHT;   
 152:         }
 153:         else if (location == AxisLocation.BOTTOM_OR_RIGHT) {
 154:             result = AxisLocation.TOP_OR_LEFT;   
 155:         }
 156:         else {
 157:             throw new IllegalStateException("AxisLocation not recognised.");   
 158:         }
 159:         return result;
 160:     }
 161:         
 162:     /**
 163:      * Ensures that serialization returns the unique instances.
 164:      * 
 165:      * @return The object.
 166:      * 
 167:      * @throws ObjectStreamException if there is a problem.
 168:      */
 169:     private Object readResolve() throws ObjectStreamException {
 170:         if (this.equals(AxisLocation.TOP_OR_RIGHT)) {
 171:             return AxisLocation.TOP_OR_RIGHT;
 172:         }
 173:         else if (this.equals(AxisLocation.BOTTOM_OR_RIGHT)) {
 174:             return AxisLocation.BOTTOM_OR_RIGHT;
 175:         }    
 176:         else if (this.equals(AxisLocation.TOP_OR_LEFT)) {
 177:             return AxisLocation.TOP_OR_LEFT;
 178:         }    
 179:         else if (this.equals(AxisLocation.BOTTOM_OR_LEFT)) {
 180:             return AxisLocation.BOTTOM_OR_LEFT;
 181:         }
 182:         return null;
 183:     }
 184:     
 185: }