8

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.

Sam Su
  • 6,532
  • 8
  • 39
  • 80
Tim
  • 379
  • 4
  • 16
  • hi can you please elaborate on what you are trying to do on package explorer. or what you are trying to override there – Naveen Babu Jan 04 '13 at 05:36
  • Already answered here: [link](http://stackoverflow.com/questions/11335491/how-to-programmatically-change-the-selection-within-package-explorer) – dreo Mar 22 '13 at 12:45

1 Answers1

5

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 ) );
gamerson
  • 5,220
  • 1
  • 28
  • 45