From 151cf4fffd1dcfa57e4d598bbdd22a4ccd3f737f Mon Sep 17 00:00:00 2001 From: Dirk Lemmermann Date: Fri, 1 Dec 2023 12:54:20 +0100 Subject: [PATCH] Added read-only property exposing the parsing error type. --- .../dlsc/phonenumberfx/PhoneNumberField.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/phonenumberfx/src/main/java/com/dlsc/phonenumberfx/PhoneNumberField.java b/phonenumberfx/src/main/java/com/dlsc/phonenumberfx/PhoneNumberField.java index 320aaa7..bdc5ad2 100644 --- a/phonenumberfx/src/main/java/com/dlsc/phonenumberfx/PhoneNumberField.java +++ b/phonenumberfx/src/main/java/com/dlsc/phonenumberfx/PhoneNumberField.java @@ -1,6 +1,7 @@ package com.dlsc.phonenumberfx; import com.google.i18n.phonenumbers.AsYouTypeFormatter; +import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; import javafx.application.Platform; @@ -335,7 +336,8 @@ public void set(String newRawPhoneNumber) { try { Phonenumber.PhoneNumber number = phoneNumberUtil.parse(newRawPhoneNumber, country.iso2Code()); setPhoneNumber(number); - } catch (Exception e) { + } catch (NumberParseException e) { + errorType.set(e.getErrorType()); setPhoneNumber(null); } } else { @@ -350,6 +352,25 @@ public void set(String newRawPhoneNumber) { } }; + // ERROR TYPE + + private final ReadOnlyObjectWrapper errorType = new ReadOnlyObjectWrapper<>(this, "errorType"); + + public final NumberParseException.ErrorType getErrorType() { + return errorType.get(); + } + + /** + * Returns the error type property of the phone number field. The error type + * represents the type of error that occurred during the parsing of the phone + * number. + * + * @return the error type property + */ + public final ReadOnlyObjectProperty errorTypeProperty() { + return errorType.getReadOnlyProperty(); + } + // RAW PHONE NUMBER /** @@ -454,7 +475,7 @@ private void setE164PhoneNumber(String e164PhoneNumber) { /** * @return The phone number parsed out from the {@link #rawPhoneNumberProperty() raw phone number}, this might be {@code null} if the - * phone number is not a valid one. + * phone number is not valid. */ public final ReadOnlyObjectProperty phoneNumberProperty() { return phoneNumber.getReadOnlyProperty();