内联复制粘贴JEditorPane HTML

时间:2013-07-02 23:30:15

标签: java swing copy-paste jtextpane dom

我绝望地尝试在HTML模式下在JTextPane中实现自定义复制/粘贴。 大部分工作都很好,我使用EditorKit.write()获取html内容,我使用editorKit.read()粘贴它。完美的世界。

但是,当我在编辑器中有<p> test </p>时,我会尝试复制“es”来获取 <p> tesest </p>,我获得了

<p>tes</p>
<p>es</p>
<p>t</p>

知道这一点,我试图找出一种方法来粘贴“内联”部分应该是内联的,并阻止复制期间阻塞的部分。典型地,

如果我有:

<p>mon beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>

如果我复制:

beau sapin</p>
<p>roi des forêts</p>
<p>que

然后在“mon”之后粘贴它,我希望:

<p>mon beau sapin</p>
<p>roi des forêts</p>
<p>que beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>

我取而代之的是:

<p>mon</p>
<p>beau sapin</p>
<p>roi des forêts</p>
<p>que</p>
<p>beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>

我尝试了各种方法,比如删除第一行和最后一行的<p></p>(EditorKit.read自己添加它),使用editorKit.insertHTML(但我应该放什么样的标签?),插入逐行(大部分时间,我在另一个p内获得p)等。

但真正的问题是,无法在htmlDocument中编写您想要的内容。如何在指定位置写sapin</p> <p>roi? EditorKit.read?它会添加<p>sapin</p> <p>roi</p> Editorkit.insertHTML?我需要精确包装标签......

我告诉你我的最后一次尝试:

    private static void insertHTMLContent(JMathTextPane jtp, String html, int offset) {
        Document doc = Jsoup.parse(html);
        Elements elts = doc.body().children();
        //unwrap the last and first element
        if(elts.size()>2) { elts.last().unwrap(); }
        if(elts.size()>=1) { elts.first().unwrap(); }
        //We add a fake DIV element and remove it just at the next line
        editorKit.insertHTML(jtp.htmlDoc, offset, "<div id='copie'>"+doc.body().html()+"</div>", 0, 0, HTML.Tag.DIV);
        jtp.getHTMLdoc().setOuterHTML(jtp.getHTMLdoc().getElement("copie"),doc.body().html());
    }

我无法向您显示结果:EditorKit.write尝试自行修复html。但HTMLDocument完全凌乱。

您可以尝试:

public class Test {

private static JTextPane editor = new Editor();
private static JMenuBar menu = new Menu();
private static String clipboard = "";

private static Action copy = new Copy();
private static Action paste = new Paste();

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setContentPane(editor);
    f.setJMenuBar(menu);
    f.setSize(600, 400);
    f.setVisible(true);
}

public static class Editor extends JTextPane {
    public Editor() {
        this.setDocument(new HTMLDocument());
        this.setEditorKit(new HTMLEditorKit());
    }
}

public static class Menu extends JMenuBar {
    public Menu() {
        add(new JButton(copy));
        add(new JButton(paste));

        getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "copy");
        getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "paste");
        getActionMap().put("copy", copy);
        getActionMap().put("paste", paste);
    }
}

public static class Copy extends AbstractAction {
    public Copy() {super("copy");}
    @Override
    public void actionPerformed(ActionEvent e) {
        StringWriter w = new StringWriter();
        try {
            editor.getEditorKit().write(w, editor.getDocument(), editor.getCaretPosition(), editor.getSelectedText().length());
        } catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
        clipboard = w.toString();
    }
}
public static class Paste extends AbstractAction {
    public Paste() {super("paste");}
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            editor.getEditorKit().read(new StringReader(clipboard), editor.getDocument(), editor.getCaretPosition());
        } catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
    }
}
}

抱歉,我很久。我接受任何帮助。

2 个答案:

答案 0 :(得分:1)

恐怕没有简单的方法。当您粘贴时,您希望保留原始段落并避免新的<p>创建,对吧?问题是当前段落和复制的段落可能具有不同的属性。例如。当前左对齐,但复制了一个右对齐。

如何解决此案?要简化此工具包,只需创建<p>元素。

您可以尝试从剪贴板内容创建一个独立的HTMLDocument,并迭代文档的结构提取元素(段落和文本)并将它们插入到原始文档中。

答案 1 :(得分:0)

对于进一步的读者,我用一个非常简单的技巧解决了它。我刚刚删除了粘贴文本之前和之后添加的\n

public static void copyContent(JTextPane jtp, String text, int offset) {
    try {
        boolean start = offset>0 ? !jtp.getText(offset-1, 1).equals("\n") : false;
        Position p = jtp.getDocument().createPosition(offset);
        new HTMLEditorKit().read(new StringReader(html), jtp.getHTMLdoc(), offset);
        if(start) {jtp.getDocument().remove(offset, 1);}
        if(offset>0) {jtp.getDocument().remove(p.getOffset()-1, 1);}
    } catch (IOException | BadLocationException ex) {
        Logger.getLogger(EditeurIO.class.getName()).log(Level.SEVERE, null, ex);
    }
}