KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > layout > ConstantSize


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.layout;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * An implementation of the {@link Size} interface that represents constant
39  * sizes described by a value and unit, for example:
40  * 10 pixel, 15 point or 4 dialog units.
41  * You can get instances of <code>ConstantSize</code> using
42  * the factory methods and constants in the {@link Sizes} class.
43  * Logical constant sizes that vary with the current layout style
44  * are delivered by the {@link com.jgoodies.forms.util.LayoutStyle} class.
45  * <p>
46  * This class supports different size units:
47  * <table>
48  * <tr><td><b>Unit</b>&nbsp;
49  * </td><td>&nbsp;<b>Abbreviation</b>&nbsp;</td><td>&nbsp;
50  * <b>Size</b></td></tr>
51  * <tr><td>Millimeter</td><td>mm</td><td>0.1 cm</td></tr>
52  * <tr><td>Centimeter</td><td>cm</td><td>10.0 mm</td></tr>
53  * <tr><td>Inch</td><td>in</td><td>25.4 mm</td></tr>
54  * <tr><td>DTP Point</td><td>pt</td><td>1/72 in</td></tr>
55  * <tr><td>Pixel</td><td>px</td><td>1/(resolution in dpi) in</td></tr>
56  * <tr><td>Dialog Unit</td><td>dlu</td><td>honors l&amp;f, resolution, and
57  * dialog font size</td></tr>
58  * </table>
59  *
60  * @author Karsten Lentzsch
61  * @version $Revision: 1.2 $
62  * @see Size
63  * @see Sizes
64  */

