Tim
Tim

Reputation: 389

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.

Upvotes: 8

Views: 1932

Answers (1)

gamerson
gamerson

Reputation: 5310

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 ) );

Upvotes: 5

Related Questions