cleaed it up a bit

This commit is contained in:
Jonas Hinterdorfer 2025-05-13 08:06:21 +02:00
parent 4fcda75290
commit e0e40cf9cb
6 changed files with 113 additions and 190 deletions

View File

@ -6,97 +6,81 @@ import at.ionas999.questioncatalog.services.QuestionCatalogService;
import at.ionas999.questioncatalog.services.QuestionService; import at.ionas999.questioncatalog.services.QuestionService;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.List;
public class AnswerTextController { public class AnswerTextController {
@FXML @FXML
private TextArea predefinedAnswerArea; private TextArea predefinedAnswerArea, userAnswerArea;
@FXML @FXML
private Button correctBtn; private Button correctBtn, incorrectBtn, skipBtn, showAnswerBtn;
@FXML @FXML
private Button incorrectBtn; private ComboBox<QuestionCatalog> selectBox;
@FXML
private TextArea userAnswerArea;
@FXML
private ComboBox selectBox;
@FXML
private Button skipBtn;
@FXML
private Button showAnswerBtn;
@FXML @FXML
private Label questionLabel; private Label questionLabel;
private Question currentQuestion = null; private Question currentQuestion;
private ObservableList<QuestionCatalog> catalogs; private List<Question> questions;
private ArrayList<Question> questions;
private int currentIdx = -1; private int currentIdx = -1;
@FXML @FXML
private void initialize() throws SQLException { private void initialize() throws SQLException {
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); ObservableList<QuestionCatalog> catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
selectBox.setItems(catalogs); selectBox.setItems(catalogs);
addObserverToSelectBox(); selectBox.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> loadQuestions(newVal));
} }
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();
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) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
});
} }
private void showNextQuestion() private void showNextQuestion() {
{ currentIdx = (currentIdx + 1) % questions.size();
this.currentIdx = ++this.currentIdx >= questions.size() ? 0 : this.currentIdx; currentQuestion = questions.get(currentIdx);
questionLabel.setText(currentQuestion.getQuestion());
resetUI();
}
this.currentQuestion = this.questions.get(this.currentIdx); private void resetUI() {
this.questionLabel.setText(this.currentQuestion.getQuestion()); incorrectBtn.setDisable(true);
correctBtn.setDisable(true);
this.incorrectBtn.setDisable(true); predefinedAnswerArea.clear();
this.correctBtn.setDisable(true); userAnswerArea.clear();
this.predefinedAnswerArea.setText(""); userAnswerArea.setEditable(true);
this.userAnswerArea.setText("");
this.userAnswerArea.setEditable(true);
} }
@FXML @FXML
private void onShowAnwerBtnClick(ActionEvent actionEvent) { private void onShowAnwerBtnClick() {
this.predefinedAnswerArea.setText(this.currentQuestion.getAnswer()); predefinedAnswerArea.setText(currentQuestion.getAnswer());
this.correctBtn.setDisable(false); correctBtn.setDisable(false);
this.incorrectBtn.setDisable(false); incorrectBtn.setDisable(false);
this.userAnswerArea.setEditable(false); userAnswerArea.setEditable(false);
} }
@FXML @FXML
private void onSkipBtnClick(ActionEvent actionEvent) { private void onSkipBtnClick() {
this.showNextQuestion();
}
@FXML
private void onCorrectBtnClick(ActionEvent actionEvent) {
//TODO: make a stat of how many time correct and how many times wrong
showNextQuestion(); showNextQuestion();
} }
@FXML @FXML
private void onIncorrectBtnClick(ActionEvent actionEvent) { private void onCorrectBtnClick() {
showNextQuestion();
}
@FXML
private void onIncorrectBtnClick() {
showNextQuestion(); showNextQuestion();
} }
} }

View File

@ -25,10 +25,4 @@ public class MainController {
Stage stage = Utils.getStageFromActionEven(actionEvent); Stage stage = Utils.getStageFromActionEven(actionEvent);
Utils.switchScenes("answerText.fxml", stage); Utils.switchScenes("answerText.fxml", stage);
} }
@FXML
private void testBtnClick(ActionEvent actionEvent) throws IOException {
Stage stage = Utils.getStageFromActionEven(actionEvent);
Utils.switchScenes("answerText.fxml", stage);
}
} }

View File

