Method from javax.swing.text.StyleContext Detail: |
public synchronized AttributeSet addAttribute(AttributeSet old,
Object name,
Object value) {
if ((old.getAttributeCount() + 1) < = getCompressionThreshold()) {
// build a search key and find/create an immutable and unique
// set.
search.removeAttributes(search);
search.addAttributes(old);
search.addAttribute(name, value);
reclaim(old);
return getImmutableUniqueSet();
}
MutableAttributeSet ma = getMutableAttributeSet(old);
ma.addAttribute(name, value);
return ma;
}
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
How
to Use Threads for more information. |
public synchronized AttributeSet addAttributes(AttributeSet old,
AttributeSet attr) {
if ((old.getAttributeCount() + attr.getAttributeCount()) < = getCompressionThreshold()) {
// build a search key and find/create an immutable and unique
// set.
search.removeAttributes(search);
search.addAttributes(old);
search.addAttributes(attr);
reclaim(old);
return getImmutableUniqueSet();
}
MutableAttributeSet ma = getMutableAttributeSet(old);
ma.addAttributes(attr);
return ma;
}
Adds a set of attributes to the element.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information. |
public void addChangeListener(ChangeListener l) {
styles.addChangeListener(l);
}
Adds a listener to track when styles are added
or removed. |
public Style addStyle(String nm,
Style parent) {
Style style = new NamedStyle(nm, parent);
if (nm != null) {
// add a named style, a class of attributes
styles.addAttribute(nm, style);
}
return style;
}
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. |
protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) {
return new SimpleAttributeSet(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. |
protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) {
return new SmallAttributeSet(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. |
public Color getBackground(AttributeSet attr) {
return StyleConstants.getBackground(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. |
public ChangeListener[] getChangeListeners() {
return ((NamedStyle)styles).getChangeListeners();
}
Returns an array of all the ChangeListener s added
to this StyleContext with addChangeListener(). |
protected int getCompressionThreshold() {
return THRESHOLD;
}
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. |
public static final StyleContext getDefaultStyleContext() {
if (defaultContext == null) {
defaultContext = new StyleContext();
}
return defaultContext;
}
Returns default AttributeContext shared by all documents that
don't bother to define/supply their own context. |
public AttributeSet getEmptySet() {
return SimpleAttributeSet.EMPTY;
}
Fetches an empty AttributeSet. |
public Font getFont(AttributeSet attr) {
// PENDING(prinz) add cache behavior
int style = Font.PLAIN;
if (StyleConstants.isBold(attr)) {
style |= Font.BOLD;
}
if (StyleConstants.isItalic(attr)) {
style |= Font.ITALIC;
}
String family = StyleConstants.getFontFamily(attr);
int size = StyleConstants.getFontSize(attr);
/**
* if either superscript or subscript is
* is set, we need to reduce the font size
* by 2.
*/
if (StyleConstants.isSuperscript(attr) ||
StyleConstants.isSubscript(attr)) {
size -= 2;
}
return getFont(family, style, size);
}
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. |
public Font getFont(String family,
int style,
int size) {
fontSearch.setValue(family, style, size);
Font f = fontTable.get(fontSearch);
if (f == null) {
// haven't seen this one yet.
Style defaultStyle =
getStyle(StyleContext.DEFAULT_STYLE);
if (defaultStyle != null) {
final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
Font defaultFont =
(Font) defaultStyle.getAttribute(FONT_ATTRIBUTE_KEY);
if (defaultFont != null
&& defaultFont.getFamily().equalsIgnoreCase(family)) {
f = defaultFont.deriveFont(style, size);
}
}
if (f == null) {
f = new Font(family, style, size);
}
if (! FontUtilities.fontSupportsDefaultEncoding(f)) {
f = FontUtilities.getCompositeFontUIResource(f);
}
FontKey key = new FontKey(family, style, size);
fontTable.put(key, f);
}
return f;
}
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. |
public FontMetrics getFontMetrics(Font f) {
// The Toolkit implementations cache, so we just forward
// to the default toolkit.
return Toolkit.getDefaultToolkit().getFontMetrics(f);
}
Returns font metrics for a font. |
public Color getForeground(AttributeSet attr) {
return StyleConstants.getForeground(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. |
AttributeSet getImmutableUniqueSet() {
// PENDING(prinz) should consider finding a alternative to
// generating extra garbage on search key.
SmallAttributeSet key = createSmallAttributeSet(search);
WeakReference< SmallAttributeSet > reference = attributesPool.get(key);
SmallAttributeSet a;
if (reference == null || (a = reference.get()) == null) {
a = key;
attributesPool.put(a, new WeakReference< SmallAttributeSet >(a));
}
return a;
}
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. |
MutableAttributeSet getMutableAttributeSet(AttributeSet a) {
if (a instanceof MutableAttributeSet &&
a != SimpleAttributeSet.EMPTY) {
return (MutableAttributeSet) a;
}
return createLargeAttributeSet(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 getStaticAttribute(Object key) {
if (thawKeyMap == null || key == null) {
return null;
}
return thawKeyMap.get(key);
}
Returns the object previously registered with
registerStaticAttributeKey . |
public static Object getStaticAttributeKey(Object key) {
return key.getClass().getName() + "." + key.toString();
}
Returns the String that key will be registered with |
public Style getStyle(String nm) {
return (Style) styles.getAttribute(nm);
}
Fetches a named style previously added to the document |
public Enumeration<?> getStyleNames() {
return styles.getAttributeNames();
}
Fetches the names of the styles defined. |
public static void readAttributeSet(ObjectInputStream in,
MutableAttributeSet a) throws ClassNotFoundException, IOException {
int n = in.readInt();
for (int i = 0; i < n; i++) {
Object key = in.readObject();
Object value = in.readObject();
if (thawKeyMap != null) {
Object staticKey = thawKeyMap.get(key);
if (staticKey != null) {
key = staticKey;
}
Object staticValue = thawKeyMap.get(value);
if (staticValue != null) {
value = staticValue;
}
}
a.addAttribute(key, value);
}
}
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. |
public void readAttributes(ObjectInputStream in,
MutableAttributeSet a) throws ClassNotFoundException, IOException {
readAttributeSet(in, a);
}
Context-specific handling of reading in attributes |
public void reclaim(AttributeSet a) {
if (SwingUtilities.isEventDispatchThread()) {
attributesPool.size(); // force WeakHashMap to expunge stale entries
}
// if current thread is not event dispatching thread
// do not bother with expunging stale entries.
}
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
How
to Use Threads for more information. |
public static void registerStaticAttributeKey(Object key) {
String ioFmt = key.getClass().getName() + "." + key.toString();
if (freezeKeyMap == null) {
freezeKeyMap = new Hashtable< Object, String >();
thawKeyMap = new Hashtable< String, Object >();
}
freezeKeyMap.put(key, ioFmt);
thawKeyMap.put(ioFmt, 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 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. |
public synchronized AttributeSet removeAttribute(AttributeSet old,
Object name) {
if ((old.getAttributeCount() - 1) < = getCompressionThreshold()) {
// build a search key and find/create an immutable and unique
// set.
search.removeAttributes(search);
search.addAttributes(old);
search.removeAttribute(name);
reclaim(old);
return getImmutableUniqueSet();
}
MutableAttributeSet ma = getMutableAttributeSet(old);
ma.removeAttribute(name);
return ma;
}
Removes an attribute from the set.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information. |
public synchronized AttributeSet removeAttributes(AttributeSet old,
Enumeration<?> names) {
if (old.getAttributeCount() < = getCompressionThreshold()) {
// build a search key and find/create an immutable and unique
// set.
search.removeAttributes(search);
search.addAttributes(old);
search.removeAttributes(names);
reclaim(old);
return getImmutableUniqueSet();
}
MutableAttributeSet ma = getMutableAttributeSet(old);
ma.removeAttributes(names);
return ma;
}
Removes a set of attributes for the element.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information. |
public synchronized AttributeSet removeAttributes(AttributeSet old,
AttributeSet attrs) {
if (old.getAttributeCount() < = getCompressionThreshold()) {
// build a search key and find/create an immutable and unique
// set.
search.removeAttributes(search);
search.addAttributes(old);
search.removeAttributes(attrs);
reclaim(old);
return getImmutableUniqueSet();
}
MutableAttributeSet ma = getMutableAttributeSet(old);
ma.removeAttributes(attrs);
return ma;
}
Removes a set of attributes for the element.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information. |
public void removeChangeListener(ChangeListener l) {
styles.removeChangeListener(l);
}
Removes a listener that was tracking styles being
added or removed. |
public void removeStyle(String nm) {
styles.removeAttribute(nm);
}
Removes a named style previously added to the document. |
synchronized void removeUnusedSets() {
attributesPool.size(); // force WeakHashMap to expunge stale entries
}
Clean the unused immutable sets out of the hashtable. |
public String toString() {
removeUnusedSets();
String s = "";
for (SmallAttributeSet set : attributesPool.keySet()) {
s = s + set + "\n";
}
return s;
}
Converts a StyleContext to a String. |
public static void writeAttributeSet(ObjectOutputStream out,
AttributeSet a) throws IOException {
int n = a.getAttributeCount();
out.writeInt(n);
Enumeration keys = a.getAttributeNames();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key instanceof Serializable) {
out.writeObject(key);
} else {
Object ioFmt = freezeKeyMap.get(key);
if (ioFmt == null) {
throw new NotSerializableException(key.getClass().
getName() + " is not serializable as a key in an AttributeSet");
}
out.writeObject(ioFmt);
}
Object value = a.getAttribute(key);
Object ioFmt = freezeKeyMap.get(value);
if (value instanceof Serializable) {
out.writeObject((ioFmt != null) ? ioFmt : value);
} else {
if (ioFmt == null) {
throw new NotSerializableException(value.getClass().
getName() + " is not serializable as a value in an AttributeSet");
}
out.writeObject(ioFmt);
}
}
}
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. |
public void writeAttributes(ObjectOutputStream out,
AttributeSet a) throws IOException {
writeAttributeSet(out, a);
}
Context-specific handling of writing out attributes |