Skip to content
Snippets Groups Projects
CoursesOfStudyDetailsController.java 3.53 KiB
package de.thdeg.grademanager.gui;

import de.thdeg.grademanager.JpaService;
import de.thdeg.grademanager.model.Course;
import de.thdeg.grademanager.model.CoursesOfStudy;
import de.thdeg.grademanager.model.enumeration.Semester;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.io.IOException;
import java.util.Optional;

public class CoursesOfStudyDetailsController {

    @FXML
    protected ComboBox<Semester> durationComboBox;
    ObservableList<Semester> durationList = FXCollections.observableArrayList(Semester.values());


    @FXML
    protected TextField name;

    @FXML
    protected TextField fieldOfStudy;

    @FXML
    protected TextField fees;

    @FXML
    protected ListView<Course> courseListView;

    static public ObservableList<Course> courseList = FXCollections.observableArrayList();

    private static CoursesOfStudy coursesOfStudy;

    public static void setCourse(CoursesOfStudy value) {
        CoursesOfStudyDetailsController.coursesOfStudy = value;
    }

    @FXML
    protected void initialize() {
        disableFields(true);
        durationComboBox.setItems(durationList);

        if (coursesOfStudy != null){
            name.setText(coursesOfStudy.getName());
            durationComboBox.setPromptText(Integer.toString(coursesOfStudy.getDuration()));
            fieldOfStudy.setText(coursesOfStudy.getFieldOfStudy());
            fees.setText(Integer.toString(coursesOfStudy.getFees()));

            courseList.setAll(coursesOfStudy.getCourses());
            if (!courseList.isEmpty()) {
                courseListView.setItems(courseList);
            }
        }
    }

    protected void disableFields(boolean value) {
        // Variables
        name.setDisable(value);
        fieldOfStudy.setDisable(value);
        fees.setDisable(true);

        // Buttons
        saveButton.setDisable(value);
        modifyButton.setDisable(!value);
        // ComboBoxes
        durationComboBox.setDisable(value);
    }

    @FXML
    protected Button modifyButton;
    @FXML
    protected void onModifyButtonClick(ActionEvent event) {
        disableFields(false);
    }


    @FXML
    protected Button saveButton;
    @FXML
    protected void onSaveButtonClick(ActionEvent event) throws IOException {
        if (!name.getText().isBlank() && !fieldOfStudy.getText().isBlank()) {
            coursesOfStudy.setName(name.getText());
            if(durationComboBox.getValue() != null){
                coursesOfStudy.setDuration(durationComboBox.getValue().getSemester());
            }
            coursesOfStudy.setFieldOfStudy(fieldOfStudy.getText());

            JpaService jpaService = JpaService.getInstance();
            jpaService.runInTransaction(entityManager -> {entityManager.merge(coursesOfStudy); return null;});
            initialize();

            disableFields(true);
        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Fehler");
            alert.setHeaderText("Bitte alle Felder ausfüllen.");
            Optional<ButtonType> result = alert.showAndWait();
        }
    }

    @FXML
    protected void switchToMain(ActionEvent event) throws IOException {
        SwitchWindowHelper.switchTo("Main", event);
    }

    @FXML
    protected void switchToCourseModification(ActionEvent event) throws IOException {
        CourseModificationController.setCoursesOfStudy(coursesOfStudy);
        SwitchWindowHelper.switchTo("Course Modification", event);
    }
}