下面列出了怎么用org.eclipse.jface.dialogs.IInputValidator的API类实例代码及写法,或者点击链接到github查看源代码。
private IInputValidator getBasicPackageValidator() {
return InputFunctionalValidator.from(
(final String name) -> {
N4JSProjectName projectName = new N4JSProjectName(name);
if (npmCli.invalidPackageName(projectName))
return "The npm package name should be specified.";
for (int i = 0; i < name.length(); i++) {
if (Character.isWhitespace(name.charAt(i)))
return "The npm package name must not contain any whitespaces.";
if (Character.isUpperCase(name.charAt(i)))
return "The npm package name must not contain any upper case letter.";
}
return null;
});
}
private String editGroupName(String initialName, final Set<String> usedNames) {
InputDialog dlg = new InputDialog(getShell(), Messages.SdkToolsControl_NewGroupName,
Messages.SdkToolsControl_EnterGroupName+':', initialName,
new IInputValidator()
{
@Override
public String isValid(String newText) {
newText = newText.trim();
if (newText.isEmpty()) {
return Messages.SdkToolsControl_NameIsEmpty;
} else if (usedNames.contains(newText)) {
return Messages.SdkToolsControl_NameIsUsed;
}
return null;
}
});
if (dlg.open() == Window.OK) {
return dlg.getValue().trim();
}
return null;
}
/**
* Return the new name to be given to the target resource.
*
* @return java.lang.String
* @param resource
* the resource to query status on
*/
protected String queryNewResourceName(final IResource resource) {
final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
final IPath prefix = resource.getFullPath().removeLastSegments(1);
final IInputValidator validator = string -> {
if (resource.getName().equals(string)) {
return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
}
final IStatus status = workspace.validateName(string, resource.getType());
if (!status.isOK()) { return status.getMessage(); }
if (workspace.getRoot().exists(prefix.append(string))) {
return IDEWorkbenchMessages.RenameResourceAction_nameExists;
}
return null;
};
final InputDialog dialog =
new InputDialog(WorkbenchHelper.getShell(), IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage, resource.getName(), validator);
dialog.setBlockOnOpen(true);
final int result = dialog.open();
if (result == Window.OK) { return dialog.getValue(); }
return null;
}
/**
* Creates an input URL dialog with OK and Cancel buttons. Note that the dialog
* will have no visual representation (no widgets) until it is told to open.
* <p>
* Note that the <code>open</code> method blocks for input dialogs.
* </p>
*
* @param parentShell
* the parent shell, or <code>null</code> to create a top-level
* shell
* @param dialogTitle
* the dialog title, or <code>null</code> if none
* @param dialogMessage
* the dialog message, or <code>null</code> if none
* @param initialValue
* the initial input value, or <code>null</code> if none
* (equivalent to the empty string)
*/
public InputURLDialog(Shell parentShell, String dialogTitle,
String dialogMessage, String initialValue) {
super(parentShell);
this.title = dialogTitle;
message = dialogMessage;
if (initialValue == null) {
value = StringUtil.EMPTY;
} else {
value = initialValue;
}
this.validator = new IInputValidator() {
public String isValid(String newText) {
try {
new URI(newText).toURL();
} catch (Exception e) {
return EplMessages.InputURLDialog_InvalidURL;
}
return null;
}
};
}
private static INewNameQuery createStaticQuery(final IInputValidator validator, final String message, final String initial, final Shell shell){
return new INewNameQuery(){
public String getNewName() throws OperationCanceledException {
InputDialog dialog= new InputDialog(shell, ReorgMessages.ReorgQueries_nameConflictMessage, message, initial, validator) {
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
@Override
protected Control createDialogArea(Composite parent) {
Control area= super.createDialogArea(parent);
TextFieldNavigationHandler.install(getText());
return area;
}
};
if (dialog.open() == Window.CANCEL)
throw new OperationCanceledException();
return dialog.getValue();
}
};
}
private static IInputValidator createResourceNameValidator(final IResource res){
IInputValidator validator= new IInputValidator(){
public String isValid(String newText) {
if (newText == null || "".equals(newText) || res.getParent() == null) //$NON-NLS-1$
return INVALID_NAME_NO_MESSAGE;
if (res.getParent().findMember(newText) != null)
return ReorgMessages.ReorgQueries_resourceWithThisNameAlreadyExists;
if (! res.getParent().getFullPath().isValidSegment(newText))
return ReorgMessages.ReorgQueries_invalidNameMessage;
IStatus status= res.getParent().getWorkspace().validateName(newText, res.getType());
if (status.getSeverity() == IStatus.ERROR)
return status.getMessage();
if (res.getName().equalsIgnoreCase(newText))
return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
return null;
}
};
return validator;
}
private static IInputValidator createCompilationUnitNameValidator(final ICompilationUnit cu) {
IInputValidator validator= new IInputValidator(){
public String isValid(String newText) {
if (newText == null || "".equals(newText)) //$NON-NLS-1$
return INVALID_NAME_NO_MESSAGE;
String newCuName= JavaModelUtil.getRenamedCUName(cu, newText);
IStatus status= JavaConventionsUtil.validateCompilationUnitName(newCuName, cu);
if (status.getSeverity() == IStatus.ERROR)
return status.getMessage();
RefactoringStatus refStatus;
refStatus= Checks.checkCompilationUnitNewName(cu, newText);
if (refStatus.hasFatalError())
return refStatus.getMessageMatchingSeverity(RefactoringStatus.FATAL);
if (cu.getElementName().equalsIgnoreCase(newCuName))
return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
return null;
}
};
return validator;
}
public static Integer openAskInt(String title, String message, int initial) {
Shell shell = EditorUtils.getShell();
String initialValue = "" + initial;
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
if (newText.length() == 0) {
return "At least 1 char must be provided.";
}
try {
Integer.parseInt(newText);
} catch (Exception e) {
return "A number is required.";
}
return null;
}
};
InputDialog dialog = new InputDialog(shell, title, message, initialValue, validator);
dialog.setBlockOnOpen(true);
if (dialog.open() == Window.OK) {
return Integer.parseInt(dialog.getValue());
}
return null;
}
@Override
public void run(IAction action) {
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
if (newText.trim().length() == 0) {
return "Name cannot be empty";
}
return null;
}
};
InputDialog d = new InputDialog(EditorUtils.getShell(), "App name",
"Name of the django app to makemigrations on", "",
validator);
int retCode = d.open();
if (retCode == InputDialog.OK) {
createApp(d.getValue().trim());
}
}
@Override
public void run(IAction action) {
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
if (newText.trim().length() == 0) {
return "Name cannot be empty";
}
return null;
}
};
InputDialog d = new InputDialog(EditorUtils.getShell(), "App name", "Name of the django app to be created", "",
validator);
int retCode = d.open();
if (retCode == InputDialog.OK) {
createApp(d.getValue().trim());
}
}
/**
* Creates a new instance
* @param parentShell
* @param dialogTitle
* @param dialogMessage
* @param choices
* @param initialValue
* @param validator
*/
public DialogComboSelection(Shell parentShell,
String dialogTitle,
String dialogMessage,
String[] choices,
String initialValue,
IInputValidator validator) {
super(parentShell);
this.title = dialogTitle;
message = dialogMessage;
this.choices = choices;
if (initialValue == null) {
value = "";//$NON-NLS-1$
} else {
value = initialValue;
}
this.validator = validator;
}
public Object execute(ExecutionEvent arg0) throws ExecutionException{
InputDialog dlg =
new InputDialog(UiDesk.getTopShell(), Messages.FallPlaneRechnung_PlanBillingHeading,
Messages.FallPlaneRechnung_PlanBillingAfterDays, "30", new IInputValidator() { //$NON-NLS-1$
public String isValid(String newText){
if (newText.matches("[0-9]*")) { //$NON-NLS-1$
return null;
}
return Messages.FallPlaneRechnung_PlanBillingPleaseEnterPositiveInteger;
}
});
if (dlg.open() == Dialog.OK) {
return dlg.getValue();
}
return null;
}
@Override
public String isValid(String newText) {
String result = null;
for (IInputValidator validator : validators) {
result = validator.isValid(newText);
if (result != null)
break;
}
return result;
}
/** Creates dialog with custom validators. */
public InstallNpmDependencyDialog(Shell parentShell, IInputValidator packageNameValidator,
IInputValidator packageVersionValidator) {
super(parentShell);
this.packageNameValidator = packageNameValidator;
this.packageVersionValidator = packageVersionValidator;
}
/**
* Validator that checks if given name is valid package name and can be used to install new package (i.e. there is
* no installed package with the same name).
*
* @return validator checking if provided name can be used to install new package
*/
IInputValidator getPackageNameToInstallValidator() {
return InputComposedValidator.compose(
getBasicPackageValidator(), InputFunctionalValidator.from(
(final String name) -> !isNpmWithNameInstalled(new N4JSProjectName(name)) ? null
/* error message */
: "The npm package '" + name + "' is already available."));
}
/**
* Validator that checks if given name is valid package name and can be used to uninstall new package (i.e. there is
* installed package with the same name).
*
* @return validator checking if provided name can be used to install new package
*/
IInputValidator getPackageNameToUninstallValidator() {
return InputComposedValidator.compose(
getBasicPackageValidator(), InputFunctionalValidator.from(
(final String name) -> isNpmWithNameInstalled(new N4JSProjectName(name)) ? null
/* error case */
: "The npm package '" + name + "' is not installed."));
}
/**
* Creates a window for entering the template name, checks the user's input and
* proceeds to save the template.
*/
public void run(IAction action) {
IFile file = ((FileEditorInput)editor.getEditorInput()).getFile();
String fullname = file.getFullPath().toString();
// create dialog
InputQueryDialog dialog = new InputQueryDialog(editor.getEditorSite().getShell(),
TexlipsePlugin.getResourceString("templateSaveDialogTitle"),
TexlipsePlugin.getResourceString("templateSaveDialogMessage").replaceAll("%s", fullname),
file.getName().substring(0,file.getName().lastIndexOf('.')),
new IInputValidator() {
public String isValid(String newText) {
if (newText != null && newText.length() > 0) {
return null; // no error
}
return TexlipsePlugin.getResourceString("templateSaveErrorFileName");
}});
if (dialog.open() == InputDialog.OK) {
String newName = dialog.getInput();
// check existing
boolean reallySave = true;
if (ProjectTemplateManager.templateExists(newName)) {
reallySave = MessageDialog.openConfirm(editor.getSite().getShell(),
TexlipsePlugin.getResourceString("templateSaveOverwriteTitle"),
TexlipsePlugin.getResourceString("templateSaveOverwriteText").replaceAll("%s", newName));
}
if (reallySave) {
ProjectTemplateManager.saveProjectTemplate(file, newName);
}
}
}
public ParameterDialog(Shell parentShell,
String dialogTitle,
String dialogMessage,
String baseCommand,
IInputValidator validator) {
super(parentShell);
fTitle = dialogTitle;
fMessage = dialogMessage;
fValue = "";//$NON-NLS-1$
fValidator = validator;
fBaseCommand = baseCommand;
}
public AddAnalysisDialog(Shell parentShell,
String dialogTitle,
IInputValidator nameValidator,
IInputValidator commandValidator) {
super(parentShell);
this.title = dialogTitle;
fNameValidator = nameValidator;
fCommandValidator = commandValidator;
}
public RenameDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue,
IInputValidator validator, boolean hasDefinitionSectionErrors) {
super(parentShell);
this.dialogTitle = dialogTitle;
this.dialogMessage = dialogMessage;
this.validator = validator;
this.modelContainsErrors = hasDefinitionSectionErrors;
}
private static IInputValidator createPackageFragmentRootNameValidator(final IPackageFragmentRoot root) {
return new IInputValidator() {
IInputValidator resourceNameValidator= createResourceNameValidator(root.getResource());
public String isValid(String newText) {
return resourceNameValidator.isValid(newText);
}
};
}
private static <T> T prompt(String title, String message, String defaultValue, Function<String, T> converter,
IInputValidator validator) {
InputDialog dialog = new InputDialog(UI.shell(), title, message, defaultValue, validator);
if (dialog.open() != IDialogConstants.OK_ID)
return null;
return converter.apply(dialog.getValue());
}
/**
* Return the new name to be given to the target resource.
*
* @return java.lang.String
* @param resource
* the resource to query status on
*/
protected String queryNewResourceName(final IResource resource) {
final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
final IPath prefix = resource.getFullPath().removeLastSegments(1);
IInputValidator validator = new IInputValidator() {
public String isValid(String string) {
if (resource.getName().equals(string)) {
return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
}
IStatus status = workspace.validateName(string, resource
.getType());
if (!status.isOK()) {
return status.getMessage();
}
if (workspace.getRoot().exists(prefix.append(string))) {
return IDEWorkbenchMessages.RenameResourceAction_nameExists;
}
return null;
}
};
InputDialog dialog = new InputDialog(shellProvider.getShell(),
IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage,
resource.getName(), validator);
dialog.setBlockOnOpen(true);
int result = dialog.open();
if (result == Window.OK)
return dialog.getValue();
return null;
}
/**
* 创建新库 ;
*/
private void createNewDatabase() {
// 数据库连接参数输入合法性检查
IStatus status = validator();
if (status.getSeverity() != IStatus.OK) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermDbManagerDialog.msgTitle"),
status.getMessage());
return;
}
SystemDBOperator sysDbOp = getCurrSysDbOp();
if (sysDbOp == null) {
return;
}
// 连接检查
if (!sysDbOp.checkDbConnection()) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermDbManagerDialog.msgTitle"),
Messages.getString("dialog.TermDbManagerDialog.msg1"));
return;
}
TermDbNameInputDialog inputDbNameialog = new TermDbNameInputDialog(getShell(),
Messages.getString("dialog.TermDbManagerDialog.inputDbNameialogTitle"),
Messages.getString("dialog.TermDbManagerDialog.inputDbNameialogMsg"), "", new IInputValidator() {
public String isValid(String newText) {
String vRs = DbValidator.valiateDbName(newText);
return vRs;
}
});
inputDbNameialog.setSystemDbOp(sysDbOp);
if (inputDbNameialog.open() == Window.OK) {
executeSearch(sysDbOp); // 刷新界面
}
}
/**
* 创建新库 ;
*/
private void createNewDatabase() {
// 数据库连接参数输入合法性检查
IStatus status = validator();
if (status.getSeverity() != IStatus.OK) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TmDbManagerDialog.msgTitle"),
status.getMessage());
return;
}
SystemDBOperator sysDbOp = getCurrSysDbOp();
if (sysDbOp == null) {
return;
}
// 连接检查
if (!sysDbOp.checkDbConnection()) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TmDbManagerDialog.msgTitle"),
Messages.getString("dialog.TmDbManagerDialog.msg1"));
return;
}
DatabaseNameInputDialog inputDbNameialog = new DatabaseNameInputDialog(getShell(),
Messages.getString("dialog.TmDbManagerDialog.inputDbNameialogTitle"),
Messages.getString("dialog.TmDbManagerDialog.inputDbNameialogMsg"), "", new IInputValidator() {
public String isValid(String newText) {
String vRs = DbValidator.valiateDbName(newText);
return vRs;
}
});
inputDbNameialog.setSystemDbOp(sysDbOp);
if (inputDbNameialog.open() == Window.OK) {
executeSearch(sysDbOp); // 刷新界面
}
}
/**
* Return the new name to be given to the target resource.
*
* @return java.lang.String
* @param resource
* the resource to query status on
*/
protected String queryNewResourceName(final IResource resource) {
final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
final IPath prefix = resource.getFullPath().removeLastSegments(1);
IInputValidator validator = new IInputValidator() {
public String isValid(String string) {
if (resource.getName().equals(string)) {
return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
}
IStatus status = workspace.validateName(string, resource
.getType());
if (!status.isOK()) {
return status.getMessage();
}
if (workspace.getRoot().exists(prefix.append(string))) {
return IDEWorkbenchMessages.RenameResourceAction_nameExists;
}
return null;
}
};
InputDialog dialog = new InputDialog(shellProvider.getShell(),
IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage,
resource.getName(), validator);
dialog.setBlockOnOpen(true);
int result = dialog.open();
if (result == Window.OK)
return dialog.getValue();
return null;
}
/**
* 创建新库 ;
*/
private void createNewDatabase() {
// 数据库连接参数输入合法性检查
IStatus status = validator();
if (status.getSeverity() != IStatus.OK) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermDbManagerDialog.msgTitle"),
status.getMessage());
return;
}
SystemDBOperator sysDbOp = getCurrSysDbOp();
if (sysDbOp == null) {
return;
}
// 连接检查
if (!sysDbOp.checkDbConnection()) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermDbManagerDialog.msgTitle"),
Messages.getString("dialog.TermDbManagerDialog.msg1"));
return;
}
TermDbNameInputDialog inputDbNameialog = new TermDbNameInputDialog(getShell(),
Messages.getString("dialog.TermDbManagerDialog.inputDbNameialogTitle"),
Messages.getString("dialog.TermDbManagerDialog.inputDbNameialogMsg"), "", new IInputValidator() {
public String isValid(String newText) {
String vRs = DbValidator.valiateDbName(newText);
return vRs;
}
});
inputDbNameialog.setSystemDbOp(sysDbOp);
if (inputDbNameialog.open() == Window.OK) {
executeSearch(sysDbOp); // 刷新界面
}
}
/**
* 创建新库 ;
*/
private void createNewDatabase() {
// 数据库连接参数输入合法性检查
IStatus status = validator();
if (status.getSeverity() != IStatus.OK) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TmDbManagerDialog.msgTitle"),
status.getMessage());
return;
}
SystemDBOperator sysDbOp = getCurrSysDbOp();
if (sysDbOp == null) {
return;
}
// 连接检查
if (!sysDbOp.checkDbConnection()) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.TmDbManagerDialog.msgTitle"),
Messages.getString("dialog.TmDbManagerDialog.msg1"));
return;
}
DatabaseNameInputDialog inputDbNameialog = new DatabaseNameInputDialog(getShell(),
Messages.getString("dialog.TmDbManagerDialog.inputDbNameialogTitle"),
Messages.getString("dialog.TmDbManagerDialog.inputDbNameialogMsg"), "", new IInputValidator() {
public String isValid(String newText) {
String vRs = DbValidator.valiateDbName(newText);
return vRs;
}
});
inputDbNameialog.setSystemDbOp(sysDbOp);
if (inputDbNameialog.open() == Window.OK) {
executeSearch(sysDbOp); // 刷新界面
}
}
public static String openInputRequest(String title, String message, Shell shell) {
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
if (newText.length() == 0) {
return "At least 1 char must be provided.";
}
return null;
}
};
return openInputRequest(title, message, shell, validator);
}
public static String openInputRequest(String title, String message, Shell shell, IInputValidator validator) {
if (shell == null) {
shell = EditorUtils.getShell();
}
String initialValue = "";
InputDialog dialog = new InputDialog(shell, title, message, initialValue, validator);
dialog.setBlockOnOpen(true);
if (dialog.open() == Window.OK) {
return dialog.getValue();
}
return null;
}