Sunday, October 12, 2008

Launching the Open Type dialog

You can use the following code to launch the JDT's Open Type dialog to select a Java Type name and then open it in the Java editor programmatically:


OpenTypeSelectionDialog dialog = new OpenTypeSelectionDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
true,
PlatformUI.getWorkbench().getProgressService(),
null,
IJavaSearchConstants.TYPE);
dialog.setTitle(JavaUIMessages.OpenTypeAction_dialogTitle);
dialog.setMessage(JavaUIMessages.OpenTypeAction_dialogMessage);
dialog.setInitialPattern("java.lang.String");

int result= dialog.open();
if (result != IDialogConstants.OK_ID)
{
return;
}

Object[] types= dialog.getResult();
if (types != null && types.length > 0) {
IType type= null;
for (int i= 0; i < types.length; i++) {
type= (IType) types[i];
try {
JavaUI.openInEditor(type, true, true);
} catch (CoreException x) {
// Handle exception
}
}
}

2 comments:

Shiyam Hoda said...

When I type your code into eclipse it complaints that "OpenTypeSelectionDialog cannot be resolved to a type"


help please!

Sandip Chitale said...

Are you trying to develop an Eclipse Plugin? Did you make sure that you added the dependency to "org.eclipse.jdt.ui" plug-in in your plug-ins dependencies.