Java源码示例:org.controlsfx.validation.ValidationSupport
示例1
public CheckedNumberTextField(final double initialValue) {
super(Double.toString(initialValue));
final ValidationSupport support = new ValidationSupport();
final Validator<String> validator = (final Control control, final String value) -> {
final boolean condition = value == null || !(value.matches(CheckedNumberTextField.NUMBER_REGEX) || CheckedNumberTextField.isNumberInfinity(value));
// change text colour depending on validity as a number
setStyle(condition ? "-fx-text-inner-color: red;" : "-fx-text-inner-color: black;");
return ValidationResult.fromMessageIf(control, "not a number", Severity.ERROR, condition);
};
support.registerValidator(this, true, validator);
snappedTopInset();
snappedBottomInset();
HBox.setHgrow(this, Priority.ALWAYS);
VBox.setVgrow(this, Priority.ALWAYS);
}
示例2
@SuppressWarnings({"rawtypes", "unchecked"})
public ParameterSheetView(ParameterSet parameters, ValidationSupport validationSupport) {
super((ObservableList) FXCollections.observableList(parameters.getParameters()));
this.parameters = parameters;
setModeSwitcherVisible(false);
setSearchBoxVisible(false);
setMode(PropertySheet.Mode.NAME);
setPrefSize(600.0, 400.0);
// Set editor factory to keep track of which editing component belongs
// to which parameter
this.editorFactory = new ParameterEditorFactory(validationSupport);
setPropertyEditorFactory(editorFactory);
}
示例3
/** Form validation */
private void registerValidators() {
ValidationSupport validationSupport = new ValidationSupport();
Validator<String> noWhitespaceName = Validator.createRegexValidator("Name cannot contain whitespace", "\\S*+", Severity.ERROR);
Validator<String> emptyName = Validator.createEmptyValidator("Name required");
validationSupport.registerValidator(nameField, Validator.combine(noWhitespaceName, emptyName));
Validator<String> noWhitespaceMessage = Validator.createRegexValidator("Message cannot be whitespace", "(\\s*+\\S.*)?", Severity.ERROR);
Validator<String> emptyMessage = Validator.createEmptyValidator("Message required");
validationSupport.registerValidator(messageField, Validator.combine(noWhitespaceMessage, emptyMessage));
}
示例4
@SuppressWarnings({"rawtypes", "unchecked"})
private void addValidator(ValidationSupport validationSupport, Parameter<?> p,
ParameterEditor<?> pe) {
ParameterValidator pv = p.getValidator();
if (pv == null)
return;
Control mainControl = pe.getMainControl();
if (mainControl == null)
return;
if (mainControl != null && pv != null) {
// Create the official validator
Validator<?> validator = (control, value) -> {
ValidationResult result = new ValidationResult();
Object currentVal = pe.getValue();
List<String> errors = new ArrayList<>();
if (pv.checkValue(currentVal, errors))
return result;
// IF no message was produced, add our own message
if (errors.isEmpty())
errors.add(p.getName() + " is not set properly");
// Copy the messages to the result
for (String er : errors) {
String m = p.getName() + ": " + er;
ValidationMessage msg = ValidationMessage.error(control, m);
result.add(msg);
}
return result;
};
// Register the validator
validationSupport.registerValidator(mainControl, false, validator);
}
}
示例5
private void initGenerateXPathFromStackTrace() {
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem("Generate from stack trace...");
item.setOnAction(e -> {
try {
Stage popup = new Stage();
FXMLLoader loader = new FXMLLoader(DesignerUtil.getFxml("generate-xpath-from-stack-trace"));
Parent root = loader.load();
Button button = (Button) loader.getNamespace().get("generateButton");
TextArea area = (TextArea) loader.getNamespace().get("stackTraceArea");
ValidationSupport validation = new ValidationSupport();
validation.registerValidator(area, Validator.createEmptyValidator("The stack trace may not be empty"));
button.disableProperty().bind(validation.invalidProperty());
button.setOnAction(f -> {
DesignerUtil.stackTraceToXPath(area.getText()).ifPresent(xpathExpressionArea::replaceText);
popup.close();
});
popup.setScene(new Scene(root));
popup.initStyle(StageStyle.UTILITY);
popup.initModality(Modality.WINDOW_MODAL);
popup.initOwner(getDesignerRoot().getMainStage());
popup.show();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
});
menu.getItems().add(item);
xpathExpressionArea.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
if (t.getButton() == MouseButton.SECONDARY) {
menu.show(xpathExpressionArea, t.getScreenX(), t.getScreenY());
}
});
}
示例6
ParameterSetupDialog(ParameterSet parameters, @Nullable String title) {
super(AlertType.CONFIRMATION);
// Set window icon
final Image mzMineIcon = new Image("file:icon" + File.separator + "mzmine-icon.png");
Stage stage = (Stage) getDialogPane().getScene().getWindow();
stage.getIcons().setAll(mzMineIcon);
if (title != null) {
setTitle(title);
} else {
setTitle("Parameters");
}
setHeaderText("Please set parameter values");
setResizable(true);
// Add Help button
final URL helpURL = parameters.getClass().getResource("help/help.html");
setupHelpButton(helpURL);
// Add validation support
ValidationSupport validationSupport = new ValidationSupport();
// Add ParmeterSheetView to edit the parameters
ParameterSheetView sheet = new ParameterSheetView(parameters, validationSupport);
getDialogPane().setContent(sheet);
// When the user presses the OK button, we need to commit all the
// changes in the editors to the actual parameters
Button okButton = (Button) getDialogPane().lookupButton(ButtonType.OK);
okButton.addEventFilter(ActionEvent.ACTION, e -> {
if (validationSupport.isInvalid()) {
e.consume();
ValidationResult vr = validationSupport.getValidationResult();
StringBuilder message = new StringBuilder("Please check the parameter settings:\n\n");
for (ValidationMessage m : vr.getMessages()) {
message.append(m.getText());
message.append("\n");
}
MZmineGUI.displayMessage(message.toString());
return;
}
// If valid, commit the values
for (Parameter<?> parameter : parameters) {
PropertyEditor<?> editor = sheet.getEditorForParameter(parameter);
Object value = editor.getValue();
parameter.setValue(value);
}
});
}
示例7
public ParameterEditorFactory(ValidationSupport validationSupport) {
this.editorsMap = new HashMap<>();
this.validationSupport = validationSupport;
}