APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text

public Class StyleContext

extends Object
implements Serializable, AttributeContext
Class Inheritance
All Implemented Interfaces
javax.swing.text.AbstractDocument.AttributeContext, java.io.Serializable
Known Direct Subclasses
javax.swing.text.html.StyleSheet
Annotations
@SuppressWarnings:serial
Imports
java.awt.Color, .Font, .FontMetrics, .Toolkit, java.io.IOException, .InvalidObjectException, .NotSerializableException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.lang.ref.WeakReference, java.util.Arrays, .Collections, .Enumeration, .EventListener, .Hashtable, .Map, .NoSuchElementException, .WeakHashMap, javax.swing.SwingUtilities, javax.swing.event.ChangeEvent, .ChangeListener, .EventListenerList, sun.font.FontUtilities

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.beans package. Please see java.beans.XMLEncoder.

Author
Timothy Prinzing

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
StyleContext.FontKey

key for a font table

pack-priv static class
StyleContext.KeyEnumeration

An enumeration of the keys in a SmallAttributeSet.

public class
StyleContext.NamedStyle

A collection of attributes, typically used to represent character and paragraph styles.

public class
StyleContext.SmallAttributeSet

This class holds a small number of attributes in an array.

Field Summary

Modifier and TypeField and Description
private transient Map<StyleContext.SmallAttributeSet, WeakReference<StyleContext.SmallAttributeSet>>
public static final String
DEFAULT_STYLE

The name given to the default logical style attached to paragraphs.

private static StyleContext
private transient StyleContext.FontKey
private transient Hashtable<StyleContext.FontKey, Font>
private static Hashtable<Object, String>
private transient MutableAttributeSet
private Style
private static Hashtable<String, Object>
pack-priv static final int
THRESHOLD

The threshold for no longer sharing the set of attributes in an immutable table.

Constructor Summary

AccessConstructor and Description
public
StyleContext()

Creates a new StyleContext object.

Method Summary

Modifier and TypeMethod and Description
public synchronized AttributeSet

Returns:

the updated attribute set
addAttribute
(AttributeSet
the old attribute set
old
,
Object
the non-null attribute name
name
,
Object
the attribute value
value
)

Implements javax.swing.text.AbstractDocument.AttributeContext.addAttribute.

Adds an attribute to the given set, and returns the new representative set.

public synchronized AttributeSet

Returns:

the updated attribute set
addAttributes
(AttributeSet
the old attribute set
old
,
AttributeSet
the attributes to add
attr
)

Implements javax.swing.text.AbstractDocument.AttributeContext.addAttributes.

Adds a set of attributes to the element.

public void
addChangeListener(ChangeListener
the change listener
l
)

Adds a listener to track when styles are added or removed.

public Style

Returns:

the created style
addStyle
(String
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.
nm
,
Style
the parent style. This may be null if unspecified attributes need not be resolved in some other style.
parent
)

Adds a new style into the style hierarchy.

protected MutableAttributeSet

Returns:

a large set of attributes that should trade off space for time
createLargeAttributeSet
(AttributeSet
The set of attributes to be represented in the larger form.
a
)

Create a large set of attributes that should trade off space for time.

protected StyleContext.SmallAttributeSet

Returns:

a compact set of attributes that might be shared
createSmallAttributeSet
(AttributeSet
The set of attributes to be represented in the compact form.
a
)

Create a compact set of attributes that might be shared.

public Color

Returns:

the color
getBackground
(AttributeSet
the set of attributes
attr
)

Takes a set of attributes and turn it into a background color specification.

public ChangeListener[]

Returns:

all of the ChangeListeners added or an empty array if no listeners have been added
getChangeListeners
()

Returns an array of all the ChangeListeners added to this StyleContext with addChangeListener().

protected int

Returns:

the threshold
getCompressionThreshold
()

Returns the maximum number of key/value pairs to try and compress into unique/immutable sets.

public static final StyleContext

Returns:

the context
getDefaultStyleContext
()

Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.

public AttributeSet

Returns:

the set
getEmptySet
()

