17
Note: Not all aliased keys use the same name in each class. For instance, FontFamily in StyledConstants is
an alias of Family in StyledConstants.CharacterConstants. However, Family in
StyledConstants.CharacterConstants is an alias of Family (the actual key) in
StyledConstants.FontConstants. Each is a reference to the same key object and it makes no
difference which one we use.
Most keys are self-explanatory in meaning. The StyleConstants API documentation page contains a
helpful diagram illustrating the meaning of some of the less self-explanitory attribute keys that apply to
paragraphs of styled text. (Each of the keys illustrated in this diagram is an alias of the actual key defined in
StyleConstants.ParagraphConstants.)
StyleConstants also defines static methods for assigning and retrieving many predefined attributes in an
AttributeSet. For example, to assign a specific font family attribute to an AttributeSet (assuming it is
mutable), we can use StyleConstants’ setFontFamily() method.
19.1.16 StyleContext
class javax.swing.text.StyleContext
StyleContext implements the AbstractDocument.AttributeContext interface, and declares a set
of methods used to modify or fetch new instances of AttributeSet implementations. AbstractContext
was designed with the understanding that the implementor may use more than one type of AttributeSet
implementation to store sets of attributes. The decision to use one type over another may be based on any
number of factors, and StyleContext takes full advantage of this design.
StyleContext‘s main role is to act as a container for Styles that may be used by one or more
DefaultStyledDocuments. It maintains a private NamedStyle instance used to store its Styles and
allow access by name. Each of these contained Styles is also an instance of NamedStyle. So, to clarify,
StyleContext maintains a NamedStyle instance whose key/value pairs are of the form
String/NamedStyle.
StyleContext also maintains a subset of these NamedStyle values in a Hashtable. Only those
NamedStyle’s whose AttributeSet contains 9 or less attributes are stored in this Hashtable and their
AttributeSets are maintained as instances of SmallAttributeSet. Those NamedStyles with an
AttributeSet containing 10 or more attributes are not stored in the Hashtable, and their
AttributeSets are maintained as instances of SimpleAttributeSet.
This partitioning is managed dynamically by StyleContext, and is the result of combining the
AbstractContext design with the use of a compression threshold (a hard-coded int value of 9).
Whenever an attribute is added or removed, StyleContext checks the number of attributes in the target
AttributeSet. If the resulting set will contain 9 or less attributes it remains, or is converted to, a
SmallAttributeSet and is added to the Hashtable if it wasn’t already there. If the resulting set will
contain 10 or more attributes it remains, or is converted to, a SimpleAttributeSet and is removed from
the Hashtable if it was already there.
The reason for this partitioning is to support efficient AttributeSet sharing. Most styled documents
contain many distinct regions of identically styled text. These regions normally have a small number of
attributes associated with them. It is clear that the best thing to do in this situation is to assign the same
AttributeSet to each of these regions. And the best AttributeSet implementation to use for this is
SmallAttributeSet due to its superior memory efficiency (since look-up speed is a minor issue with a
very small number of attributes). Larger sets of attributes are, in general, rare. The best AttributeSet
implementation to use for this is SimpleAttributeSet due to its superior look-up capabilities (since
memory usage will most likely be a minor issue with a relatively small number of SimpleAttributeSets).