Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » IJavaElement from ITextSelection
IJavaElement from ITextSelection [message #249751] Wed, 21 November 2007 17:29 Go to next message
Jeffrey K Czyz is currently offline Jeffrey K CzyzFriend
Messages: 6
Registered: July 2009
Junior Member
Hello all,

Is there a simple way of obtaining the IJavaElement associated with the
currently selected text? The only way I can accomplish this is using an
internal class, which I would like to avoid:

if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
IWorkbenchPage activePage =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
if (activePage != null) {
IEditorPart editor = activePage.getActiveEditor();
if (editor instanceof CompilationUnitEditor) {
CompilationUnitEditor unitEditor = (CompilationUnitEditor) editor;
IJavaElement element = (IJavaElement) unitEditor.getViewPartInput();
}
}
}

In the above code, CompilationUnitEditor is internal. Any help would be
appreciated.

Jeff
Re: IJavaElement from ITextSelection [message #249757 is a reply to message #249751] Thu, 22 November 2007 08:31 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Jeffrey K. Czyz wrote:

> Hello all,
>
> Is there a simple way of obtaining the IJavaElement associated with
> the currently selected text? The only way I can accomplish this is
> using an internal class, which I would like to avoid:
>
> if (selection instanceof ITextSelection) {
> ITextSelection textSelection = (ITextSelection) selection;
> IWorkbenchPage activePage =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
> if (activePage != null) {
> IEditorPart editor = activePage.getActiveEditor();
> if (editor instanceof CompilationUnitEditor) {
> CompilationUnitEditor unitEditor = (CompilationUnitEditor) editor;
> IJavaElement element = (IJavaElement)
> unitEditor.getViewPartInput();

I'm not sure about which CompilationUnitEditor you're talking about but
the one I know does not have a getViewPartInput() method. Anyway, what
you should use ist:
typeRoot= org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot(IEditorInpu t)
typeRoot.codeSelect(selectionOffset, selectionLength)

Dani

> }
> }
> }
>
> In the above code, CompilationUnitEditor is internal. Any help would
> be appreciated.
>
> Jeff
Re: IJavaElement from ITextSelection [message #249828 is a reply to message #249757] Mon, 26 November 2007 19:44 Go to previous messageGo to next message
Jeffrey K Czyz is currently offline Jeffrey K CzyzFriend
Messages: 6
Registered: July 2009
Junior Member
Daniel Megert wrote:
> Jeffrey K. Czyz wrote:
>
>> Hello all,
>>
>> Is there a simple way of obtaining the IJavaElement associated with
>> the currently selected text? The only way I can accomplish this is
>> using an internal class, which I would like to avoid:
>>
>> if (selection instanceof ITextSelection) {
>> ITextSelection textSelection = (ITextSelection) selection;
>> IWorkbenchPage activePage =
>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
>> if (activePage != null) {
>> IEditorPart editor = activePage.getActiveEditor();
>> if (editor instanceof CompilationUnitEditor) {
>> CompilationUnitEditor unitEditor = (CompilationUnitEditor) editor;
>> IJavaElement element = (IJavaElement)
>> unitEditor.getViewPartInput();
>
> I'm not sure about which CompilationUnitEditor you're talking about but
> the one I know does not have a getViewPartInput() method. Anyway, what
> you should use ist:
> typeRoot= org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot(IEditorInpu t)
> typeRoot.codeSelect(selectionOffset, selectionLength)
>
> Dani
>

Hi Dani,

Thanks for the reply. CompilationUnitEditor is a class in the internal
package org.eclipse.jdt.internal.ui.javaeditor. I wanted to avoid using it.

The example code you gave uses JavaUI.getEditorInputTypeRoot(IEditorInput),
which is new in Eclipse 3.4. Is there a different way of accomplishing this
using Eclipse 3.3?

Thanks,

Jeff


>> }
>> }
>> }
>>
>> In the above code, CompilationUnitEditor is internal. Any help would
>> be appreciated.
>>
>> Jeff
Re: IJavaElement from ITextSelection [message #249835 is a reply to message #249828] Tue, 27 November 2007 08:50 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Jeffrey K. Czyz wrote:

> Daniel Megert wrote:
>
>> Jeffrey K. Czyz wrote:
>>
>>> Hello all,
>>>
>>> Is there a simple way of obtaining the IJavaElement associated with
>>> the currently selected text? The only way I can accomplish this is
>>> using an internal class, which I would like to avoid:
>>>
>>> if (selection instanceof ITextSelection) {
>>> ITextSelection textSelection = (ITextSelection) selection;
>>> IWorkbenchPage activePage =
>>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
>>> if (activePage != null) {
>>> IEditorPart editor = activePage.getActiveEditor();
>>> if (editor instanceof CompilationUnitEditor) {
>>> CompilationUnitEditor unitEditor = (CompilationUnitEditor)
>>> editor;
>>> IJavaElement element = (IJavaElement)
>>> unitEditor.getViewPartInput();
>>
>>
>> I'm not sure about which CompilationUnitEditor you're talking about
>> but the one I know does not have a getViewPartInput() method. Anyway,
>> what you should use ist:
>> typeRoot=
>> org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot(IEditorInpu t)
>> typeRoot.codeSelect(selectionOffset, selectionLength)
>>
>> Dani
>>
>
> Hi Dani,
>
> Thanks for the reply. CompilationUnitEditor is a class in the
> internal package org.eclipse.jdt.internal.ui.javaeditor.

I know that. But this one does not have a getViewPartInput() method.

> I wanted to avoid using it.
>
> The example code you gave uses
> JavaUI.getEditorInputTypeRoot(IEditorInput), which is new in Eclipse
> 3.4. Is there a different way of accomplishing this using Eclipse 3.3?

org.eclipse.jdt.ui.JavaUI.getEditorInputJavaElement(IEditorI nput)

Dani

>
> Thanks,
>
> Jeff
>
>
>>> }
>>> }
>>> }
>>>
>>> In the above code, CompilationUnitEditor is internal. Any help
>>> would be appreciated.
>>>
>>> Jeff
>>
Re: IJavaElement from ITextSelection [message #249864 is a reply to message #249835] Tue, 27 November 2007 23:19 Go to previous message
Jeffrey K Czyz is currently offline Jeffrey K CzyzFriend
Messages: 6
Registered: July 2009
Junior Member
Daniel Megert wrote:
> Jeffrey K. Czyz wrote:
>
>> Daniel Megert wrote:
>>
>>> Jeffrey K. Czyz wrote:
>>>
>>>> Hello all,
>>>>
>>>> Is there a simple way of obtaining the IJavaElement associated with
>>>> the currently selected text? The only way I can accomplish this is
>>>> using an internal class, which I would like to avoid:
>>>>
>>>> if (selection instanceof ITextSelection) {
>>>> ITextSelection textSelection = (ITextSelection) selection;
>>>> IWorkbenchPage activePage =
>>>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
>>>> if (activePage != null) {
>>>> IEditorPart editor = activePage.getActiveEditor();
>>>> if (editor instanceof CompilationUnitEditor) {
>>>> CompilationUnitEditor unitEditor = (CompilationUnitEditor)
>>>> editor;
>>>> IJavaElement element = (IJavaElement)
>>>> unitEditor.getViewPartInput();
>>>
>>>
>>> I'm not sure about which CompilationUnitEditor you're talking about
>>> but the one I know does not have a getViewPartInput() method. Anyway,
>>> what you should use ist:
>>> typeRoot=
>>> org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot(IEditorInpu t)
>>> typeRoot.codeSelect(selectionOffset, selectionLength)
>>>
>>> Dani
>>>
>>
>> Hi Dani,
>>
>> Thanks for the reply. CompilationUnitEditor is a class in the
>> internal package org.eclipse.jdt.internal.ui.javaeditor.
>
> I know that. But this one does not have a getViewPartInput() method.
>

It is located in the super class (JavaEditor).

>> I wanted to avoid using it.
>>
>> The example code you gave uses
>> JavaUI.getEditorInputTypeRoot(IEditorInput), which is new in Eclipse
>> 3.4. Is there a different way of accomplishing this using Eclipse 3.3?
>
> org.eclipse.jdt.ui.JavaUI.getEditorInputJavaElement(IEditorI nput)
>
> Dani

Thanks, that did the trick.

Jeff

>
>>
>> Thanks,
>>
>> Jeff
>>
>>
>>>> }
>>>> }
>>>> }
>>>>
>>>> In the above code, CompilationUnitEditor is internal. Any help
>>>> would be appreciated.
>>>>
>>>> Jeff
>>>
Previous Topic:Getting the exit code of a Launched Java process
Next Topic:Prevent lines from unwrapping in the formatter
Goto Forum:
  


Current Time: Tue Sep 17 11:09:25 GMT 2024

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

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

Back to the top