private void selectFiles(IProject project, List<? extends IResource> createdFiles) { // Attempt to select the newly created files in the Package Explorer IWorkbench workbench = AdtPlugin.getDefault().getWorkbench(); IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); IViewPart viewPart = page.findView(JavaUI.ID_PACKAGES); if (viewPart != null) { IWorkbenchPartSite site = viewPart.getSite(); IJavaProject javaProject = null; try { javaProject = BaseProjectHelper.getJavaProject(project); } catch (CoreException e) { AdtPlugin.log(e, null); } final ISelectionProvider provider = site.getSelectionProvider(); if (provider != null) { List<TreePath> pathList = new ArrayList<TreePath>(); for (IResource file : createdFiles) { // Create a TreePath for the given file, // which should be the JavaProject, followed by the folders down to // the final file. List<Object> segments = new ArrayList<Object>(); segments.add(file); IContainer folder = file.getParent(); if (folder != null && !(folder instanceof IProject)) { segments.add(folder); // res folder folder = folder.getParent(); if (folder != null && !(folder instanceof IProject)) { segments.add(folder); } } // project segments.add(javaProject); Collections.reverse(segments); TreePath path = new TreePath(segments.toArray()); pathList.add(path); // IDEA: Maybe normalize the files backwards (IFile objects aren't unique) // by maybe using the package explorer icons instead } TreePath[] paths = pathList.toArray(new TreePath[pathList.size()]); final TreeSelection selection = new TreeSelection(paths); provider.setSelection(selection); // Workaround: The above doesn't always work; it will frequently select // some siblings of the real files. I've tried a number of workarounds: // normalizing the IFile objects by looking up the canonical ones via // their relative paths from the project; deferring execution with // Display.asyncRun; first calling select on the parents, etc. // However, it turns out a simple workaround works best: Calling this // method TWICE. The first call seems to expand all the necessary parents, // and the second call ensures that the correct children are selected! provider.setSelection(selection); viewPart.setFocus(); } } }