2
* Copyright (c) 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package com.jgoodies.forms.layout;
33
import java.awt.Component;
34
import java.awt.Container;
35
import java.io.Serializable;
36
import java.util.Iterator;
37
import java.util.List;
38
import java.util.Locale;
40
import com.jgoodies.forms.layout.ConstantSize.Unit;
41
import com.jgoodies.forms.util.DefaultUnitConverter;
42
import com.jgoodies.forms.util.UnitConverter;
46
* Consists only of static methods that create and convert sizes
47
* as required by the FormLayout. The conversion of sizes
48
* that are not based on pixel is delegated to an implementation
49
* of {@link UnitConverter}. The conversion methods require the
50
* layout container as parameter to read its current font and resolution.
52
* @author Karsten Lentzsch
53
* @version $Revision: 1.14 $
57
* @see DefaultUnitConverter
59
public final class Sizes {
62
// Common Constant Sizes ************************************************
64
public static final ConstantSize ZERO = pixel(0);
66
public static final ConstantSize DLUX1 = dluX( 1);
67
public static final ConstantSize DLUX2 = dluX( 2);
68
public static final ConstantSize DLUX3 = dluX( 3);
69
public static final ConstantSize DLUX4 = dluX( 4);
70
public static final ConstantSize DLUX5 = dluX( 5);
71
public static final ConstantSize DLUX6 = dluX( 6);
72
public static final ConstantSize DLUX7 = dluX( 7);
73
public static final ConstantSize DLUX8 = dluX( 8);
74
public static final ConstantSize DLUX9 = dluX( 9);
75
public static final ConstantSize DLUX11 = dluX(11);
76
public static final ConstantSize DLUX14 = dluX(14);
78
* 21 horizontal dialog units.
81
public static final ConstantSize DLUX21 = dluX(21);
83
public static final ConstantSize DLUY1 = dluY( 1);
84
public static final ConstantSize DLUY2 = dluY( 2);
85
public static final ConstantSize DLUY3 = dluY( 3);
86
public static final ConstantSize DLUY4 = dluY( 4);
87
public static final ConstantSize DLUY5 = dluY( 5);
88
public static final ConstantSize DLUY6 = dluY( 6);
89
public static final ConstantSize DLUY7 = dluY( 7);
90
public static final ConstantSize DLUY8 = dluY( 8);
91
public static final ConstantSize DLUY9 = dluY( 9);
92
public static final ConstantSize DLUY11 = dluY(11);
93
public static final ConstantSize DLUY14 = dluY(14);
95
* 21 vertical dialog units.
98
public static final ConstantSize DLUY21 = dluY(21);
101
// Static Component Sizes ***********************************************
104
* Use the maximum of all component minimum sizes as column or row size.
106
public static final ComponentSize MINIMUM = new ComponentSize("minimum");
109
* Use the maximum of all component preferred sizes as column or row size.
111
public static final ComponentSize PREFERRED = new ComponentSize("preferred");
114
* Use the maximum of all component sizes as column or row size;
115
* measures preferred sizes when asked for the preferred size
116
* and minimum sizes when asked for the minimum size.
118
public static final ComponentSize DEFAULT = new ComponentSize("default");
121
* An array of all enumeration values used to canonicalize
122
* deserialized component sizes.
124
private static final ComponentSize[] VALUES =
125
{ MINIMUM, PREFERRED, DEFAULT};
128
// Singleton State *******************************************************
131
* Holds the current converter that maps non-pixel sizes to pixels.
133
* @see #setUnitConverter(UnitConverter)
135
private static UnitConverter unitConverter;
139
* Holds the Unit that is used if no Unit is provided in encoded
142
* @see #setDefaultUnit(Unit)
144
private static Unit defaultUnit = ConstantSize.PIXEL;
147
// Instance Creation ******************************************************
150
// Suppresses default constructor, prevents instantiation.
154
// Creation of Size Instances *********************************************
157
* Creates and returns an instance of <code>ConstantSize</code> from the
158
* given encoded size and unit description.
160
* @param encodedValueAndUnit value and unit in string representation
161
* @param horizontal true for horizontal, false for vertical
162
* @return a <code>ConstantSize</code> for the given value and unit
164
public static ConstantSize constant(String encodedValueAndUnit,
165
boolean horizontal) {
166
String lowerCase = encodedValueAndUnit.toLowerCase(Locale.ENGLISH);
167
String trimmed = lowerCase.trim();
168
return ConstantSize.valueOf(trimmed, horizontal);
172
* Creates and returns a ConstantSize for the specified value
173
* in horizontal dialog units.
175
* @param value size value in horizontal dialog units
176
* @return the associated <code>ConstantSize</code>
178
public static ConstantSize dluX(int value) {
179
return ConstantSize.dluX(value);
183
* Creates and returns a ConstantSize for the specified value
184
* in vertical dialog units.
186
* @param value size value in vertical dialog units
187
* @return the associated <code>ConstantSize</code>
189
public static ConstantSize dluY(int value) {
190
return ConstantSize.dluY(value);
194
* Creates and returns a ConstantSize
195
* for the specified pixel value.
197
* @param value value in pixel
198
* @return the associated <code>ConstantSize</code>
200
public static ConstantSize pixel(int value) {
201
return new ConstantSize(value, ConstantSize.PIXEL);
205
* Creates and returns a BoundedSize for the given basis
206
* using the specified lower and upper bounds.
208
* @param basis the base size
209
* @param lowerBound the lower bound size
210
* @param upperBound the upper bound size
211
* @return a <code>BoundedSize</code> for the given basis and bounds
212
* @throws NullPointerException if {@code basis} is {@code null},
213
* or if both {@code lowerBound} and {@code upperBound} are {@code null}.
215
public static Size bounded(Size basis, Size lowerBound, Size upperBound) {
216
return new BoundedSize(basis, lowerBound, upperBound);
220
// Unit Conversion ******************************************************
223
* Converts Inches and returns pixels using the specified resolution.
225
* @param in the Inches
226
* @param component the component that provides the graphics object
227
* @return the given Inches as pixels
229
public static int inchAsPixel(double in, Component component) {
232
: getUnitConverter().inchAsPixel(in, component);
236
* Converts Millimeters and returns pixels using the resolution of the
237
* given component's graphics object.
239
* @param mm Millimeters
240
* @param component the component that provides the graphics object
241
* @return the given Millimeters as pixels
243
public static int millimeterAsPixel(double mm, Component component) {
246
: getUnitConverter().millimeterAsPixel(mm, component);
250
* Converts Centimeters and returns pixels using the resolution of the
251
* given component's graphics object.
253
* @param cm Centimeters
254
* @param component the component that provides the graphics object
255
* @return the given Centimeters as pixels
257
public static int centimeterAsPixel(double cm, Component component) {
260
: getUnitConverter().centimeterAsPixel(cm, component);
264
* Converts DTP Points and returns pixels using the resolution of the
265
* given component's graphics object.
267
* @param pt DTP Points
268
* @param component the component that provides the graphics object
269
* @return the given Points as pixels
271
public static int pointAsPixel(int pt, Component component) {
274
: getUnitConverter().pointAsPixel(pt, component);
278
* Converts horizontal dialog units and returns pixels.
279
* Honors the resolution, dialog font size, platform, and l&f.
281
* @param dluX the horizontal dialog units
282
* @param component the component that provides the graphics object
283
* @return the given horizontal dialog units as pixels
285
public static int dialogUnitXAsPixel(int dluX, Component component) {
288
: getUnitConverter().dialogUnitXAsPixel(dluX, component);
292
* Converts vertical dialog units and returns pixels.
293
* Honors the resolution, dialog font size, platform, and l&f.
295
* @param dluY the vertical dialog units
296
* @param component the component that provides the graphics object
297
* @return the given vertical dialog units as pixels
299
public static int dialogUnitYAsPixel(int dluY, Component component) {
302
: getUnitConverter().dialogUnitYAsPixel(dluY, component);
306
// Accessing the Unit Converter *******************************************
309
* Returns the current {@link UnitConverter}. If it has not been initialized
310
* before it will get an instance of {@link DefaultUnitConverter}.
312
* @return the current <code>UnitConverter</code>
314
public static UnitConverter getUnitConverter() {
315
if (unitConverter == null) {
316
unitConverter = DefaultUnitConverter.getInstance();
318
return unitConverter;
322
* Sets a new UnitConverter that will be used to convert
323
* font-dependent sizes to pixel sizes.
325
* @param newUnitConverter the unit converter to be set
327
public static void setUnitConverter(UnitConverter newUnitConverter) {
328
unitConverter = newUnitConverter;
332
// Default Unit ***********************************************************
335
* Returns the Unit that is used if an encoded ConstantSize contains
338
* @return the Unit if no unit string is provided
342
public static Unit getDefaultUnit() {
348
* Sets the Unit that shall be used if an encoded ConstantSize
349
* provides no unit string.
351
* @param unit the new default Unit, {@code null} for dialog units
353
* @throws IllegalArgumentException if {@code unit} is
354
* {@link ConstantSize#DLUX} or {@link ConstantSize#DLUY}.
358
public static void setDefaultUnit(Unit unit) {
359
if ((unit == ConstantSize.DLUX) || (unit == ConstantSize.DLUY)) {
360
throw new IllegalArgumentException(
361
"The unit must not be DLUX or DLUY. "
362
+ "To use DLU as default unit, invoke this method with null.");
368
// Helper Class *********************************************************
371
* An ordinal-based serializable typesafe enumeration that implements
372
* the {@link Size} interface for the component sizes:
373
* <em>min, pref, default</em>.
375
static final class ComponentSize implements Size, Serializable {
377
private final transient String name;
379
private ComponentSize(String name) {
384
* Returns an instance of <code>ComponentSize</code> that corresponds
385
* to the specified string.
386
* @param str the encoded component size
387
* @return the corresponding ComponentSize or null if none matches
389
static ComponentSize valueOf(String str) {
390
if (str.equals("m") || str.equals("min"))
392
if (str.equals("p") || str.equals("pref"))
394
if (str.equals("d") || str.equals("default"))
400
* Computes the maximum size for the given list of components, using
401
* this form spec and the specified measure.
403
* Invoked by FormLayout to determine the size of one of my elements
405
* @param container the layout container
406
* @param components the list of components to measure
407
* @param minMeasure the measure used to determine the minimum size
408
* @param prefMeasure the measure used to determine the preferred size
409
* @param defaultMeasure the measure used to determine the default size
410
* @return the maximum size in pixels for the given list of components
412
public int maximumSize(
415
FormLayout.Measure minMeasure,
416
FormLayout.Measure prefMeasure,
417
FormLayout.Measure defaultMeasure) {
419
FormLayout.Measure measure = this == MINIMUM
421
: (this == PREFERRED ? prefMeasure : defaultMeasure);
423
for (Iterator i = components.iterator(); i.hasNext();) {
424
Component c = (Component) i.next();
425
maximum = Math.max(maximum, measure.sizeOf(c));
431
* Describes if this Size can be compressed, if container space gets scarce.
432
* Used by the FormLayout size computations in <code>#compressedSizes</code>
433
* to check whether a column or row can be compressed or not.<p>
435
* The DEFAULT ComponentSize is compressible, MINIMUM and PREFERRED
436
* are incompressible.
438
* @return <code>true</code> for the DEFAULT size,
439
* <code>false</code> otherwise
443
public boolean compressible() {
444
return this == DEFAULT;
448
public String toString() {
454
* Returns a parseable string representation of this ComponentSize.
456
* @return a String that can be parsed by the Forms parser
460
public String encode() {
461
return name.substring(0, 1);
465
// Serialization *****************************************************
467
private static int nextOrdinal = 0;
469
private final int ordinal = nextOrdinal++;
471
private Object readResolve() {
472
return VALUES[ordinal]; // Canonicalize