65
66 public final class ConstantSize implements Size {
67     
68     // Public Units *********************************************************
69

70     public static final Unit PIXEL = new Unit("Pixel", "px", true);
71     public static final Unit POINT = new Unit("Point", "pt", true);
72     public static final Unit DIALOG_UNITS_X = new Unit("Dialog units X", "dluX", true);
73     public static final Unit DLUX = DIALOG_UNITS_X;
74     public static final Unit DIALOG_UNITS_Y = new Unit("Dialog units Y", "dluY", true);
75     public static final Unit DLUY = DIALOG_UNITS_Y;
76     public static final Unit MILLIMETER = new Unit("Millimeter", "mm", false);
77     public static final Unit MM = MILLIMETER;
78     public static final Unit CENTIMETER = new Unit("Centimeter", "cm", false);
79     public static final Unit CM = CENTIMETER;
80     public static final Unit INCH = new Unit("Inch", "in", false);
81     public static final Unit IN = INCH;
82     
83     
84     // Fields ***************************************************************
85

86     private final double value;
87     private final Unit unit;
88     
89     
90     // Instance Creation ****************************************************
91

92     /**
93      * Constructs an instance of <code>ConstantSize</code> from the given
94      * encoded size and unit description.
95      *
96      * @param value the size value interpreted in the given units
97      * @param unit the size's unit
98      */

99     ConstantSize(int value, Unit unit) {
100         this.value = value;
101         this.unit = unit;
102     }
103     
104     /**
105      * Constructs an instance of <code>ConstantSize</code> from the given
106      * encoded size and unit description.
107      *
108      * @param value the size value interpreted in the given units
109      * @param unit the size's unit
110      */

111     ConstantSize(double value, Unit unit) {
112         this.value = value;
113         this.unit = unit;
114     }
115     
116     /**
117      * Constructs an instance of <code>ConstantSize</code> from the given
118      * encoded size and unit description.
119      *
120      * @param encodedValueAndUnit the size's value and unit as string
121      * @param horizontal true for horizontal, false for vertical
122      * @throws IllegalArgumentException if the unit requires integer
123      * but the value is not an integer
124      */

125     static ConstantSize valueOf(String JavaDoc encodedValueAndUnit, boolean horizontal) {
126         String JavaDoc split[] = ConstantSize.splitValueAndUnit(encodedValueAndUnit);
127         String JavaDoc encodedValue = split[0];
128         String JavaDoc encodedUnit = split[1];
129         Unit unit = Unit.valueOf(encodedUnit, horizontal);
130         double value = Double.parseDouble(encodedValue);
131         if (unit.requiresIntegers) {
132             if (value != (int) value)
133                 throw new IllegalArgumentException JavaDoc(unit.toString()
134                     + " value " + encodedValue + " must be an integer.");
135         }
136         return new ConstantSize(value, unit);
137     }
138     
139     /**
140      * Answers an instance of <code>Size</code> for the specified value
141      * in horizontal dialog units.
142      *
143      * @param value size value in horizontal dialog units
144      * @return the associated Size instance
145      */

146     static ConstantSize dluX(int value) {
147         return new ConstantSize(value, DLUX);
148     }
149     
150     /**
151      * Answers an instance of <code>Size</code> for the specified value
152      * in vertical dialog units.
153      *
154      * @param value size value in vertical dialog units
155      * @return the associated Size instance
156      */

157     static ConstantSize dluY(int value) {
158         return new ConstantSize(value, DLUY);
159     }
160     
161
162     // Accessing the Value **************************************************
163

164     /**
165      * Converts the size if necessary and answers the value in pixels.
166      *
167      * @param component the associated component
168      * @return the size in pixels
169      */

170     public int getPixelSize(Component JavaDoc component) {
171         if (unit == PIXEL)
172             return intValue();
173         else if (unit == POINT)
174             return Sizes.pointAsPixel(intValue(), component);
175         else if (unit == INCH)
176             return Sizes.inchAsPixel(value, component);
177         else if (unit == MILLIMETER)
178             return Sizes.millimeterAsPixel(value, component);
179         else if (unit == CENTIMETER)
180             return Sizes.centimeterAsPixel(value, component);
181         else if (unit == DIALOG_UNITS_X)
182             return Sizes.dialogUnitXAsPixel(intValue(), component);
183         else if (unit == DIALOG_UNITS_Y)
184             return Sizes.dialogUnitYAsPixel(intValue(), component);
185         else
186             throw new IllegalStateException JavaDoc("Invalid unit " + unit);
187     }
188     
189     
190     // Implementing the Size Interface **************************************
191

192     /**
193      * Returns this size as pixel size. Neither requires the component
194      * list nor the specified measures.
195      * <p>
196      * Invoked by {@link com.jgoodies.forms.layout.FormSpec} to determine
197      * the size of a column or row.
198      *
199      * @param container the layout container
200      * @param components the list of components used to compute the size
201      * @param minMeasure the measure that determines the minimum sizes
202      * @param prefMeasure the measure that determines the preferred sizes
203      * @param defaultMeasure the measure that determines the default sizes
204      */

205     public int maximumSize(Container JavaDoc container,
206                     List JavaDoc components,
207                     FormLayout.Measure minMeasure,
208                     FormLayout.Measure prefMeasure,
209                     FormLayout.Measure defaultMeasure) {
210         return getPixelSize(container);
211     }
212     
213     // Overriding Object Behavior *******************************************
214

215     /**
216      * Indicates whether some other ConstantSize is "equal to"
217      * this one.
218      *
219      * @param o the Object with which to compare
220      * @return <code>true</code> if this object is the same as the obj
221      * argument; <code>false</code> otherwise.
222      * @see java.lang.Object#hashCode()
223      * @see java.util.Hashtable
224      */

225     public boolean equals(Object JavaDoc o) {
226         if (!(o instanceof ConstantSize))
227             return false;
228         ConstantSize size = (ConstantSize) o;
229         return this.value == size.value
230              && this.unit == size.unit;
231     }
232     
233     /**
234      * Returns a hash code value for the object. This method is
235      * supported for the benefit of hashtables such as those provided by
236      * <code>java.util.Hashtable</code>.
237      *
238      * @return a hash code value for this object.
239      * @see java.lang.Object#equals(java.lang.Object)
240      * @see java.util.Hashtable
241      */

242     public int hashCode() {
243         return new Double JavaDoc(value).hashCode() + 37 * unit.hashCode();
244     }
245     
246     /**
247      * Returns a string representation of this size object.
248      *
249      * @return a string representation of the constant size
250      */

251     public String JavaDoc toString() {
252         return (value == intValue())
253             ? Integer.toString(intValue()) + unit.abbreviation()
254             : Double.toString(value) + unit.abbreviation();
255     }
256     
257
258     // Helper Code **********************************************************
259

260     private int intValue() {
261         return (int) Math.round(value);
262     }
263     
264     /**
265      * Splits a string that encodes size with unit into the size and unit
266      * substrings. Answers an array of two strings.
267      *
268      * @param encodedValueAndUnit a strings that represents a size with unit
269      * @return the first element is size, the second is unit
270      */

271     static String JavaDoc[] splitValueAndUnit(String JavaDoc encodedValueAndUnit) {
272         String JavaDoc[] result = new String JavaDoc[2];
273         int len = encodedValueAndUnit.length();
274         int firstLetterIndex = len;
275         while (firstLetterIndex > 0
276                 && Character.isLetter(encodedValueAndUnit.charAt(firstLetterIndex-1))) {
277                 firstLetterIndex--;
278         }
279         result[0] = encodedValueAndUnit.substring(0, firstLetterIndex);
280         result[1] = encodedValueAndUnit.substring(firstLetterIndex);
281         return result;
282     }
283
284     // Helper Class *********************************************************
285

286     /**
287      * A typesafe enumeration for units as used in instances of
288      * {@link ConstantSize}.
289      */

290     public static final class Unit {
291         
292         private final String JavaDoc name;
293         private final String JavaDoc abbreviation;
294                  final boolean requiresIntegers;
295
296         private Unit(String JavaDoc name, String JavaDoc abbreviation, boolean requiresIntegers) {
297             this.name = name;
298             this.abbreviation = abbreviation;
299             this.requiresIntegers = requiresIntegers;
300         }
301
302         /**
303          * Answers an instance of <code>Unit</code> that corresponds to the
304          * specified string.
305          *
306          * @param str the encoded unit
307          * @return the corresponding Unit
308          * @throws IllegalArgumentException if no Unit exists for the string
309          */

310         static Unit valueOf(String JavaDoc str, boolean horizontal) {
311             String JavaDoc lowerCase = str.toLowerCase();
312             if (lowerCase.equals("px") || lowerCase.length() == 0)
313                 return PIXEL;
314             else if (lowerCase.equals("dlu"))
315                 return horizontal ? DIALOG_UNITS_X : DIALOG_UNITS_Y;
316             else if (lowerCase.equals("pt"))
317                 return POINT;
318             else if (lowerCase.equals("in"))
319                 return INCH;
320             else if (lowerCase.equals("mm"))
321                 return MILLIMETER;
322             else if (lowerCase.equals("cm"))
323                 return CENTIMETER;
324             else
325                 throw new IllegalArgumentException JavaDoc(
326                     "Invalid unit name '" + str + "'. Must be one of: " +
327                     "px, dlu, pt, mm, cm, in");
328         }
329
330         public String JavaDoc toString() {
331             return name;
332         }
333
334         public String JavaDoc abbreviation() {
335             return abbreviation;
336         }
337         
338     }
339     
340     
341 }
Popular Tags