Moving Cursor on Page change

Post here questions and problems related to oXygen frameworks/document types.
lhopkins
Posts: 4
Joined: Thu Nov 16, 2023 10:28 pm

Moving Cursor on Page change

Post 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!
sorin_carbunaru
Posts: 402
Joined: Mon May 09, 2016 9:37 am

Re: Moving Cursor on Page change

Post 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
lhopkins
Posts: 4
Joined: Thu Nov 16, 2023 10:28 pm

Re: Moving Cursor on Page change

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

Re: Moving Cursor on Page change

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply