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: * AxisSpace.java
029: * --------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: AxisSpace.java,v 1.7.2.1 2005/10/25 20:37:34 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 03-Jul-2003 : Version 1 (DG);
040: * 14-Aug-2003 : Implemented Cloneable (DG);
041: * 18-Aug-2003 : Implemented Serializable (DG);
042: * 17-Mar-2004 : Added a toString() method for debugging (DG);
043: * 07-Jan-2005 : Updated equals() method (DG);
044: * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
045: * release (DG);
046: *
047: */
048:
049: package org.jfree.chart.axis;
050:
051: import java.awt.geom.Rectangle2D;
052: import java.io.Serializable;
053:
054: import org.jfree.ui.RectangleEdge;
055: import org.jfree.util.PublicCloneable;
056:
057: /**
058: * A record that contains the space required at each edge of a plot.
059: */
060: public class AxisSpace implements Cloneable, PublicCloneable,
061: Serializable {
062:
063: /** For serialization. */
064: private static final long serialVersionUID = -2490732595134766305L;
065:
066: /** The top space. */
067: private double top;
068:
069: /** The bottom space. */
070: private double bottom;
071:
072: /** The left space. */
073: private double left;
074:
075: /** The right space. */
076: private double right;
077:
078: /**
079: * Creates a new axis space record.
080: */
081: public AxisSpace() {
082: this .top = 0.0;
083: this .bottom = 0.0;
084: this .left = 0.0;
085: this .right = 0.0;
086: }
087:
088: /**
089: * Returns the space reserved for axes at the top of the plot area.
090: *
091: * @return The space (in Java2D units).
092: */
093: public double getTop() {
094: return this .top;
095: }
096:
097: /**
098: * Sets the space reserved for axes at the top of the plot area.
099: *
100: * @param space the space (in Java2D units).
101: */
102: public void setTop(double space) {
103: this .top = space;
104: }
105:
106: /**
107: * Returns the space reserved for axes at the bottom of the plot area.
108: *
109: * @return The space (in Java2D units).
110: */
111: public double getBottom() {
112: return this .bottom;
113: }
114:
115: /**
116: * Sets the space reserved for axes at the bottom of the plot area.
117: *
118: * @param space the space (in Java2D units).
119: */
120: public void setBottom(double space) {
121: this .bottom = space;
122: }
123:
124: /**
125: * Returns the space reserved for axes at the left of the plot area.
126: *
127: * @return The space (in Java2D units).
128: */
129: public double getLeft() {
130: return this .left;
131: }
132:
133: /**
134: * Sets the space reserved for axes at the left of the plot area.
135: *
136: * @param space the space (in Java2D units).
137: */
138: public void setLeft(double space) {
139: this .left = space;
140: }
141:
142: /**
143: * Returns the space reserved for axes at the right of the plot area.
144: *
145: * @return The space (in Java2D units).
146: */
147: public double getRight() {
148: return this .right;
149: }
150:
151: /**
152: * Sets the space reserved for axes at the right of the plot area.
153: *
154: * @param space the space (in Java2D units).
155: */
156: public void setRight(double space) {
157: this .right = space;
158: }
159:
160: /**
161: * Adds space to the top, bottom, left or right edge of the plot area.
162: *
163: * @param space the space (in Java2D units).
164: * @param edge the edge (<code>null</code> not permitted).
165: */
166: public void add(double space, RectangleEdge edge) {
167: if (edge == null) {
168: throw new IllegalArgumentException("Null 'edge' argument.");
169: }
170: if (edge == RectangleEdge.TOP) {
171: this .top += space;
172: } else if (edge == RectangleEdge.BOTTOM) {
173: this .bottom += space;
174: } else if (edge == RectangleEdge.LEFT) {
175: this .left += space;
176: } else if (edge == RectangleEdge.RIGHT) {
177: this .right += space;
178: } else {
179: throw new IllegalStateException(
180: "Unrecognised 'edge' argument.");
181: }
182: }
183:
184: /**
185: * Ensures that this object reserves at least as much space as another.
186: *
187: * @param space the other space.
188: */
189: public void ensureAtLeast(AxisSpace space) {
190: this .top = Math.max(this .top, space.top);
191: this .bottom = Math.max(this .bottom, space.bottom);
192: this .left = Math.max(this .left, space.left);
193: this .right = Math.max(this .right, space.right);
194: }
195:
196: /**
197: * Ensures there is a minimum amount of space at the edge corresponding to
198: * the specified axis location.
199: *
200: * @param space the space.
201: * @param edge the location.
202: */
203: public void ensureAtLeast(double space, RectangleEdge edge) {
204: if (edge == RectangleEdge.TOP) {
205: if (this .top < space) {
206: this .top = space;
207: }
208: } else if (edge == RectangleEdge.BOTTOM) {
209: if (this .bottom < space) {
210: this .bottom = space;
211: }
212: } else if (edge == RectangleEdge.LEFT) {
213: if (this .left < space) {
214: this .left = space;
215: }
216: } else if (edge == RectangleEdge.RIGHT) {
217: if (this .right < space) {
218: this .right = space;
219: }
220: } else {
221: throw new IllegalStateException(
222: "AxisSpace.ensureAtLeast(): unrecognised AxisLocation.");
223: }
224: }
225:
226: /**
227: * Shrinks an area by the space attributes.
228: *
229: * @param area the area to shrink.
230: * @param result an optional carrier for the result.
231: *
232: * @return The result.
233: */
234: public Rectangle2D shrink(Rectangle2D area, Rectangle2D result) {
235: if (result == null) {
236: result = new Rectangle2D.Double();
237: }
238: result.setRect(area.getX() + this .left, area.getY() + this .top,
239: area.getWidth() - this .left - this .right, area
240: .getHeight()
241: - this .top - this .bottom);
242: return result;
243: }
244:
245: /**
246: * Expands an area by the amount of space represented by this object.
247: *
248: * @param area the area to expand.
249: * @param result an optional carrier for the result.
250: *
251: * @return The result.
252: */
253: public Rectangle2D expand(Rectangle2D area, Rectangle2D result) {
254: if (result == null) {
255: result = new Rectangle2D.Double();
256: }
257: result.setRect(area.getX() - this .left, area.getY() - this .top,
258: area.getWidth() + this .left + this .right, area
259: .getHeight()
260: + this .top + this .bottom);
261: return result;
262: }
263:
264: /**
265: * Calculates the reserved area.
266: *
267: * @param area the area.
268: * @param edge the edge.
269: *
270: * @return The reserved area.
271: */
272: public Rectangle2D reserved(Rectangle2D area, RectangleEdge edge) {
273: Rectangle2D result = null;
274: if (edge == RectangleEdge.TOP) {
275: result = new Rectangle2D.Double(area.getX(), area.getY(),
276: area.getWidth(), this .top);
277: } else if (edge == RectangleEdge.BOTTOM) {
278: result = new Rectangle2D.Double(area.getX(), area.getMaxY()
279: - this .top, area.getWidth(), this .bottom);
280: } else if (edge == RectangleEdge.LEFT) {
281: result = new Rectangle2D.Double(area.getX(), area.getY(),
282: this .left, area.getHeight());
283: } else if (edge == RectangleEdge.RIGHT) {
284: result = new Rectangle2D.Double(
285: area.getMaxX() - this .right, area.getY(),
286: this .right, area.getHeight());
287: }
288: return result;
289: }
290:
291: /**
292: * Returns a clone of the object.
293: *
294: * @return A clone.
295: *
296: * @throws CloneNotSupportedException This class won't throw this exception,
297: * but subclasses (if any) might.
298: */
299: public Object clone() throws CloneNotSupportedException {
300: return super .clone();
301: }
302:
303: /**
304: * Tests this object for equality with another object.
305: *
306: * @param obj the object to compare against.
307: *
308: * @return <code>true</code> or <code>false</code>.
309: */
310: public boolean equals(Object obj) {
311: if (obj == this ) {
312: return true;
313: }
314: if (!(obj instanceof AxisSpace)) {
315: return false;
316: }
317: AxisSpace that = (AxisSpace) obj;
318: if (this .top != that.top) {
319: return false;
320: }
321: if (this .bottom != that.bottom) {
322: return false;
323: }
324: if (this .left != that.left) {
325: return false;
326: }
327: if (this .right != that.right) {
328: return false;
329: }
330: return true;
331: }
332:
333: /**
334: * Returns a hash code for this object.
335: *
336: * @return A hash code.
337: */
338: public int hashCode() {
339: int result = 23;
340: long l = Double.doubleToLongBits(this .top);
341: result = 37 * result + (int) (l ^ (l >>> 32));
342: l = Double.doubleToLongBits(this .bottom);
343: result = 37 * result + (int) (l ^ (l >>> 32));
344: l = Double.doubleToLongBits(this .left);
345: result = 37 * result + (int) (l ^ (l >>> 32));
346: l = Double.doubleToLongBits(this .right);
347: result = 37 * result + (int) (l ^ (l >>> 32));
348: return result;
349: }
350:
351: /**
352: * Returns a string representing the object (for debugging purposes).
353: *
354: * @return A string.
355: */
356: public String toString() {
357: return super .toString() + "[left=" + this .left + ",right="
358: + this .right + ",top=" + this .top + ",bottom="
359: + this .bottom + "]";
360: }
361:
362: }
|