Purpose of Element in java Swing and its relationship to StyledDocument and paragraphs
I'm trying to understand the purpose of Element and how it can help with manipulating a StyledDocument that is to be displayed in a JEditorPane or JTextPane. I'm also trying to grasp how it relates to the concept of a "paragraph".
The javadoc for javax.swing.text.Element is almost nothing:
public interface Element Interface to describe a structural piece of a document. It is intended to capture the spirit of an SGML element.
I am very familiar with the concept of an element in HTML and XML, and apparently this is something similar, but I just can't see its purpose as it relates to StyledDocument. At first I figured it was just something the StyledDocument used internally to manage the stop and start points of different styles, but then I saw code examples on the web where they used instances of Element.
I already have my own tree structure of the data I need to display in different fonts and colors, and traversing it will tell me where to change the font or color as needed. It looks like I'll be able to meet the immediate need wit开发者_如何学Ch a series of calls to StyledDocument.setCharacterAttributes and setParagraphAttributes, without touching Element myself.
But I get the impression that using Element will be more efficient or cleaner. Please help me to get a proper understanding of Element and how it helps with StyledDocument and the concept of a paragrah, so even if I don't use any Elements right now I'll at least appreciate what I'm missing and know if and how to use it for the next similar situation.
In fact DefaultStyledDocument is a tree of Elements. There are BranchElements and LeafElements. Leaf represents a piece of text with text attributes like font size/style, font color attributes - bold, italic, underline etc. BrachElement contains Leaves or another branch elements. In simplest case the branches are paragraphs. But root Element is also instance of BranchElement. All Elements may have own AttributeSet. To find e.g. color of text LeafElement's set is asked. If color isn't defined the leaf's parent element is asked.
You can use this to see how Document is represented (Model) and How the model is represented in views. http://java-sl.com/JEditorPaneStructureTool.html
The example shows HTMLDocument's structure but you can use the same code to see structure of StyledEditorKit as well
精彩评论