Search code examples
javaswingjeditorpanehtmleditorkit

JEditorPane, HTMLEditorKit - custom action inserting custom tag


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

  1. The custom tag must be bounded with non-whispace characters (here x)
  2. 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...


Solution

  • I've found a workaround. The tag is inserted this way:

    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 ModifiedHTMLDocument contains a method insertHTML(), which calls the medhod hidden by reflection:

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

    The last piece of our brick-box is a method looking for the current element:

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

    This method is also a member of the ModifiedHTMLDocument class.

    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.