Pointer Off Side When Inserting an Element

Having trouble installing Oxygen? Got a bug to report? Post it all here.
gymac
Posts: 19
Joined: Fri Dec 10, 2010 2:11 am

Pointer Off Side When Inserting an Element

Post 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!
Radu
Posts: 9446
Joined: Fri Jul 09, 2004 5:18 pm

Re: Pointer Off Side When Inserting an Element

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
gymac
Posts: 19
Joined: Fri Dec 10, 2010 2:11 am

Re: Pointer Off Side When Inserting an Element

Post by gymac »

Radu,

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