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 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<QuestionCatalog> selectBox;
@FXML
private Label questionLabel;
private Question currentQuestion = null;
private ObservableList<QuestionCatalog> catalogs;
private ArrayList<Question> questions;
private Question currentQuestion;
private List<Question> questions;
private int currentIdx = -1;
@FXML
private void initialize() throws SQLException {
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
ObservableList<QuestionCatalog> 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();
}
}

View File

@ -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);
}
}

View File

@ -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<QuestionCatalog> catalogListViw;
@FXML private Label fileNameLabel;
ObservableList<QuestionCatalog> catalogs;
private ObservableList<QuestionCatalog> 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();
}
}

View File

@ -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<QuestionCatalog> 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<Question> questionListView;
private Question currentQuestion = null;
private ObservableList<QuestionCatalog> catalogs;
private Question currentQuestion;
private ObservableList<Question> questions;
private boolean isInEditState = false;
private boolean isInEditState;
@FXML
private void initialize() throws SQLException {
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
ObservableList<QuestionCatalog> 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);
}
}

View File

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

View File

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