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());
ValidationSupport support = new ValidationSupport(); Validator<String> validator = new Validator<String>() { @Override public ValidationResult apply( Control control, String value ) { boolean condition = value != null ? !value .matches( "[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*" ) : value == null; System.out.println( condition ); return ValidationResult.fromMessageIf( control, "not a number", Severity.ERROR, condition ); } }; support.registerValidator( textfield, true, validator );
private void registerBasicValidators() { Validator<String> noWhitespaceName = Validator.createRegexValidator("Name cannot contain whitespace", "\\S*+", Severity.ERROR); Validator<String> emptyName = Validator.createEmptyValidator("Name required"); Validator<String> uniqueName = (c, val) -> { long sameNameDescriptors = backingDescriptorList.getOrElse(FXCollections.emptyObservableList()) .stream() .map(PropertyDescriptorSpec::getName) .filter(getName()::equals) .count(); return new ValidationResult().addErrorIf(c, "The name must be unique", sameNameDescriptors > 1); }; validationSupport.registerValidator(nameField, Validator.combine(noWhitespaceName, emptyName, uniqueName)); Validator<String> noWhitespaceDescription = Validator.createRegexValidator("Message cannot be whitespace", "(\\s*+\\S.*)?", Severity.ERROR); Validator<String> emptyDescription = Validator.createEmptyValidator("Message required"); validationSupport.registerValidator(descriptionField, Validator.combine(noWhitespaceDescription, emptyDescription)); }
this.validationSupport = new ValidationSupport(); btOk.disableProperty().bind(validationSupport.invalidProperty());
/** * Redecorates all known components * Only decorations related to validation are affected */ // TODO needs optimization public void redecorate() { Optional<ValidationDecoration> odecorator = Optional.ofNullable(getValidationDecorator()); for (Control target : getRegisteredControls()) { odecorator.ifPresent( decorator -> { decorator.removeDecorations(target); decorator.applyRequiredDecoration(target); if ( dataChanged.get() && isErrorDecorationEnabled()) { getHighestMessage(target).ifPresent(msg -> decorator.applyValidationDecoration(msg)); } }); } }
@Override public void initialize(URL location, ResourceBundle resources) { commitButton.setOnAction(e -> { commitHandler.ifPresent(Runnable::run); getStage().close(); this.free(); }); commitButton.disableProperty().bind(validationSupport.invalidProperty()); Platform.runLater(() -> { typeId.bind(typeChoiceBox.getSelectionModel().selectedItemProperty()); typeChoiceBox.setConverter(DesignerUtil.stringConverter(PropertyTypeId::getStringId, PropertyTypeId::lookupMnemonic)); typeChoiceBox.getItems().addAll(PropertyTypeId.typeIdsToConstants().values()); FXCollections.sort(typeChoiceBox.getItems()); }); Platform.runLater(this::registerBasicValidators); typeIdProperty().values() .filter(Objects::nonNull) .subscribe(this::registerTypeDependentValidators); }
private NameTextField(ProjectFolder folder) { Objects.requireNonNull(folder); new ValidationSupport().registerValidator(nameTextField, Validator.createEmptyValidator(RESOURCE_BUNDLE.getString("MandatoryName"))); nameTextField.setText(null); fileAlreadyExistsLabel.setTextFill(Color.RED); folderUnique = name -> name == null || !folder.getChild(name).isPresent(); nameTextField.textProperty().addListener((observable, oldName, newName) -> uniqueName.setValue(folderUnique.test(newName))); nameTextField.disabledProperty().addListener((observable, oldName, newName) -> uniqueName.setValue(newName)); uniqueName.addListener((observable, oldUnique, newUnique) -> { if (newUnique || nameTextField.isDisabled()) { fileAlreadyExistsLabel.setText(null); } else { fileAlreadyExistsLabel.setText(MessageFormat.format(RESOURCE_BUNDLE.getString("FileAlreadyExistsInThisFolder"), nameTextField.getText())); } }); }
private void registerTypeDependentValidators(PropertyTypeId typeId) { Validator<String> valueValidator = (c, val) -> ValidationResult.fromErrorIf(valueField, "The value couldn't be parsed", Try.tryGet(() -> getValueParser(typeId).valueOf(getValue())).isFailure()); validationSupport.registerValidator(valueField, valueValidator); }
); if (mandatory) { new ValidationSupport().registerValidator(textField, Validator.createEmptyValidator(RESOURCE_BUNDLE.getString("Mandatory"))); textField.setText(null);
/** * Registers {@link Validator} for specified control and makes control required * @param c control to validate * @param validator {@link Validator} to be used * @return true if registration is successful */ public <T> boolean registerValidator( final Control c, final Validator<T> validator ) { return registerValidator(c, true, validator); }
protected AbstractSecurityAnalysisRunnerPane(T node, Scene scene, GseContext context) { this.node = Objects.requireNonNull(node); Project project = node.getProject(); caseSelectionPane = new ProjectNodeSelectionPane<>(project, RESOURCE_BUNDLE.getString("Case"), true, scene.getWindow(), context, ProjectFile.class, ProjectCase.class); contingencyStoreSelectionPane = new ProjectNodeSelectionPane<>(project, RESOURCE_BUNDLE.getString("ContingencyStore"), true, scene.getWindow(), context, ProjectFile.class, ContingenciesProvider.class); setVgap(5); setHgap(5); setPrefWidth(450); setPrefHeight(150); ColumnConstraints column0 = new ColumnConstraints(); ColumnConstraints column1 = new ColumnConstraints(); column1.setHgrow(Priority.ALWAYS); getColumnConstraints().addAll(column0, column1); add(new Label(RESOURCE_BUNDLE.getString("Name") + ":"), 0, 0); add(nameTextField, 1, 0, 2, 1); add(caseSelectionPane.getLabel(), 0, 1); add(caseSelectionPane.getTextField(), 1, 1); add(caseSelectionPane.getButton(), 2, 1); add(contingencyStoreSelectionPane.getLabel(), 0, 2); add(contingencyStoreSelectionPane.getTextField(), 1, 2); add(contingencyStoreSelectionPane.getButton(), 2, 2); new ValidationSupport().registerValidator(nameTextField, Validator.createEmptyValidator(RESOURCE_BUNDLE.getString("MandatoryName"))); }
/** * Setup of the validation of the UI controls. */ private void setupValidation() { validationSupport.registerValidator(tfName, Validator.createEmptyValidator(context.getResources().getString("st.dlg.sporttype.error.no_name"))); }
txPassword.setText(new String(initialUserInfo == null ? "" : initialUserInfo.getValue())); //$NON-NLS-1$ ValidationSupport validationSupport = new ValidationSupport(); Platform.runLater( () -> { String requiredFormat = "'%s' is required"; //$NON-NLS-1$ validationSupport.registerValidator(txUserName, Validator.createEmptyValidator( String.format( requiredFormat, userNameCation ))); validationSupport.registerValidator(txPassword, Validator.createEmptyValidator(String.format( requiredFormat, passwordCaption )));
/** * Setup of the validation of the UI controls. */ private void setupValidation() { // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpStart, Validator.createEmptyValidator(context.getResources().getString("st.dlg.filter.error.date"))); validationSupport.registerValidator(dpEnd, Validator.createEmptyValidator(context.getResources().getString("st.dlg.filter.error.date"))); }); }
validationSupport.registerValidator(dpDate, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.exercise.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.exercise.error.time"))); validationSupport.registerValidator(cbSportType, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.exercise.error.no_sport_type"))); validationSupport.registerValidator(cbSportSubtype, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.exercise.error.no_sport_subtype"))); validationSupport.registerValidator(cbIntensity, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.exercise.error.no_intensity"))); validationSupport.registerValidator(tfDistance, true, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfDistance, context.getResources().getString("st.dlg.exercise.error.distance"), !ValidationUtils.isValueDoubleBetween(newValue, exerciseViewModel.sportTypeRecordDistance.get() ? 0.001f : 0, Float.MAX_VALUE))); validationSupport.registerValidator(tfAvgSpeed, true, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfAvgSpeed, context.getResources().getString("st.dlg.exercise.error.avg_speed"), !ValidationUtils.isValueSpeed(newValue, exerciseViewModel.getSpeedConverter(), exerciseViewModel.sportTypeRecordDistance.get()))); validationSupport.registerValidator(tfDuration, true, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfDuration, context.getResources().getString("st.dlg.exercise.error.duration"), !ValidationUtils.isValueTimeInSecondsBetween(newValue, 1, Integer.MAX_VALUE))); validationSupport.registerValidator(tfAscent, false, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfAscent, context.getResources().getString("st.dlg.exercise.error.ascent"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, Integer.MAX_VALUE))); validationSupport.registerValidator(tfDescent, false, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfDescent, context.getResources().getString("st.dlg.exercise.error.descent"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, Integer.MAX_VALUE)));
@Override protected void setupDialogControls() { // setup binding between view model and the UI controls dpDate.valueProperty().bindBidirectional(noteViewModel.date); // use text formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(noteViewModel.time); tfTime.setTextFormatter(timeTextFormatter); taText.textProperty().bindBidirectional(noteViewModel.comment); FxWorkarounds.fixDatePickerTextEntry(dpDate); // setup validation of the UI controls // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpDate, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.time"))); validationSupport.registerValidator(taText, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.no_text"))); }); }
@Override protected void setupDialogControls() { laWeightUnit.setText(context.getFormatUtils().getWeightUnitName()); // setup binding between view model and the UI controls dpDate.valueProperty().bindBidirectional(weightViewModel.date); // use text formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(weightViewModel.time); tfTime.setTextFormatter(timeTextFormatter); tfValue.textProperty().bindBidirectional(weightViewModel.value, new NumberStringConverter()); taComment.textProperty().bindBidirectional(weightViewModel.comment); FxWorkarounds.fixDatePickerTextEntry(dpDate); // setup validation of the UI controls // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpDate, Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.time"))); validationSupport.registerValidator(tfValue, true, (Control control, String newValue) -> ValidationResult .fromErrorIf(tfValue, context.getResources().getString("st.dlg.weight.error.weight"), !ValidationUtils.isValueDoubleBetween(newValue, 0.1d, 1000))); }); }