Page 1 of 1

Moving Cursor on Page change

Posted: Tue Feb 27, 2024 11:31 pm
by lhopkins
Hi,
I'm working on a plugin method that reads a Processing Instruction which is created when the user saves in the editor. The next time they open the document, I would like to have the cursor move to the start offset of the node where I find the PI. So far I can save the instruction just fine, but I cannot seem to find a way to move the cursor. I did see that there is a deprecated method using AuthorAccess and a MoveCaretOperation Interface, but I have not found a way to implement this behavior.

Is there a way to do this in a WorkspaceAccess plugin?

Thank you!

Re: Moving Cursor on Page change

Posted: Wed Feb 28, 2024 10:55 am
by sorin_carbunaru
Hi,

Have you taken a look at ro.sync.exml.workspace.api.editor.page.WSTextBasedEditorPage.setCaretPosition(int)?

You can use it in a code similar to this one:

Code: Select all

    WSEditor editor = PluginWorkspaceProvider.getPluginWorkspace()
        .getCurrentEditorAccess(PluginWorkspace.MAIN_EDITING_AREA);
    if (editor != null) {
      WSEditorPage currentPage = editor.getCurrentPage();
      if (currentPage instanceof WSTextBasedEditorPage) {
        int position = computeCaretPosition();
        ((WSTextBasedEditorPage) currentPage).setCaretPosition(position);
      }
    }
Best wishes,
Sorin Carbunaru
Oxygen XML Editor

Re: Moving Cursor on Page change

Posted: Wed Feb 28, 2024 4:57 pm
by lhopkins
Thank you! I looked over using that, but that did work to get the position and attempt to set it. :D
The only issue is that on editorPageChanged I use the method like you provided and the caret jumps to where I wanted, but then immediately jumps back to the top of the document. Is there another event firing after the page change that would cause the cursor to be moved again?

Re: Moving Cursor on Page change

Posted: Mon Mar 04, 2024 10:04 am
by Radu
Hi,
From what I remember Oxygen should also try to restore the caret position to the last location where it was in the document when it was last closed.
Maybe you can use invoke later to post pone a bit setting the caret to the custom location where you want to set it:

Code: Select all

      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          ((WSTextBasedEditorPage) currentPage).setCaretPosition(position);
        }
      });
Regards,
Radu