}
Shell shell = Display.getCurrent().getActiveShell();
ILabelProvider labelProvider =
new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
ITreeContentProvider contentProvider = new StandardJavaElementContentProvider();
ElementTreeSelectionDialog dialog =
new ElementTreeSelectionDialog(shell, labelProvider, contentProvider);
dialog.setTitle("ClientFactory interface selection");
dialog.setMessage("Choose a ClientFactory interface:");
dialog.setAllowMultiple(false);
dialog.setHelpAvailable(false);
//
dialog.setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
if (selection.length == 1) {
if (selection[0] instanceof ICompilationUnit) {
try {
if (validateFactoryCompilationUnit((ICompilationUnit) selection[0]) == null) {
return StatusUtils.OK_STATUS;
}
} catch (Exception e) {
DesignerPlugin.log(e);
}
}
}
return StatusUtils.ERROR_STATUS;
}
});
dialog.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
// check project
if (element instanceof IJavaProject) {
return element.equals(packageRoot.getJavaProject());
}
// check package fragment root
if (element instanceof IPackageFragmentRoot) {
return element.equals(packageRoot);
}
// check package fragment
if (element instanceof IPackageFragment) {
try {
IPackageFragment packageFragment = (IPackageFragment) element;
return packageFragment.getCompilationUnits().length > 0
&& Utils.isModuleSourcePackage(packageFragment);
} catch (Exception e) {
DesignerPlugin.log(e);
return false;
}
}
// check *.java
if (element instanceof ICompilationUnit) {
return true;
}
return false;
}
});
dialog.setInput(packageRoot);
{
String factoryClassName = factoryNameField.getText();
if (!StringUtils.isEmpty(factoryClassName)) {
IPackageFragment packageFragment =
packageRoot.getPackageFragment(CodeUtils.getPackage(factoryClassName));
if (packageFragment.exists()) {
ICompilationUnit compilationUnit =
packageFragment.getCompilationUnit(CodeUtils.getShortClass(factoryClassName)
+ ".java");
if (compilationUnit.exists()) {
dialog.setInitialSelection(compilationUnit);
}
}
}
}
if (dialog.open() == Window.OK) {
Object element = dialog.getFirstResult();
if (element instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) element;
factoryNameField.setText(unit.findPrimaryType().getFullyQualifiedName());
}
}