javax.swing.text
Class StyleContext

java.lang.Object
  |
  +--javax.swing.text.StyleContext
All Implemented Interfaces:
javax.swing.text.AbstractDocument.AttributeContext, Serializable

public class StyleContext
extends Object
implements Serializable, javax.swing.text.AbstractDocument.AttributeContext

Safe:

See Also:
Serialized Form

Field Summary
private  Hashtable attributesPool
           
static String DEFAULT_STYLE
          Suppressed: The name given to the default logical style attached to paragraphs.
private static StyleContext defaultContext
           
private  javax.swing.text.StyleContext.FontKey fontSearch
           
private  Hashtable fontTable
           
private static Hashtable freezeKeyMap
           
private  MutableAttributeSet search
           
private  Style styles
           
private static Hashtable thawKeyMap
           
(package private) static int THRESHOLD
          The threshold for no longer sharing the set of attributes in an immutable table.
private  int unusedSets
          Number of immutable sets that are not currently being used.
 
Constructor Summary
StyleContext()
          Enabled: Creates a new StyleContext object.
 
Method Summary
 AttributeSet addAttribute(AttributeSet old, Object name, Object value)
          Enabled: Adds an attribute to the given set, and returns the new representative set.
 AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
          Enabled: Adds a set of attributes to the element.
 void addChangeListener(ChangeListener l)
          Enabled: Adds a listener to track when styles are added or removed.
 Style addStyle(String nm, Style parent)
          Enabled: Adds a new style into the style hierarchy.
protected  MutableAttributeSet createLargeAttributeSet(AttributeSet a)
          Create a large set of attributes that should trade off space for time.
protected  javax.swing.text.StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
          Create a compact set of attributes that might be shared.
 Color getBackground(AttributeSet attr)
          Suppressed: Takes a set of attributes and turn it into a background color specification.
 ChangeListener[] getChangeListeners()
          Suppressed: Returns an array of all the ChangeListeners added to this StyleContext with addChangeListener().
protected  int getCompressionThreshold()
          Returns the maximum number of key/value pairs to try and compress into unique/immutable sets.
static StyleContext getDefaultStyleContext()
          Enabled: Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.
 AttributeSet getEmptySet()
          Enabled: Fetches an empty AttributeSet.
 Font getFont(AttributeSet attr)
          Enabled: Gets the font from an attribute set.
 Font getFont(String family, int style, int size)
          Enabled: Gets a new font.
 FontMetrics getFontMetrics(Font f)
          Enabled: Returns font metrics for a font.
 Color getForeground(AttributeSet attr)
          Enabled: Takes a set of attributes and turn it into a foreground color specification.
(package private)  AttributeSet getImmutableUniqueSet()
          Search for an existing attribute set using the current search parameters.
(package private)  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.
static Object getStaticAttribute(Object key)
          Enabled: Returns the object previously registered with registerStaticAttributeKey.
static Object getStaticAttributeKey(Object key)
          Enabled: Returns the String that key will be registered with
 Style getStyle(String nm)
          Enabled: Fetches a named style previously added to the document
 Enumeration getStyleNames()
          Enabled: Fetches the names of the styles defined.
 void readAttributes(ObjectInputStream in, MutableAttributeSet a)
          Enabled: Context-specific handling of reading in attributes
static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a)
          Suppressed: Reads a set of attributes from the given object input stream that have been previously written out with writeAttributeSet.
private  void readObject(ObjectInputStream s)
           
 void reclaim(AttributeSet a)
          Enabled: Returns a set no longer needed by the MutableAttributeSet implmentation.
static void registerStaticAttributeKey(Object key)
          Suppressed: Registers an object as a static object that is being used as a key in attribute sets.
 AttributeSet removeAttribute(AttributeSet old, Object name)
          Enabled: Removes an attribute from the set.
 AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
          Enabled: Removes a set of attributes for the element.
 AttributeSet removeAttributes(AttributeSet old, Enumeration names)
          Enabled: Removes a set of attributes for the element.
 void removeChangeListener(ChangeListener l)
          Suppressed: Removes a listener that was tracking styles being added or removed.
 void removeStyle(String nm)
          Enabled: Removes a named style previously added to the document.
(package private)  void removeUnusedSets()
          Clean the unused immutable sets out of the hashtable.
 String toString()
          Suppressed: Converts a StyleContext to a String.
 void writeAttributes(ObjectOutputStream out, AttributeSet a)
          Enabled: Context-specific handling of writing out attributes
static void writeAttributeSet(ObjectOutputStream out, AttributeSet a)
          Enabled: Writes a set of attributes to the given object stream for the purpose of serialization.
