From 53943ded0e8606c6d4fe0f1fc77622addf96fff7 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Tue, 13 May 2025 15:55:45 +0200 Subject: [PATCH] updated --- .../controller/TestController.java | 82 ++++++++++++------- .../controller/ViewController.java | 35 ++++++-- 2 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/main/java/at/ionas999/questioncatalog/controller/TestController.java b/src/main/java/at/ionas999/questioncatalog/controller/TestController.java index f025d84..40c587f 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/TestController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/TestController.java @@ -56,42 +56,62 @@ public class TestController { private int correctCount = 0; private int inCorrectCount = 0; - @FXML - private void initialize() throws SQLException { - catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); - catalogComboBox.setItems(catalogs); - correctAnswerArea.visibleProperty().bind(isInCorrectionState); - correctBtn.visibleProperty().bind(isInCorrectionState); - incorrectBtn.visibleProperty().bind(isInCorrectionState); - userAnswerArea.editableProperty().bind(isInCorrectionState.not()); - questionSlider.disableProperty().bind(isInConfiguringState.not()); - questionCountField.disableProperty().bind(isInConfiguringState.not()); - catalogComboBox.disableProperty().bind(isInConfiguringState.not()); - startBtn.textProperty().bind(Bindings.when(isInConfiguringState).then("Start").otherwise("Cancle")); - startBtn.visibleProperty().bind(isInConfiguringState); - nextQuestionBtn.disableProperty().bind(isInConfiguringState); - nextQuestionBtn.visibleProperty().bind(isInCorrectionState.not()); - correctAnswerLable.visibleProperty().bind(isInCorrectionState); +@FXML +private void initialize() throws SQLException { + catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); + catalogComboBox.setItems(catalogs); + correctAnswerArea.visibleProperty().bind(isInCorrectionState); + correctBtn.visibleProperty().bind(isInCorrectionState); + incorrectBtn.visibleProperty().bind(isInCorrectionState); + userAnswerArea.editableProperty().bind(isInCorrectionState.not()); + questionSlider.disableProperty().bind(isInConfiguringState.not()); + questionCountField.disableProperty().bind(isInConfiguringState.not()); + catalogComboBox.disableProperty().bind(isInConfiguringState.not()); + startBtn.textProperty().bind(Bindings.when(isInConfiguringState).then("Start").otherwise("Cancel")); + startBtn.visibleProperty().bind(isInConfiguringState); + nextQuestionBtn.disableProperty().bind(isInConfiguringState); + nextQuestionBtn.visibleProperty().bind(isInCorrectionState.not()); + correctAnswerLable.visibleProperty().bind(isInCorrectionState); - Bindings.bindBidirectional(questionCountField.textProperty(), questionSlider.valueProperty(), new NumberStringConverter()); + Bindings.bindBidirectional(questionCountField.textProperty(), questionSlider.valueProperty(), new NumberStringConverter()); - questionSlider.setBlockIncrement(1); - questionSlider.valueProperty().addListener((_, _, newValue) -> { - questionSlider.setValue(newValue.intValue()); - }); + questionSlider.setBlockIncrement(1); + questionSlider.valueProperty().addListener((_, _, newValue) -> { + questionSlider.setValue(newValue.intValue()); + }); - catalogComboBox.valueProperty().addListener((_,_,newValue) -> { - if(newValue == null) - return; - QuestionCatalog c = (QuestionCatalog) newValue; - try { - this.questionSlider.setMax(QuestionCatalogService.GetAmountOfQuestionsFromCatalog(c.getId())); - } catch (SQLException e) { - throw new RuntimeException(e); + catalogComboBox.valueProperty().addListener((_, _, newValue) -> { + if (newValue == null) + return; + QuestionCatalog c = (QuestionCatalog) newValue; + try { + this.questionSlider.setMax(QuestionCatalogService.GetAmountOfQuestionsFromCatalog(c.getId())); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }); + + // Add validation for questionCountField + questionCountField.textProperty().addListener((observable, oldValue, newValue) -> { + if (!newValue.matches("\\d*")) { + questionCountField.setText(newValue.replaceAll("[^\\d]", "")); + } else { + int value = newValue.isEmpty() ? 0 : Integer.parseInt(newValue); + if (value < questionSlider.getMin() || value > questionSlider.getMax()) { + questionCountField.setStyle("-fx-border-color: red;"); + } else { + questionCountField.setStyle(""); } - }); + } + }); - } + // Add validation for catalogComboBox + startBtn.disableProperty().bind( + catalogComboBox.valueProperty().isNull() + .or(questionCountField.textProperty().isEmpty()) + .or(questionCountField.styleProperty().isEqualTo("-fx-border-color: red;")) + ); +} @FXML private void onCorrectBtnClick(ActionEvent actionEvent) { diff --git a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java index 5bcec19..8045eae 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java @@ -37,9 +37,9 @@ public class ViewController { private ObjectProperty currentQuestion = new SimpleObjectProperty<>(); private FilteredList questions; - private BooleanProperty isInEditState = new SimpleBooleanProperty(false); + private final BooleanProperty isInEditState = new SimpleBooleanProperty(false); - @FXML + @FXML private void initialize() throws SQLException { ObservableList catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); selectBox.setItems(catalogs); @@ -51,11 +51,27 @@ public class ViewController { answerField.editableProperty().bind(isInEditState); deleteButton.disableProperty().bind(isInEditState); questionListView.disableProperty().bind(isInEditState); - searchField.textProperty().addListener((_,_,newValue) ->{ - if(newValue == null) - newValue = ""; + + // Disable searchField if no catalog is selected + searchField.disableProperty().bind(selectBox.getSelectionModel().selectedItemProperty().isNull()); + + // Disable editButton if fields are empty + editButton.disableProperty().bind( + questionField.textProperty().isEmpty() + .or(answerField.textProperty().isEmpty()) + .or(selectBox.getSelectionModel().selectedItemProperty().isNull()) + ); + deleteButton.disableProperty().bind( + questionField.textProperty().isEmpty() + .or(answerField.textProperty().isEmpty()) + .or(selectBox.getSelectionModel().selectedItemProperty().isNull()) + ); + + searchField.textProperty().addListener((_, _, newValue) -> { + if (newValue == null) + newValue = ""; String finalNewValue = newValue; - questions.setPredicate(x -> x.getQuestion().contains(finalNewValue)); + questions.setPredicate(x -> x.getQuestion().toLowerCase().contains(finalNewValue.toLowerCase())); }); } @@ -84,9 +100,9 @@ public class ViewController { private void deleteQuestion() { if (currentQuestion.get() == null || !showConfirmationButton("Delete question: \n" + currentQuestion.get().getQuestion())) return; QuestionService.DeleteQuestion(currentQuestion.get().getId()); - questions.remove(currentQuestion.get()); + questions.getSource().remove(currentQuestion.get()); questionListView.refresh(); - currentQuestion = null; + currentQuestion.set(null); } @FXML @@ -96,6 +112,7 @@ public class ViewController { currentQuestion.get().setAnswer(answerField.getText()); QuestionService.UpdateQuestion(currentQuestion.get()); isInEditState.set(false); + questionListView.refresh(); } else { isInEditState.set(true); } @@ -106,4 +123,4 @@ public class ViewController { Stage stage = Utils.getStageFromActionEven(actionEvent); Utils.switchScenes("main.fxml", stage); } -} +} \ No newline at end of file