下面列出了怎么用org.eclipse.ui.dialogs.ISelectionStatusValidator的API类实例代码及写法,或者点击链接到github查看源代码。
private void openTypeSelectionDialog(){
int elementKinds= IJavaSearchConstants.TYPE;
final IJavaSearchScope scope= createWorkspaceSourceScope();
FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false,
getWizard().getContainer(), scope, elementKinds);
dialog.setTitle(RefactoringMessages.MoveMembersInputPage_choose_Type);
dialog.setMessage(RefactoringMessages.MoveMembersInputPage_dialogMessage);
dialog.setValidator(new ISelectionStatusValidator(){
public IStatus validate(Object[] selection) {
Assert.isTrue(selection.length <= 1);
if (selection.length == 0)
return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, RefactoringMessages.MoveMembersInputPage_Invalid_selection, null);
Object element= selection[0];
if (! (element instanceof IType))
return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, RefactoringMessages.MoveMembersInputPage_Invalid_selection, null);
IType type= (IType)element;
return validateDestinationType(type, type.getElementName());
}
});
dialog.setInitialPattern(createInitialFilter());
if (dialog.open() == Window.CANCEL)
return;
IType firstResult= (IType)dialog.getFirstResult();
fDestinationField.setText(firstResult.getFullyQualifiedName('.'));
}
/**
* Creates a new package fragment root selection dialog.
*
* @param shell The parent shell for the dialog.
* @param title The title of the dialog.
* @param message The message of the dialog.
* @param errorMessage the error message to display if an invalid selection is made.
*/
public PackageFragmentRootSelectionDialog(Shell shell, String title, String message,
String errorMessage) {
super(shell, new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT),
new StandardJavaElementContentProvider());
setTitle(title);
setMessage(message);
setAllowMultiple(false);
setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
setValidator(new ISelectionStatusValidator() {
@Override
public IStatus validate(Object[] sel) {
if (sel.length == 1 && sel[0] instanceof IPackageFragmentRoot) {
return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, "", null);
}
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, errorMessage,
null);
}
});
addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentObject, Object element) {
if (element instanceof IPackageFragmentRoot) {
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element;
return !packageFragmentRoot.isArchive() && !packageFragmentRoot.isExternal();
}
return element instanceof IJavaModel || element instanceof IJavaProject;
}
});
}
public WorkspaceResourceSelectionDialog(Shell parent, Mode mode) {
super(parent, new WorkbenchLabelProvider(), new WorkbenchContentProvider());
this.mode = mode;
setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
if (selection.length > 0 && checkMode(selection[0])) {
return new Status(IStatus.OK, TypeScriptUIPlugin.PLUGIN_ID, IStatus.OK, EMPTY_STRING, null);
}
return new Status(IStatus.ERROR, TypeScriptUIPlugin.PLUGIN_ID, IStatus.ERROR, EMPTY_STRING, null);
}
});
setInput(ResourcesPlugin.getWorkspace().getRoot());
}
private IType chooseException() {
IJavaElement[] elements= new IJavaElement[] { fProject.getJavaProject() };
final IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false,
PlatformUI.getWorkbench().getProgressService(), scope, IJavaSearchConstants.CLASS);
dialog.setTitle(RefactoringMessages.ChangeExceptionsControl_choose_title);
dialog.setMessage(RefactoringMessages.ChangeExceptionsControl_choose_message);
dialog.setInitialPattern("*Exception*"); //$NON-NLS-1$
dialog.setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
if (selection.length == 0)
return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
try {
return checkException((IType)selection[0]);
} catch (JavaModelException e) {
JavaPlugin.log(e);
return StatusInfo.OK_STATUS;
}
}
});
if (dialog.open() == Window.OK) {
return (IType) dialog.getFirstResult();
}
return null;
}
/**
* Creates and returns a dialog to choose an existing workspace file.
* @param title the title
* @param message the dialog message
* @return the dialog
*/
protected ElementTreeSelectionDialog createWorkspaceFileSelectionDialog(String title, String message) {
int labelFlags= JavaElementLabelProvider.SHOW_BASICS
| JavaElementLabelProvider.SHOW_OVERLAY_ICONS
| JavaElementLabelProvider.SHOW_SMALL_ICONS;
final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(labelFlags), new ProblemsLabelDecorator(null));
ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), provider, new StandardJavaElementContentProvider());
dialog.setComparator(new JavaElementComparator());
dialog.setAllowMultiple(false);
dialog.setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
StatusInfo res= new StatusInfo();
// only single selection
if (selection.length == 1 && (selection[0] instanceof IFile))
res.setOK();
else
res.setError(""); //$NON-NLS-1$
return res;
}
});
dialog.addFilter(new EmptyInnerPackageFilter());
dialog.addFilter(new LibraryFilter());
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setStatusLineAboveButtons(true);
dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot()));
return dialog;
}
private IFolder chooseFolder(String title, String message, IPath initialPath) {
Class<?>[] acceptedClasses= new Class[] { IFolder.class };
ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, null);
ILabelProvider lp= new WorkbenchLabelProvider();
ITreeContentProvider cp= new WorkbenchContentProvider();
IProject currProject= fCurrJProject.getProject();
ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), lp, cp);
dialog.setValidator(validator);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.addFilter(filter);
dialog.setInput(currProject);
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
IResource res= currProject.findMember(initialPath);
if (res != null) {
dialog.setInitialSelection(res);
}
if (dialog.open() == Window.OK) {
return (IFolder) dialog.getFirstResult();
}
return null;
}
private IContainer chooseContainer() {
Class<?>[] acceptedClasses= new Class[] { IProject.class, IFolder.class };
ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
IProject[] allProjects= fWorkspaceRoot.getProjects();
ArrayList<IProject> rejectedElements= new ArrayList<IProject>(allProjects.length);
IProject currProject= fCurrJProject.getProject();
for (int i= 0; i < allProjects.length; i++) {
if (!allProjects[i].equals(currProject)) {
rejectedElements.add(allProjects[i]);
}
}
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
ILabelProvider lp= new WorkbenchLabelProvider();
ITreeContentProvider cp= new WorkbenchContentProvider();
IResource initSelection= null;
if (fOutputLocationPath != null) {
initSelection= fWorkspaceRoot.findMember(fOutputLocationPath);
}
FolderSelectionDialog dialog= new FolderSelectionDialog(getShell(), lp, cp);
dialog.setTitle(NewWizardMessages.BuildPathsBlock_ChooseOutputFolderDialog_title);
dialog.setValidator(validator);
dialog.setMessage(NewWizardMessages.BuildPathsBlock_ChooseOutputFolderDialog_description);
dialog.addFilter(filter);
dialog.setInput(fWorkspaceRoot);
dialog.setInitialSelection(initSelection);
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
if (dialog.open() == Window.OK) {
return (IContainer)dialog.getFirstResult();
}
return null;
}
private IFolder chooseFolder(String title, String message, IPath initialPath) {
Class<?>[] acceptedClasses= new Class[] { IFolder.class };
ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, null);
ILabelProvider lp= new WorkbenchLabelProvider();
ITreeContentProvider cp= new WorkbenchContentProvider();
IProject currProject= fNewElement.getJavaProject().getProject();
ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), lp, cp) {
@Override
protected Control createDialogArea(Composite parent) {
Control result= super.createDialogArea(parent);
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.BP_CHOOSE_EXISTING_FOLDER_TO_MAKE_SOURCE_FOLDER);
return result;
}
};
dialog.setValidator(validator);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.addFilter(filter);
dialog.setInput(currProject);
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
IResource res= currProject.findMember(initialPath);
if (res != null) {
dialog.setInitialSelection(res);
}
if (dialog.open() == Window.OK) {
return (IFolder) dialog.getFirstResult();
}
return null;
}
public void setValidator(ISelectionStatusValidator validator) {
fValidator= validator;
}
private static ISelectionStatusValidator createValidator(int entries) {
AddGetterSetterSelectionStatusValidator validator= new AddGetterSetterSelectionStatusValidator(entries);
return validator;
}
/**
* Creates a selection dialog that lists all packages under the given package
* fragment root.
* The caller is responsible for opening the dialog with <code>Window.open</code>,
* and subsequently extracting the selected packages (of type
* <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
*
* @param packageFragments the package fragments
* @return a new selection dialog
*/
protected SelectionDialog createPackageDialog(Set<IJavaElement> packageFragments) {
List<IPackageFragment> packages= new ArrayList<IPackageFragment>(packageFragments.size());
for (Iterator<IJavaElement> iter= packageFragments.iterator(); iter.hasNext();) {
IPackageFragment fragment= (IPackageFragment)iter.next();
boolean containsJavaElements= false;
int kind;
try {
kind= fragment.getKind();
containsJavaElements= fragment.getChildren().length > 0;
} catch (JavaModelException ex) {
ExceptionHandler.handle(ex, getContainer().getShell(), JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_title, Messages.format(JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_message, JavaElementLabels.getElementLabel(fragment, JavaElementLabels.ALL_DEFAULT)));
continue;
}
if (kind != IPackageFragmentRoot.K_BINARY && containsJavaElements)
packages.add(fragment);
}
StandardJavaElementContentProvider cp= new StandardJavaElementContentProvider() {
@Override
public boolean hasChildren(Object element) {
// prevent the + from being shown in front of packages
return !(element instanceof IPackageFragment) && super.hasChildren(element);
}
};
final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT), new ProblemsLabelDecorator(null));
ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getContainer().getShell(), provider, cp);
dialog.setDoubleClickSelects(false);
dialog.setComparator(new JavaElementComparator());
dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot()));
dialog.addFilter(new EmptyInnerPackageFilter());
dialog.addFilter(new LibraryFilter());
dialog.addFilter(new SealPackagesFilter(packages));
dialog.setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
StatusInfo res= new StatusInfo();
for (int i= 0; i < selection.length; i++) {
if (!(selection[i] instanceof IPackageFragment)) {
res.setError(JarPackagerMessages.JarManifestWizardPage_error_mustContainPackages);
return res;
}
}
res.setOK();
return res;
}
});
return dialog;
}
@Override
public ISelectionStatusValidator getSelectionValidator() {
return getValidator();
}
private IContainer chooseOutputLocation() {
IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot();
final Class<?>[] acceptedClasses= new Class[] { IProject.class, IFolder.class };
IProject[] allProjects= root.getProjects();
ArrayList<IProject> rejectedElements= new ArrayList<IProject>(allProjects.length);
for (int i= 0; i < allProjects.length; i++) {
if (!allProjects[i].equals(fCurrProject)) {
rejectedElements.add(allProjects[i]);
}
}
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
ILabelProvider lp= new WorkbenchLabelProvider();
ITreeContentProvider cp= new WorkbenchContentProvider();
IResource initSelection= null;
if (fOutputLocation != null) {
initSelection= root.findMember(fOutputLocation);
}
FolderSelectionDialog dialog= new FolderSelectionDialog(getShell(), lp, cp);
dialog.setTitle(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_title);
dialog.setValidator(new ISelectionStatusValidator() {
ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
public IStatus validate(Object[] selection) {
IStatus typedStatus= validator.validate(selection);
if (!typedStatus.isOK())
return typedStatus;
if (selection[0] instanceof IFolder) {
IFolder folder= (IFolder) selection[0];
try {
IStatus result= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, folder.getFullPath(), fAllowInvalidClasspath, fCPJavaProject);
if (result.getSeverity() == IStatus.ERROR)
return result;
} catch (CoreException e) {
JavaPlugin.log(e);
}
return new StatusInfo();
} else {
return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
}
}
});
dialog.setMessage(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_description);
dialog.addFilter(filter);
dialog.setInput(root);
dialog.setInitialSelection(initSelection);
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
if (dialog.open() == Window.OK) {
return (IContainer)dialog.getFirstResult();
}
return null;
}
/**
* Constructs a dialog for moving resource.
*
* @param files
*/
public MoveResourceDialog( final Collection<File> files )
{
super( false, false, null );
setTitle( Messages.getString( "MoveResourceDialog.Title" ) );
setMessage( Messages.getString( "MoveResourceDialog.Message" ) );
setDoubleClickSelects( true );
setAllowMultiple( false );
setHelpAvailable( false );
setEmptyFolderShowStatus( IResourceContentProvider.ALWAYS_SHOW_EMPTYFOLDER );
setValidator( new ISelectionStatusValidator( ) {
public IStatus validate( Object[] selection )
{
for ( Object s : selection )
{
if ( s instanceof ResourceEntry )
{
URL url = ( (ResourceEntry) s ).getURL( );
try {
url = URIUtil.toURI(url).toURL();
} catch (Exception e1) {
}
for ( File f : files )
{
try
{
if ( url.equals( f.getParentFile( )
.toURI( )
.toURL( ) )
|| url.equals( f.toURI( ).toURL( ) ) )
return new Status( IStatus.ERROR,
ReportPlugin.REPORT_UI,
"" );
}
catch ( MalformedURLException e )
{
}
}
}
}
return new Status( IStatus.OK, ReportPlugin.REPORT_UI, "" );
}
} );
}
public void setValidator( ISelectionStatusValidator validator )
{
fValidator = validator;
}
/**
* Creates a special validator that considers that items may be gotten from what's filtered (not only actually selected).
*/
private ISelectionStatusValidator createValidator() {
return new ISelectionStatusValidator() {
@Override
public IStatus validate(Object[] selection) {
if (selection != null && selection.length == 1) {
return new Status(IStatus.OK, PydevPlugin.getPluginID(), getEntry(selection[0].toString()));
}
TreeItem[] items = getTreeViewer().getTree().getItems();
if (selection == null || selection.length == 0) {
//not available in selection
if (items != null) {
if (items.length == 1) {
return new Status(IStatus.OK, PydevPlugin.getPluginID(), getEntry(items[0].getData()
.toString()));
}
if (items.length > 0) {
String textInEditor = text.getText();
for (TreeItem item : items) {
if (item.getData().toString().equals(textInEditor)) {
//exact match of what's written to an item, so, just use it.
return new Status(IStatus.OK, PydevPlugin.getPluginID(), textInEditor);
}
}
}
}
}
if ((selection == null || selection.length == 0) && (items == null || items.length == 0)) {
return new Status(IStatus.ERROR, PydevPlugin.getPluginID(), "No selection available.");
}
return new Status(IStatus.ERROR, PydevPlugin.getPluginID(), "Only 1 entry may be selected or visible.");
}
private String getEntry(String string) {
if (NEW_ENTRY_TEXT.equals(string)) {
return text.getText();
}
return string;
}
};
}
/**
* Sets an optional validator to check if the selection is valid. The
* validator is invoked whenever the selection changes.
*
* @param validator
* the validator to validate the selection.
*/
public void setValidator(ISelectionStatusValidator validator) {
fValidator = validator;
}
/**
* Returns the selection validator or <code>null</code> if
* selection validation is not required. The elements passed
* to the selection validator are of type {@link IType}.
*
* @return the selection validator or <code>null</code>
*/
public ISelectionStatusValidator getSelectionValidator() {
return null;
}
/**
* Sets a new validator.
*
* @param validator
* the new validator
*/
public void setValidator(ISelectionStatusValidator validator) {
fValidator= validator;
}
/**
* Sets an optional validator to check if the selection is valid.
* The validator is invoked whenever the selection changes.
* @param validator the validator to validate the selection.
*/
public void setValidator(ISelectionStatusValidator validator) {
fValidator = validator;
}
/**
* Sets an optional validator to check if the selection is valid.
* The validator is invoked whenever the selection changes.
* @param validator the validator to validate the selection.
*/
public void setValidator(ISelectionStatusValidator validator) {
fValidator = validator;
}
/**
* Sets an optional validator to check if the selection is valid.
* The validator is invoked whenever the selection changes.
* @param validator the validator to validate the selection.
*/
public void setValidator(ISelectionStatusValidator validator) {
fValidator = validator;
}