4

私はを信じていJEditorPaneます。簡単なエディタが必要です。カスタム(2)タグを含むHTMLのロードと変更に関する問題を解決しました(以前の投稿を参照)。ドキュメントが正しく表示され、編集することもできます。テキストを書いたり、文字やカスタム要素を削除したりできます。私は戦いに勝ったが、戦争には勝てなかった。次のステップも残念ながら非常に問題があります。カスタムタグを挿入できません。

カスタムアクションがあります:

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);
        }
    }
}

正しく動作します。要素を挿入できspanます。しかし、この方法で非標準のタグを挿入することはできません。code、などを挿入できますspanが、タグは挿入できません。私のタグには、これを使用する必要があります。

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

2つの重大な問題があります

  1. カスタムタグは、ウィスペース以外の文字(ここではx)で囲む必要があります
  2. 現在の要素の本体が分割されます

spanに要素を挿入すると、期待どおり<p>paragraph</p>になり<p>par<span>ahoj</span>agraph</p>ます。ただし、不明なタグは常にbody要素の子として挿入され、結果(不明なタグxなど)は<p>par</p><x>ahoj</x><p>agraph</p>です。

仕事は疲れ果てています。私は数週間以来、この比較的単純なタスクを信じています。私はすでに無駄になっています。挿入が機能しない場合は、すべて廃棄できます...

4

2 に答える 2

2

これがhttp://java-sl.com/custom_tag_html_kit.htmlに役立つことを願っています

于 2011-09-30T10:35:02.260 に答える
2

回避策を見つけました。タグは次のように挿入されます。

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

Myには、リフレクションによって隠されている medhod を呼び出すModifiedHTMLDocumentmethod が含まれています。insertHTML()

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;
}

このメソッドもModifiedHTMLDocumentクラスのメンバーです。

解決策は純粋ではありませんが、暫定的に問題を解決します。もっと良いキットが見つかることを願っています。JWebEngineについて考えています。これは現在の poor の代わりになるはずですがHTMLEditorKit、カスタム タグを追加できるかどうかはわかりません。

于 2011-10-03T10:20:01.293 に答える