我有一个JEditorPane
自定义EditorKit
和一个自定义Document
(源自 DefaultStyledDocument)。
以下是内容的示例JEditorPane
:
第一段_
第二段
对于上面的示例,我得到了一个具有以下 XML 等效项的文档结构:
<root>
<section>
<paragraph>
<content>first</content>
<content bold="true">paragraph</content>
</paragraph>
<paragraph>
<content>second paragraph</content>
<content>\n</content>
</paragraph>
</section>
</root>
请注意,上面的标签名称由 Element.getName() 函数确定。
我的意图是,通过自定义元素类型扩展此结构以编辑样式文本以外的内容。
一个例子是将编辑器扩展为一个音符编辑器,以获得这样的 XML 结构:
<root>
<section>
<paragraph>
<content>first</content>
<content bold="true">paragraph</content>
</paragraph>
<musicnotes>
<bar>
<note>C</note>
<note>D</note>
<note>E</note>
</bar>
</musicnotes>
</section>
</root>
如我所见,样式和段落元素是根据 Document.insertString() 和 Document.setCharacterAttributes() 方法创建的。
我的问题是我不知道如何覆盖这些方法(或编写挂件)以不返回默认结构而是使用自定义元素种类。
我什至不知道这是否是正确的方法。我是否必须创建自己的文档接口实现来创建自定义文档结构?