@ -10,34 +10,17 @@ import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; 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.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
public class UploadController { public class UploadController {
public TextArea csvContentArea; @FXML private TextArea csvContentArea;
@FXML @FXML private Button deleteCatalogButton, addCatalogButton, uploadButton, importButton;
private Button deleteCatalogButton; @FXML private TextField newCatalogField;
@FXML @FXML private ListView<QuestionCatalog> catalogListViw;
private TextField newCatalogField; @FXML private Label fileNameLabel;
@FXML
private Button addCatalogButton;
@FXML
private ListView catalogListViw;
@FXML
private Button uploadButton;
@FXML
private Label fileNameLabel;
@FXML
private Button importButton;
ObservableList<QuestionCatalog> catalogs; private ObservableList<QuestionCatalog> catalogs;
@FXML @FXML
private void initialize() throws SQLException { private void initialize() throws SQLException {
@ -48,40 +31,34 @@ public class UploadController {
@FXML @FXML
private void onAddNewCatalogBtnClick(ActionEvent actionEvent) throws SQLException { private void onAddNewCatalogBtnClick(ActionEvent actionEvent) throws SQLException {
String name = newCatalogField.getText(); String name = newCatalogField.getText();
QuestionCatalog c = new QuestionCatalog(-1, name, null); QuestionCatalog catalog = new QuestionCatalog(-1, name, null);
QuestionCatalogService.AddQuestionCatalogToDb(c); QuestionCatalogService.AddQuestionCatalogToDb(catalog);
this.catalogs.add(c); catalogs.add(catalog);
catalogListViw.refresh(); catalogListViw.refresh();
} }
@FXML @FXML
private void onDeleteCatalogBtnClick(ActionEvent actionEvent) { private void onDeleteCatalogBtnClick(ActionEvent actionEvent) {
QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); QuestionCatalog selectedCatalog = catalogListViw.getSelectionModel().getSelectedItem();
if (c == null) { if (selectedCatalog == null || !Utils.showConfirmationButton("Do you want to proceed with deleting a catalog with all questions contained?")) {
return; return;
} }
boolean result = Utils.showConfirmationButton("Do you want to proceed with deleting a catalog with all questions contained?"); QuestionCatalogService.DeleteQuestionCatalog(selectedCatalog.getId());
if (!result) { catalogs.remove(selectedCatalog);
return;
}
QuestionCatalogService.DeleteQuestionCatalog(c.getId());
catalogs.remove(c);
catalogListViw.refresh(); catalogListViw.refresh();
} }
@FXML @FXML
private void onImportBtnClick(ActionEvent actionEvent) { private void onImportBtnClick(ActionEvent actionEvent) {
QuestionCatalog c = (QuestionCatalog) catalogListViw.getSelectionModel().getSelectedItem(); QuestionCatalog selectedCatalog = catalogListViw.getSelectionModel().getSelectedItem();
String csvContent = csvContentArea.getText(); if (selectedCatalog == null) return;
String[] lines = csvContent.split("\n");
for(String line : lines) String[] lines = csvContentArea.getText().split("\n");
{ for (String line : lines) {
String[] segments = line.split(";"); String[] segments = line.split(";");
Question q = new Question(-1, segments[0], segments[1], c.getId()); Question question = new Question(-1, segments[0], segments[1], selectedCatalog.getId());
QuestionService.AddQuestionToDb(q); QuestionService.AddQuestionToDb(question);
} }
csvContentArea.setText(""); csvContentArea.clear();
} }
} }

View File

@ -4,117 +4,85 @@ import at.ionas999.questioncatalog.model.Question;
import at.ionas999.questioncatalog.model.QuestionCatalog; import at.ionas999.questioncatalog.model.QuestionCatalog;
import at.ionas999.questioncatalog.services.QuestionCatalogService; import at.ionas999.questioncatalog.services.QuestionCatalogService;
import at.ionas999.questioncatalog.services.QuestionService; import at.ionas999.questioncatalog.services.QuestionService;
import at.ionas999.questioncatalog.services.ServiceBase;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static at.ionas999.questioncatalog.Utils.showConfirmationButton; import static at.ionas999.questioncatalog.Utils.showConfirmationButton;
public class ViewController { public class ViewController {
@FXML @FXML
private ComboBox selectBox; private ComboBox<QuestionCatalog> selectBox;
@FXML @FXML
private Button editButton; private Button editButton, deleteButton;
@FXML @FXML
private Button deleteButton; private TextField answerField, questionField;
@FXML @FXML
private TextField answerField; private ListView<Question> questionListView;
@FXML
private ListView questionListView;
@FXML
private TextField questionField;
private Question currentQuestion = null; private Question currentQuestion;
private ObservableList<QuestionCatalog> catalogs;
private ObservableList<Question> questions; private ObservableList<Question> questions;
private boolean isInEditState = false; private boolean isInEditState;
@FXML @FXML
private void initialize() throws SQLException { private void initialize() throws SQLException {
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions()); ObservableList<QuestionCatalog> catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
selectBox.setItems(catalogs); selectBox.setItems(catalogs);
addObserverToSelectBox(); selectBox.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> loadQuestions(newVal));
addListenerToListView(); 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 addObserverToSelectBox() {
selectBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
return;
} }
private void loadQuestions(QuestionCatalog catalog) {
if (catalog == null) return;
try { try {
setQuestionListView(((QuestionCatalog) newValue).getId()); questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalog.getId()));
questionListView.setItems(questions);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
});
} }
private void setQuestionListView(int catalogId) throws SQLException { private void updateCurrentQuestion(Question question) {
this.questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalogId)); if (question == null) return;
questionListView.setItems(questions); currentQuestion = question;
questionField.setText(question.getQuestion());
answerField.setText(question.getAnswer());
} }
@FXML @FXML
private void deleteQuestion(ActionEvent actionEvent) { private void deleteQuestion() {
if (!showConfirmationButton("Do you want to proceed with deletion of the question: \n" + currentQuestion.getQuestion())) { if (currentQuestion == null || !showConfirmationButton("Delete question: \n" + currentQuestion.getQuestion())) return;
return;
}
QuestionService.DeleteQuestion(currentQuestion.getId()); QuestionService.DeleteQuestion(currentQuestion.getId());
questions.remove(currentQuestion); questions.remove(currentQuestion);
this.questionListView.refresh(); questionListView.refresh();
this.currentQuestion = null; currentQuestion = null;
} }
@FXML @FXML
private void editQuestion(ActionEvent actionEvent) throws SQLException { private void editQuestion() throws SQLException {
if (this.isInEditState) { if (isInEditState) {
this.currentQuestion.setQuestion(this.questionField.getText()); currentQuestion.setQuestion(questionField.getText());
this.currentQuestion.setAnswer(this.answerField.getText()); currentQuestion.setAnswer(answerField.getText());
QuestionService.UpdateQuestion(this.currentQuestion); QuestionService.UpdateQuestion(currentQuestion);
toggleEditState(false);
this.editButton.setText("Edit");
questionListView.refresh();
this.isInEditState = false;
this.setEditableAndSetDisabled(false);
} else { } else {
this.setEditableAndSetDisabled(true); toggleEditState(true);
this.editButton.setText("Save");
this.isInEditState = true;
} }
} }
private void setEditableAndSetDisabled(boolean value) { private void toggleEditState(boolean editing) {
questionField.setEditable(value); isInEditState = editing;
answerField.setEditable(value); editButton.setText(editing ? "Save" : "Edit");
this.questionListView.setDisable(value); setEditable(editing);
this.deleteButton.setDisable(value);
} }
private void setEditable(boolean editable) {
questionField.setEditable(editable);
answerField.setEditable(editable);
questionListView.setDisable(editable);
deleteButton.setDisable(editable);
}
} }

