Class DefaultUnitConverter
- All Implemented Interfaces:
com.jgoodies.common.bean.ObservableBean
,com.jgoodies.common.bean.ObservableBean2
,UnitConverter
,Serializable
UnitConverter
interface.
It converts horizontal and vertical dialog base units to pixels.The horizontal base unit is equal to the average width, in pixels, of the characters in the system font; the vertical base unit is equal to the height, in pixels, of the font. Each horizontal base unit is equal to 4 horizontal dialog units; each vertical base unit is equal to 8 vertical dialog units.
The DefaultUnitConverter computes dialog base units using a default font and a test string for the average character width. You can configure the font and the test string via the bound Bean properties defaultDialogFont and averageCharacterWidthTestString. See also Microsoft's suggestion for a custom computation custom computation. More information how to use dialog units in screen design can be found in Microsoft's Design Specifications and Guidelines.
Since the Forms 1.1 this converter logs font information at
the CONFIG
level.
- Version:
- $Revision: 1.23 $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Describes horizontal and vertical dialog base units. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
Holds the string that is used to compute the average character width.static final String
private Font
Holds a cached default dialog font that is used as fallback, if no default dialog font has been set.Holds the horizontal dialog base units that are valid for the FontMetrics stored incachedFontMetrics
.private FontMetrics
Holds the FontMetrics used to compute the per-component dialog units.Holds the lazily created cached global dialog base units that are used if a component is not (yet) available - for example in a Border.private Font
Holds a custom font that is used to compute the global dialog base units.private static DefaultUnitConverter
Holds the sole instance that will be lazily instantiated.private static final Logger
static final String
static final String
static final String
static final String
Fields inherited from class com.jgoodies.common.bean.Bean
changeSupport
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Constructs a DefaultUnitConverter and registers a listener that handles changes in the look&feel. -
Method Summary
Modifier and TypeMethodDescription(package private) void
Invalidates the caches.computeDialogBaseUnits
(FontMetrics metrics) Computes and returns the horizontal dialog base units.Computes the global dialog base units.private static Component
Creates and returns a component that is used to lookup the default font metrics.Returns the string used to compute the average character width.private Font
Lazily creates and returns a fallback for the dialog font that is used to compute the dialog base units.Returns the dialog font that is used to compute the dialog base units.Looks up and returns the dialog base units for the given component.protected double
getDialogBaseUnitsX
(Component component) Returns the cached or computed horizontal dialog base units.protected double
getDialogBaseUnitsY
(Component component) Returns the cached or computed vertical dialog base units for the given component.Lazily computes and answer the global dialog base units.static DefaultUnitConverter
Lazily instantiates and returns the sole instance.private static Font
Looks up and returns the font used by buttons.void
setAverageCharacterWidthTestString
(String newTestString) Sets a string that will be used to compute the average character width.void
setDefaultDialogFont
(Font newFont) Sets a dialog font that will be used to compute the dialog base units.Methods inherited from class com.jgoodies.forms.util.AbstractUnitConverter
centimeterAsPixel, centimeterAsPixel, computeAverageCharWidth, dialogUnitXAsPixel, dialogUnitXAsPixel, dialogUnitYAsPixel, dialogUnitYAsPixel, getDefaultScreenResolution, getScreenResolution, inchAsPixel, inchAsPixel, millimeterAsPixel, millimeterAsPixel, pointAsPixel, pointAsPixel
Methods inherited from class com.jgoodies.common.bean.Bean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, createPropertyChangeSupport, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, fireMultiplePropertiesChanged, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
Field Details
-
PROPERTY_AVERAGE_CHARACTER_WIDTH_TEST_STRING
- See Also:
-
PROPERTY_DEFAULT_DIALOG_FONT
- See Also:
-
OLD_AVERAGE_CHARACTER_TEST_STRING
- Since:
- 1.6
- See Also:
-
MODERN_AVERAGE_CHARACTER_TEST_STRING
- Since:
- 1.4
- See Also:
-
BALANCED_AVERAGE_CHARACTER_TEST_STRING
- Since:
- 1.4
- See Also:
-
LOGGER
-
instance
Holds the sole instance that will be lazily instantiated. -
averageCharWidthTestString
Holds the string that is used to compute the average character width. Since 1.6 the default value is the balanced average character test string, where it was just "X" before. -
defaultDialogFont
Holds a custom font that is used to compute the global dialog base units. If not set, a fallback font is is lazily created in method #getCachedDefaultDialogFont, which in turn looks up a font in method #lookupDefaultDialogFont. -
cachedGlobalDialogBaseUnits
Holds the lazily created cached global dialog base units that are used if a component is not (yet) available - for example in a Border. -
cachedDialogBaseUnits
Holds the horizontal dialog base units that are valid for the FontMetrics stored incachedFontMetrics
. -
cachedFontMetrics
Holds the FontMetrics used to compute the per-component dialog units. The latter are valid, if a FontMetrics equals this stored metrics. -
cachedDefaultDialogFont
Holds a cached default dialog font that is used as fallback, if no default dialog font has been set.
-
-
Constructor Details
-
DefaultUnitConverter
private DefaultUnitConverter()Constructs a DefaultUnitConverter and registers a listener that handles changes in the look&feel.
-
-
Method Details
-
getInstance
Lazily instantiates and returns the sole instance.- Returns:
- the lazily instantiated sole instance
-
getAverageCharacterWidthTestString
Returns the string used to compute the average character width. By default it is initialized toBALANCED_AVERAGE_CHARACTER_TEST_STRING
.- Returns:
- the test string used to compute the average character width
-
setAverageCharacterWidthTestString
Sets a string that will be used to compute the average character width. By default it is initialized toBALANCED_AVERAGE_CHARACTER_TEST_STRING
. You can provide other test strings, for example:- "Xximeee"
- "ABCEDEFHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- Parameters:
newTestString
- the test string to be used- Throws:
NullPointerException
- ifnewTestString
isnull
IllegalArgumentException
- ifnewTestString
is empty or whitespace
-
getDefaultDialogFont
Returns the dialog font that is used to compute the dialog base units. If a default dialog font has been set usingsetDefaultDialogFont(Font)
, this font will be returned. Otherwise a cached fallback will be lazily created.- Returns:
- the font used to compute the dialog base units
-
setDefaultDialogFont
Sets a dialog font that will be used to compute the dialog base units.- Parameters:
newFont
- the default dialog font to be set
-
getDialogBaseUnitsX
Returns the cached or computed horizontal dialog base units.- Specified by:
getDialogBaseUnitsX
in classAbstractUnitConverter
- Parameters:
component
- a Component that provides the font and graphics- Returns:
- the horizontal dialog base units
-
getDialogBaseUnitsY
Returns the cached or computed vertical dialog base units for the given component.- Specified by:
getDialogBaseUnitsY
in classAbstractUnitConverter
- Parameters:
component
- a Component that provides the font and graphics- Returns:
- the vertical dialog base units
-
getGlobalDialogBaseUnits
Lazily computes and answer the global dialog base units. Should be re-computed if the l&f, platform, or screen changes.- Returns:
- a cached DialogBaseUnits object used globally if no container is available
-
getDialogBaseUnits
Looks up and returns the dialog base units for the given component. In case the component isnull
the global dialog base units are answered.Before we compute the dialog base units we check whether they have been computed and cached before - for the same component
FontMetrics
.- Parameters:
c
- the component that provides the graphics object- Returns:
- the DialogBaseUnits object for the given component
-
computeDialogBaseUnits
Computes and returns the horizontal dialog base units. Honors the font, font size and resolution.Implementation Note: 14dluY map to 22 pixel for 8pt Tahoma on 96 dpi. I could not yet manage to compute the Microsoft compliant font height. Therefore this method adds a correction value that seems to work well with the vast majority of desktops.
TODO: Revise the computation of vertical base units as soon as there are more information about the original computation in Microsoft environments.
- Parameters:
metrics
- the FontMetrics used to measure the dialog font- Returns:
- the horizontal and vertical dialog base units
-
computeGlobalDialogBaseUnits
Computes the global dialog base units. The current implementation assumes a fixed 8pt font and on 96 or 120 dpi. A better implementation should ask for the main dialog font and should honor the current screen resolution.Should be re-computed if the l&f, platform, or screen changes.
- Returns:
- a DialogBaseUnits object used globally if no container is available
-
getCachedDefaultDialogFont
Lazily creates and returns a fallback for the dialog font that is used to compute the dialog base units. This fallback font is cached and will be reset if the L&F changes.- Returns:
- the cached fallback font used to compute the dialog base units
-
lookupDefaultDialogFont
Looks up and returns the font used by buttons. First, tries to request the button font from the UIManager; if this fails a JButton is created and asked for its font.- Returns:
- the font used for a standard button
-
createDefaultGlobalComponent
Creates and returns a component that is used to lookup the default font metrics. The current implementation creates aJPanel
. Since this panel has no parent, it has no toolkit assigned. And so, requesting the font metrics will end up using the default toolkit and its deprecated methodToolKit#getFontMetrics()
.TODO: Consider publishing this method and providing a setter, so that an API user can set a realized component that has a toolkit assigned.
- Returns:
- a component used to compute the default font metrics
-
clearCache
void clearCache()Invalidates the caches. Resets the global dialog base units, clears the Map fromFontMetrics
to dialog base units, and resets the fallback for the default dialog font. This is invoked after a change of the look&feel.
-