package de.thdeg.grademanager.gui; import de.thdeg.grademanager.JpaService; import de.thdeg.grademanager.model.CoursesOfStudy; import de.thdeg.grademanager.model.Student; import de.thdeg.grademanager.model.enumeration.Gender; 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; import static de.thdeg.grademanager.gui.MainController.coursesOfStudyList; public class StudentDetailsController { @FXML protected ComboBox<Gender> genderComboBox; @FXML protected ComboBox<Semester> semesterComboBox; ObservableList<Gender> genderList = FXCollections.observableArrayList(Gender.values()); ObservableList<Semester> semesterList = FXCollections.observableArrayList(Semester.values()); private static Student student; public static void setStudent(Student student) { StudentDetailsController.student = student; } @FXML protected ComboBox<CoursesOfStudy> coursesOfStudyComboBox; @FXML protected TextField firstName; @FXML protected TextField lastName; @FXML protected TextField placeOfResidence; @FXML protected TextField birthPlace; @FXML protected TextField coursesOfStudy; @FXML protected TextField studentID; @FXML protected TextField officialEmail; @FXML protected void initialize() { disableFields(true); genderComboBox.setItems(genderList); semesterComboBox.setItems(semesterList); if (student != null) { genderComboBox.setPromptText(student.getGender()); firstName.setText(student.getFirstName()); lastName.setText(student.getLastName()); placeOfResidence.setText(student.getPlaceOfResidence()); birthPlace.setText(student.getBirthPlace()); if (student.getCoursesOfStudy() != null){ coursesOfStudy.setText(student.getCoursesOfStudy().getName()); } semesterComboBox.setPromptText(Integer.toString(student.getSemester())); officialEmail.setText(student.getOfficialEmail()); studentID.setText(Integer.toString(student.getId())); coursesOfStudyComboBox.setItems(coursesOfStudyList); } } protected void disableFields(boolean value) { // Variables firstName.setDisable(value); lastName.setDisable(value); placeOfResidence.setDisable(value); birthPlace.setDisable(value); officialEmail.setDisable(value); // Buttons saveButton.setDisable(value); modifyButton.setDisable(!value); // ComboBoxes genderComboBox.setDisable(value); semesterComboBox.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 (!firstName.getText().isBlank() && !lastName.getText().isBlank() && !placeOfResidence.getText().isBlank() && !birthPlace.getText().isBlank() && !officialEmail.getText().isBlank()) { if(genderComboBox.getValue() != null){ student.setGender(genderComboBox.getValue().getGender()); } student.setFirstName(firstName.getText()); student.setLastName(lastName.getText()); student.setPlaceOfResidence(placeOfResidence.getText()); student.setBirthPlace(birthPlace.getText()); if(semesterComboBox.getValue() != null){ student.setSemester(semesterComboBox.getValue().getSemester()); } student.setOfficialEmail(officialEmail.getText()); JpaService jpaService = JpaService.getInstance(); jpaService.runInTransaction(entityManager -> {entityManager.merge(student); 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 assignCoursesOfStudy(ActionEvent event) { if (student != null && coursesOfStudyComboBox.getValue() != null && event.getSource() == coursesOfStudyComboBox){ student.setCoursesOfStudy(coursesOfStudyComboBox.getValue()); JpaService jpaService = JpaService.getInstance(); jpaService.runInTransaction(entityManager -> {entityManager.merge(student); return null;}); initialize(); } } @FXML protected void switchToMain(ActionEvent event) throws IOException { student = null; SwitchWindowHelper.switchTo("Main", event); } @FXML protected void switchToStudentCourses(ActionEvent event) throws IOException { StudentCoursesController.setStudent(student); SwitchWindowHelper.switchTo("Student Courses", event); } @FXML protected void switchToStudentStats(ActionEvent event) throws IOException{ StudentStatsController.setStudent(student); SwitchWindowHelper.switchTo("Student Stats", event); } }