KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import com.jgoodies.forms.util.DefaultUnitConverter;
39 import com.jgoodies.forms.util.UnitConverter;
40
41
42 /**
43  * Consists only of static methods that create and convert sizes
44  * as required by the {@link FormLayout}. The conversion of sizes
45  * that are not based on pixel is delegated to an implementation
46  * of {@link UnitConverter}. The conversion methods require the
47  * layout container as parameter to read its current font and resolution.
48  *
49  * @author Karsten Lentzsch
50  * @version $Revision: 1.4 $
51  * @see Size
52  * @see UnitConverter
53  * @see DefaultUnitConverter
54  */

55 public final class Sizes {
56     
57     
58     // Common Constant Sizes ************************************************
59

60     public static final ConstantSize ZERO = pixel(0);
61     
62     public static final ConstantSize DLUX1 = dluX( 1);
63     public static final ConstantSize DLUX2 = dluX( 2);
64     public static final ConstantSize DLUX3 = dluX( 3);
65     public static final ConstantSize DLUX4 = dluX( 4);
66     public static final ConstantSize DLUX7 = dluX( 7);
67     public static final ConstantSize DLUX9 = dluX( 9);
68     public static final ConstantSize DLUX11 = dluX(11);
69     public static final ConstantSize DLUX14 = dluX(14);
70
71     public static final ConstantSize DLUY1 = dluY( 1);
72     public static final ConstantSize DLUY2 = dluY( 2);
73     public static final ConstantSize DLUY3 = dluY( 3);
74     public static final ConstantSize DLUY4 = dluY( 4);
75     public static final ConstantSize DLUY6 = dluY( 6);
76     public static final ConstantSize DLUY7 = dluY( 7);
77     public static final ConstantSize DLUY9 = dluY( 9);
78     public static final ConstantSize DLUY11 = dluY(11);
79     public static final ConstantSize DLUY14 = dluY(14);
80     
81
82     // Static Component Sizes ***********************************************
83

84     /**
85      * Use the maximum of all component minimum sizes as column or row size.
86      */

87     public static final ComponentSize MINIMUM = new ComponentSize("minimum");
88
89     /**
90      * Use the maximum of all component preferred sizes as column or row size.
91      */

92     public static final ComponentSize PREFERRED = new ComponentSize("preferred");
93
94     /**
95      * Use the maximum of all component sizes as column or row size;
96      * measures preferred sizes when asked for the preferred size
97      * and minimum sizes when asked for the minimum size.
98      */

99     public static final ComponentSize DEFAULT = new ComponentSize("default");
100
101
102     // Singleton State *******************************************************
103

104     /**
105      * Holds the current converter that maps non-pixel sizes to pixels.
106      */

107     private static UnitConverter unitConverter;
108     
109     
110     // Instance Creation ******************************************************
111

112     private Sizes() {
113         // Suppresses default constructor, ensuring non-instantiability.
114
}
115
116
117     // Creation of Size Instances *********************************************
118

119     /**
120      * Creates and answers an instance of <code>ConstantSize</code> from the
121      * given encoded size and unit description.
122      *
123      * @param encodedValueAndUnit value and unit in string representation
124      * @param horizontal true for horizontal, false for vertical
125      * @return a <code>ConstantSize</code> for the given value and unit
126      */

127     public static ConstantSize constant(String JavaDoc encodedValueAndUnit,
128                                          boolean horizontal) {
129         return ConstantSize.valueOf(encodedValueAndUnit, horizontal);
130     }
131         
132     /**
133      * Answers an instance of <code>Size</code> for the specified value
134      * in horizontal dialog units.
135      *
136      * @param value size value in horizontal dialog units
137      * @return the associated <code>ConstantSize</code>
138      */

139     public static ConstantSize dluX(int value) {
140         return ConstantSize.dluX(value);
141     }
142     
143     /**
144      * Answers an instance of <code>Size</code> for the specified value
145      * in vertical dialog units.
146      *
147      * @param value size value in vertical dialog units
148      * @return the associated <code>ConstantSize</code>
149      */

150     public static ConstantSize dluY(int value) {
151         return ConstantSize.dluY(value);
152     }
153     
154     /**
155      * Answers an instance of <code>Size</code> for the specified pixel value.
156      *
157      * @param value value in pixel
158      * @return the associated <code>ConstantSize</code>
159      */

160     public static ConstantSize pixel(int value) {
161         return new ConstantSize(value, ConstantSize.PIXEL);
162     }
163     
164     /**
165      * Creates and answers a <code>BoundedSize</code> for the given basis
166      * using the specified lower and upper bounds.
167      *
168      * @param basis the base size
169      * @param lowerBound the lower bound size
170      * @param upperBound the upper bound size
171      * @return a <code>BoundedSize</code> for the given basis and bounds
172      * @throws NullPointerException if basis is null
173      */

174     public static Size bounded(Size basis, Size lowerBound, Size upperBound) {
175         return new BoundedSize(basis, lowerBound, upperBound);
176     }
177     
178
179     // Unit Conversion ******************************************************
180

181     /**
182      * Converts Inches and answers pixels using the specified resolution.
183      *
184      * @param in the Inches
185      * @param component the component that provides the graphics object
186      * @return the given Inches as pixels
187      */

188     public static int inchAsPixel(double in, Component JavaDoc component) {
189         return getUnitConverter().inchAsPixel(in, component);
190     }
191
192     /**
193      * Converts Millimeters and answers pixels using the resolution of the
194      * given component's graphics object.
195      *
196      * @param mm Millimeters
197      * @param component the component that provides the graphics object
198      * @return the given Millimeters as pixels
199      */

200     public static int millimeterAsPixel(double mm, Component JavaDoc component) {
201         return getUnitConverter().millimeterAsPixel(mm, component);
202     }
203
204     /**
205      * Converts Centimeters and answers pixels using the resolution of the
206      * given component's graphics object.
207      *
208      * @param cm Centimeters
209      * @param component the component that provides the graphics object
210      * @return the given Centimeters as pixels
211      */

212     public static int centimeterAsPixel(double cm, Component JavaDoc component) {
213         return getUnitConverter().centimeterAsPixel(cm, component);
214     }
215
216     /**
217      * Converts DTP Points and answers pixels using the resolution of the
218      * given component's graphics object.
219      *
220      * @param pt DTP Points
221      * @param component the component that provides the graphics object
222      * @return the given Points as pixels
223      */

224     public static int pointAsPixel(int pt, Component JavaDoc component) {
225         return getUnitConverter().pointAsPixel(pt, component);
226     }
227     
228     /**
229      * Converts horizontal dialog units and answers pixels.
230      * Honors the resolution, dialog font size, platform, and l&amp;f.
231      *
232      * @param dluX the horizontal dialog units
233      * @param component the component that provides the graphics object
234      * @return the given horizontal dialog units as pixels
235      */

236     public static int dialogUnitXAsPixel(int dluX, Component JavaDoc component) {
237         return getUnitConverter().dialogUnitXAsPixel(dluX, component);
238     }
239
240     /**
241      * Converts vertical dialog units and answers pixels.
242      * Honors the resolution, dialog font size, platform, and l&amp;f.
243      *
244      * @param dluY the vertical dialog units
245      * @param component the component that provides the graphics object
246      * @return the given vertical dialog units as pixels
247      */

248     public static int dialogUnitYAsPixel(int dluY, Component JavaDoc component) {
249         return getUnitConverter().dialogUnitYAsPixel(dluY, component);
250     }
251     
252     
253     // Accessing the Unit Converter *******************************************
254

255     /**
256      * Returns the current {@link UnitConverter}. If it has not been initialized
257      * before it will get an instance of {@link DefaultUnitConverter}.
258      */

259     public static UnitConverter getUnitConverter() {
260         if (unitConverter == null) {
261             unitConverter = DefaultUnitConverter.getInstance();
262         }
263         return unitConverter;
264     }
265     
266     /**
267      * Sets a new {@link UnitConverter} that will be used to convert
268      * font-dependent sizes to pixel sizes.
269      *
270      * @param newUnitConverter the unit converter to be set
271      */

272     public static void setUnitConverter(UnitConverter newUnitConverter) {
273         unitConverter = newUnitConverter;
274     }
275
276
277     // Helper Class *********************************************************
278

279     /**
280      * A {@link Size} interface implementation for the component sizes.
281      */

282     static final class ComponentSize implements Size {
283         
284         private final String JavaDoc name;
285
286         private ComponentSize(String JavaDoc name) {
287             this.name = name;
288         }
289
290         /**
291          * Answers an instance of <code>ComponentSize</code> that corresponds
292          * to the specified string.
293          * @param str the encoded component size
294          * @return the corresponding ComponentSize or null if none matches
295          */

296         static ComponentSize valueOf(String JavaDoc str) {
297             if (str.equals("m") || str.equals("min"))
298                 return MINIMUM;
299             if (str.equals("p") || str.equals("pref"))
300                 return PREFERRED;
301             if (str.equals("d") || str.equals("default"))
302                 return DEFAULT;
303             else
304                 return null;
305         }
306         
307         /**
308          * Computes the maximum size for the given list of components, using
309          * this form spec and the specified measure.
310          * <p>
311          * Invoked by FormLayout to determine the size of one of my elements
312          *
313          * @return the maximum size for the given list of components
314          */

315         public int maximumSize(
316             Container JavaDoc container,
317             List JavaDoc components,
318             FormLayout.Measure minMeasure,
319             FormLayout.Measure prefMeasure,
320             FormLayout.Measure defaultMeasure) {
321                 
322             FormLayout.Measure measure = this == MINIMUM
323                     ? minMeasure
324                     : (this == PREFERRED ? prefMeasure : defaultMeasure);
325             int maximum = 0;
326             for (Iterator JavaDoc i = components.iterator(); i.hasNext();) {
327                 Component JavaDoc c = (Component JavaDoc) i.next();
328                 maximum = Math.max(maximum, measure.sizeOf(c));
329             }
330             return maximum;
331         }
332
333         public String JavaDoc toString() { return name.substring(0, 1); }
334
335     }
336     
337     
338 }
Popular Tags