View File

@ -13,6 +13,5 @@
<Button text="Add New" onAction="#addNewBtnClick" prefWidth="150" /> <Button text="Add New" onAction="#addNewBtnClick" prefWidth="150" />
<Button text="View" onAction="#viewBtnClick" prefWidth="149" /> <Button text="View" onAction="#viewBtnClick" prefWidth="149" />
<Button text="Learn" onAction="#learnBtnClick" prefWidth="150" /> <Button text="Learn" onAction="#learnBtnClick" prefWidth="150" />
<Button text="Test" onAction="#testBtnClick" prefWidth="150" />
</VBox> </VBox>
</VBox> </VBox>

View File

@ -37,6 +37,7 @@
<Insets top="10" right="10" bottom="10" left="10"/> <Insets top="10" right="10" bottom="10" left="10"/>
</padding> </padding>
<Label text="Paste CSV Content Below:"/> <Label text="Paste CSV Content Below:"/>
<Label text="without Header, this format: Question;Answer"/>
<TextArea fx:id="csvContentArea" promptText="Paste your CSV content here ... " prefHeight="200.0"/> <TextArea fx:id="csvContentArea" promptText="Paste your CSV content here ... " prefHeight="200.0"/>
<Button fx:id="importButton" text="Import Questions" onAction="#onImportBtnClick"/> <Button fx:id="importButton" text="Import Questions" onAction="#onImportBtnClick"/>
</VBox> </VBox>