KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------
23  * Spacer.java
24  * -----------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: Spacer.java,v 1.4 2003/08/20 11:30:11 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 07-Feb-2002 : Version 1 (DG);
35  * 18-Sep-2002 : Added trim(..) method, completed Javadocs and fixed Checkstyle issues (DG);
36  * 26-Mar-2003 : Implemented Serializable (DG);
37  *
38  */

39
40 package org.jfree.chart;
41
42 import java.awt.geom.Rectangle2D JavaDoc;
43 import java.io.Serializable JavaDoc;
44
45 /**
46  * Represents an amount of blank space inside (or sometimes outside) a
47  * rectangle. This class is similar in function to the Insets class, but
48  * allows for the space to be specified in relative terms as well as absolute
49  * terms.
50  * <P>
51  * Instances of this class are immutable.
52  *
53  * @author David Gilbert
54  */

55 public class Spacer implements Serializable JavaDoc {
56
57     /** A constant for 'relative' spacing. */
58     public static final int RELATIVE = 0;
59
60     /** A constant for 'absolute' spacing. */
61     public static final int ABSOLUTE = 1;
62
63     /** The spacing type (relative or absolute). */
64     private int type;
65
66     /** The space on the left. */
67     private double left;
68
69     /** The space on the right. */
70     private double right;
71
72     /** The space at the top. */
73     private double top;
74
75     /** The space at the bottom. */
76     private double bottom;
77
78     /**
79      * Creates a new Spacer object.
80      * <p>
81      * The space can be specified in relative or absolute terms (using the constants
82      * <code>RELATIVE</code> and <code>ABSOLUTE</code> for the <code>type</code> argument.
83      * For relative spacing, the margins are specified as percentages (of the overall height
84      * or width). For absolute spacing, the margins are specified in points (1/72 inch).
85      *
86      * @param type the type of spacing (relative or absolute).
87      * @param left the left margin.
88      * @param top the top margin.
89      * @param right the right margin.
90      * @param bottom the bottom margin.
91      */

92     public Spacer(int type, double left, double top, double right, double bottom) {
93
94         this.type = type;
95         this.left = left;
96         this.top = top;
97         this.right = right;
98         this.bottom = bottom;
99
100     }
101
102     /**
103      * Returns the amount of space for the left hand side of a rectangular area.
104      * <p>
105      * The width argument is only used for calculating 'relative' spacing.
106      *
107      * @param width the overall width of the rectangular area.
108      *
109      * @return the space (in points).
110      */

111     public double getLeftSpace(double width) {
112
113         double result = 0.0;
114
115         if (type == ABSOLUTE) {
116             result = left;
117         }
118         else if (type == RELATIVE) {
119             result = left * width;
120         }
121
122         return result;
123
124     }
125
126     /**
127      * Returns the amount of space for the right hand side of a rectangular area.
128      * <p>
129      * The width argument is only used for calculating 'relative' spacing.
130      *
131      * @param width the overall width of the rectangular area.
132      *
133      * @return the space (in points).
134      */

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

159     public double getTopSpace(double height) {
160
161         double result = 0.0;
162
163         if (type == ABSOLUTE) {
164             result = top;
165         }
166         else if (type == RELATIVE) {
167             result = top * height;
168         }
169
170         return result;
171
172     }
173
174     /**
175      * Returns the amount of space for the bottom of a rectangular area.
176      * <p>
177      * The height argument is only used for calculating 'relative' spacing.
178      *
179      * @param height the overall height of the rectangular area.
180      *
181      * @return the space (in points).
182      */

183     public double getBottomSpace(double height) {
184
185         double result = 0.0;
186
187         if (type == ABSOLUTE) {
188             result = bottom;
189         }
190         else if (type == RELATIVE) {
191             result = bottom * height;
192         }
193
194         return result;
195
196     }
197
198     /**
199      * Returns the width after adding the left and right spacing amounts.
200      *
201      * @param width the original width.
202      *
203      * @return the adjusted width.
204      */

205     public double getAdjustedWidth(double width) {
206
207         double result = width;
208
209         if (type == ABSOLUTE) {
210             result = result + left + right;
211         }
212         else if (type == RELATIVE) {
213             result = result + (left * width) + (right * width);
214         }
215
216         return result;
217
218     }
219
220     /**
221      * Returns the height after adding the top and bottom spacing amounts.
222      *
223      * @param height the original height.
224      *
225      * @return the adjusted height.
226      */

227     public double getAdjustedHeight(double height) {
228
229         double result = height;
230
231         if (type == ABSOLUTE) {
232             result = result + top + bottom;
233         }
234         else if (type == RELATIVE) {
235             result = result + (top * height) + (bottom * height);
236         }
237
238         return result;
239
240     }
241
242     /**
243      * Calculates the margins and trims them from the supplied area.
244      *
245      * @param area the area to be trimmed.
246      */

247     public void trim(Rectangle2D JavaDoc area) {
248         double x = area.getX();
249         double y = area.getY();
250         double h = area.getHeight();
251         double w = area.getWidth();
252         double l = getLeftSpace(w);
253         double r = getRightSpace(w);
254         double t = getTopSpace(h);
255         double b = getBottomSpace(h);
256         area.setRect(x + l, y + t, w - l - r, h - t - b);
257     }
258
259     /**
260      * Tests this object for equality with another object.
261      *
262      * @param obj the other object.
263      *
264      * @return <code>true</code> or <code>false</code>.
265      */

266     public boolean equals(Object JavaDoc obj) {
267
268         if (obj == null) {
269             return false;
270         }
271
272         if (obj == this) {
273             return true;
274         }
275
276         if (obj instanceof Spacer) {
277             Spacer s = (Spacer) obj;
278             boolean b0 = (this.type == s.type);
279             boolean b1 = (this.left == s.left);
280             boolean b2 = (this.right == s.right);
281             boolean b3 = (this.top == s.top);
282             boolean b4 = (this.bottom == s.bottom);
283             return b0 && b1 && b2 && b3 && b4;
284         }
285
286         return false;
287     }
288
289 }
290
Popular Tags