Skip to main content



      Home
Home » Newcomers » Newcomers » How to get selected text from a java editor window
How to get selected text from a java editor window [message #63368] Thu, 12 May 2005 04:30 Go to next message
Eclipse UserFriend
Originally posted by: afluegge.vitria.com

Hallo,

trivial question but difficult to solve for a newbie: I want to be able to
select some text in a java source file and in my plugin I want to have a
button that takes that text and puts it in a list box (in the plugins view).
The problem I am faced with is how to get a hand on the selected text. The
other problem is the other way around: there is some text in the plugins
list box and I want to put it into the java editor at the current text
marker location.

Anyone out there who can help me out with a few lines of sample code?

Regards,
Andreas
Re: How to get selected text from a java editor window [message #63486 is a reply to message #63368] Thu, 12 May 2005 08:24 Go to previous messageGo to next message
Eclipse UserFriend
Andreas Fluegge wrote:
> trivial question but difficult to solve for a newbie: I want to be able to
> select some text in a java source file and in my plugin I want to have a
> button that takes that text and puts it in a list box (in the plugins view).
> The problem I am faced with is how to get a hand on the selected text. The
> other problem is the other way around: there is some text in the plugins
> list box and I want to put it into the java editor at the current text
> marker location.

String getSelectedText(ITextEditor editor) {
return getSelection(editor).getText();
}

ITextSelection getSelection(ITextEditor editor) {
ISelection selection = editor.getSelectionProvider()
.getSelection();
return (ITextSelection) selection;
}

void setStringAtSelection(ITextEditor editor, String text) {
ITextSelection selection= getSelection(editor);
IDocument document = editor.getDocumentProvider()
.getDocument(editor.getEditorInput());
document.replace(selection.getOffset(),
selection.getLength(), text);
}
Re: How to get selected text from a java editor window [message #64215 is a reply to message #63486] Sat, 14 May 2005 12:51 Go to previous message
Eclipse UserFriend
Originally posted by: Andreas.Fluegge.T-Online.de

Tom,

thanks for your reply. The trick now is to get the ITextEditor, right?
Well, to be honest I have no clue. Can you or someone else please help me
out with finding the ITextEditor of the currently visible text editor?

Many thanks,
Andreas


"Tom Eicher" <eclipse@tom.eicher.name> wrote in message
news:d5vier$j9$1@news.eclipse.org...
> Andreas Fluegge wrote:
>> trivial question but difficult to solve for a newbie: I want to be able
>> to select some text in a java source file and in my plugin I want to have
>> a button that takes that text and puts it in a list box (in the plugins
>> view). The problem I am faced with is how to get a hand on the selected
>> text. The other problem is the other way around: there is some text in
>> the plugins list box and I want to put it into the java editor at the
>> current text marker location.
>
> String getSelectedText(ITextEditor editor) {
> return getSelection(editor).getText();
> }
>
> ITextSelection getSelection(ITextEditor editor) {
> ISelection selection = editor.getSelectionProvider()
> .getSelection();
> return (ITextSelection) selection;
> }
>
> void setStringAtSelection(ITextEditor editor, String text) {
> ITextSelection selection= getSelection(editor);
> IDocument document = editor.getDocumentProvider()
> .getDocument(editor.getEditorInput());
> document.replace(selection.getOffset(),
> selection.getLength(), text);
> }
Previous Topic:Eclipse 3.0.2 could not start on the ext filesystem
Next Topic:Error trying to run a Java Project
Goto Forum:
  


Current Time: Thu May 08 17:31:08 EDT 2025

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

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

Back to the top