- java.lang.Object
- 
- javax.swing.text.StyleContext
 
- 
- All Implemented Interfaces:
- Serializable,- AbstractDocument.AttributeContext
 - Direct Known Subclasses:
- StyleSheet
 
 public class StyleContext extends Object implements Serializable, AbstractDocument.AttributeContext A pool of styles and their associated resources. This class determines the lifetime of a group of resources by being a container that holds caches for various resources such as font and color that get reused by the various style definitions. This can be shared by multiple documents if desired to maximize the sharing of related resources.This class also provides efficient support for small sets of attributes and compresses them by sharing across uses and taking advantage of their immutable nature. Since many styles are replicated, the potential for sharing is significant, and copies can be extremely cheap. Larger sets reduce the possibility of sharing, and therefore revert automatically to a less space-efficient implementation. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beanspackage. Please seeXMLEncoder.- See Also:
- Serialized Form
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description classStyleContext.NamedStyleA collection of attributes, typically used to represent character and paragraph styles.classStyleContext.SmallAttributeSetThis class holds a small number of attributes in an array.
 - 
Field SummaryFields Modifier and Type Field Description static StringDEFAULT_STYLEThe name given to the default logical style attached to paragraphs.
 - 
Constructor SummaryConstructors Constructor Description StyleContext()Creates a new StyleContext object.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AttributeSetaddAttribute(AttributeSet old, Object name, Object value)Adds an attribute to the given set, and returns the new representative set.AttributeSetaddAttributes(AttributeSet old, AttributeSet attr)Adds a set of attributes to the element.voidaddChangeListener(ChangeListener l)Adds a listener to track when styles are added or removed.StyleaddStyle(String nm, Style parent)Adds a new style into the style hierarchy.protected MutableAttributeSetcreateLargeAttributeSet(AttributeSet a)Create a large set of attributes that should trade off space for time.protected StyleContext.SmallAttributeSetcreateSmallAttributeSet(AttributeSet a)Create a compact set of attributes that might be shared.ColorgetBackground(AttributeSet attr)Takes a set of attributes and turn it into a background color specification.ChangeListener[]getChangeListeners()Returns an array of all theChangeListeners added to this StyleContext with addChangeListener().protected intgetCompressionThreshold()Returns the maximum number of key/value pairs to try and compress into unique/immutable sets.static StyleContextgetDefaultStyleContext()Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.AttributeSetgetEmptySet()Fetches an empty AttributeSet.FontgetFont(String family, int style, int size)Gets a new font.FontgetFont(AttributeSet attr)Gets the font from an attribute set.FontMetricsgetFontMetrics(Font f)Returns font metrics for a font.ColorgetForeground(AttributeSet attr)Takes a set of attributes and turn it into a foreground color specification.static ObjectgetStaticAttribute(Object key)Returns the object previously registered withregisterStaticAttributeKey.static ObjectgetStaticAttributeKey(Object key)Returns the String thatkeywill be registered with.StylegetStyle(String nm)Fetches a named style previously added to the documentEnumeration<?>getStyleNames()Fetches the names of the styles defined.voidreadAttributes(ObjectInputStream in, MutableAttributeSet a)Context-specific handling of reading in attributesstatic voidreadAttributeSet(ObjectInputStream in, MutableAttributeSet a)Reads a set of attributes from the given object input stream that have been previously written out withwriteAttributeSet.voidreclaim(AttributeSet a)Returns a set no longer needed by the MutableAttributeSet implementation.static voidregisterStaticAttributeKey(Object key)Registers an object as a static object that is being used as a key in attribute sets.AttributeSetremoveAttribute(AttributeSet old, Object name)Removes an attribute from the set.AttributeSetremoveAttributes(AttributeSet old, Enumeration<?> names)Removes a set of attributes for the element.AttributeSetremoveAttributes(AttributeSet old, AttributeSet attrs)Removes a set of attributes for the element.voidremoveChangeListener(ChangeListener l)Removes a listener that was tracking styles being added or removed.voidremoveStyle(String nm)Removes a named style previously added to the document.StringtoString()Converts a StyleContext to a String.voidwriteAttributes(ObjectOutputStream out, AttributeSet a)Context-specific handling of writing out attributesstatic voidwriteAttributeSet(ObjectOutputStream out, AttributeSet a)Writes a set of attributes to the given object stream for the purpose of serialization.
 
- 
- 
- 
Field Detail- 
DEFAULT_STYLEpublic static final String DEFAULT_STYLE The name given to the default logical style attached to paragraphs.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
getDefaultStyleContextpublic static final StyleContext getDefaultStyleContext() Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.- Returns:
- the context
 
 - 
addStylepublic Style addStyle(String nm, Style parent) Adds a new style into the style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.- Parameters:
- nm- the name of the style (must be unique within the collection of named styles in the document). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
- parent- the parent style. This may be null if unspecified attributes need not be resolved in some other style.
- Returns:
- the created style
 
 - 
removeStylepublic void removeStyle(String nm) Removes a named style previously added to the document.- Parameters:
- nm- the name of the style to remove
 
 - 
getStylepublic Style getStyle(String nm) Fetches a named style previously added to the document- Parameters:
- nm- the name of the style
- Returns:
- the style
 
 - 
getStyleNamespublic Enumeration<?> getStyleNames() Fetches the names of the styles defined.- Returns:
- the list of names as an enumeration
 
 - 
addChangeListenerpublic void addChangeListener(ChangeListener l) Adds a listener to track when styles are added or removed.- Parameters:
- l- the change listener
 
 - 
removeChangeListenerpublic void removeChangeListener(ChangeListener l) Removes a listener that was tracking styles being added or removed.- Parameters:
- l- the change listener
 
 - 
getChangeListenerspublic ChangeListener[] getChangeListeners() Returns an array of all theChangeListeners added to this StyleContext with addChangeListener().- Returns:
- all of the ChangeListeners added or an empty array if no listeners have been added
- Since:
- 1.4
 
 - 
getFontpublic Font getFont(AttributeSet attr) Gets the font from an attribute set. This is implemented to try and fetch a cached font for the given AttributeSet, and if that fails the font features are resolved and the font is fetched from the low-level font cache.- Parameters:
- attr- the attribute set
- Returns:
- the font
 
 - 
getForegroundpublic Color getForeground(AttributeSet attr) Takes a set of attributes and turn it into a foreground color specification. This might be used to specify things like brighter, more hue, etc. By default it simply returns the value specified by the StyleConstants.Foreground attribute.- Parameters:
- attr- the set of attributes
- Returns:
- the color
 
 - 
getBackgroundpublic Color getBackground(AttributeSet attr) Takes a set of attributes and turn it into a background color specification. This might be used to specify things like brighter, more hue, etc. By default it simply returns the value specified by the StyleConstants.Background attribute.- Parameters:
- attr- the set of attributes
- Returns:
- the color
 
 - 
getFontpublic Font getFont(String family, int style, int size) Gets a new font. This returns a Font from a cache if a cached font exists. If not, a Font is added to the cache. This is basically a low-level cache for 1.1 font features.- Parameters:
- family- the font family (such as "Monospaced")
- style- the style of the font (such as Font.PLAIN)
- size- the point size >= 1
- Returns:
- the new font
 
 - 
getFontMetricspublic FontMetrics getFontMetrics(Font f) Returns font metrics for a font.- Parameters:
- f- the font
- Returns:
- the metrics
 
 - 
addAttributepublic AttributeSet addAttribute(AttributeSet old, Object name, Object value) Adds an attribute to the given set, and returns the new representative set.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- addAttributein interface- AbstractDocument.AttributeContext
- Parameters:
- old- the old attribute set
- name- the non-null attribute name
- value- the attribute value
- Returns:
- the updated attribute set
- See Also:
- MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
 
 - 
addAttributespublic AttributeSet addAttributes(AttributeSet old, AttributeSet attr) Adds a set of attributes to the element.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- addAttributesin interface- AbstractDocument.AttributeContext
- Parameters:
- old- the old attribute set
- attr- the attributes to add
- Returns:
- the updated attribute set
- See Also:
- MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
 
 - 
removeAttributepublic AttributeSet removeAttribute(AttributeSet old, Object name) Removes an attribute from the set.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- removeAttributein interface- AbstractDocument.AttributeContext
- Parameters:
- old- the old set of attributes
- name- the non-null attribute name
- Returns:
- the updated attribute set
- See Also:
- MutableAttributeSet.removeAttribute(java.lang.Object)
 
 - 
removeAttributespublic AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names) Removes a set of attributes for the element.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- removeAttributesin interface- AbstractDocument.AttributeContext
- Parameters:
- old- the old attribute set
- names- the attribute names
- Returns:
- the updated attribute set
- See Also:
- MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
 
 - 
removeAttributespublic AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) Removes a set of attributes for the element.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- removeAttributesin interface- AbstractDocument.AttributeContext
- Parameters:
- old- the old attribute set
- attrs- the attributes
- Returns:
- the updated attribute set
- See Also:
- MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
 
 - 
