Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Getting the cursor position in the current editor
Getting the cursor position in the current editor [message #518241] Wed, 03 March 2010 15:17 Go to next message
sameer is currently offline sameerFriend
Messages: 4
Registered: March 2010
Location: Switzerland
Junior Member
Hi ,

I am making some application in Java in which I have to get the cursor position or a mouseclick position in the current active editor... I created a very small code to start with

public class MouseTest {

public static void main(String[] args) {

System.out.println(PlatformUI.isWorkbenchRunning());
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
if (editor instanceof ITextEditor) {
ISelectionProvider selectionProvider = ((ITextEditor)editor).getSelectionProvider();
ISelection selection = selectionProvider.getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection)selection;
int offset = textSelection.getOffset(); // etc.
}
}
System.out.println( "hi --");
}
}


But when I run this application I get an error
Exception in thread "main" java.lang.IllegalStateException: Workbench has not been created yet.
Re: Getting the cursor position in the current editor [message #518543 is a reply to message #518241] Thu, 04 March 2010 12:38 Go to previous messageGo to next message
Remy Suen is currently offline Remy SuenFriend
Messages: 462
Registered: July 2009
Senior Member
sameer wrote on Wed, 03 March 2010 10:17
But when I run this application I get an error
Exception in thread "main" java.lang.IllegalStateException: Workbench has not been created yet.

Sameer, as the error implies, the workbench is not up. Generally speaking, you cannot refer to random Eclipse APIs in the context of a generic Java application. All you have is a main method. You need to be running from an OSGi. Instead of writing a regular Java application with a main method, you should be creating plug-ins that contribute functionality instead.
Re: Getting the cursor position in the current editor [message #518885 is a reply to message #518543] Fri, 05 March 2010 11:25 Go to previous messageGo to next message
sameer is currently offline sameerFriend
Messages: 4
Registered: March 2010
Location: Switzerland
Junior Member
hey thanx ... I was not getting it earlier .... I was making a java application ..
I made it as a plug in and then was able to run it successfully ...
Re: Getting the cursor position in the current editor [message #519453 is a reply to message #518885] Mon, 08 March 2010 21:01 Go to previous messageGo to next message
Kitesurfer () is currently offline Kitesurfer ()Friend
Messages: 87
Registered: December 2009
Member
Hi,
I need to do the same thing (get the current line and columnnumber).
With textSelection.getStartLine (plus 1) I get the current line.
How about the current column where the cursor is ? I mean textSelection.getOffset returns the offset, where a tab is one position, as on the screen a tab is more then one position.

Is there a way to prevent Tab-characters in the editor-text (when the user presses the Tab-button that it is converted to the right number of spaces) ?
Re: Getting the cursor position in the current editor [message #527458 is a reply to message #519453] Thu, 15 April 2010 10:55 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 100
Registered: March 2010
Senior Member
Hello Kitesurfer

I make a editor too and i dont know to gets of columnos number

have you make this??

Thanks a lot


Jose

[Updated on: Thu, 15 April 2010 10:55]

Report message to a moderator

Re: Getting the cursor position in the current editor [message #527817 is a reply to message #527458] Fri, 16 April 2010 15:03 Go to previous message
Kitesurfer () is currently offline Kitesurfer ()Friend
Messages: 87
Registered: December 2009
Member
With this code you can the current linenumber and offset (offset is offset in the whole editortext) :
lcSelectionProvider = ((ITextEditor)lcEditor).getSelectionProvider();
selection = lcSelectionProvider.getSelection();
if (selection instanceof ITextSelection) {
textSelection = (ITextSelection)selection;
offSet = textSelection.getOffset(); // etc.
lineNr = textSelection.getStartLine();
}

You can get the number of spaces for each tab-char in file : \WORKSPACE Java\.settings\org.eclipse.ui.editors.prefs
Lines:
spacesForTabs=true
tabWidth=13 (when it's not there its value is 4)

So with this info you can do it all.

PS. It would have been handier if they added a getColumn method to ITextSelection.
Previous Topic:referenced projects and user libraries
Next Topic:[ant] [eclipse 3.4] Build does not include external dependencies
Goto Forum:
  


Current Time: Thu Apr 25 01:56:12 GMT 2024

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

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

Back to the top