CODEXE

Add filter to common navigator

Filter in custom common navigator can control the type of elements shown on it. In the below example, we try to hide Java Project.

1. Add org.eclipse.ui.navigator.navigatorContent extension. Using this extension point, your can create your own contents, actions, and filter to the navigator.

2. Add common Filter to the org.eclipse.ui.navigator.navigatorContent extension and initialize the filter class.Remember to set active(activeByDefault: true). Also, add the Filter as a contentExtension to the viewerContentBinding.

org.eclipse.ui.navigtor.navigatorContent

The filtering code to hide Java Project should be implemented in the filter class(customplugin.filters.NavigatorFilter). We can specify the type of project through nature id. The nature id of Java project is org.eclipse.jdt.core.javanature.

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

public class NavigatorFilter extends ViewerFilter {

	public NavigatorFilter() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		// TODO Auto-generated method stub
		if(element instanceof IProject){
			IProject prj = (IProject) element;
			try {
				if(prj.hasNature("org.eclipse.jdt.core.javanature")){//org.eclipse.jdt.core.javanature is the nature id of Java project
					return false;
				}
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

}
			 

3. Result

Create a Java Project from package explorer. We found that the project created disappear from the custom project explorer.

filter common navigator