getEmptySetpublic AttributeSet getEmptySet() Fetches an empty AttributeSet.- Specified by:
- getEmptySetin interface- AbstractDocument.AttributeContext
- Returns:
- the set
 
 - 
reclaimpublic void reclaim(AttributeSet a) Returns a set no longer needed by the MutableAttributeSet implementation. This is useful for operation under 1.1 where there are no weak references. This would typically be called by the finalize method of the MutableAttributeSet implementation.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- reclaimin interface- AbstractDocument.AttributeContext
- Parameters:
- a- the set to reclaim
 
 - 
getCompressionThresholdprotected int getCompressionThreshold() Returns the maximum number of key/value pairs to try and compress into unique/immutable sets. Any sets above this limit will use hashtables and be a MutableAttributeSet.- Returns:
- the threshold
 
 - 
createSmallAttributeSetprotected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a) Create a compact set of attributes that might be shared. This is a hook for subclasses that want to alter the behavior of SmallAttributeSet. This can be reimplemented to return an AttributeSet that provides some sort of attribute conversion.- Parameters:
- a- The set of attributes to be represented in the the compact form.
- Returns:
- a compact set of attributes that might be shared
 
 - 
createLargeAttributeSetprotected MutableAttributeSet createLargeAttributeSet(AttributeSet a) Create a large set of attributes that should trade off space for time. This set will not be shared. This is a hook for subclasses that want to alter the behavior of the larger attribute storage format (which is SimpleAttributeSet by default). This can be reimplemented to return a MutableAttributeSet that provides some sort of attribute conversion.- Parameters:
- a- The set of attributes to be represented in the the larger form.
- Returns:
- a large set of attributes that should trade off space for time
 
 - 
toStringpublic String toString() Converts a StyleContext to a String.
 - 
writeAttributespublic void writeAttributes(ObjectOutputStream out, AttributeSet a) throws IOException Context-specific handling of writing out attributes- Parameters:
- out- the output stream
- a- the attribute set
- Throws:
- IOException- on any I/O error
 
 - 
readAttributespublic void readAttributes(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException Context-specific handling of reading in attributes- Parameters:
- in- the object stream to read the attribute data from.
- a- the attribute set to place the attribute definitions in.
- Throws:
- ClassNotFoundException- passed upward if encountered when reading the object stream.
- IOException- passed upward if encountered when reading the object stream.
 
 - 
writeAttributeSetpublic static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException Writes a set of attributes to the given object stream for the purpose of serialization. This will take special care to deal with static attribute keys that have been registered wit theregisterStaticAttributeKeymethod. Any attribute key not registered as a static key will be serialized directly. All values are expected to be serializable.- Parameters:
- out- the output stream
- a- the attribute set
- Throws:
- IOException- on any I/O error
 
 - 
readAttributeSetpublic static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException Reads a set of attributes from the given object input stream that have been previously written out withwriteAttributeSet. This will try to restore keys that were static objects to the static objects in the current virtual machine considering only those keys that have been registered with theregisterStaticAttributeKeymethod. The attributes retrieved from the stream will be placed into the given mutable set.- Parameters:
- in- the object stream to read the attribute data from.
- a- the attribute set to place the attribute definitions in.
- Throws:
- ClassNotFoundException- passed upward if encountered when reading the object stream.
- IOException- passed upward if encountered when reading the object stream.
 
 - 
registerStaticAttributeKeypublic static void registerStaticAttributeKey(Object key) Registers an object as a static object that is being used as a key in attribute sets. This allows the key to be treated specially for serialization.For operation under a 1.1 virtual machine, this uses the value returned by toStringconcatenated to the classname. The value returned by toString should not have the class reference in it (ie it should be reimplemented from the definition in Object) in order to be the same when recomputed later.- Parameters:
- key- the non-null object key
 
 - 
getStaticAttributepublic static Object getStaticAttribute(Object key) Returns the object previously registered withregisterStaticAttributeKey.- Parameters:
- key- the object key
- Returns:
- Returns the object previously registered with
 registerStaticAttributeKey
 
 - 
getStaticAttributeKeypublic static Object getStaticAttributeKey(Object key) Returns the String thatkeywill be registered with.- Parameters:
- key- the object key
- Returns:
- the String that keywill be registered with
- See Also:
- getStaticAttribute(java.lang.Object),- registerStaticAttributeKey(java.lang.Object)
 
 
- 
 
-