From e0e40cf9cb58a9879e1d3578b6004ccccbbae113 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Tue, 13 May 2025 08:06:21 +0200 Subject: [PATCH] cleaed it up a bit --- .../controller/AnswerTextController.java | 108 +++++++--------- .../controller/MainController.java | 6 - .../controller/UploadController.java | 67 ++++------ .../controller/ViewController.java | 118 +++++++----------- .../at/ionas999/questioncatalog/main.fxml | 1 - .../at/ionas999/questioncatalog/upload.fxml | 3 +- 6 files changed, 113 insertions(+), 190 deletions(-) diff --git a/src/main/java/at/ionas999/questioncatalog/controller/AnswerTextController.java b/src/main/java/at/ionas999/questioncatalog/controller/AnswerTextController.java index 6d3230e..e26ac21 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/AnswerTextController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/AnswerTextController.java @@ -6,97 +6,81 @@ import at.ionas999.questioncatalog.services.QuestionCatalogService; import at.ionas999.questioncatalog.services.QuestionService; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.ComboBox; -import javafx.scene.control.Label; -import javafx.scene.control.TextArea; +import javafx.scene.control.*; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.List; public class AnswerTextController { @FXML - private TextArea predefinedAnswerArea; + private TextArea predefinedAnswerArea, userAnswerArea; @FXML - private Button correctBtn; + private Button correctBtn, incorrectBtn, skipBtn, showAnswerBtn; @FXML - private Button incorrectBtn; - @FXML - private TextArea userAnswerArea; - @FXML - private ComboBox selectBox; - @FXML - private Button skipBtn; - @FXML - private Button showAnswerBtn; + private ComboBox selectBox; @FXML private Label questionLabel; - private Question currentQuestion = null; - private ObservableList catalogs; - private ArrayList questions; + private Question currentQuestion; + private List questions; private int currentIdx = -1; + @FXML private void initialize() throws SQLException { - this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); + ObservableList catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); selectBox.setItems(catalogs); - addObserverToSelectBox(); - } - private void addObserverToSelectBox() { - selectBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - if (newValue == null) { - return; - } - this.skipBtn.setDisable(false); - this.showAnswerBtn.setDisable(false); - try { - this.questions = QuestionService.GetQuestionsFromCatalog(((QuestionCatalog) newValue).getId()); - this.showNextQuestion(); - - } catch (SQLException e) { - throw new RuntimeException(e); - } - }); + selectBox.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> loadQuestions(newVal)); } - private void showNextQuestion() - { - this.currentIdx = ++this.currentIdx >= questions.size() ? 0 : this.currentIdx; + private void loadQuestions(QuestionCatalog catalog) { + if (catalog == null) return; + skipBtn.setDisable(false); + showAnswerBtn.setDisable(false); + try { + questions = QuestionService.GetQuestionsFromCatalog(catalog.getId()); + showNextQuestion(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } - this.currentQuestion = this.questions.get(this.currentIdx); - this.questionLabel.setText(this.currentQuestion.getQuestion()); + private void showNextQuestion() { + currentIdx = (currentIdx + 1) % questions.size(); + currentQuestion = questions.get(currentIdx); + questionLabel.setText(currentQuestion.getQuestion()); + resetUI(); + } - this.incorrectBtn.setDisable(true); - this.correctBtn.setDisable(true); - this.predefinedAnswerArea.setText(""); - this.userAnswerArea.setText(""); - this.userAnswerArea.setEditable(true); + private void resetUI() { + incorrectBtn.setDisable(true); + correctBtn.setDisable(true); + predefinedAnswerArea.clear(); + userAnswerArea.clear(); + userAnswerArea.setEditable(true); } @FXML - private void onShowAnwerBtnClick(ActionEvent actionEvent) { - this.predefinedAnswerArea.setText(this.currentQuestion.getAnswer()); - this.correctBtn.setDisable(false); - this.incorrectBtn.setDisable(false); - this.userAnswerArea.setEditable(false); + private void onShowAnwerBtnClick() { + predefinedAnswerArea.setText(currentQuestion.getAnswer()); + correctBtn.setDisable(false); + incorrectBtn.setDisable(false); + userAnswerArea.setEditable(false); } @FXML - private void onSkipBtnClick(ActionEvent actionEvent) { - this.showNextQuestion(); - } - - @FXML - private void onCorrectBtnClick(ActionEvent actionEvent) { - //TODO: make a stat of how many time correct and how many times wrong + private void onSkipBtnClick() { showNextQuestion(); } @FXML - private void onIncorrectBtnClick(ActionEvent actionEvent) { + private void onCorrectBtnClick() { showNextQuestion(); } -} + + @FXML + private void onIncorrectBtnClick() { + showNextQuestion(); + } +} \ No newline at end of file diff --git a/src/main/java/at/ionas999/questioncatalog/controller/MainController.java b/src/main/java/at/ionas999/questioncatalog/controller/MainController.java index 083bc44..455fc4d 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/MainController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/MainController.java @@ -25,10 +25,4 @@ public class MainController { Stage stage = Utils.getStageFromActionEven(actionEvent); Utils.switchScenes("answerText.fxml", stage); } - - @FXML - private void testBtnClick(ActionEvent actionEvent) throws IOException { - Stage stage = Utils.getStageFromActionEven(actionEvent); - Utils.switchScenes("answerText.fxml", stage); - } } diff --git a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java index f61adee..52490a9 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/UploadController.java @@ -10,34 +10,17 @@ import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; -import javafx.stage.FileChooser; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; public class UploadController { - public TextArea csvContentArea; - @FXML - private Button deleteCatalogButton; - @FXML - private TextField newCatalogField; - @FXML - private Button addCatalogButton; - @FXML - private ListView catalogListViw; - @FXML - private Button uploadButton; - @FXML - private Label fileNameLabel; - @FXML - private Button importButton; + @FXML private TextArea csvContentArea; + @FXML private Button deleteCatalogButton, addCatalogButton, uploadButton, importButton; + @FXML private TextField newCatalogField; + @FXML private ListView catalogListViw; + @FXML private Label fileNameLabel; - ObservableList catalogs; + private ObservableList catalogs; @FXML private void initialize() throws SQLException { @@ -48,40 +31,34 @@ public class UploadController { @FXML private void onAddNewCatalogBtnClick(ActionEvent actionEvent) throws SQLException { String name = newCatalogField.getText(); - QuestionCatalog c = new QuestionCatalog(-1, name, null); - QuestionCatalogService.AddQuestionCatalogToDb(c); - this.catalogs.add(c); + QuestionCatalog catalog = new QuestionCatalog(-1, name, null); + QuestionCatalogService.AddQuestionCatalogToDb(catalog); + catalogs.add(catalog); catalogListViw.refresh(); } @FXML private void onDeleteCatalogBtnClick(ActionEvent actionEvent) { - QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); - if (c == null) { + QuestionCatalog selectedCatalog = catalogListViw.getSelectionModel().getSelectedItem(); + if (selectedCatalog == null || !Utils.showConfirmationButton("Do you want to proceed with deleting a catalog with all questions contained?")) { return; } - boolean result = Utils.showConfirmationButton("Do you want to proceed with deleting a catalog with all questions contained?"); - if (!result) { - return; - } - QuestionCatalogService.DeleteQuestionCatalog(c.getId()); - catalogs.remove(c); + QuestionCatalogService.DeleteQuestionCatalog(selectedCatalog.getId()); + catalogs.remove(selectedCatalog); catalogListViw.refresh(); } - @FXML private void onImportBtnClick(ActionEvent actionEvent) { - QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); - String csvContent = csvContentArea.getText(); - String[] lines = csvContent.split("\n"); + QuestionCatalog selectedCatalog = catalogListViw.getSelectionModel().getSelectedItem(); + if (selectedCatalog == null) return; - for(String line : lines) - { - String[] segments = line.split(";"); - Question q = new Question(-1, segments[0], segments[1], c.getId()); - QuestionService.AddQuestionToDb(q); - } - csvContentArea.setText(""); + String[] lines = csvContentArea.getText().split("\n"); + for (String line : lines) { + String[] segments = line.split(";"); + Question question = new Question(-1, segments[0], segments[1], selectedCatalog.getId()); + QuestionService.AddQuestionToDb(question); + } + csvContentArea.clear(); } } \ No newline at end of file diff --git a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java index 420430c..a9055d7 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/ViewController.java @@ -4,117 +4,85 @@ import at.ionas999.questioncatalog.model.Question; import at.ionas999.questioncatalog.model.QuestionCatalog; import at.ionas999.questioncatalog.services.QuestionCatalogService; import at.ionas999.questioncatalog.services.QuestionService; -import at.ionas999.questioncatalog.services.ServiceBase; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; import static at.ionas999.questioncatalog.Utils.showConfirmationButton; public class ViewController { @FXML - private ComboBox selectBox; + private ComboBox selectBox; @FXML - private Button editButton; + private Button editButton, deleteButton; @FXML - private Button deleteButton; + private TextField answerField, questionField; @FXML - private TextField answerField; - @FXML - private ListView questionListView; - @FXML - private TextField questionField; + private ListView questionListView; - private Question currentQuestion = null; - private ObservableList catalogs; + private Question currentQuestion; private ObservableList questions; - private boolean isInEditState = false; + private boolean isInEditState; @FXML private void initialize() throws SQLException { - this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); + ObservableList catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); selectBox.setItems(catalogs); - addObserverToSelectBox(); - addListenerToListView(); + selectBox.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> loadQuestions(newVal)); + questionListView.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> updateCurrentQuestion(newVal)); } - private void addListenerToListView() { - questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> { - if (newValue != null) { - currentQuestion = (Question) newValue; - System.out.println("changed " + currentQuestion); - Question q = (Question) newValue; - questionField.setText(q.getQuestion()); - answerField.setText(q.getAnswer()); - } - }); + private void loadQuestions(QuestionCatalog catalog) { + if (catalog == null) return; + try { + questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalog.getId())); + questionListView.setItems(questions); + } catch (SQLException e) { + throw new RuntimeException(e); + } } - private void addObserverToSelectBox() { - selectBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - if (newValue == null) { - return; - } - - try { - setQuestionListView(((QuestionCatalog) newValue).getId()); - } catch (SQLException e) { - throw new RuntimeException(e); - } - }); + private void updateCurrentQuestion(Question question) { + if (question == null) return; + currentQuestion = question; + questionField.setText(question.getQuestion()); + answerField.setText(question.getAnswer()); } - private void setQuestionListView(int catalogId) throws SQLException { - this.questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalogId)); - questionListView.setItems(questions); - } - - @FXML - private void deleteQuestion(ActionEvent actionEvent) { - if (!showConfirmationButton("Do you want to proceed with deletion of the question: \n" + currentQuestion.getQuestion())) { - return; - } - + private void deleteQuestion() { + if (currentQuestion == null || !showConfirmationButton("Delete question: \n" + currentQuestion.getQuestion())) return; QuestionService.DeleteQuestion(currentQuestion.getId()); questions.remove(currentQuestion); - this.questionListView.refresh(); - this.currentQuestion = null; + questionListView.refresh(); + currentQuestion = null; } @FXML - private void editQuestion(ActionEvent actionEvent) throws SQLException { - if (this.isInEditState) { - this.currentQuestion.setQuestion(this.questionField.getText()); - this.currentQuestion.setAnswer(this.answerField.getText()); - QuestionService.UpdateQuestion(this.currentQuestion); - - this.editButton.setText("Edit"); - questionListView.refresh(); - - this.isInEditState = false; - this.setEditableAndSetDisabled(false); + private void editQuestion() throws SQLException { + if (isInEditState) { + currentQuestion.setQuestion(questionField.getText()); + currentQuestion.setAnswer(answerField.getText()); + QuestionService.UpdateQuestion(currentQuestion); + toggleEditState(false); } else { - this.setEditableAndSetDisabled(true); - this.editButton.setText("Save"); - this.isInEditState = true; + toggleEditState(true); } } - private void setEditableAndSetDisabled(boolean value) { - questionField.setEditable(value); - answerField.setEditable(value); - this.questionListView.setDisable(value); - this.deleteButton.setDisable(value); + private void toggleEditState(boolean editing) { + isInEditState = editing; + editButton.setText(editing ? "Save" : "Edit"); + setEditable(editing); } - - + private void setEditable(boolean editable) { + questionField.setEditable(editable); + answerField.setEditable(editable); + questionListView.setDisable(editable); + deleteButton.setDisable(editable); + } } diff --git a/src/main/resources/at/ionas999/questioncatalog/main.fxml b/src/main/resources/at/ionas999/questioncatalog/main.fxml index 1487b55..d2eea2d 100644 --- a/src/main/resources/at/ionas999/questioncatalog/main.fxml +++ b/src/main/resources/at/ionas999/questioncatalog/main.fxml @@ -13,6 +13,5 @@