@ SuppressWarnings ( "static-method" ) public IJavaElement getSelectedResource ( IStructuredSelection selection ) { IJavaElement elem = null ; if ( selection != null && ! selection . isEmpty ( ) ) { final Object object = selection . getFirstElement ( ) ; if ( object instanceof IAdaptable ) { final IAdaptable adaptable = ( IAdaptable ) object ; elem = adaptable . getAdapter ( IJavaElement . class ) ; if ( elem == null ) { elem = getPackage ( adaptable ) ; } } } if ( elem == null ) { final IWorkbenchPage activePage = PlatformUI . getWorkbench ( ) . getActiveWorkbenchWindow ( ) . getActivePage ( ) ; IWorkbenchPart part = activePage . getActivePart ( ) ; if ( part instanceof ContentOutline ) { part = activePage . getActiveEditor ( ) ; } if ( part instanceof XtextEditor ) { final IXtextDocument doc = ( ( XtextEditor ) part ) . getDocument ( ) ; final IFile file = doc . getAdapter ( IFile . class ) ; elem = getPackage ( file ) ; } } if ( elem == null || elem . getElementType ( ) == IJavaElement . JAVA_MODEL ) { try { final IJavaProject [ ] projects = JavaCore . create ( ResourcesPlugin . getWorkspace ( ) . getRoot ( ) ) . getJavaProjects ( ) ; if ( projects . length == 1 ) { elem = projects [ 0 ] ; } } catch ( JavaModelException e ) { throw new RuntimeException ( e . getMessage ( ) ) ; } } return elem ; }
Replies the Java element that corresponds to the given selection .