Source for org.jfree.ui.RectangleEdge

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class 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/jcommon/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:  * RectangleEdge
  29:  * -------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: RectangleEdge.java,v 1.4 2005/10/18 13:18:34 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 14-Jul-2003 (DG);
  40:  * 
  41:  */
  42: 
  43: package org.jfree.ui;
  44: 
  45: import java.awt.geom.Rectangle2D;
  46: import java.io.ObjectStreamException;
  47: import java.io.Serializable;
  48: 
  49: /**
  50:  * Used to indicate the edge of a rectangle.
  51:  *
  52:  * @author David Gilbert
  53:  */
  54: public final class RectangleEdge implements Serializable {
  55: 
  56:     /** For serialization. */
  57:     private static final long serialVersionUID = -7400988293691093548L;
  58:     
  59:     /** Top. */
  60:     public static final RectangleEdge TOP 
  61:         = new RectangleEdge("RectangleEdge.TOP");
  62: 
  63:     /** Bottom. */
  64:     public static final RectangleEdge BOTTOM 
  65:         = new RectangleEdge("RectangleEdge.BOTTOM");
  66: 
  67:     /** Left. */
  68:     public static final RectangleEdge LEFT 
  69:         = new RectangleEdge("RectangleEdge.LEFT");
  70: 
  71:     /** Right. */
  72:     public static final RectangleEdge RIGHT 
  73:         = new RectangleEdge("RectangleEdge.RIGHT");
  74: 
  75:     /** The name. */
  76:     private String name;
  77: 
  78:     /**
  79:      * Private constructor.
  80:      *
  81:      * @param name  the name.
  82:      */
  83:     private RectangleEdge(final String name) {
  84:         this.name = name;
  85:     }
  86: 
  87:     /**
  88:      * Returns a string representing the object.
  89:      *
  90:      * @return The string.
  91:      */
  92:     public String toString() {
  93:         return this.name;
  94:     }
  95: 
  96:     /**
  97:      * Returns <code>true</code> if this object is equal to the specified 
  98:      * object, and <code>false</code> otherwise.
  99:      *
 100:      * @param o  the other object.
 101:      *
 102:      * @return A boolean.
 103:      */
 104:     public boolean equals(final Object o) {
 105: 
 106:         if (this == o) {
 107:             return true;
 108:         }
 109:         if (!(o instanceof RectangleEdge)) {
 110:             return false;
 111:         }
 112: 
 113:         final RectangleEdge order = (RectangleEdge) o;
 114:         if (!this.name.equals(order.name)) {
 115:             return false;
 116:         }
 117: 
 118:         return true;
 119: 
 120:     }
 121: 
 122:     /**
 123:      * Returns a hash code value for the object.
 124:      *
 125:      * @return the hashcode
 126:      */
 127:     public int hashCode() {
 128:         return this.name.hashCode();
 129:     }
 130: 
 131:     /**
 132:      * Returns <code>true</code> if the edge is <code>TOP</code> or 
 133:      * <code>BOTTOM</code>, and <code>false</code> otherwise.
 134:      * 
 135:      * @param edge  the edge.
 136:      * 
 137:      * @return A boolean.
 138:      */
 139:     public static boolean isTopOrBottom(final RectangleEdge edge) {
 140:         return (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM);    
 141:     }
 142:     
 143:     /**
 144:      * Returns <code>true</code> if the edge is <code>LEFT</code> or 
 145:      * <code>RIGHT</code>, and <code>false</code> otherwise.
 146:      * 
 147:      * @param edge  the edge.
 148:      * 
 149:      * @return A boolean.
 150:      */
 151:     public static boolean isLeftOrRight(final RectangleEdge edge) {
 152:         return (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT);    
 153:     }
 154: 
 155:     /**
 156:      * Returns the opposite edge.
 157:      * 
 158:      * @param edge  an edge.
 159:      * 
 160:      * @return The opposite edge.
 161:      */
 162:     public static RectangleEdge opposite(final RectangleEdge edge) {
 163:         RectangleEdge result = null;
 164:         if (edge == RectangleEdge.TOP) {
 165:             result = RectangleEdge.BOTTOM;
 166:         }
 167:         else if (edge == RectangleEdge.BOTTOM) {
 168:             result = RectangleEdge.TOP;
 169:         }
 170:         else if (edge == RectangleEdge.LEFT) {
 171:             result = RectangleEdge.RIGHT;
 172:         }
 173:         else if (edge == RectangleEdge.RIGHT) {
 174:             result = RectangleEdge.LEFT;
 175:         }
 176:         return result;
 177:     }
 178:     
 179:     /**
 180:      * Returns the x or y coordinate of the specified edge.
 181:      * 
 182:      * @param rectangle  the rectangle.
 183:      * @param edge  the edge.
 184:      * 
 185:      * @return The coordinate.
 186:      */
 187:     public static double coordinate(final Rectangle2D rectangle, 
 188:                                     final RectangleEdge edge) {
 189:         double result = 0.0;
 190:         if (edge == RectangleEdge.TOP) {
 191:             result = rectangle.getMinY();
 192:         }
 193:         else if (edge == RectangleEdge.BOTTOM) {
 194:             result = rectangle.getMaxY();
 195:         }
 196:         else if (edge == RectangleEdge.LEFT) {
 197:             result = rectangle.getMinX();
 198:         }
 199:         else if (edge == RectangleEdge.RIGHT) {
 200:             result = rectangle.getMaxX();
 201:         }
 202:         return result;
 203:     }
 204:     
 205:     /**
 206:      * Ensures that serialization returns the unique instances.
 207:      * 
 208:      * @return The object.
 209:      * 
 210:      * @throws ObjectStreamException if there is a problem.
 211:      */
 212:     private Object readResolve() throws ObjectStreamException {
 213:         RectangleEdge result = null;
 214:         if (this.equals(RectangleEdge.TOP)) {
 215:             result = RectangleEdge.TOP;
 216:         }
 217:         else if (this.equals(RectangleEdge.BOTTOM)) {
 218:             result = RectangleEdge.BOTTOM;
 219:         }
 220:         else if (this.equals(RectangleEdge.LEFT)) {
 221:             result = RectangleEdge.LEFT;
 222:         }
 223:         else if (this.equals(RectangleEdge.RIGHT)) {
 224:             result = RectangleEdge.RIGHT;
 225:         }
 226:         return result;
 227:     }
 228:     
 229: }