package local.fractal.frontend;

import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;

import java.util.Objects;

/**
 * This class provide utils for validation of the {@code TextField}.
 */
public class TextFormatterUtil {

    /**
     * Add validators (using TextFormatter and onFocus event) for input integer number in the text field.
     * @param textField text field
     * @param min minimum value of the number
     * @param max maximum value of the number
     * @param defaultValue default value of the number
     */
    public static void setIntegerRange(TextField textField, int min, int max, int defaultValue) {
        Objects.requireNonNull(textField);
        if (min > max)
            throw new IllegalArgumentException("min is less max");
        if (defaultValue < min)
            throw new IllegalArgumentException("defaultValue is less min");
        if (defaultValue > max)
            throw new IllegalArgumentException("defaultValue is great max");

        // set initial value
        textField.setText(String.valueOf(defaultValue));

        // allow enter only digits 0-9
        textField.setTextFormatter(new TextFormatter<Integer>(change -> {
            // pass empty string
            if (change.getControlNewText().isEmpty())
                return change;
            // if new value isn't integer, that reject changes
            try {
                Integer.valueOf(change.getControlNewText());
                return change;
            } catch (NumberFormatException e) {
                return null;
            }
        }));

        // check value range
        textField.focusedProperty().addListener((obs, oldValF, newValF) -> {
            if (!newValF) {
                if (textField.getText().isEmpty()) {
                    // set default value
                    textField.setText(String.valueOf(defaultValue));
                }
                // check current value
                int currentValue = Integer.valueOf(textField.getText());
                if (currentValue < min)
                    textField.setText(String.valueOf(min));
                if (currentValue > max)
                    textField.setText(String.valueOf(max));
            }
        });

    }


    /*
        extends TextFormatter<Integer> {
    public IntegerTextFormatter(@NamedArg("min") int min, @NamedArg("max") int max, @NamedArg("initValue") int initValue) {
        super(new IntegerStringConverter(),
                initValue,
                change -> {
                    // pass empty string
                    if (change.getControlNewText().isEmpty())
                        return change;
                    // if new value isn't integer, that reject changes
                    try {
                        Integer newVal = Integer.valueOf(change.getControlNewText());
                        if (newVal > max)
                            change.
                        return change;
                    } catch (NumberFormatException e) {
                        return null;
                    }
                }
        );
    }*/
}

@@ -134,16 +134,52 @@
 Objects;
+%0Aimport java.util.function.Function;
 %0A%0A/**%0A *
@@ -337,12 +337,9 @@
 tter
- and
+,
  onF
@@ -343,16 +343,29 @@
 onFocus 
+and onAction 
 event) f
@@ -407,16 +407,23 @@
  field.%0A
+     *%0A
      * @
@@ -434,24 +434,27 @@
 m textField 
+   
 text field%0A 
@@ -470,16 +470,25 @@
 ram min 
+         
 minimum 
@@ -525,16 +525,25 @@
 ram max 
+         
 maximum 
@@ -1711,109 +1711,57 @@
     
-textField.focusedProperty().addListener((obs, oldValF, newValF) -%3E %7B%0A            if (!newValF) %7B%0A    
+Function%3CString, String%3E checkText = (text) -%3E %7B%0A
     
@@ -1821,26 +1821,25 @@
         
-    
 // 
-s
+r
 et
+urn
  default
@@ -1861,38 +1861,23 @@
         
-    textField.setText(
+return 
 String.v
@@ -1896,17 +1896,16 @@
 ltValue)
-)
 ;%0A      
@@ -1906,34 +1906,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1939,32 +1939,50 @@
 k current value%0A
+            try %7B%0A
                 
@@ -2098,34 +2098,23 @@
         
-textField.setText(
+return 
 String.v
@@ -2124,17 +2124,16 @@
 eOf(min)
-)
 ;%0A      
@@ -2186,34 +2186,23 @@
         
-textField.setText(
+return 
 String.v
@@ -2212,17 +2212,16 @@
 eOf(max)
-)
 ;%0A      
@@ -2231,272 +2231,135 @@
    %7D
-%0A        %7D);%0A%0A    %7D%0A%0A%0A    /*%0A        extends TextFormatter%3CInteger%3E %7B%0A    public IntegerTextFormatter(@NamedArg(%22min%22) int min, @NamedArg(%22max%22) int max, @NamedArg(%22initValue%22) int initValue) %7B%0A        super(new IntegerStringConverter(),%0A                ini
+ catch (NumberFormatException e) %7B%0A                // return default value%0A                return String.valueOf(defaul
 tValue
-,
+);
 %0A   
@@ -2371,30 +2371,10 @@
     
-    change -%3E %7B%0A      
+%7D%0A
     
@@ -2385,105 +2385,41 @@
     
-  
 // 
-pass empty string%0A                    if (change.getControlNewText().isEmpty())%0A            
+return value without changing%0A
     
@@ -2425,38 +2425,36 @@
          return 
-change
+text
 ;%0A              
@@ -2443,33 +2443,36 @@
 xt;%0A        
- 
+%7D;%0A%0A
         
    // if new
@@ -2463,68 +2463,99 @@
     
-   // if new value isn't integer, that reject changes%0A      
+// add validate listeners%0A        textField.focusedProperty().addListener((obs, o, n) -%3E %7B%0A
     
@@ -2554,37 +2554,39 @@
 %3E %7B%0A            
-  try
+if (!n)
  %7B%0A             
@@ -2592,76 +2592,63 @@
     
-        Integer newVal = Integer.valueOf(change.getControlNew
+textField.setText(checkText.apply(textField.get
 Text())
+)
 ;%0A  
@@ -2653,36 +2653,34 @@
             
-    
+%7D%0A
         
 if (newVal %3E
@@ -2671,250 +2671,110 @@
     
-if (newVal %3E max)%0A                            change.%0A                        return change;%0A                    %7D catch (NumberFormatException e) %7B%0A                        return null;%0A                    %7D%0A                %7D%0A        
+%7D);%0A        textField.setOnAction(e -%3E textField.setText(checkText.apply(textField.getText()))
 );%0A    %7D
 */%0A%7D
@@ -2769,13 +2769,11 @@
 );%0A    %7D
-*/
 %0A%7D%0A

Correct the range validator for textField.