|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.text.StyleContext
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 JavaBeansTM
has been added to the java.beans
package.
Please see XMLEncoder
.
Nested Class Summary | |
(package private) static class |
StyleContext.FontKey
key for a font table |
(package private) class |
StyleContext.KeyBuilder
Sorts the key strings so that they can be very quickly compared in the attribute set searchs. |
(package private) class |
StyleContext.KeyEnumeration
An enumeration of the keys in a SmallAttributeSet. |
class |
StyleContext.NamedStyle
A collection of attributes, typically used to represent character and paragraph styles. |
class |
StyleContext.SmallAttributeSet
This class holds a small number of attributes in an array. |
Field Summary | |
private Map |
attributesPool
|
static String |
DEFAULT_STYLE
The name given to the default logical style attached to paragraphs. |
private static StyleContext |
defaultContext
|
private 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()
Creates a new StyleContext object. |
Method Summary | |
AttributeSet |
addAttribute(AttributeSet old,
Object name,
Object value)
Adds an attribute to the given set, and returns the new representative set. |
AttributeSet |
addAttributes(AttributeSet old,
AttributeSet attr)
Adds a set of attributes to the element. |
void |
addChangeListener(ChangeListener l)
Adds a listener to track when styles are added or removed. |
Style |
addStyle(String nm,
Style parent)
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 StyleContext.SmallAttributeSet |
createSmallAttributeSet(AttributeSet a)
Create a compact set of attributes that might be shared. |
Color |
getBackground(AttributeSet attr)
Takes a set of attributes and turn it into a background color specification. |
ChangeListener[] |
getChangeListeners()
Returns an array of all the ChangeListener s 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()
Returns default AttributeContext shared by all documents that don't bother to define/supply their own context. |
AttributeSet |
getEmptySet()
Fetches an empty AttributeSet. |
Font |
getFont(AttributeSet attr)
Gets the font from an attribute set. |
Font |
getFont(String family,
int style,
int size)
Gets a new font. |
FontMetrics |
getFontMetrics(Font f)
Returns font metrics for a font. |
Color |
getForeground(AttributeSet attr)
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)
Returns the object previously registered with registerStaticAttributeKey . |
static Object |
getStaticAttributeKey(Object key)
Returns the String that key will be registered with |
Style |
getStyle(String nm)
Fetches a named style previously added to the document |
Enumeration |
getStyleNames()
Fetches the names of the styles defined. |
void |
readAttributes(ObjectInputStream in,
MutableAttributeSet a)
Context-specific handling of reading in attributes |
static void |
readAttributeSet(ObjectInputStream in,
MutableAttributeSet a)
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)
Returns a set no longer needed by the MutableAttributeSet implmentation. |
static void |
registerStaticAttributeKey(Object key)
Registers an object as a static object that is being used as a key in attribute sets. |
AttributeSet |
removeAttribute(AttributeSet old,
Object name)
Removes an attribute from the set. |
AttributeSet |
removeAttributes(AttributeSet old,
AttributeSet attrs)
Removes a set of attributes for the element. |
AttributeSet |
removeAttributes(AttributeSet old,
Enumeration names)
Removes a set of attributes for the element. |
void |
removeChangeListener(ChangeListener l)
Removes a listener that was tracking styles being added or removed. |
void |
removeStyle(String nm)
Removes a named style previously added to the document. |
(package private) void |
removeUnusedSets()
Clean the unused immutable sets out of the hashtable. |
String |
toString()
Converts a StyleContext to a String. |
void |
writeAttributes(ObjectOutputStream out,
AttributeSet a)
Context-specific handling of writing out attributes |
static void |
writeAttributeSet(ObjectOutputStream out,
AttributeSet a)
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 |
private static StyleContext defaultContext
public static final String DEFAULT_STYLE
private static Hashtable freezeKeyMap
private static Hashtable thawKeyMap
private Style styles
private transient StyleContext.FontKey fontSearch
private transient Hashtable fontTable
private transient Map attributesPool
private transient MutableAttributeSet search
private int unusedSets
static final int THRESHOLD
Constructor Detail |
public StyleContext()
Method Detail |
public static final StyleContext getDefaultStyleContext()
public Style addStyle(String nm, Style parent)
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.
public void removeStyle(String nm)
nm
- the name of the style to removepublic Style getStyle(String nm)
nm
- the name of the style
public Enumeration getStyleNames()
public void addChangeListener(ChangeListener l)
l
- the change listenerpublic void removeChangeListener(ChangeListener l)
l
- the change listenerpublic ChangeListener[] getChangeListeners()
ChangeListener
s added
to this StyleContext with addChangeListener().
ChangeListener
s added or an empty
array if no listeners have been addedpublic Font getFont(AttributeSet attr)
attr
- the attribute set
public Color getForeground(AttributeSet attr)
attr
- the set of attributes
public Color getBackground(AttributeSet attr)
attr
- the set of attributes
public Font getFont(String family, int style, int size)
family
- the font family (such as "Monospaced")style
- the style of the font (such as Font.PLAIN)size
- the point size >= 1
public FontMetrics getFontMetrics(Font f)
f
- the font
public AttributeSet addAttribute(AttributeSet old, Object name, Object value)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
addAttribute
in interface AbstractDocument.AttributeContext
old
- the old attribute setname
- the non-null attribute namevalue
- the attribute value
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
addAttributes
in interface AbstractDocument.AttributeContext
old
- the old attribute setattr
- the attributes to add
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSet removeAttribute(AttributeSet old, Object name)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
removeAttribute
in interface AbstractDocument.AttributeContext
old
- the old set of attributesname
- the non-null attribute name
MutableAttributeSet.removeAttribute(java.lang.Object)
public AttributeSet removeAttributes(AttributeSet old, Enumeration names)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
removeAttributes
in interface AbstractDocument.AttributeContext
old
- the old attribute setnames
- the attribute names
MutableAttributeSet.removeAttributes(java.util.Enumeration)
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
removeAttributes
in interface AbstractDocument.AttributeContext
old
- the old attribute setattrs
- the attributes
MutableAttributeSet.removeAttributes(java.util.Enumeration)
public AttributeSet getEmptySet()
getEmptySet
in interface AbstractDocument.AttributeContext
public void reclaim(AttributeSet a)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
reclaim
in interface AbstractDocument.AttributeContext
a
- the set to reclaimprotected int getCompressionThreshold()
protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
a
- The set of attributes to be represented in the
the compact form.protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
a
- The set of attributes to be represented in the
the larger form.void removeUnusedSets()
AttributeSet getImmutableUniqueSet()
MutableAttributeSet getMutableAttributeSet(AttributeSet a)
public String toString()
toString
in class Object
public void writeAttributes(ObjectOutputStream out, AttributeSet a) throws IOException
IOException
public void readAttributes(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException
ClassNotFoundException
IOException
public static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException
registerStaticAttributeKey
method.
Any attribute key not regsitered as a static key
will be serialized directly. All values are expected
to be serializable.
out
- the output streama
- the attribute set
IOException
- on any I/O errorpublic static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException
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.
in
- the object stream to read the attribute data from.a
- the attribute set to place the attribute
definitions in.
ClassNotFoundException
- passed upward if encountered
when reading the object stream.
IOException
- passed upward if encountered when
reading the object stream.public static void registerStaticAttributeKey(Object key)
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.
key
- the non-null object keypublic static Object getStaticAttribute(Object key)
registerStaticAttributeKey
.
public static Object getStaticAttributeKey(Object key)
key
will be registered with
getStaticAttribute(java.lang.Object)
,
registerStaticAttributeKey(java.lang.Object)
private void writeObject(ObjectOutputStream s) throws IOException
IOException
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
ClassNotFoundException
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |