English 中文(简体)
JEditorPane,HTMLEditor Kit - 习俗行动
原标题:JEditorPane, HTMLEditorKit - custom action inserting custom tag

I m Religioning with JEditorPane. 我需要简单的编辑。 我先解决了这一问题,并修改了含有习惯(两)标签的超文本(见my Senior post )。 它恰当地展示了该文件,现在我甚至可以it。 我可以撰写案文,删除文字或习惯内容。 我赢得了一场战斗,但 t子赢得了战争。 下一个步骤令人遗憾地也非常成问题。 我无法插入我的习俗标签。

我有习惯行动:

import my.own.HTMLEditorKit; //extends standard HTMLEditorKit
import my.own.HTMLDocument; //extends standard HTMLDocument

class InsertElementAction extends StyledTextAction {
    private static final long serialVersionUID = 1L;

    public InsertElementAction(String actionName) {
        super(actionName);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JEditorPane editor = getEditor(e);

        if (editor == null)
            return;

        HTMLDocument doc = (HTMLDocument) editor.getDocument();
        HTMLEditorKit ekit = (HTMLEditorKit) editor.getEditorKit();
        int offset = editor.getSelectionStart();

        try {
            ekit.insertHTML(doc, offset, "<span>ahoj</span>", 0, 0, HTML.Tag.SPAN);
            Element ele = doc.getRootElements()[0];
            ele = ele.getElement(1).getElement(0);
            doc.setInnerHTML(ele, "<bar medium="#DEFAULT" type="packaged" source="identifier" />");
        }
        catch (BadLocationException ble) {
            throw new Error(ble);
        }
        catch (IOException ioe) {
            throw new Error(ioe);
        }
    }
}

它工作得当。 页: 1 但我不能以这种方式插入非标准标签。 我可以仅插入<条码><>代码/代码>,span,等等,但不是我的标签。 我的帽子Im被迫使用:

ekit.insertHTML(doc, offset, "x<bar medium="#DEFAULT" type="packaged" source="identifier" />x", 0, 0, null);

有两个关键问题:

  1. The custom tag must be bounded with non-whispace characters (here x)
  2. The current element s body is split

在I插入span 元素后插入<p>/p><p>par<span>ahoj</span>agraph</p>。 Howerever known tag is allways included as child of one/code> elements and the result (e.g. for known tag x;p>par</p><x>ahoj</x><p>agraph</p>

The work is dead exhausting. I m faithing with this relatively simple task since weeks. I m already wasted. If the insertion won t to work, I can scrap it all...

最佳回答

我找到了工作。 插手方式如下:

ModifiedHTMLDocument doc = (ModifiedHTMLDocument) editor.getDocument();
int offset = editor.getSelectionStart();
//insert our special tag (if the tag is not bounded with non-whitespace character, nothing happens)
doc.insertHTML(offset, "-<specialTag />-");
//remove leading and trailing minuses
doc.remove(offset, 1); //at the current position is the minus before tag inserted
doc.remove(offset + 1, 1); //the next sign is minus after new tag (the tag is nowhere)
//Note: no, you really cannot do that: doc.remove(offset, 2), because then the tag is deleted

缩略语 包含一种方法insert sediment(),该方法称:

public void insertHTML(int offset, String htmlText) throws BadLocationException, IOException {
    if (getParser() == null)
        throw new IllegalStateException("No HTMLEditorKit.Parser");

    Element elem = getCurrentElement(offset);

    //the method insertHTML is not visible
    try {
        Method insertHTML = javax.swing.text.html.HTMLDocument.class.getDeclaredMethod("insertHTML",
                new Class[] {Element.class, int.class, String.class, boolean.class});
        insertHTML.setAccessible(true);
        insertHTML.invoke(this, new Object[] {elem, offset, htmlText, false});
    }
    catch (Exception e) {
        throw new IOException("The method insertHTML() could not be invoked", e);
    }
}

我们最后的砖箱是寻找目前要素的一种方法:

public Element getCurrentElement(int offset) {
    ElementIterator ei = new ElementIterator(this);
    Element elem, currentElem = null;
    int elemLength = Integer.MAX_VALUE;

    while ((elem = ei.next()) != null) { //looking for closest element
        int start = elem.getStartOffset(), end = elem.getEndOffset(), len = end - start;
        if (elem.isLeaf() || elem.getName().equals("html"))
            continue;
        if (start <= offset && offset < end && len <= elemLength) {
            currentElem = elem;
            elemLength = len;
        }
    }

    return currentElem;
}

这种方法也是<代码>的成员。 经修订的超文本文件类别。

The solution is not pure, but it solves provisionally the problem. I hope I ll find a better kit. I m thinking about JWebEngine. That should be replacement for current poor HTMLEditorKit, but I don t know, whether it allows me to add my custom tags.

问题回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...