private  void writeObject(ObjectOutputStream s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultContext

private static StyleContext defaultContext

DEFAULT_STYLE

public static final String DEFAULT_STYLE
Suppressed: The name given to the default logical style attached to paragraphs.


freezeKeyMap

private static Hashtable freezeKeyMap

thawKeyMap

private static Hashtable thawKeyMap

styles

private Style styles

fontSearch

private transient javax.swing.text.StyleContext.FontKey fontSearch

fontTable

private transient Hashtable fontTable

attributesPool

private transient Hashtable attributesPool

search

private transient MutableAttributeSet search

unusedSets

private int unusedSets
Number of immutable sets that are not currently being used. This helps indicate when the sets need to be cleaned out of the hashtable they are stored in.


THRESHOLD

static final int THRESHOLD
The threshold for no longer sharing the set of attributes in an immutable table.

Constructor Detail

StyleContext

public StyleContext()
Enabled: Creates a new StyleContext object.

Method Detail

getDefaultStyleContext

public static final StyleContext getDefaultStyleContext()
Enabled: Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.

Returns:
the context

addStyle

public Style addStyle(String nm,
                      Style parent)
Enabled: 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

removeStyle

public void removeStyle(String nm)
Enabled: Removes a named style previously added to the document.

Parameters:
nm - the name of the style to remove

getStyle

public Style getStyle(String nm)
Enabled: Fetches a named style previously added to the document

Parameters:
nm - the name of the style
Returns:
the style

getStyleNames

public Enumeration getStyleNames()
Enabled: Fetches the names of the styles defined.

Returns:
the list of names as an enumeration

addChangeListener

public void addChangeListener(ChangeListener l)
Enabled: Adds a listener to track when styles are added or removed.

Parameters:
l - the change listener

removeChangeListener

public void removeChangeListener(ChangeListener l)
Suppressed: Removes a listener that was tracking styles being added or removed.

Parameters:
l - the change listener

getChangeListeners

public ChangeListener[] getChangeListeners()
Suppressed: Returns an array of all the ChangeListeners 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

getFont

public Font getFont(AttributeSet attr)
Enabled: 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

getForeground

public Color getForeground(AttributeSet attr)
Enabled: 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

getBackground

public Color getBackground(AttributeSet attr)
Suppressed: 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

getFont

public Font getFont(String family,
                    int style,
                    int size)
Enabled: 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

getFontMetrics

public FontMetrics getFontMetrics(Font f)
Enabled: Returns font metrics for a font.

Parameters:
f - the font
Returns:
the metrics

addAttribute

public AttributeSet addAttribute(AttributeSet old,
                                 Object name,
                                 Object value)
Enabled: 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 Threads and Swing for more information.

Specified by:
addAttribute in interface javax.swing.text.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)

addAttributes

public AttributeSet addAttributes(AttributeSet old,
                                  AttributeSet attr)
Enabled: Adds a set of attributes to the element.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
addAttributes in interface javax.swing.text.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)

removeAttribute

public AttributeSet removeAttribute(AttributeSet old,
                                    Object name)
Enabled: Removes an attribute from the set.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
removeAttribute in interface javax.swing.text.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)

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     Enumeration names)
Enabled: Removes a set of attributes for the element.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
removeAttributes in interface javax.swing.text.AbstractDocument.AttributeContext
Parameters:
old - the old attribute set
names - the attribute names
Returns:
the updated attribute set
See Also:
MutableAttributeSet.removeAttributes(java.util.Enumeration)

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     AttributeSet attrs)
Enabled: Removes a set of attributes for the element.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
removeAttributes in interface javax.swing.text.AbstractDocument.AttributeContext
Parameters:
old - the old attribute set
attrs - the attributes
Returns:
the updated attribute set
See Also:
MutableAttributeSet.removeAttributes(java.util.Enumeration)

getEmptySet

public AttributeSet getEmptySet()
Enabled: Fetches an empty AttributeSet.

Specified by:
getEmptySet in interface javax.swing.text.AbstractDocument.AttributeContext
Returns:
the set

reclaim

public void reclaim(AttributeSet a)
Enabled: Returns a set no longer needed by the MutableAttributeSet implmentation. 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 Threads and Swing for more information.

Specified by:
reclaim in interface javax.swing.text.AbstractDocument.AttributeContext
Parameters:
a - the set to reclaim

getCompressionThreshold

protected 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

createSmallAttributeSet

protected javax.swing.text.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.

createLargeAttributeSet

protected 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.

removeUnusedSets

void removeUnusedSets()
Clean the unused immutable sets out of the hashtable.


getImmutableUniqueSet

AttributeSet getImmutableUniqueSet()
Search for an existing attribute set using the current search parameters. If a matching set is found, return it. If a match is not found, we create a new set and add it to the pool.


getMutableAttributeSet

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.


toString

public String toString()
Suppressed: Converts a StyleContext to a String.

Overrides:
toString in class Object
Returns:
the string

writeAttributes

public void writeAttributes(ObjectOutputStream out,
                            AttributeSet a)
                     throws IOException
Enabled: Context-specific handling of writing out attributes

IOException

readAttributes

public void readAttributes(ObjectInputStream in,
                           MutableAttributeSet a)
                    throws ClassNotFoundException,
                           IOException
Enabled: Context-specific handling of reading in attributes

ClassNotFoundException
IOException

writeAttributeSet

public static void writeAttributeSet(ObjectOutputStream out,
                                     AttributeSet a)
                              throws IOException
Enabled: 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 the registerStaticAttributeKey method. Any attribute key not regsitered as a static key will be serialized directly. All values are expected to be serializable.

Parameters:
out - the output stream
a - the attribute set
IOException

readAttributeSet

public static void readAttributeSet(ObjectInputStream in,
                                    MutableAttributeSet a)
                             throws ClassNotFoundException,
                                    IOException
Suppressed: Reads a set of attributes from the given object input stream that have been previously written out with writeAttributeSet. 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 the registerStaticAttributeKey method. 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.
ClassNotFoundException
IOException

registerStaticAttributeKey

public static void registerStaticAttributeKey(Object key)
Suppressed: 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 toString concatenated 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

getStaticAttribute

public static Object getStaticAttribute(Object key)
Enabled: Returns the object previously registered with registerStaticAttributeKey.


getStaticAttributeKey

public static Object getStaticAttributeKey(Object key)
Enabled: Returns the String that key will be registered with

See Also:
getStaticAttribute(java.lang.Object), registerStaticAttributeKey(java.lang.Object)

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
IOException

readObject

private void readObject(ObjectInputStream s)
                 throws ClassNotFoundException,
                        IOException
ClassNotFoundException
IOException


comments?