have a problem to clone DefaultStyledDocument
Hi!
I my application I need to clone DefaultStyledDocument. So I have written a clone-method in class MyDefaultStyledDocument which extends DefaultStyledDocument. But obviously I made a mistake and I can't find out what I am doing wrong. I hope someone here can help me, I will appreciate it!
Here is my SSCCE to demonstrate the problem:
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
publicclass CloneStyledDocumentSSCCE{
public CloneStyledDocumentSSCCE(){
UIManager.put("TextPane.font",
new javax.swing.plaf.FontUIResource(
new Font("SansSerif", Font.PLAIN, 20)));
JFrame frame =new JFrame("CloneStyledDocumentSSCCE");
frame.setLayout(new GridLayout(1,2,2,2));
MyDefaultStyledDocument doc =new MyDefaultStyledDocument();
MyDefaultStyledDocument clonedDoc = (MyDefaultStyledDocument)doc.clone();
JTextPane tp1 =new JTextPane(clonedDoc);
frame.add(tp1);
JTextPane tp2 =new JTextPane(new MyDefaultStyledDocument());
frame.add(tp2);
tp1.setText("clone");
tp2.setText("no clone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(400,400);
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
new CloneStyledDocumentSSCCE();
}
class MyDefaultStyledDocumentextends DefaultStyledDocument{
public Object clone(){
MyDefaultStyledDocument clonedDocument =new MyDefaultStyledDocument();
Element styledRootElement = this.getDefaultRootElement();
cloneRec(styledRootElement, clonedDocument);
return clonedDocument;
}
privatevoid cloneRec(Element element, MyDefaultStyledDocument clonedDocument){
for (int i=0; i<element.getElementCount(); i++){
Element child = element.getElement(i);
if (child.isLeaf()){
MutableAttributeSet as =new SimpleAttributeSet(child.getAttributes());
if (StyleConstants.getComponent(as) !=null){
// clone Component
}
String text =new String(this.getTextOfElement(child));
try{
clonedDocument.insertString(clonedDocument.getLength(), text, as);
}
catch (BadLocationException x){
x.printStackTrace();
}
if (i == element.getElementCount()-1){
Element paragraphElement = child.getParentElement();
int offset = paragraphElement.getStartOffset();
int length = paragraphElement.getEndOffset() - offset;
clonedDocument.setParagraphAttributes(
offset, length, paragraphElement.getAttributes(),false);
}
}
elseif (childinstanceof BranchElement){
this.cloneRec(child, clonedDocument);
}
else{
this.cloneRec(child, clonedDocument);
}
}
}
private String getTextOfElement(Element styledElement){
String text ="";
int startOffset = styledElement.getStartOffset();
int endOffset = styledElement.getEndOffset();
try{
text = this.getText(startOffset, endOffset-startOffset);
}
catch (BadLocationException x){
x.printStackTrace();
}
return text;
}
}
}
The problem is that the Font that I set with
UIManager.put("TextPane.font",
new javax.swing.plaf.FontUIResource(
new Font("SansSerif", Font.PLAIN, 20)));
gets lost in the cloned document.
Thank you for your time!>

