KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------
27  * AxisSpace.java
28  * --------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: AxisSpace.java,v 1.7 2005/05/19 13:58:11 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 03-Jul-2003 : Version 1 (DG);
39  * 14-Aug-2003 : Implemented Cloneable (DG);
40  * 18-Aug-2003 : Implemented Serializable (DG);
41  * 17-Mar-2004 : Added a toString() method for debugging (DG);
42  * 07-Jan-2005 : Updated equals() method (DG);
43  * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
44  * release (DG);
45  *
46  */

47  
48 package org.jfree.chart.axis;
49
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.io.Serializable JavaDoc;
52
53 import org.jfree.ui.RectangleEdge;
54 import org.jfree.util.PublicCloneable;
55
56 /**
57  * A record that contains the space required at each edge of a plot.
58  */

59 public class AxisSpace implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
60     
61     /** For serialization. */
62     private static final long serialVersionUID = -2490732595134766305L;
63     
64     /** The top space. */
65     private double top;
66     
67     /** The bottom space. */
68     private double bottom;
69     
70     /** The left space. */
71     private double left;
72     
73     /** The right space. */
74     private double right;
75     
76     /**
77      * Creates a new axis space record.
78      */

79     public AxisSpace() {
80         this.top = 0.0;
81         this.bottom = 0.0;
82         this.left = 0.0;
83         this.right = 0.0;
84     }
85
86     /**
87      * Returns the space reserved for axes at the top of the plot area.
88      *
89      * @return The space (in Java2D units).
90      */

91     public double getTop() {
92         return this.top;
93     }
94     
95     /**
96      * Sets the space reserved for axes at the top of the plot area.
97      *
98      * @param space the space (in Java2D units).
99      */

100     public void setTop(double space) {
101         this.top = space;
102     }
103
104     /**
105      * Returns the space reserved for axes at the bottom of the plot area.
106      *
107      * @return The space (in Java2D units).
108      */

109     public double getBottom() {
110         return this.bottom;
111     }
112     
113     /**
114      * Sets the space reserved for axes at the bottom of the plot area.
115      *
116      * @param space the space (in Java2D units).
117      */

118     public void setBottom(double space) {
119         this.bottom = space;
120     }
121
122     /**
123      * Returns the space reserved for axes at the left of the plot area.
124      *
125      * @return The space (in Java2D units).
126      */

127     public double getLeft() {
128         return this.left;
129     }
130     
131     /**
132      * Sets the space reserved for axes at the left of the plot area.
133      *
134      * @param space the space (in Java2D units).
135      */

136     public void setLeft(double space) {
137         this.left = space;
138     }
139
140     /**
141      * Returns the space reserved for axes at the right of the plot area.
142      *
143      * @return The space (in Java2D units).
144      */

145     public double getRight() {
146         return this.right;
147     }
148     
149     /**
150      * Sets the space reserved for axes at the right of the plot area.
151      *
152      * @param space the space (in Java2D units).
153      */

154     public void setRight(double space) {
155         this.right = space;
156     }
157
158     /**
159      * Adds space to the top, bottom, left or right edge of the plot area.
160      *
161      * @param space the space (in Java2D units).
162      * @param edge the edge (<code>null</code> not permitted).
163      */

164     public void add(double space, RectangleEdge edge) {
165         if (edge == null) {
166             throw new IllegalArgumentException JavaDoc("Null 'edge' argument.");
167         }
168         if (edge == RectangleEdge.TOP) {
169             this.top += space;
170         }
171         else if (edge == RectangleEdge.BOTTOM) {
172             this.bottom += space;
173         }
174         else if (edge == RectangleEdge.LEFT) {
175             this.left += space;
176         }
177         else if (edge == RectangleEdge.RIGHT) {
178             this.right += space;
179         }
180         else {
181             throw new IllegalStateException JavaDoc("Unrecognised 'edge' argument.");
182         }
183     }
184     
185     /**
186      * Ensures that this object reserves at least as much space as another.
187      *
188      * @param space the other space.
189      */

190     public void ensureAtLeast(AxisSpace space) {
191         this.top = Math.max(this.top, space.top);
192         this.bottom = Math.max(this.bottom, space.bottom);
193         this.left = Math.max(this.left, space.left);
194         this.right = Math.max(this.right, space.right);
195     }
196     
197     /**
198      * Ensures there is a minimum amount of space at the edge corresponding to
199      * the specified axis location.
200      *
201      * @param space the space.
202      * @param edge the location.
203      */

