Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Converting from CompilationUnitEditor to a Java Model element(Need to listen for caret updates within the Java Model, but receive an (internal) CompilationUnitEditor instead.)
Converting from CompilationUnitEditor to a Java Model element [message #544874] Mon, 05 July 2010 19:18 Go to next message
Ciera  is currently offline Ciera Friend
Messages: 5
Registered: June 2010
Junior Member
I'm trying to listen for caret updates on Java source files. I need to retrieve something in the Java AST when this occurs.

Right now, I have the following:
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
	if (selection instanceof ITextSelection && part instanceof AbstractDecoratedTextEditor) {
		AbstractDecoratedTextEditor editor = (AbstractDecoratedTextEditor)part;
		//do stuff and refresh my view
	}
}


That part is actually a CompilationUnitEditor, but I noticed CompilationUnitEditor is in an internal package rather than an external one. However, the only way I see to get to the Java Model is through that class by calling getViewPartInput().

I found two other posts on this topic, but no answers on either:

http://www.eclipse.org/forums/index.php?t=msg&th=58457&a mp;#msg_185047

http://www.eclipse.org/forums/index.php?t=msg&goto=24855 3&

What is the suggested mechanism for going from an IWorkbenchPart and ISelection to, say, an ASTNode of the first complete statement on that line?
Re: Converting from CompilationUnitEditor to a Java Model element [message #545063 is a reply to message #544874] Tue, 06 July 2010 14:37 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Ciera wrote:
> I'm trying to listen for caret updates on Java source files. I need to
> retrieve something in the Java AST when this occurs.
In order to also get caret movement you should use a post selection
changed listener. To get the CU use this:
org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot(IEditorInpu t)
to get the AST:
org.eclipse.jdt.ui.SharedASTProvider.getAST(ITypeRoot, WAIT_FLAG,
IProgressMonitor)

Dani
>
> Right now, I have the following:
>
> public void selectionChanged(IWorkbenchPart part, ISelection selection) {
> if (selection instanceof ITextSelection && part instanceof
> AbstractDecoratedTextEditor) {
> AbstractDecoratedTextEditor editor =
> (AbstractDecoratedTextEditor)part;
> //do stuff and refresh my view
> }
> }
>
>
> That part is actually a CompilationUnitEditor, but I noticed
> CompilationUnitEditor is in an internal package rather than an
> external one. However, the only way I see to get to the Java Model is
> through that class by calling getViewPartInput().
>
> I found two other posts on this topic, but no answers on either:
>
> http://www.eclipse.org/forums/index.php?t=msg&th=58457&a mp;#msg_185047
>
> http://www.eclipse.org/forums/index.php?t=msg&goto=24855 3&
>
> What is the suggested mechanism for going from an IWorkbenchPart and
> ISelection to, say, an ASTNode of the first complete statement on that
> line?
Re: Converting from CompilationUnitEditor to a Java Model element [message #545179 is a reply to message #545063] Wed, 07 July 2010 00:02 Go to previous message
Ciera  is currently offline Ciera Friend
Messages: 5
Registered: June 2010
Junior Member
Thanks Dani! That got it for me.

Yes, I was using addPostSelectionListener, but was having trouble finding my way through the API to a CompilationUnit. Finished snippet for anyone else having this problem:

public void selectionChanged(IWorkbenchPart part, ISelection selection) {
	if (selection instanceof ITextSelection && part instanceof IEditorPart) {
		IEditorPart editor = (IEditorPart)part;
		ITypeRoot root = JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
				
		if (root != null && root.getElementType() == IJavaElement.COMPILATION_UNIT) {
			ICompilationUnit compUnit = (ICompilationUnit)root;
			//use the comp unit to refresh my view
		}
	}
}

Previous Topic:How to debug with Eclipse?
Next Topic:Eclipse JDT: generate Error in Java enum
Goto Forum:
  


Current Time: Tue Apr 23 16:28:10 GMT 2024

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

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

Back to the top