Class StyleContext
- All Implemented Interfaces:
-
Serializable
,AbstractDocument.AttributeContext
- Direct Known Subclasses:
StyleSheet
public class StyleContext extends Object implements Serializable, AbstractDocument.AttributeContext
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 XMLEncoder
.
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
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
Modifier and Type | Field | Description |
---|---|---|
static final String |
DEFAULT_STYLE |
The name given to the default logical style attached to paragraphs. |
Constructor Summary
Constructor | Description |
---|---|
StyleContext() |
Creates a new StyleContext object. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
AttributeSet |
addAttribute |
Adds an attribute to the given set, and returns the new representative set. |
AttributeSet |
addAttributes |
Adds a set of attributes to the element. |
void |
addChangeListener |
Adds a listener to track when styles are added or removed. |
Style |
addStyle |
Adds a new style into the style hierarchy. |
protected MutableAttributeSet |
createLargeAttributeSet |
Create a large set of attributes that should trade off space for time. |
protected StyleContext.SmallAttributeSet |
createSmallAttributeSet |
Create a compact set of attributes that might be shared. |
Color |
getBackground |
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 final 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 |
Gets a new font. |
Font |
getFont |
Gets the font from an attribute set. |
FontMetrics |
getFontMetrics |
Returns font metrics for a font. |
Color |
getForeground |
Takes a set of attributes and turn it into a foreground color specification. |
static Object |
getStaticAttribute |
Returns the object previously registered with registerStaticAttributeKey . |
static Object |
getStaticAttributeKey |
Returns the String that key will be registered with. |
Style |
getStyle |
Fetches a named style previously added to the document |
Enumeration<?> |
getStyleNames() |
Fetches the names of the styles defined. |
void |
readAttributes |
Context-specific handling of reading in attributes |
static void |
readAttributeSet |
Reads a set of attributes from the given object input stream that have been previously written out with writeAttributeSet . |
void |
reclaim |
Returns a set no longer needed by the MutableAttributeSet implementation. |
static void |
registerStaticAttributeKey |
Registers an object as a static object that is being used as a key in attribute sets. |
AttributeSet |
removeAttribute |
Removes an attribute from the set. |
AttributeSet |
removeAttributes |
Removes a set of attributes for the element. |
AttributeSet |
removeAttributes |
Removes a set of attributes for the element. |
void |
removeChangeListener |
Removes a listener that was tracking styles being added or removed. |
void |
removeStyle |
Removes a named style previously added to the document. |
String |
toString() |
Converts a StyleContext to a String. |
void |
writeAttributes |
Context-specific handling of writing out attributes |
static void |
writeAttributeSet |
Writes a set of attributes to the given object stream for the purpose of serialization. |
Field Details
DEFAULT_STYLE
public static final String DEFAULT_STYLE
- See Also:
Constructor Details
StyleContext
public StyleContext()
Method Details
getDefaultStyleContext
public static final StyleContext getDefaultStyleContext()
- Returns:
- the context
addStyle
public Style addStyle(String nm, Style 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)
- Parameters:
-
nm
- the name of the style to remove
getStyle
public Style getStyle(String nm)
- Parameters:
-
nm
- the name of the style - Returns:
- the style
getStyleNames
public Enumeration<?> getStyleNames()
- Returns:
- the list of names as an enumeration
addChangeListener
public void addChangeListener(ChangeListener l)
- Parameters:
-
l
- the change listener
removeChangeListener
public void removeChangeListener(ChangeListener l)
- Parameters:
-
l
- the change listener
getChangeListeners
public ChangeListener[] getChangeListeners()
ChangeListener
s added to this StyleContext with addChangeListener().- Returns:
- all of the
ChangeListener
s added or an empty array if no listeners have been added - Since:
- 1.4
getFont
public Font getFont(AttributeSet attr)
- Parameters:
-
attr
- the attribute set - Returns:
- the font
getForeground
public Color getForeground(AttributeSet attr)
- Parameters:
-
attr
- the set of attributes - Returns:
- the color
getBackground
public Color getBackground(AttributeSet attr)
- Parameters:
-
attr
- the set of attributes - Returns:
- the color
getFont
public Font getFont(String family, int style, int size)
- 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)
- Parameters:
-
f
- the font - Returns:
- the metrics
addAttribute
public AttributeSet addAttribute(AttributeSet old, Object name, Object value)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
addAttribute
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
old
- the old attribute set -
name
- the non-null attribute name -
value
- the attribute value - Returns:
- the updated attribute set
- See Also:
addAttributes
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
addAttributes
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
old
- the old attribute set -
attr
- the attributes to add - Returns:
- the updated attribute set
- See Also:
removeAttribute
public AttributeSet removeAttribute(AttributeSet old, Object name)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
removeAttribute
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
old
- the old set of attributes -
name
- the non-null attribute name - Returns:
- the updated attribute set
- See Also:
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
removeAttributes
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
old
- the old attribute set -
names
- the attribute names - Returns:
- the updated attribute set
- See Also:
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
removeAttributes
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
old
- the old attribute set -
attrs
- the attributes - Returns:
- the updated attribute set
- See Also:
getEmptySet
public AttributeSet getEmptySet()
- Specified by:
-
getEmptySet
in interfaceAbstractDocument.AttributeContext
- Returns:
- the set
reclaim
public void reclaim(AttributeSet a)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
reclaim
in interfaceAbstractDocument.AttributeContext
- Parameters:
-
a
- the set to reclaim
getCompressionThreshold
protected int getCompressionThreshold()
- Returns:
- the threshold
createSmallAttributeSet
protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
- Parameters:
-
a
- The set of attributes to be represented in the the compact form. - Returns:
- a compact set of attributes that might be shared
createLargeAttributeSet
protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
- 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
toString
public String toString()
writeAttributes
public void writeAttributes(ObjectOutputStream out, AttributeSet a) throws IOException
- Parameters:
-
out
- the output stream -
a
- the attribute set - Throws:
-
IOException
- on any I/O error
readAttributes
public void readAttributes(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException
- 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.
writeAttributeSet
public static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException
registerStaticAttributeKey
method. 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
readAttributeSet
public 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.- 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.
registerStaticAttributeKey
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.
- Parameters:
-
key
- the non-null object key
getStaticAttribute
public static Object getStaticAttribute(Object key)
registerStaticAttributeKey
.- Parameters:
-
key
- the object key - Returns:
- Returns the object previously registered with
registerStaticAttributeKey
getStaticAttributeKey
public static Object getStaticAttributeKey(Object key)
key
will be registered with.- Parameters:
-
key
- the object key - Returns:
- the String that
key
will be registered with - See Also:
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/StyleContext.html