Package jmri.util

Class ValidatingInputPane<T>

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

final class ValidatingInputPane<T> extends JPanel
A helper Panel for input-validating input boxes. It converts and validates the text input, disabling confirmUI component (usually a button) when the input is not valid.
  • Constructor Details

    • ValidatingInputPane

      public ValidatingInputPane(Function<String,T> convertor)
      Creates new form ValidatingInputPane.
      Parameters:
      convertor - converts String to the desired data type.
  • Method Details

    • attachConfirmUI

      Attaches a component used to confirm/proceed. The component will be disabled if the input is erroneous. The first validation will happen after this component appears on the screen. Typically, the OK button should be passed here.
      Parameters:
      confirm - the "confirm" control.
      Returns:
      this instance.
    • addNotify

      public void addNotify()
      Overrides:
      addNotify in class JComponent
    • message

      Configures a prompt message for the panel. The prompt message appears above the input line.
      Parameters:
      msg - message text
      Returns:
      this instance.
    • getException

      Returns the exception from the most recent validation. Only exceptions from unsuccessful conversion or thrown by validator are returned. To check whether the input is valid, call hasError(). If the validator just rejects the input with no exception, this method returns null
      Returns:
      exception thrown by converter or validator.
    • validator

      Configures the validator. Validator is called to check the value after the String input is converted to the target type. The validator can either just return false to reject the value with a generic message, or throw a IllegalArgumentException subclass with a custom message. The message will be then displayed below the input line.
      Parameters:
      val - validator instance, null to disable.
      Returns:
      this instance
    • hasError

      boolean hasError()
      Determines if the input is erroneous.
      Returns:
      error status
    • setText

      void setText(String text)
      Sets the input value, as text.
      Parameters:
      text - input text
    • getText

      Gets the input value, as text.
      Returns:
      the input text
    • getValue

      Gets the input value after conversion. May throw IllegalArgumentException if the conversion fails (text input cannot be converted to the target type). Returns null for empty (all whitespace) input.
      Returns:
      the entered value or null for empty input.
    • getErrorMessage

      Gets the error message. Either a custom message from an exception thrown by converter or validator, or the default message for failed validation. Returns null for valid input.
      Returns:
      if input is invalid, returns the error message. If the input is valid, returns null.
    • validateText

      void validateText(String text)
      Should be called from tests only
      Parameters:
      text - String to check for validation
    • getTextField