Java: Swing: Text components and Document: (Mutable)AttributeSet and Style
There seems to be some confusion about what is an attribute: is it a value (as getAttribute() seems to suggest) or is it a key-value pair (as other methods names would imply).
Anyway, having almost each method name contain "attribute" makes the code unreadable.
The only standard implementation is StyleContext, which also adds support for the Style management of a StyledDocument. The Styles of StyleContext also may use the shared immutable storage implementations.
Style adds observability to MutableAttributeSet. Without Style, any AttributeSets must always be copied. The original object may have changed without you knowing.
Style is actually misnamed: it has nothing more to do with style than attribute sets. Even for non-StyledDocuments, it may make sense to use Styles (but even than not much).
There is no readonly observable AttributeSet. A Style can always be modified. That's one reason why it is of limited use.
If a Style has a resolving parent, and that is also a Style (if it is not, then you are stuck), it should observe that Style and forward any events from it, as if it changed itself (this is actually easy to implement). Otherwise the code in the clients gets horribly complicated when they have to listen to all Styles in the resolving chain and especially figure out changes in the chain structure.
StyleContext.NamedStyle doesn't do that. It doesn't really matter with DefaultStyledDocument, since that will fire a DocumentEvent for the whole Document whenever a single Style changed.
Even the simple assumption that getName() and getAttribute(AttributeSet.NameAttribute) are equal fails with HTMLDocument, where NameAttribute is an HTML.Tag and not a String.
(C) 2001-2009 Christian Kaufhold (email@example.com)