Implements javax.swing.text.AbstractDocument.AttributeContext.getEmptySet.

Fetches an empty AttributeSet.

public Font

Returns:

the font
getFont
(AttributeSet
the attribute set
attr
)

Gets the font from an attribute set.

public Font

Returns:

the new font
getFont
(String
the font family (such as "Monospaced")
family
,
int
the style of the font (such as Font.PLAIN)
style
,
int
the point size >= 1
size
)

Gets a new font.

public FontMetrics

Returns:

the metrics
getFontMetrics
(Font
the font
f
)

Returns font metrics for a font.

public Color

Returns:

the color
getForeground
(AttributeSet
the set of attributes
attr
)

Takes a set of attributes and turn it into a foreground color specification.

pack-priv AttributeSet
getImmutableUniqueSet()

Search for an existing attribute set using the current search parameters.

pack-priv MutableAttributeSet
getMutableAttributeSet(AttributeSet a)

Creates a mutable attribute set to hand out because the current needs are too big to try and use a shared version.

public static Object

Returns:

Returns the object previously registered with registerStaticAttributeKey
getStaticAttribute
(Object
the object key
key
)

Returns the object previously registered with registerStaticAttributeKey.

public static Object

Returns:

the String that key will be registered with
getStaticAttributeKey
(Object
the object key
key
)

Returns the String that key will be registered with.

public Style

Returns:

the style
getStyle
(String
the name of the style
nm
)

Fetches a named style previously added to the document

public Enumeration<?>

Returns:

the list of names as an enumeration
getStyleNames
()

Fetches the names of the styles defined.

public void
readAttributes(ObjectInputStream
the object stream to read the attribute data from.
in
,
MutableAttributeSet
the attribute set to place the attribute definitions in.
a
)

Context-specific handling of reading in attributes

public static void
readAttributeSet(ObjectInputStream
the object stream to read the attribute data from.
in
,
MutableAttributeSet
the attribute set to place the attribute definitions in.
a
)

Reads a set of attributes from the given object input stream that have been previously written out with writeAttributeSet.

private void
public void
reclaim(AttributeSet
the set to reclaim
a
)

Implements javax.swing.text.AbstractDocument.AttributeContext.reclaim.

Returns a set no longer needed by the MutableAttributeSet implementation.

public static void
registerStaticAttributeKey(Object
the non-null object key
key
)

Registers an object as a static object that is being used as a key in attribute sets.

public synchronized AttributeSet

Returns:

the updated attribute set
removeAttribute
(AttributeSet
the old set of attributes
old
,
Object
the non-null attribute name
name
)

Implements javax.swing.text.AbstractDocument.AttributeContext.removeAttribute.

Removes an attribute from the set.

public synchronized AttributeSet

Returns:

the updated attribute set
removeAttributes
(AttributeSet
the old attribute set
old
,
Enumeration<?>
the attribute names
names
)

Implements javax.swing.text.AbstractDocument.AttributeContext.removeAttributes.

Removes a set of attributes for the element.

public synchronized AttributeSet

Returns:

the updated attribute set
removeAttributes
(AttributeSet
the old attribute set
old
,
AttributeSet
the attributes
attrs
)

Implements javax.swing.text.AbstractDocument.AttributeContext.removeAttributes.

Removes a set of attributes for the element.

public void
removeChangeListener(ChangeListener
the change listener
l
)

Removes a listener that was tracking styles being added or removed.

public void
removeStyle(String
the name of the style to remove
nm
)

Removes a named style previously added to the document.

pack-priv synchronized void
removeUnusedSets()

Clean the unused immutable sets out of the hashtable.

public String

Returns:

the string
toString
()

Overrides java.lang.Object.toString.

Converts a StyleContext to a String.

public void
writeAttributes(ObjectOutputStream
the output stream
out
,
AttributeSet
the attribute set
a
)

Context-specific handling of writing out attributes

public static void
writeAttributeSet(ObjectOutputStream
the output stream
out
,
AttributeSet
the attribute set
a
)

Writes a set of attributes to the given object stream for the purpose of serialization.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait