How to modify phone field?

2

I have a stylized component with phone formatting, as follows

  

(XX) XXXXX-XXXX

I would like to know how I can remove the part of DD → (XX) , so that the field looks like this: XXXXX-XXXX

I tried to modify the cases, but not very sure.

My component:

import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import java.util.function.UnaryOperator;

public class CampoTelefone extends TextField {
    UnaryOperator filter;

    public CampoTelefone() {
        /*Quando o campo é denominado apenas para numeros inteiros*/
        filter = new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change t) {
                String text = t.getText();
                for (int i = 0; i < text.length(); i++) {
                    if (!Character.isDigit(text.charAt(i)) && text.charAt(i) != '-' && text.charAt(i) != '.'
                            && text.charAt(i) != '/' && text.charAt(i) != '(' && text.charAt(i) != ')' && text.charAt(i) != ':') {
                        return null;
                    }
                }
                return t;
            }
        };

        setTextFormatter(new TextFormatter<String>(filter));
        focusedProperty().addListener(e -> mascara(getText(), getText()));

        setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(final KeyEvent keyEvent) {
                if (keyEvent.getCode().equals(KeyCode.UNDEFINED)
                        || keyEvent.getCode().equals(KeyCode.MINUS)
                        || keyEvent.getCode().equals(KeyCode.SUBTRACT)
                        || keyEvent.getCode().equals(KeyCode.DIVIDE)
                        || keyEvent.getCode().equals(KeyCode.PERIOD)) {
                    setText(getText().replace("-", "").replace("/", "").replace(".", "").replace("(", "").replace(")", "").replace(":", "").replace(" ", ""));
                }

                if (keyEvent.getCode().equals(KeyCode.CONTROL)) {
                    Clipboard clpbrd = Clipboard.getSystemClipboard();
                    final ClipboardContent content = new ClipboardContent();
                    String rep = clpbrd.getString().replaceAll("[^\d.]", "");
                    content.putString(rep);
                    clpbrd.setContent(content);
                }
            }

        });

        textProperty().addListener((ObservableValue<? extends String> ov, String antigo, String novo) -> {

                    if (!(novo).isEmpty() && novo.length() > antigo.length()) {
                        novo = novo.replace(".", "").replace("-", "").replace("/", "").replace("(", "").replace(")", "").replace(",", "").replace(":", "").replace(" ", "");
                        //antigo = antigo.replace(".", "").replace("-", "").replace("/", "").replace(",", "").replace("(", "").replace(")", "");
                        mascara(novo, antigo);
                    }
                }
        );
    }

    public void mascara(String novo, String antigo) {
        switch (novo.length()) {
            case 1:
                setText("(" + novo);
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 2:
                setText("(" + novo + ")");
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 3:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 3));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 4:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 4));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 5:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 5));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 6:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-");
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 7:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 7));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 8:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 8));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 9:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 9));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 10:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 6) + "-" + novo.substring(6, 10));
                Platform.runLater(() -> {
                    end();
                });
                break;
            case 11:
                setText("(" + novo.substring(0, 2) + ")" + novo.substring(2, 7) + "-" + novo.substring(7, 11));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 12:
                setText(antigo);
                break;
            default:
                setText(antigo);

        }
    }

    public void setValor(Object valor) {
        setText(String.valueOf(valor)); //To change body of generated methods, choose Tools | Templates.
    }

    public Object getValor() {

        return ((!getText().isEmpty())
                ? (getText()).replace("-", "").replace(".", "").replace("/", "").replace("(", "").replace(")", "") : "");
    }

    @Override
    public String getText(int start, int end) {
        if (start > end) {

        }

        if (start < 0 || end > getLength()) {
            //throw new IndexOutOfBoundsException();
        }
        return getContent().get(0, 0);
    }   
}
    
asked by anonymous 01.09.2017 / 13:50

1 answer

0

Just change the mascara method. The switch checks the size of the string, depending on the size, the characters of the mask are being added.

In the old method, the cases are adding the parentheses in the first 2 digits. Just remove this logic.

In cases 1, 2 and 3, no action needs to be taken, just set the text. From case 4 it is necessary to add a hyphen after the fourth digit. In%% with% 9 it is necessary to add a hyphen after the fifth digit.

public void mascara(String novo, String antigo) {
        switch (novo.length()) {
            case 1:
            case 2:
            case 3:
                setText(novo);
                break;
            case 4:
                setText(novo+"-");
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 5:
                setText(novo.substring(0, 4) + "-" + novo.substring(4, 5));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 6:
                setText(novo.substring(0, 4) + "-" + novo.substring(4, 6));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 7:
                setText(novo.substring(0, 4) + "-" + novo.substring(4, 7));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 8:
                setText(novo.substring(0, 4) + "-" + novo.substring(4, 8));
                Platform.runLater(() -> {
                    end();
                });

                break;
            case 9:
                setText(novo.substring(0, 5) + "-" + novo.substring(5, 9));
                Platform.runLater(() -> {
                    end();
                });

                break;
            default:
                setText(antigo);
        }
    }
    
06.12.2017 / 02:37