KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > Spacer


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 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  * Spacer.java
28  * -----------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: Spacer.java,v 1.9 2005/06/01 14:12:29 taqua Exp $
35  *
36  * Changes
37  * -------
38  * 07-Feb-2002 : Version 1 (DG);
39  * 18-Sep-2002 : Added trim() method, completed Javadocs and fixed Checkstyle
40  * issues (DG);
41  * 26-Mar-2003 : Implemented Serializable (DG);
42  * 01-Apr-2004 : Added trimWidth() method (DG);
43  * 01-Nov-2004 : Added NO_SPACE constant (DG);
44  * 09-Nov-2004 : Renamed getAdjustedWidth() --> calculateExtendedWidth(),
45  * getAdjustedHeight() --> calculateExtendedHeight, and tidied
46  * up the equals() method (DG);
47  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
48  * release (DG);
49  *
50  */

51
52 package org.jfree.ui;
53
54 import java.awt.geom.Rectangle2D JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 /**
58  * Represents an amount of blank space inside (or sometimes outside) a
59  * rectangle. This class is similar in function to the <code>Insets</code>
60  * class, but allows for the space to be specified in relative terms as well
61  * as absolute terms. Instances of this class are immutable.
62  * <p>
63  * In general, using {@link RectangleInsets} is preferred to this class.
64  *
65  * @author David Gilbert
66  */

67 public class Spacer implements Serializable JavaDoc {
68     
69     /** For serialization. */
70     private static final long serialVersionUID = 1464655984225158198L;
71     
72     /** A constant for 'relative' spacing. */
73     public static final int RELATIVE = 0;
74
75     /** A constant for 'absolute' spacing. */
76     public static final int ABSOLUTE = 1;
77
78     /** A spacer that adds no space around a rectangle. */
79     public static final Spacer NO_SPACE
80         = new Spacer(ABSOLUTE, 0.0, 0.0, 0.0, 0.0);
81
82     /** The spacing type (relative or absolute). */
83     private int type;
84
85     /** The space on the left. */
86     private double left;
87
88     /** The space on the right. */
89     private double right;
90
91     /** The space at the top. */
92     private double top;
93
94     /** The space at the bottom. */
95     private double bottom;
96
97     /**
98      * Creates a new Spacer object.
99      * <p>
100      * The space can be specified in relative or absolute terms (using the
101      * constants <code>RELATIVE</code> and <code>ABSOLUTE</code> for the
102      * <code>type</code> argument. For relative spacing, the margins are
103      * specified as percentages (of the overall height or width). For
104      * absolute spacing, the margins are specified in points (1/72 inch).
105      *
106      * @param type the type of spacing (relative or absolute).
107      * @param left the left margin.
108      * @param top the top margin.
109      * @param right the right margin.
110      * @param bottom the bottom margin.
111      */

112     public Spacer(final int type,
113                   final double left,
114                   final double top,
115                   final double right,
116                   final double bottom) {
117
118         this.type = type;
119         this.left = left;
120         this.top = top;
121         this.right = right;
122         this.bottom = bottom;
123
124     }
125
126     /**
127      * Returns the amount of space for the left hand side of a rectangular
128      * area. The width argument is only used for calculating 'relative'
129      * spacing.
130      *
131      * @param width the overall width of the rectangular area.
132      *
133      * @return The space (in points).
134      */

135     public double getLeftSpace(final double width) {
136         double result = 0.0;
137         if (this.type == ABSOLUTE) {
138             result = this.left;
139         }
140         else if (this.type == RELATIVE) {
141             result = this.left * width;
142         }
143         return result;
144     }
145
146     /**
147      * Returns the amount of space for the right hand side of a rectangular
148      * area. The width argument is only used for calculating 'relative'
149      * spacing.
150      *
151      * @param width the overall width of the rectangular area.
152      *
153      * @return The space (in points).
154      */

155     public double getRightSpace(final double width) {
156         double result = 0.0;
157         if (this.type == ABSOLUTE) {
158             result = this.right;
159         }
160         else if (this.type == RELATIVE) {
161             result = this.right * width;
162         }
163         return result;
164     }
165
166     /**
167      * Returns the amount of space for the top of a rectangular area. The
168      * height argument is only used for calculating 'relative' spacing.
169      *
170      * @param height the overall height of the rectangular area.
171      *
172      * @return The space (in points).
173      */

174     public double getTopSpace(final double height) {
175         double result = 0.0;
176         if (this.type == ABSOLUTE) {
177             result = this.top;
178         }
179         else if (this.type == RELATIVE) {
180             result = this.top * height;
181         }
182         return result;
183     }
184
185     /**
186      * Returns the amount of space for the bottom of a rectangular area. The
187      * height argument is only used for calculating 'relative' spacing.
188      *
189      * @param height the overall height of the rectangular area.
190      *
191      * @return The space (in points).
192      */

193     public double getBottomSpace(final double height) {
194         double result = 0.0;
195         if (this.type == ABSOLUTE) {
196             result = this.bottom;
197         }
198         else if (this.type == RELATIVE) {
199             result = this.bottom * height;
200         }
201         return result;
202     }
203
204     /**
205      * Returns the width after adding the left and right spacing amounts.
206      *
207      * @param width the original width.
208      *
209      * @return The extended width.
210      */

211     public double calculateExtendedWidth(final double width) {
212         double result = width;
213         if (this.type == ABSOLUTE) {
214             result = result + this.left + this.right;
215         }
216         else if (this.type == RELATIVE) {
217             result = result + (this.left * width) + (this.right * width);
218         }
219         return result;
220     }
221
222     /**
223      * Calculates the extended height after adding the top and bottom spacing
224      * amounts.
225      *
226      * @param height the original height.
227      *
228      * @return The extended height.
229      */

230     public double calculateExtendedHeight(final double height) {
231         double result = height;
232         if (this.type == ABSOLUTE) {
233             result = result + this.top + this.bottom;
234         }
235         else if (this.type == RELATIVE) {
236             result = result + (this.top * height) + (this.bottom * height);
237         }
238         return result;
239     }
240
241     /**
242      * Calculates the margins and trims them from the supplied area.
243      *
244      * @param area the area to be trimmed (<code>null</code> not permitted).
245      */

246     public void trim(final Rectangle2D JavaDoc area) {
247         if (area == null) {
248             throw new IllegalArgumentException JavaDoc("Null 'area' argument.");
249         }
250         final double x = area.getX();
251         final double y = area.getY();
252         final double h = area.getHeight();
253         final double w = area.getWidth();
254         final double l = getLeftSpace(w);
255         final double r = getRightSpace(w);
256         final double t = getTopSpace(h);
257         final double b = getBottomSpace(h);
258         area.setRect(x + l, y + t, w - l - r, h - t - b);
259     }
260     
261     /**
262      * Reduces the specified width according to the left and right space
263      * settings.
264      *
265      * @param w the width.
266      *
267      * @return The reduced width.
268      */

269     public double trimWidth(final double w) {
270         return w - getLeftSpace(w) - getRightSpace(w);
271     }
272
273     /**
274      * Tests this object for equality with another object.
275      *
276      * @param obj the other object (<code>null</code> permitted).
277      *
278      * @return <code>true</code> or <code>false</code>.
279      */

280     public boolean equals(final Object JavaDoc obj) {
281         if (obj == this) {
282             return true;
283         }
284         if (!(obj instanceof Spacer)) {
285             return false;
286         }
287         final Spacer that = (Spacer) obj;
288         if (this.type != that.type) {
289             return false;
290         }
291         if (this.left != that.left) {
292             return false;
293         }
294         if (this.right != that.right) {
295             return false;
296         }
297         if (this.top != that.top) {
298             return false;
299         }
300         if (this.bottom != that.bottom) {
301             return false;
302         }
303         return true;
304     }
305
306     /**
307      * Returns a hashcode for this instance.
308      *
309      * @return A hashcode.
310      */

311     public int hashCode() {
312         int result;
313         long temp;
314         result = this.type;
315         temp = this.left != +0.0d ? Double.doubleToLongBits(this.left) : 0l;
316         result = 29 * result + (int) (temp ^ (temp >>> 32));
317         temp = this.right != +0.0d ? Double.doubleToLongBits(this.right) : 0l;
318         result = 29 * result + (int) (temp ^ (temp >>> 32));
319         temp = this.top != +0.0d ? Double.doubleToLongBits(this.top) : 0l;
320         result = 29 * result + (int) (temp ^ (temp >>> 32));
321         temp = this.bottom != +0.0d ? Double.doubleToLongBits(this.bottom) : 0l;
322         result = 29 * result + (int) (temp ^ (temp >>> 32));
323         return result;
324     }
325
326 }
327
Popular Tags