Page 1 of 1

Pointer Off Side When Inserting an Element

Posted: Mon Mar 07, 2011 9:33 pm
by gymac
Hi all,

I just created a java class, that can insert certain kinds of tags into a particular document type.

Now everything works perfectly except for when I insert this into a particular position. For example, the original xml file looks like:

<p><i> Text </i></p>

and I switch to the author mode, and I put my cursor after <P> but before <i> and insert the element. Ideally, the element inserted should reside between <p> and <i>, however, after insertion,the element will always be after the <i> instead of <p>.

This seems to be a small problem, but I've re-checked my Java class, and the only thing that may relate to this problem is the following code:

int caretPosition = authorAccess.getCaretOffset();
authorAccess.insertXMLFragment(mediaXML, caretPosition);

which looks correct to me.

So I would be grateful if anyone could help me debug. Thanks in advance!

Re: Pointer Off Side When Inserting an Element

Posted: Tue Mar 08, 2011 11:35 am
by Radu
Hi,

Probably the caret position is not between the <p> and the <i>.
For example in the Author page you can switch from the toolbar the Tags display to Full tags and see exactly where the caret is before invoking the operation.

You can also adjust the insert position a bit in this particular case like:

Code: Select all



int caretPosition = authorAccess.getEditorAccess().getCaretOffset();
//Check if the caret position is right inside the <i> like:
//<p><i>{CARET}text</i></p>
//And adjust it if necessary
AuthorNode nodeAtOffset = authorAccess.getDocumentController().getNodeAtOffset(caretPosition);
if(nodeAtOffset.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
AuthorElement elemAtOffset = (AuthorElement) nodeAtOffset;
if(elemAtOffset.getName().equals("i")) {
//Inside an italic
if(elemAtOffset.getStartOffset() + 1 == caretPosition) {
//The case in which the caret position is right after the <i> tag.
AuthorNode parentOfI = authorAccess.getDocumentController().getNodeAtOffset(caretPosition - 1);
if(parentOfI.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
AuthorElement parentAtOffset = (AuthorElement) parentOfI;
if(parentAtOffset.getName().equals("p")) {
//We'll adjust the position where the insertion will take place
caretPosition --;
}
}
}
}
}
Regards,
Radu

Re: Pointer Off Side When Inserting an Element

Posted: Tue Mar 08, 2011 7:30 pm
by gymac
Radu,

Thanks a lot for the code, which is very helpful.