I'm faithing with JEditorPane
. I need simple editor. I've solved the problem with loading and modified HTML containing custom (two) tags (see my older post). It displays the document properly and I can even edit it now. I can write text, delete either characters or my custom elements. I won a battle, but haven't won the war. The next step is regrettably very problematical too. I'm unable to insert my custom tags.
I have a custom action:
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);
}
}
}
It workts properly. I can insert the span
element. But I cannot insert non-standard tag in this way. I can insert just code
, span
and so on, but not my tag. For my tag I'm forced to use this:
ekit.insertHTML(doc, offset, "x<bar medium=\"#DEFAULT\" type=\"packaged\" source=\"identifier\" />x", 0, 0, null);
There are two critical problems
- The custom tag must be bounded with non-whispace characters (here x)
- The current element's body is split
When I insert span
element into <p>paragraph</p>
, I get <p>par<span>ahoj</span>agraph</p>
as expected. Howerever unknown tag is allways inserted as child of body
element and the result (e.g. for unknown tag x
) is <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...