restrict the ElementTreeSelectionDialog

2019-09-13 04:46发布

问题:

I wanna to implement Workspace Browse. I write the below code.

ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
            Display.getDefault().getActiveShell(),
            new WorkbenchLabelProvider(),
            new BaseWorkbenchContentProvider());

dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.open();

How can I change the code to only root elements (projects or folders) that contains ".ecore" file shown? And when the user collapse any root elements only .ecore files shown?

回答1:

You can add a viewer filter to the dialog to restrict what is shown.

dialog.addFilter(new FileExtensionFilter("ecore"));

adds a filter.

A file extension filter would look something like:

public class FileExtensionFilter extends ViewerFilter {

  private String fTargetExtension;

  public FileExtensionFilter(String targetExtension) {
    fTargetExtension = targetExtension;
  }

  @Override
  public boolean select(Viewer viewer, Object parent, Object element) {
    if (element instanceof IFile) {
      return ((IFile)element).getName().toLowerCase().endsWith("." + fTargetExtension); 
    }

    if (element instanceof IProject && !((IProject)element).isOpen())
      return false;

    if (element instanceof IContainer) { // i.e. IProject, IFolder
      try {
        IResource[] resources = ((IContainer)element).members();
        for (IResource resource : resources) {
          if (select(viewer, parent, resource))
            return true;
        }
      } catch (CoreException e) {
        // Ignore
      }
    }

    return false;  
  }
}

This is based on org.eclipse.pde.internal.ui.util.FileExtensionFilter



标签: java swt jface