subelergiris.YeniSubeController.java Source code

Java tutorial

Introduction

Here is the source code for subelergiris.YeniSubeController.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 subelergiris;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class YeniSubeController implements Initializable {

    @FXML
    private Button bt_subeEkle;
    @FXML
    private Button bt_vazgec;
    @FXML
    private TextField tf_baslik;
    @FXML
    private TextField tf_telefon;
    @FXML
    private TextField tf_mail;
    @FXML
    private DatePicker dp_tarih;
    @FXML
    private TextArea ta_adres;
    protected static boolean uygunMail = false;
    private boolean mailGecerlimi = true;
    protected static boolean icerikVarmi = false;

    private void subeyeDon() throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("SubelerGiris.fxml"));
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                SessionFactory sf = NewHibernateUtil.getSessionFactory();
                Session session = sf.openSession();
                session.close();
                sf.close();
            }
        });
    }

    @FXML
    public void mailKontrol(KeyEvent evt) {
        if (isValidEmailAddress(tf_mail.getText()) || tf_mail.getText().equals("")) {
            mailGecerlimi = true;
            uygunMail = mailGecerlimi;
            System.out.println("Geerli mail" + mailGecerlimi);
            System.out.println("uygun mail" + uygunMail);
        } else {
            mailGecerlimi = false;
            uygunMail = mailGecerlimi;
            System.out.println("Geerli mail" + mailGecerlimi);
            System.out.println("uygun mail" + uygunMail);
        }
    }

    public static boolean isValidEmailAddress(String email) {
        boolean stricterFilter = true;
        String stricterFilterString = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        String laxString = ".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
        String emailRegex = stricterFilter ? stricterFilterString : laxString;
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(emailRegex);
        java.util.regex.Matcher m = p.matcher(email);
        return m.matches();
    }

    @FXML
    private void vazgec(ActionEvent evt) throws IOException {
        if (!tf_baslik.getText().equals("") || !tf_mail.getText().equals("") || !tf_telefon.getText().equals("")
                || !ta_adres.getText().equals("")) {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle("UYARI");
            alert.setHeaderText("Yaplan deiiklikler var. Kaydetmeden kmak istedinize emin misiniz ?");

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                subeyeDon();
                ((Node) evt.getSource()).getScene().getWindow().hide();
            } else {
                evt.consume();
            }
        } else {
            subeyeDon();
            ((Node) evt.getSource()).getScene().getWindow().hide();
        }
    }

    private void subeyiEkle() {
        SessionFactory sf1 = NewHibernateUtil.getSessionFactory();
        Session session = sf1.openSession();
        Subeler sb = new Subeler();
        sb.setSubeID(Integer.SIZE);
        sb.setSuperID(1);
        sb.setSBaslik(tf_baslik.getText().trim());
        sb.setSAdres(ta_adres.getText());
        sb.setSTelefon(tf_telefon.getText());
        sb.setSMail(tf_mail.getText());
        Date date = new Date();
        sb.setSTarih(date);
        Transaction tr = session.beginTransaction();
        session.save(sb);
        tr.commit();
        System.out.println("ekleme baarl");
        tf_baslik.setText("");
        tf_mail.setText("");
        tf_telefon.setText("");
        ta_adres.setText("");
    }

    @FXML
    private void icerikKontrol(KeyEvent evt) {

        if (!tf_mail.getText().equals("") || !tf_baslik.getText().equals("") || !tf_telefon.getText().equals("")
                || !ta_adres.getText().equals("")) {
            icerikVarmi = true;
            System.out.println("ierik var m :" + icerikVarmi);
        } else {
            icerikVarmi = false;
            System.out.println("ierik var m :" + icerikVarmi);
        }

    }

    @FXML
    private void ekle(ActionEvent evt) throws IOException {
        if ((tf_mail.getText().equals("")) || (tf_baslik.getText().equals("") || tf_mail.getText().equals("")
                || tf_telefon.getText().equals("") || ta_adres.getText().equals(""))) {
            if (!tf_mail.getText().equals("") && !mailGecerlimi) {
                Alert alert2 = new Alert(Alert.AlertType.WARNING);
                alert2.setTitle("UYARI");
                alert2.setHeaderText("Geersiz mail !!!");
                alert2.showAndWait();
                evt.consume();
            } else {
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("UYARI");
                alert.setHeaderText("Bo alanlar var. Bylece kaydedilsin mi " + "\n ube ad :"
                        + tf_baslik.getText() + " \n ube mail :" + tf_mail.getText() + " \nube telefon :"
                        + tf_telefon.getText() + " \nube adres :" + ta_adres.getText());
                Optional<ButtonType> result = alert.showAndWait();
                if (result.get() == ButtonType.OK) {
                    subeyiEkle();
                } else {
                    evt.consume();
                }
            }
        } else {
            if (mailGecerlimi) {
                Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
                alert2.setTitle("KAYDEDLSN M ?");
                alert2.setHeaderText("ube ad :" + tf_baslik.getText() + " \nube mail :" + tf_mail.getText()
                        + " \nube telefon :" + tf_telefon.getText() + " \nube adres :" + ta_adres.getText());
                Optional<ButtonType> result = alert2.showAndWait();
                if (result.get() == ButtonType.OK) {
                    subeyiEkle();
                } else {
                    evt.consume();
                }
            } else {
                Alert alert2 = new Alert(Alert.AlertType.WARNING);
                alert2.setTitle("Geersiz Mail !!!");
                alert2.showAndWait();
                evt.consume();
            }
        }

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        System.out.println(mailGecerlimi);

    }

}