Imediava's Blog

Just another WordPress.com site

Tag Archives: JDT

Create search for type dialog with Eclipse JDT API

If you’ve ever wondered how to get the cool window that allow you to select a class type in Eclipse (see the snapshot above) and reuse it for your plugin, the good news are that doing so is fairly simple.

Selec type window

You just have to decide what types you want Eclipse to allow the user to chose. If you want Eclipse to allow any type within a project, get the reference to the project and pass it to JavaUI.createTypeDialog(). Here’s how to do it with groovyMonkey:

/*
 * Menu: Remove Markers
 * Script-Path: /GroovyMonkeyScripts/propios/seleccion_classes.gm
 * Kudos: ERVIN
 * License: EPL 1.0
 * Job: UIJob
 * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
 */

import org.eclipse.jdt.core.JavaCore
import org.eclipse.jdt.core.search.SearchEngine
import org.eclipse.jdt.ui.JavaUI
import org.eclipse.jdt.ui.IJavaElementSearchConstants
import org.eclipse.jdt.core.IJavaElement
import org.eclipse.jface.dialogs.ProgressMonitorDialog

project = workspace.root.projects.find { project -> project.isOpen() && project.isNatureEnabled("org.eclipse.jdt.core.javanature")}
javaProject = JavaCore.create(project)
typeDialog = JavaUI.createTypeDialog(window.getShell(), new ProgressMonitorDialog(window.getShell()),
                                     project,
                                     IJavaElementSearchConstants.CONSIDER_TYPES,
                                     false)
typeDialog.open()

// Type selected by the user
result = typeDialog.getResult()

First you get the project from the workspace and then you need to use JavaCore.create() to get a reference to it as an IJavaProject.

If you want to be picky and decide what specific types you want to be int the dialog, you can do so using SearchEngine.createJavaSearchScope(). Here’s an example on how to add any type in any open Java project in the workspace to the dialog:

/*
 * Menu: Remove Markers
 * Script-Path: /GroovyMonkeyScripts/propios/seleccion_classes.gm
 * Kudos: ERVIN
 * License: EPL 1.0
 * Job: UIJob
 * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
 */

import org.eclipse.jdt.core.JavaCore
import org.eclipse.jdt.core.search.SearchEngine
import org.eclipse.jdt.ui.JavaUI
import org.eclipse.jdt.ui.IJavaElementSearchConstants
import org.eclipse.jdt.core.IJavaElement
import org.eclipse.jface.dialogs.ProgressMonitorDialog

import org.eclipse.core.resources.*
import org.eclipse.jdt.core.IPackageFragmentRoot
import org.eclipse.core.runtime.Path

List types = []
workspace.root.projects.each { project ->

   //selects only the java projects
   if (project.isOpen() && project.isNatureEnabled("org.eclipse.jdt.core.javanature")){
 	   javaProject = JavaCore.create(project)
	   roots = javaProject.getPackageFragments()

	   // Filters the source code packages
	   .findAll{ fragment -> fragment.getKind() == IPackageFragmentRoot.K_SOURCE}

	   .each { fragment ->
	 	    fragment.compilationUnits.each { compilationUnit ->
	 	        types.addAll(compilationUnit.getAllTypes())
	 	    }

	   }

   }
}

searchScope = SearchEngine.createJavaSearchScope(types.toArray(new IJavaElement[0]))
typeDialog = JavaUI.createTypeDialog(window.getShell(),
                                  new ProgressMonitorDialog(window.getShell()),
                                  searchScope,
                                  IJavaElementSearchConstants.CONSIDER_TYPES,
                                  false)
typeDialog.open()

// Type selected by the user
result = typeDialog.getResult()

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.

%d bloggers like this: