内联复制粘贴JEditorPane HTML [英] inline copy paste JEditorPane HTML
问题描述
我绝望地试图在HTML模式下在JTextPane中实现自定义的复制/粘贴。
大部分工作正常,我使用EditorKit.write()获取html内容,我使用editorKit.read()粘贴它。完美的世界。
I'm desperatly trying to implement a customized copy/paste in a JTextPane in HTML mode. The most part is working well, I get the html content using EditorKit.write(), I paste it using editorKit.read(). Perfect world.
但是当我有:< p>在我的编辑器中测试< / p>
,我尝试复制es以获得
< p> tesest< / p>
,我取而代之
BUT, when I have : <p> test </p>
in my editor and I try to copy "es" to obtain
<p> tesest </p>
, I obtain instead
<p>tes</p>
<p>es</p>
<p>t</p>
知道,我试图找出一种方法来粘贴内联部分应该是内联,并阻止复制期间阻止的部分。通常,如果我有:
Knowing that, I m trying to figure out a way to paste "inline" the part supposed to be inline, and in block the part which was in block during the copy. Typically,
if I have :
<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>
And paste it after "mon", I expect :
<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>< /第一行和最后一行(EditorKit.read自己添加它)的p>
,使用editorKit.insertHTML(但是我应该放什么样的标签?),逐行插入(大部分次,我在另一个 p
中获得一个 p
I tried various approach, like removing the <p></p>
of the first and last lines (EditorKit.read add it back by itself), using editorKit.insertHTML (but what kind of Tag should I put ?), insert line by line (most part of times, I obtain a p
inside another p
) etc.
但真正的问题是无法在htmlDocument中写出所需的内容。我如何写 sapin< / p> < p> roi
在指定的位置?
EditorKit.read?它将添加< p> sapin< / p> < p> roi< / p>
Editorkit.insertHTML?我需要精确的一个包装标签...
but the real problem that it's impossible to write what you want in the htmlDocument. How can I write sapin</p> <p>roi
at a specified position ?
EditorKit.read ? it will add <p>sapin</p> <p>roi</p>
Editorkit.insertHTML ? I need to precise a wrapping Tag...
我给你看看我最后一次尝试:
I show you my last try :
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是完全凌乱的。
I can't show you the result : EditorKit.write tries to fix the html by itself. But the HTMLDocument is completely messy.
要尝试:
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);}
}
}
}
对不起我很久我接受任何帮助。
Sorry I was long. I accept any help.
推荐答案
对于更多的读者,我用一个很简单的方法解决了这个问题。我刚刚删除了在需要时粘贴的文本之前和之后添加的 \\\
。
For further readers, I solved it with a very easy trick. I just removed the \n
added before and after the pasted text when needed.
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);
}
}
这篇关于内联复制粘贴JEditorPane HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!