204     public void ensureAtLeast(double space, RectangleEdge edge) {
205         if (edge == RectangleEdge.TOP) {
206             if (this.top < space) {
207                 this.top = space;
208             }
209         }
210         else if (edge == RectangleEdge.BOTTOM) {
211             if (this.bottom < space) {
212                 this.bottom = space;
213             }
214         }
215         else if (edge == RectangleEdge.LEFT) {
216             if (this.left < space) {
217                 this.left = space;
218             }
219         }
220         else if (edge == RectangleEdge.RIGHT) {
221             if (this.right < space) {
222                 this.right = space;
223             }
224         }
225         else {
226             throw new IllegalStateException JavaDoc(
227                 "AxisSpace.ensureAtLeast(): unrecognised AxisLocation."
228             );
229         }
230     }
231     
232     /**
233      * Shrinks an area by the space attributes.
234      *
235      * @param area the area to shrink.
236      * @param result an optional carrier for the result.
237      *
238      * @return The result.
239      */

240     public Rectangle2D JavaDoc shrink(Rectangle2D JavaDoc area, Rectangle2D JavaDoc result) {
241         if (result == null) {
242             result = new Rectangle2D.Double JavaDoc();
243         }
244         result.setRect(
245             area.getX() + this.left,
246             area.getY() + this.top,
247             area.getWidth() - this.left - this.right,
248             area.getHeight() - this.top - this.bottom
249         );
250         return result;
251     }
252
253     /**
254      * Expands an area by the amount of space represented by this object.
255      *
256      * @param area the area to expand.
257      * @param result an optional carrier for the result.
258      *
259      * @return The result.
260      */

261     public Rectangle2D JavaDoc expand(Rectangle2D JavaDoc area, Rectangle2D JavaDoc result) {
262         if (result == null) {
263             result = new Rectangle2D.Double JavaDoc();
264         }
265         result.setRect(
266             area.getX() - this.left,
267             area.getY() - this.top,
268             area.getWidth() + this.left + this.right,
269             area.getHeight() + this.top + this.bottom
270         );
271         return result;
272     }
273     
274     /**
275      * Calculates the reserved area.
276      *
277      * @param area the area.
278      * @param edge the edge.
279      *
280      * @return The reserved area.
281      */

282     public Rectangle2D JavaDoc reserved(Rectangle2D JavaDoc area, RectangleEdge edge) {
283         Rectangle2D JavaDoc result = null;
284         if (edge == RectangleEdge.TOP) {
285             result = new Rectangle2D.Double JavaDoc(
286                 area.getX(), area.getY(), area.getWidth(), this.top
287             );
288         }
289         else if (edge == RectangleEdge.BOTTOM) {
290             result = new Rectangle2D.Double JavaDoc(
291                 area.getX(), area.getMaxY() - this.top,
292                 area.getWidth(), this.bottom
293             );
294         }
295         else if (edge == RectangleEdge.LEFT) {
296             result = new Rectangle2D.Double JavaDoc(
297                 area.getX(), area.getY(), this.left, area.getHeight()
298             );
299         }
300         else if (edge == RectangleEdge.RIGHT) {
301             result = new Rectangle2D.Double JavaDoc(
302                 area.getMaxX() - this.right, area.getY(),
303                 this.right, area.getHeight()
304             );
305         }
306         return result;
307     }
308     
309     /**
310      * Returns a clone of the object.
311      *
312      * @return A clone.
313      *
314      * @throws CloneNotSupportedException This class won't throw this exception,
315      * but subclasses (if any) might.
316      */

317     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
318         return super.clone();
319     }
320     
321     /**
322      * Tests this object for equality with another object.
323      *
324      * @param obj the object to compare against.
325      *
326      * @return <code>true</code> or <code>false</code>.
327      */

328     public boolean equals(Object JavaDoc obj) {
329         if (obj == this) {
330             return true;
331         }
332         if (!(obj instanceof AxisSpace)) {
333             return false;
334         }
335         AxisSpace that = (AxisSpace) obj;
336         if (this.top != that.top) {
337             return false;
338         }
339         if (this.bottom != that.bottom) {
340             return false;
341         }
342         if (this.left != that.left) {
343             return false;
344         }
345         if (this.right != that.right) {
346             return false;
347         }
348         return true;
349     }
350     
351     /**
352      * Returns a hash code for this object.
353      *
354      * @return A hash code.
355      */

356     public int hashCode() {
357         int result = 23;
358         long l = Double.doubleToLongBits(this.top);
359         result = 37 * result + (int) (l ^ (l >>> 32));
360         l = Double.doubleToLongBits(this.bottom);
361         result = 37 * result + (int) (l ^ (l >>> 32));
362         l = Double.doubleToLongBits(this.left);
363         result = 37 * result + (int) (l ^ (l >>> 32));
364         l = Double.doubleToLongBits(this.right);
365         result = 37 * result + (int) (l ^ (l >>> 32));
366         return result;
367     }
368     
369     /**
370      * Returns a string representing the object (for debugging purposes).
371      *
372      * @return A string.
373      */

374     public String JavaDoc toString() {
375         return super.toString() + "[left=" + this.left + ",right=" + this.right
376                     + ",top=" + this.top + ",bottom=" + this.bottom + "]";
377     }
378     
379 }
380
Popular Tags