Imediava's Blog

Just another WordPress.com site

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()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: