Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
12 kB
3
Indexable
Never
package cookbook.controller;

import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class CardsModifyDeleteUserController {

    @FXML
    private Circle circlePicture;
    @FXML
    private ImageView imageProfile;


    @FXML
    private Label nameLabel;
    @FXML
    private Label emailLabel;
    @FXML
    private Label passwordLabel;

    @FXML
    private TextField nameField;
    @FXML
    private TextField emailField;
    @FXML
    private TextField passwordField;

    @FXML
    private Circle redCircle;
    @FXML
    private Circle hoverCircle;
    @FXML
    private ImageView deleteImage;

    String nameT = ""; 
    String emailT = "";
    String passwordT = "";

    

    public void initialize() {

        //ImageView profilePicture = new ImageView(new Image("images/1619378021617.jpg"));
        Circle cercle = new Circle(19, 22, 18, Color.RED);
        imageProfile.setClip(cercle);
        
        // Name label and textField
        nameField.setEditable(false);
        nameField.setVisible(false);
        nameLabel.setVisible(true);
        nameLabel.setText(nameT);

        // Email label and textField
        emailField.setEditable(false);
        emailField.setVisible(false);
        emailLabel.setVisible(true);
        emailLabel.setText(emailT);

        // Password label and textField
        passwordField.setEditable(false);
        passwordField.setVisible(false);
        passwordLabel.setVisible(true);
        passwordLabel.setText(passwordT);


        // click name
        String name = nameLabel.getText();
        nameLabel.setOnMouseClicked(eName -> {
            clickedName(null, name);
        });

        // click email
        String email = emailLabel.getText();
        emailLabel.setOnMouseClicked(eEmail -> {
            clickedEmail(null, email);
        });

        // click password
        String passwordFi = passwordLabel.getText();
        StringBuilder hidden = new StringBuilder(passwordFi.length());
        for (int i = 0; i < passwordFi.length(); i++) {
            hidden.append("*");
        }
        passwordLabel.setText(hidden.toString());
        passwordLabel.setOnMouseClicked(ePassword -> {
            clickedPassword(null, hidden.toString());
        });

        // This is the hoover for when the mouse is on the round
        Interpolator interpolator = Interpolator.SPLINE(0.25, 0.1, 0.25, 1);
        hoverCircle.setOnMouseEntered(onmouse -> {
            Timeline growDeleteImageX = new Timeline (
                new KeyFrame(Duration.ZERO, new KeyValue(deleteImage.scaleXProperty(), 0)),
                new KeyFrame(Duration.millis(200), new KeyValue(deleteImage.scaleXProperty(), 3, interpolator))
            );
            growDeleteImageX.play();
            Timeline growDeleteImageY = new Timeline (
                new KeyFrame(Duration.ZERO, new KeyValue(deleteImage.scaleYProperty(), 0)),
                new KeyFrame(Duration.millis(200), new KeyValue(deleteImage.scaleYProperty(), 3, interpolator))
            );
            growDeleteImageY.play();
            FadeTransition fadeSmallDot = new FadeTransition(Duration.millis(100), redCircle);
            fadeSmallDot.setFromValue(1);
            fadeSmallDot.setToValue(0);
            fadeSmallDot.play();
        });
        // This is the animation for when the mouse exists the round
        hoverCircle.setOnMouseExited(onmouse -> {
            Timeline shrinkDeleteImageX = new Timeline (
                new KeyFrame(Duration.ZERO, new KeyValue(deleteImage.scaleXProperty(), 3)),
                new KeyFrame(Duration.millis(200), new KeyValue(deleteImage.scaleXProperty(), 0, interpolator))
            );
            shrinkDeleteImageX.play();
            Timeline shrinkDeleteImageY = new Timeline (
                new KeyFrame(Duration.ZERO, new KeyValue(deleteImage.scaleYProperty(), 3)),
                new KeyFrame(Duration.millis(200), new KeyValue(deleteImage.scaleYProperty(), 0, interpolator))
            );
            shrinkDeleteImageY.play();
            FadeTransition fadeSmallDot = new FadeTransition(Duration.millis(100), redCircle);
            fadeSmallDot.setFromValue(0);
            fadeSmallDot.setToValue(1);
            fadeSmallDot.play();
        });
    }

    public void clickedName(ActionEvent event, String aVoir){
        //Kill focus
        emailField.setEditable(false);
        emailField.setVisible(false);
        passwordField.setEditable(false);
        passwordField.setVisible(false);
        // Show Label
        passwordLabel.setVisible(true);
        emailLabel.setVisible(true);

        nameField.setStyle("-fx-text-box-border: None ; -fx-focus-color: None ;");
        
        nameField.setText(nameT);
        nameField.setEditable(true);
        nameField.setVisible(true);
        nameLabel.setVisible(false);
        nameField.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ENTER) {
                String enteredName = nameField.getText();
                nameLabel.setText(enteredName);
                nameField.setText(enteredName);
                nameT = enteredName;
                initialize();
            }
        });
        // When someone clicks outside, the focus exists
        Parent rootField = nameField.getScene().getRoot();
        rootField.setOnMouseClicked(ev -> {
            nameField.setText(nameT);
            if (ev.getTarget() != nameField && nameField.isFocused() ) {
                String enteredName = nameField.getText();
                nameLabel.setText(enteredName);
                nameField.setText(enteredName);
                nameT = enteredName;
                initialize();
            }
        });
    }

    public void clickedEmail(ActionEvent event, String aVoir){
        // Kill focus
        nameField.setEditable(false);
        nameField.setVisible(false);
        passwordField.setEditable(false);
        passwordField.setVisible(false);
        // Show Label
        nameLabel.setVisible(true);
        passwordLabel.setVisible(true);

        emailField.setStyle("-fx-text-box-border: None ; -fx-focus-color: None ;");

        emailField.setText(emailT);
        emailField.setEditable(true);
        emailField.setVisible(true);
        emailLabel.setVisible(false);
        emailField.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ENTER) {
                String enteredEmail = emailField.getText();
                if (!enteredEmail.contains("@")) {
                    // set the style of the TextField to make it appear red
                    emailField.setStyle("-fx-text-box-border: None ; -fx-focus-color: red ;");
                } else {
                emailField.setStyle("");
                emailLabel.setText(enteredEmail);
                emailField.setText(enteredEmail);
                emailT = enteredEmail;
                initialize();
                }
            }
        });
        // When someone clicks outside, the focus exists
        Parent rootField = emailField.getScene().getRoot();
        rootField.setOnMouseClicked(ev -> {
            emailField.setText(emailT);
            if (ev.getTarget() != emailField && emailField.isFocused() ) {
                String enteredEmail = emailField.getText();
                System.out.println("La le texte c'est caaaa" + enteredEmail);
                if (!enteredEmail.contains("@")) {
                    // set the style of the TextField to make it appear red
                    emailField.setStyle("-fx-text-box-border: None ; -fx-focus-color: red ;");
                } else {
                
                emailField.setStyle("");
                emailLabel.setText(enteredEmail);
                emailField.setText(enteredEmail);
                emailT = enteredEmail;
                initialize();
                }
            }
        });
    }

    public void clickedPassword(ActionEvent event, String aVoir){
        //Kill focus
        nameField.setEditable(false);
        nameField.setVisible(false);
        emailField.setEditable(false);
        emailField.setVisible(false);
        // Show Label
        nameLabel.setVisible(true);
        emailLabel.setVisible(true);

        passwordField.setStyle("-fx-text-box-border: None ; -fx-focus-color: None ;");


        passwordField.setText(passwordT);
        passwordField.setEditable(true);
        passwordField.setVisible(true);
        passwordLabel.setVisible(false);
        passwordField.textProperty().addListener((observable, oldValue, newValue) -> {
            // Get the last character in the text field
            String lastChar = newValue.substring(newValue.length() - 1);
            // Replace all characters except the last one with stars
            String maskedText = newValue.substring(0, newValue.length() - 1).replaceAll(".", "*");
            // Set the masked text with the last character appended
            passwordField.setText(maskedText + lastChar);
        });
        passwordField.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ENTER) {
                String enteredPassword = passwordField.getText();
                if (enteredPassword.length() < 6) {
                    // set the style of the TextField to make it appear red
                    passwordField.setStyle("-fx-text-box-border: red ; -fx-focus-color: red ;");
                } else {
                passwordField.setStyle("");
                passwordLabel.setText(enteredPassword);
                passwordField.setText(enteredPassword);
                passwordT = enteredPassword;
                initialize();
                }
            }
        });
        // When someone clicks outside, the focus exists
        Parent rootField = passwordField.getScene().getRoot();
        rootField.setOnMouseClicked(ev -> {
            passwordField.setText(passwordT);
            if (ev.getTarget() != passwordField && passwordField.isFocused() ) {
                String enteredPassword = passwordField.getText();
                if (enteredPassword.length() < 6) {
                    // set the style of the TextField to make it appear red
                    passwordField.setStyle("-fx-text-box-border: red ; -fx-focus-color: red ;");
                } else {
                passwordField.setStyle("");
                passwordLabel.setText(enteredPassword);
                passwordField.setText(enteredPassword);
                passwordT = enteredPassword;
                initialize();
                }
            }
        });
    }

    public void setInformation(String name, String email, String password) {
        nameT = name;
        emailT = email;
        passwordT = password;
        nameLabel.setText(nameT);
        emailLabel.setText(emailT);
        passwordLabel.setText(passwordT);
        nameField.setText(nameT);
        emailField.setText(emailT);
        passwordField.setText(passwordT);
    }


    
}