public final class DefaultUnitConverter extends AbstractUnitConverter
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.
UnitConverter,
Size,
Sizes,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
private static class |
DefaultUnitConverter.DialogBaseUnits
Describes horizontal and vertical dialog base units.
|
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
averageCharWidthTestString
Holds the string that is used to compute the average character width.
|
static java.lang.String |
BALANCED_AVERAGE_CHARACTER_TEST_STRING |
private java.awt.Font |
cachedDefaultDialogFont
Holds a cached default dialog font that is used as fallback,
if no default dialog font has been set.
|
private DefaultUnitConverter.DialogBaseUnits |
cachedDialogBaseUnits
Holds the horizontal dialog base units that are valid
for the FontMetrics stored in
cachedFontMetrics. |
private java.awt.FontMetrics |
cachedFontMetrics
Holds the FontMetrics used to compute the per-component dialog units.
|
private DefaultUnitConverter.DialogBaseUnits |
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.
|
private java.awt.Font |
defaultDialogFont
Holds a custom font that is used to compute the global dialog base units.
|
private static DefaultUnitConverter |
instance
Holds the sole instance that will be lazily instantiated.
|
private static java.util.logging.Logger |
LOGGER |
static java.lang.String |
MODERN_AVERAGE_CHARACTER_TEST_STRING |
static java.lang.String |
OLD_AVERAGE_CHARACTER_TEST_STRING |
static java.lang.String |
PROPERTY_AVERAGE_CHARACTER_WIDTH_TEST_STRING |
static java.lang.String |
PROPERTY_DEFAULT_DIALOG_FONT |
| Modifier | Constructor and Description |
|---|---|
private |
DefaultUnitConverter()
Constructs a DefaultUnitConverter and registers
a listener that handles changes in the look&feel.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
clearCache()
Invalidates the caches.
|
private DefaultUnitConverter.DialogBaseUnits |
computeDialogBaseUnits(java.awt.FontMetrics metrics)
Computes and returns the horizontal dialog base units.
|
private DefaultUnitConverter.DialogBaseUnits |
computeGlobalDialogBaseUnits()
Computes the global dialog base units.
|
private static java.awt.Component |
createDefaultGlobalComponent()
Creates and returns a component that is used to lookup the default
font metrics.
|
java.lang.String |
getAverageCharacterWidthTestString()
Returns the string used to compute the average character width.
|
private java.awt.Font |
getCachedDefaultDialogFont()
Lazily creates and returns a fallback for the dialog font
that is used to compute the dialog base units.
|
java.awt.Font |
getDefaultDialogFont()
Returns the dialog font that is used to compute the dialog base units.
|
private DefaultUnitConverter.DialogBaseUnits |
getDialogBaseUnits(java.awt.Component c)
Looks up and returns the dialog base units for the given component.
|
protected double |
getDialogBaseUnitsX(java.awt.Component component)
Returns the cached or computed horizontal dialog base units.
|
protected double |
getDialogBaseUnitsY(java.awt.Component component)
Returns the cached or computed vertical dialog base units
for the given component.
|
private DefaultUnitConverter.DialogBaseUnits |
getGlobalDialogBaseUnits()
Lazily computes and answer the global dialog base units.
|
static DefaultUnitConverter |
getInstance()
Lazily instantiates and returns the sole instance.
|
private static java.awt.Font |
lookupDefaultDialogFont()
Looks up and returns the font used by buttons.
|
void |
setAverageCharacterWidthTestString(java.lang.String newTestString)
Sets a string that will be used to compute the average character width.
|
void |
setDefaultDialogFont(java.awt.Font newFont)
Sets a dialog font that will be used to compute the dialog base units.
|
centimeterAsPixel, centimeterAsPixel, computeAverageCharWidth, dialogUnitXAsPixel, dialogUnitXAsPixel, dialogUnitYAsPixel, dialogUnitYAsPixel, getDefaultScreenResolution, getScreenResolution, inchAsPixel, inchAsPixel, millimeterAsPixel, millimeterAsPixel, pointAsPixel, pointAsPixeladdPropertyChangeListener, 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, removeVetoableChangeListenerpublic static final java.lang.String PROPERTY_AVERAGE_CHARACTER_WIDTH_TEST_STRING
public static final java.lang.String PROPERTY_DEFAULT_DIALOG_FONT
public static final java.lang.String OLD_AVERAGE_CHARACTER_TEST_STRING
public static final java.lang.String MODERN_AVERAGE_CHARACTER_TEST_STRING
public static final java.lang.String BALANCED_AVERAGE_CHARACTER_TEST_STRING
private static final java.util.logging.Logger LOGGER
private static DefaultUnitConverter instance
private java.lang.String averageCharWidthTestString
private java.awt.Font defaultDialogFont
private DefaultUnitConverter.DialogBaseUnits cachedGlobalDialogBaseUnits
private DefaultUnitConverter.DialogBaseUnits cachedDialogBaseUnits
cachedFontMetrics.private java.awt.FontMetrics cachedFontMetrics
private java.awt.Font cachedDefaultDialogFont
getDefaultDialogFont(),
setDefaultDialogFont(Font)private DefaultUnitConverter()
public static DefaultUnitConverter getInstance()
public java.lang.String getAverageCharacterWidthTestString()
BALANCED_AVERAGE_CHARACTER_TEST_STRING.public void setAverageCharacterWidthTestString(java.lang.String newTestString)
BALANCED_AVERAGE_CHARACTER_TEST_STRING. You can provide
other test strings, for example:
newTestString - the test string to be usedjava.lang.NullPointerException - if newTestString is nulljava.lang.IllegalArgumentException - if newTestString is empty or whitespacepublic java.awt.Font getDefaultDialogFont()
setDefaultDialogFont(Font), this font will be returned.
Otherwise a cached fallback will be lazily created.public void setDefaultDialogFont(java.awt.Font newFont)
newFont - the default dialog font to be setprotected double getDialogBaseUnitsX(java.awt.Component component)
getDialogBaseUnitsX in class AbstractUnitConvertercomponent - a Component that provides the font and graphicsprotected double getDialogBaseUnitsY(java.awt.Component component)
getDialogBaseUnitsY in class AbstractUnitConvertercomponent - a Component that provides the font and graphicsprivate DefaultUnitConverter.DialogBaseUnits getGlobalDialogBaseUnits()
private DefaultUnitConverter.DialogBaseUnits getDialogBaseUnits(java.awt.Component c)
null 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.
c - the component that provides the graphics objectprivate DefaultUnitConverter.DialogBaseUnits computeDialogBaseUnits(java.awt.FontMetrics metrics)
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.
metrics - the FontMetrics used to measure the dialog fontprivate DefaultUnitConverter.DialogBaseUnits computeGlobalDialogBaseUnits()
Should be re-computed if the l&f, platform, or screen changes.
private java.awt.Font getCachedDefaultDialogFont()
private static java.awt.Font lookupDefaultDialogFont()
private static java.awt.Component createDefaultGlobalComponent()
JPanel.
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 method ToolKit#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.
void clearCache()
FontMetrics to dialog base units,
and resets the fallback for the default dialog font.
This is invoked after a change of the look&feel.