Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically select nodes in Package Explorer in Eclipse plugin

Does anybody know or have an example on how to select node(s) programmatically in the Package Explorer view in Eclipse plugin? I see some help on how to get current selection but not on how to set them.

Thanks.

like image 290
Tim Avatar asked Feb 16 '12 17:02

Tim


1 Answers

Although a commenter has already pointed to a solution, it uses internal API. If you wanted a a portable API implementation try this. It will select all "open" projects in your workspace.

List<Object> openProjects = new ArrayList<Object>();

for( IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects() )
{
    if( project.isOpen() )
    {
        final IJavaProject javaProject = JavaCore.create( project );

        if( javaProject != null )
        {
            openProjects.add( javaProject );
        }

        openProjects.add( project );
    }
}

Object[] projectsToSelect = openProjects.toArray();
IViewPart view = window.getActivePage().showView( "org.eclipse.jdt.ui.PackageExplorer" );
view.getSite().getSelectionProvider().setSelection( new StructuredSelection( projectsToSelect ) );
like image 81
gamerson Avatar answered Sep 28 '22 08:09

gamerson