私はを信じてい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つの重大な問題があります
- カスタムタグは、ウィスペース以外の文字(ここではx)で囲む必要があります
- 現在の要素の本体が分割されます
span
に要素を挿入すると、期待どおり<p>paragraph</p>
になり<p>par<span>ahoj</span>agraph</p>
ます。ただし、不明なタグは常にbody
要素の子として挿入され、結果(不明なタグx
など)は<p>par</p><x>ahoj</x><p>agraph</p>
です。
仕事は疲れ果てています。私は数週間以来、この比較的単純なタスクを信じています。私はすでに無駄になっています。挿入が機能しない場合は、すべて廃棄できます...