ua.com.ecotep.debtprevention.AuthParamsController.java Source code

Java tutorial

Introduction

Here is the source code for ua.com.ecotep.debtprevention.AuthParamsController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.com.ecotep.debtprevention;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.apache.commons.codec.binary.Base64;
import ua.com.ecotep.debtprevention.utils.ConfigEncoder;
import ua.com.rocketlv.customcontrol.TextMaxLengthFieldInstaller;
import ua.com.rocketlv.messages.AlertDialog;

/**
 * FXML Controller class
 *
 * @author Andrey
 */
public class AuthParamsController implements Initializable {

    @FXML
    private TextField adminName;
    @FXML
    private TextField databasePath;
    @FXML
    private PasswordField adminPassword;
    @FXML
    private PasswordField adminPasswordCopy;

    private Stage currentStage;
    private MainApp mainApp;

    public void setStage(Stage currentStage) {
        this.currentStage = currentStage;
    }

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TextMaxLengthFieldInstaller mlControl = new TextMaxLengthFieldInstaller();
        mlControl.install(adminName, 30);
        mlControl.install(databasePath, 60);
        mlControl.install(adminPassword, 30);
        mlControl.install(adminPasswordCopy, 30);
    }

    @FXML
    private void handleSaveAction(ActionEvent event) {
        if (adminName.textProperty().get().length() == 0) {
            AlertDialog.showSimpleMessage(
                    "? ? `? ?  .",
                    AlertDialog.ICON_INFO, currentStage);
            return;
        }
        if (databasePath.getText().length() == 0) {
            AlertDialog.showSimpleMessage("?  ?   .",
                    AlertDialog.ICON_INFO, currentStage);
            return;
        }
        if ((adminPassword.getText().length() < 3)
                || (!adminPassword.getText().equals(adminPasswordCopy.getText()))) {
            AlertDialog.showSimpleMessage(
                    "?  ,   ???.",
                    AlertDialog.ICON_INFO, currentStage);
            return;
        }
        DataOutputStream out = null;
        try {
            ConfigEncoder ce = new ConfigEncoder();
            File file = new File("debtpreventing.xml");
            Properties properties = new Properties();

            if (!file.exists()) {
                file.createNewFile();
                properties.setProperty("serverpath",
                        new String(Base64.encodeBase64(ce.encode(databasePath.getText()))));
                properties.setProperty("user", new String(Base64.encodeBase64(ce.encode(adminName.getText()))));
                properties.setProperty("md5", new String(Base64.encodeBase64(ce.encode(adminPassword.getText()))));
                properties.storeToXML(new FileOutputStream(file), "");
            }

            mainApp.showAuthScene();
        } catch (Exception ex) {
            Logger.getLogger(AuthParamsController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(AuthParamsController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @FXML
    private void handleCloseAction(ActionEvent event) {
        /*AlertDialog ad = new AlertDialog();
         int result=ad.showQuestionDialog(currentStage, "   ?");
         if (result==AlertDialog.YES_BUTTON) {           
         ad = new AlertDialog();
         ad.showMessageDialog(currentStage, " ?", AlertDialog.ICON_INFO);
         }*/
        System.exit(0);
    }
}