IJavaElement from ITextSelection [message #249751] |
Wed, 21 November 2007 12:29  |
Eclipse User |
|
|
|
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 #249864 is a reply to message #249835] |
Tue, 27 November 2007 18:19  |
Eclipse User |
|
|
|
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
>>>
|
|
|
Powered by
FUDForum. Page generated in 0.07711 seconds