Imediava's Blog

Just another WordPress.com site

Hide a project from the workspace in Eclipse

When you’re developing an Eclipse plugin sometimes you need to use the functionality JDT provides for tasks you don’t want the users of your plugin to be aware of. I ran into an example of this while developing a plugin. I needed to create a project in the workspace that I didn’t want users to see.

The solution came from the JavaElementFilters extension. To set up this extension you have to go to your plugin.xml file and add something like the following:

<extension
       point="org.eclipse.jdt.ui.javaElementFilters">
    <filter
          class="org.test.FilterClass"
          description="Description of the filter you wanna create."
          enabled="true"
          id="filter-id"
          name="FilterName">
    </filter>
 </extension>

The description of each of this fields can be found at:

http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/extension-points/org_eclipse_jdt_ui_javaElementFilters.html

However the most important field is the class field. The class field points out to the actual class which implements the project filter. This class must extend org.eclipse.jface.viewers.ViewerFilter. To extend this abstract class we just have to override the select method. As an example of overriding this method I’m gonna use the code I used to solve my initial problem:

@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		if(element instanceof IJavaProject){
			return !((IJavaProject) element).getElementName().equals("ProjectNameToFilter");
		}
		return true;
	}

What this example does is just:

  • First it makes sure that the element received is an instance of IJavaProject (it’s a Java project in the workspace).
  • Then it filters the project if it has a concrete name.

PS: I have to thank Prakash from http://www.eclipse-tips.com for redirecting me to the JavaElementFilters extension as the solution of my problem.

Leave a comment