import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
 
public class JEditorPaneWithSpace {

    static String testText=null;
    public JEditorPaneWithSpace() {
    }
 
    public static void main(String[] args) {
        JEditorPaneWithSpace frame=new JEditorPaneWithSpace();
        frame.init().setVisible(true);
    }
 
    public JFrame init() {
        JFrame frame = new JFrame("Performance test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        final JEditorPane editorCustom = new JEditorPane();
 
        final JScrollPane scroll = new JScrollPane(editorCustom);
        editorCustom.getDocument().putProperty("i18n", Boolean.TRUE);
        setText(editorCustom, "");
        frame.getContentPane().add(scroll);
 
        JToolBar tb=new JToolBar();
        JButton btnSetText=new JButton("Set text");
        tb.add(btnSetText);
        btnSetText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setText(editorCustom, "");
//                final int old=scroll.getVerticalScrollBarPolicy();
//                scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//                SwingUtilities.invokeLater(new Runnable() {
//                    public void run() {
//                        setText(editorCustom, "");
//                        scroll.setVerticalScrollBarPolicy(old);
//                    }
//                });
            }
        });
        JButton btnClear=new JButton("Clear");
        tb.add(btnClear);
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editorCustom.setText("");
            }
        });
        frame.getContentPane().add(tb, BorderLayout.NORTH);
 
        frame.setBounds(0,0,400,400);
        frame.setLocationRelativeTo(null);
        return frame;
    }
 
    private void setText(JEditorPane editor, String txt) {
        long t1=System.currentTimeMillis();
        editor.setText(getTestText());
        long t2=System.currentTimeMillis();
        System.out.println(txt+" setText time="+(t2-t1));
    }
 
    public static String getTestText() {
        if (testText ==null) {
            
            String mid= "<p>\n" +
                    "It's a long long long long long long long long long long" +
                    " long long long long long long long long long long long long" +
                    " long long long long long long long long long long long long" +
                    " long long long long long long long long long long long long" + 
                    " long long long long long long long long long long long long" +
                    " long long long long long long long long long long long long" +
                    " long long long long long text" +
                    "\n";
            StringBuilder buff=new StringBuilder();
            for (int i=0; i<5060; i++) {
                buff.append(mid);
            }
 
            System.out.println("Text length="+buff.length());
            testText = buff.toString();
        }
 
        return testText;
    }
}
