Page 1 of 1

place cursor in text mode by API

Posted: Thu Jun 08, 2017 3:26 pm
by Patrik
Hi,

I am implementing a AuthorOperation which should jump to a specific element within an xml file that needs to be opened in text mode.
I already managed to open the file:

Code: Select all

PluginWorkspaceProvider.getPluginWorkspace().open(url, EditorPageConstants.PAGE_TEXT);
(and also to modify the content with editor.reloadContent()

But how can I place the cursor at a specific position? The target element even has an id attribute. So basically I want to trigger the behavior is if i'm clicking on a link "my-file.xml#my-id".

Thanks and regards,
Patrik

Re: place cursor in text mode by API

Posted: Thu Jun 08, 2017 4:09 pm
by Radu
Hi Patrik,

After the file is opened you can do something like:

Code: Select all

    WSEditor editorAccess = PluginWorkspaceProvider.getPluginWorkspace().getEditorAccess(url, PluginWorkspace.MAIN_EDITING_AREA);
if(editorAccess != null){
WSEditorPage currentPage = editorAccess.getCurrentPage();
if(currentPage instanceof WSXMLTextEditorPage){
WSXMLTextEditorPage xmlTP = (WSXMLTextEditorPage) currentPage;
WSXMLTextNodeRange[] ranges = xmlTP.findElementsByXPath("//*[@id='myID']");
if(ranges != null && ranges.length > 0){
int startLine = ranges[0].getStartLine();
int startColumn = ranges[0].getStartColumn();
int offsetInDoc = xmlTP.getOffsetOfLineStart(startLine) + startColumn;
//This should place the caret immediately before the element.
xmlTP.setCaretPosition(offsetInDoc);
xmlTP.scrollCaretToVisible();
}
}
}
Regards,
Radu

Re: place cursor in text mode by API

Posted: Thu Jun 08, 2017 4:48 pm
by Patrik
Thanks alot, Radu. This works fine - and knowing about the WSXMLTextNodeRange solves some other problems as well... :D

Regards,
Patrik