Source for org.jfree.chart.axis.AxisSpace

   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:  * AxisSpace.java
  29:  * --------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: AxisSpace.java,v 1.7.2.1 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 03-Jul-2003 : Version 1 (DG);
  40:  * 14-Aug-2003 : Implemented Cloneable (DG);
  41:  * 18-Aug-2003 : Implemented Serializable (DG);
  42:  * 17-Mar-2004 : Added a toString() method for debugging (DG);
  43:  * 07-Jan-2005 : Updated equals() method (DG);
  44:  * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0 
  45:  *               release (DG);
  46:  * 
  47:  */
  48:  
  49: package org.jfree.chart.axis;
  50: 
  51: import java.awt.geom.Rectangle2D;
  52: import java.io.Serializable;
  53: 
  54: import org.jfree.ui.RectangleEdge;
  55: import org.jfree.util.PublicCloneable;
  56: 
  57: /**
  58:  * A record that contains the space required at each edge of a plot.
  59:  */
  60: public class AxisSpace implements Cloneable, PublicCloneable, Serializable {
  61:     
  62:     /** For serialization. */
  63:     private static final long serialVersionUID = -2490732595134766305L;
  64:     
  65:     /** The top space. */
  66:     private double top;
  67:     
  68:     /** The bottom space. */
  69:     private double bottom;
  70:     
  71:     /** The left space. */
  72:     private double left;
  73:     
  74:     /** The right space. */
  75:     private double right;
  76:     
  77:     /**
  78:      * Creates a new axis space record.
  79:      */
  80:     public AxisSpace() {
  81:         this.top = 0.0;
  82:         this.bottom = 0.0;
  83:         this.left = 0.0;
  84:         this.right = 0.0;
  85:     }
  86: 
  87:     /**
  88:      * Returns the space reserved for axes at the top of the plot area.
  89:      * 
  90:      * @return The space (in Java2D units).
  91:      */
  92:     public double getTop() {
  93:         return this.top;
  94:     }
  95:     
  96:     /**
  97:      * Sets the space reserved for axes at the top of the plot area. 
  98:      * 
  99:      * @param space  the space (in Java2D units).
 100:      */
 101:     public void setTop(double space) {
 102:         this.top = space;
 103:     }
 104: 
 105:     /**
 106:      * Returns the space reserved for axes at the bottom of the plot area.
 107:      * 
 108:      * @return The space (in Java2D units).
 109:      */
 110:     public double getBottom() {
 111:         return this.bottom;
 112:     }
 113:     
 114:     /**
 115:      * Sets the space reserved for axes at the bottom of the plot area. 
 116:      * 
 117:      * @param space  the space (in Java2D units).
 118:      */
 119:     public void setBottom(double space) {
 120:         this.bottom = space;
 121:     }
 122: 
 123:     /**
 124:      * Returns the space reserved for axes at the left of the plot area.
 125:      * 
 126:      * @return The space (in Java2D units).
 127:      */
 128:     public double getLeft() {
 129:         return this.left;
 130:     }
 131:     
 132:     /**
 133:      * Sets the space reserved for axes at the left of the plot area. 
 134:      * 
 135:      * @param space  the space (in Java2D units).
 136:      */
 137:     public void setLeft(double space) {
 138:         this.left = space;
 139:     }
 140: 
 141:     /**
 142:      * Returns the space reserved for axes at the right of the plot area.
 143:      * 
 144:      * @return The space (in Java2D units).
 145:      */
 146:     public double getRight() {
 147:         return this.right;
 148:     }
 149:     
 150:     /**
 151:      * Sets the space reserved for axes at the right of the plot area. 
 152:      * 
 153:      * @param space  the space (in Java2D units).
 154:      */
 155:     public void setRight(double space) {
 156:         this.right = space;
 157:     }
 158: 
 159:     /**
 160:      * Adds space to the top, bottom, left or right edge of the plot area.
 161:      * 
 162:      * @param space  the space (in Java2D units).
 163:      * @param edge  the edge (<code>null</code> not permitted).
 164:      */
 165:     public void add(double space, RectangleEdge edge) {
 166:         if (edge == null) {
 167:             throw new IllegalArgumentException("Null 'edge' argument.");
 168:         }
 169:         if (edge == RectangleEdge.TOP) {     
 170:             this.top += space;
 171:         }
 172:         else if (edge == RectangleEdge.BOTTOM) {
 173:             this.bottom += space;
 174:         }
 175:         else if (edge == RectangleEdge.LEFT) {
 176:             this.left += space;
 177:         }
 178:         else if (edge == RectangleEdge.RIGHT) {
 179:             this.right += space;
 180:         }
 181:         else {
 182:             throw new IllegalStateException("Unrecognised 'edge' argument.");
 183:         }
 184:     }
 185:     
 186:     /**
 187:      * Ensures that this object reserves at least as much space as another.
 188:      * 
 189:      * @param space  the other space.
 190:      */
 191:     public void ensureAtLeast(AxisSpace space) {
 192:         this.top = Math.max(this.top, space.top);
 193:         this.bottom = Math.max(this.bottom, space.bottom);
 194:         this.left = Math.max(this.left, space.left);
 195:         this.right = Math.max(this.right, space.right);
 196:     }
 197:     
 198:     /**
 199:      * Ensures there is a minimum amount of space at the edge corresponding to 
 200:      * the specified axis location.
 201:      * 
 202:      * @param space  the space.
 203:      * @param edge  the location.
 204:      */
 205:     public void ensureAtLeast(double space, RectangleEdge edge) {
 206:         if (edge == RectangleEdge.TOP) {
 207:             if (this.top < space) {
 208:                 this.top = space;
 209:             }
 210:         }
 211:         else if (edge == RectangleEdge.BOTTOM) {
 212:             if (this.bottom < space) {
 213:                 this.bottom = space;
 214:             }
 215:         }
 216:         else if (edge == RectangleEdge.LEFT) {
 217:             if (this.left < space) {
 218:                 this.left = space;
 219:             }
 220:         }
 221:         else if (edge == RectangleEdge.RIGHT) {
 222:             if (this.right < space) {
 223:                 this.right = space;
 224:             }
 225:         }
 226:         else {
 227:             throw new IllegalStateException(
 228:                 "AxisSpace.ensureAtLeast(): unrecognised AxisLocation."
 229:             );
 230:         }
 231:     }
 232:     
 233:     /**
 234:      * Shrinks an area by the space attributes.
 235:      * 
 236:      * @param area  the area to shrink.
 237:      * @param result  an optional carrier for the result.
 238:      * 
 239:      * @return The result.
 240:      */
 241:     public Rectangle2D shrink(Rectangle2D area, Rectangle2D result) {
 242:         if (result == null) {
 243:             result = new Rectangle2D.Double();
 244:         }
 245:         result.setRect(
 246:             area.getX() + this.left, 
 247:             area.getY() + this.top,
 248:             area.getWidth() - this.left - this.right,
 249:             area.getHeight() - this.top - this.bottom
 250:         );
 251:         return result;
 252:     }
 253: 
 254:     /**
 255:      * Expands an area by the amount of space represented by this object.
 256:      * 
 257:      * @param area  the area to expand.
 258:      * @param result  an optional carrier for the result.
 259:      * 
 260:      * @return The result.
 261:      */
 262:     public Rectangle2D expand(Rectangle2D area, Rectangle2D result) {
 263:         if (result == null) {
 264:             result = new Rectangle2D.Double();
 265:         }
 266:         result.setRect(
 267:             area.getX() - this.left, 
 268:             area.getY() - this.top,
 269:             area.getWidth() + this.left + this.right,
 270:             area.getHeight() + this.top + this.bottom
 271:         );
 272:         return result;
 273:     }
 274:     
 275:     /**
 276:      * Calculates the reserved area.
 277:      * 
 278:      * @param area  the area.
 279:      * @param edge  the edge.
 280:      * 
 281:      * @return The reserved area.
 282:      */
 283:     public Rectangle2D reserved(Rectangle2D area, RectangleEdge edge) {
 284:         Rectangle2D result = null;
 285:         if (edge == RectangleEdge.TOP) {
 286:             result = new Rectangle2D.Double(
 287:                 area.getX(), area.getY(), area.getWidth(), this.top
 288:             );
 289:         }
 290:         else if (edge == RectangleEdge.BOTTOM) {
 291:             result = new Rectangle2D.Double(
 292:                 area.getX(), area.getMaxY() - this.top,
 293:                 area.getWidth(), this.bottom
 294:             );
 295:         }
 296:         else if (edge == RectangleEdge.LEFT) {
 297:             result = new Rectangle2D.Double(
 298:                 area.getX(), area.getY(), this.left, area.getHeight()
 299:             );
 300:         }
 301:         else if (edge == RectangleEdge.RIGHT) {
 302:             result = new Rectangle2D.Double(
 303:                 area.getMaxX() - this.right, area.getY(),
 304:                 this.right, area.getHeight()
 305:             );
 306:         }
 307:         return result;
 308:     }
 309:     
 310:     /**
 311:      * Returns a clone of the object.
 312:      * 
 313:      * @return A clone.
 314:      * 
 315:      * @throws CloneNotSupportedException This class won't throw this exception,
 316:      *         but subclasses (if any) might.
 317:      */
 318:     public Object clone() throws CloneNotSupportedException {
 319:         return super.clone();
 320:     }
 321:     
 322:     /**
 323:      * Tests this object for equality with another object.
 324:      * 
 325:      * @param obj  the object to compare against.
 326:      * 
 327:      * @return <code>true</code> or <code>false</code>.
 328:      */
 329:     public boolean equals(Object obj) {       
 330:         if (obj == this) {
 331:             return true;
 332:         }
 333:         if (!(obj instanceof AxisSpace)) {
 334:             return false;
 335:         }
 336:         AxisSpace that = (AxisSpace) obj;
 337:         if (this.top != that.top) {
 338:             return false;
 339:         }   
 340:         if (this.bottom != that.bottom) {
 341:             return false;
 342:         }
 343:         if (this.left != that.left) {
 344:             return false;
 345:         }
 346:         if (this.right != that.right) {
 347:             return false;
 348:         }
 349:         return true;
 350:     }
 351:     
 352:     /**
 353:      * Returns a hash code for this object.
 354:      * 
 355:      * @return A hash code.
 356:      */
 357:     public int hashCode() {
 358:         int result = 23;
 359:         long l = Double.doubleToLongBits(this.top);
 360:         result = 37 * result + (int) (l ^ (l >>> 32));
 361:         l = Double.doubleToLongBits(this.bottom);
 362:         result = 37 * result + (int) (l ^ (l >>> 32));
 363:         l = Double.doubleToLongBits(this.left);
 364:         result = 37 * result + (int) (l ^ (l >>> 32));
 365:         l = Double.doubleToLongBits(this.right);
 366:         result = 37 * result + (int) (l ^ (l >>> 32));
 367:         return result;
 368:     }
 369:     
 370:     /**
 371:      * Returns a string representing the object (for debugging purposes).
 372:      * 
 373:      * @return A string.
 374:      */
 375:     public String toString() {
 376:         return super.toString() + "[left=" + this.left + ",right=" + this.right 
 377:                     + ",top=" + this.top + ",bottom=" + this.bottom + "]";
 378:     }
 379:     
 380: }