I am trying to create a hierarchical Document where I have a root element and some headers that are children of that root like this:
BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)
The Java source for my custom Document is listed below. Its producing output like this which I don't want:
BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)
Everytime I add a Element it becomes a child of the previous element. How can I manipulate the ElementSpecs to generate the hierarchy at the top that I want? Thanks.
BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)
The Java source for my custom Document is listed below. Its producing output like this which I don't want:
BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)
Everytime I add a Element it becomes a child of the previous element. How can I manipulate the ElementSpecs to generate the hierarchy at the top that I want? Thanks.
Code:
class CustomDocument extends DefaultStyledDocument {
public void insertRoot(String root) {
char[] chars = root.toCharArray();
int length = chars.length;
ElementSpec[] es = new ElementSpec[] {
new ElementSpec(null, ElementSpec.StartTagType),
new ElementSpec(null, ElementSpec.ContentType, chars, 0, length),
new ElementSpec(null, ElementSpec.EndTagType)
};
create(es);
}
public void insertHeader(String header) {
char[] chars = header.toCharArray();
int length = chars.length;
int offset = getLength();
ElementSpec[] es = new ElementSpec[] {
new ElementSpec(null, ElementSpec.StartTagType),
new ElementSpec(null, ElementSpec.ContentType, chars, 0, length),
new ElementSpec(null, ElementSpec.EndTagType)
};
try {
insert(offset, es);
} catch (BadLocationException e) {
}
}
public static void main(String a[]) {
CustomDocument doc = new CustomDocument();
doc.insertRoot("root");
doc.insertHeader("Header 1");
doc.insertHeader("Header 2");
doc.insertHeader("Header 3");
}
}