Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [SOLVED] Getting Current Cursor Location
icon14.gif  [SOLVED] Getting Current Cursor Location [message #765311] Tue, 13 December 2011 20:22 Go to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi,

Is there an Eclipse API that I can use which will return me the current location of the cursor on the selected (i.e., focused, active) Eclipse editor?
The results can be something like:
- offset: 315
or
- SomeClassFile.java:315

Thanks in advance, best regards,

[Updated on: Wed, 14 December 2011 12:31]

Report message to a moderator

Re: Getting Current Cursor Location [message #765509 is a reply to message #765311] Wed, 14 December 2011 08:18 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 13.12.2011 21:22, Kivanc Muslu wrote:
> Hi,
> Is there an Eclipse API that I can use which will return me the
> current location of the cursor on the selected (i.e., focused, active)
> Eclipse editor? The results can be something like:
> - offset: 315 or
> - SomeClassFile.java:315
See org.eclipse.ui.texteditor.ITextEditor.getSelectionProvider().

Dani
>
> Thanks in advance, best regards,
Re: Getting Current Cursor Location [message #765579 is a reply to message #765509] Wed, 14 December 2011 10:29 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

Though this partly provides what I need, I cannot get the changes done on the cursor (i.e., exact movement path of the cursor on the screen) with this. After adding a listener to a file, I only get if something is selected on that particular file.

I realized that my initial question might be misleading. My problem is following. Let's say that I have a file open in Eclipse and the cursor is on offset 10. I want to be able to get all changes on the cursor (i.e., its new location) as user changes it on the screen.

e.g.,
User moves the cursor by mouse to offset 45 (without selecting any text), I want to get offset = 45
User hits down key, I want to get: offset = 20 (assuming that one line is 10 characters long), etc.

Is there an API (listener) for this?

Thanks,
Re: Getting Current Cursor Location [message #765590 is a reply to message #765579] Wed, 14 December 2011 10:54 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 14.12.2011 11:29, Kivanc Muslu wrote:
> Hi Dani,
> Though this partly provides what I need, I cannot get the changes done
> on the cursor (i.e., exact movement path of the cursor on the screen)
> with this. After adding a listener to a file, I only get if something
> is selected on that particular file.
>
> I realized that my initial question might be misleading. My problem is
> following. Let's say that I have a file open in Eclipse and the cursor
> is on offset 10. I want to be able to get all changes on the cursor
> (i.e., its new location) as user changes it on the screen.
Add a post selection listener. Either add it on the (post) selection
provider you get from the editor or ask the editor site for the
selection service and add it there. See org.eclipse.ui.ISelectionService
for details.

Dani
>
> e.g., User moves the cursor by mouse to offset 45 (without selecting
> any text), I want to get offset = 45
> User hits down key, I want to get: offset = 20 (assuming that one line
> is 10 characters long), etc.
>
> Is there an API (listener) for this?
>
> Thanks,
Re: Getting Current Cursor Location [message #765636 is a reply to message #765590] Wed, 14 December 2011 12:30 Go to previous message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

It works beautifully, thanks a lot for the help.

For people who have the same problem (or might have the same problem), here is some more detailed code:

Adding the listener:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
window.getSelectionService().addPostSelectionListener(new SynchronizerCursorListener());


where the listener is implemented as:
public class SynchronizerCursorListener implements ISelectionListener
{
    @Override
    public void selectionChanged(IWorkbenchPart part, ISelection selection)
    {
        if (selection instanceof TextSelection)
        {
            TextSelection textSelection = (TextSelection) selection;
            int offset = textSelection.getOffset();
            // Do something with offset
        }
        // Not a text selection event, ignore.
    }
}
Previous Topic:RCP export product
Next Topic:How to run unit tests of underlying packages
Goto Forum:
  


Current Time: Thu Apr 25 01:05:54 GMT 2024

Powered by FUDForum. Page generated in 0.03185 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top