implemented a property
This commit is contained in:
parent
e0e40cf9cb
commit
6f4d8adf18
@ -4,6 +4,11 @@ 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 javafx.beans.binding.Bindings;
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
@ -23,9 +28,9 @@ public class ViewController {
|
||||
@FXML
|
||||
private ListView<Question> questionListView;
|
||||
|
||||
private Question currentQuestion;
|
||||
private ObjectProperty<Question> currentQuestion = new SimpleObjectProperty<>();
|
||||
private ObservableList<Question> questions;
|
||||
private boolean isInEditState;
|
||||
private BooleanProperty isInEditState = new SimpleBooleanProperty(false);
|
||||
|
||||
@FXML
|
||||
private void initialize() throws SQLException {
|
||||
@ -33,6 +38,12 @@ public class ViewController {
|
||||
selectBox.setItems(catalogs);
|
||||
selectBox.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> loadQuestions(newVal));
|
||||
questionListView.getSelectionModel().selectedItemProperty().addListener((_, _, newVal) -> updateCurrentQuestion(newVal));
|
||||
|
||||
editButton.textProperty().bind(Bindings.when(isInEditState).then("Edit").otherwise("Save"));
|
||||
questionField.editableProperty().bind(isInEditState);
|
||||
answerField.editableProperty().bind(isInEditState);
|
||||
deleteButton.disableProperty().bind(isInEditState);
|
||||
questionListView.disableProperty().bind(isInEditState);
|
||||
}
|
||||
|
||||
private void loadQuestions(QuestionCatalog catalog) {
|
||||
@ -47,42 +58,29 @@ public class ViewController {
|
||||
|
||||
private void updateCurrentQuestion(Question question) {
|
||||
if (question == null) return;
|
||||
currentQuestion = question;
|
||||
currentQuestion.set(question);
|
||||
questionField.setText(question.getQuestion());
|
||||
answerField.setText(question.getAnswer());
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void deleteQuestion() {
|
||||
if (currentQuestion == null || !showConfirmationButton("Delete question: \n" + currentQuestion.getQuestion())) return;
|
||||
QuestionService.DeleteQuestion(currentQuestion.getId());
|
||||
questions.remove(currentQuestion);
|
||||
if (currentQuestion.get() == null || !showConfirmationButton("Delete question: \n" + currentQuestion.get().getQuestion())) return;
|
||||
QuestionService.DeleteQuestion(currentQuestion.get().getId());
|
||||
questions.remove(currentQuestion.get());
|
||||
questionListView.refresh();
|
||||
currentQuestion = null;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void editQuestion() throws SQLException {
|
||||
if (isInEditState) {
|
||||
currentQuestion.setQuestion(questionField.getText());
|
||||
currentQuestion.setAnswer(answerField.getText());
|
||||
QuestionService.UpdateQuestion(currentQuestion);
|
||||
toggleEditState(false);
|
||||
if (isInEditState.get()) {
|
||||
currentQuestion.get().setQuestion(questionField.getText());
|
||||
currentQuestion.get().setAnswer(answerField.getText());
|
||||
QuestionService.UpdateQuestion(currentQuestion.get());
|
||||
isInEditState.set(false);
|
||||
} else {
|
||||
toggleEditState(true);
|
||||
isInEditState.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,18 @@
|
||||
package at.ionas999.questioncatalog.model;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
|
||||
public class Question {
|
||||
private int id;
|
||||
private String question;
|
||||
private String answer;
|
||||
private StringProperty answer = new SimpleStringProperty();
|
||||
private StringProperty question = new SimpleStringProperty();
|
||||
private int questionCatalogId;
|
||||
|
||||
public Question(int id,String question, String answer, int questionCatalogId) {
|
||||
this.id = id;
|
||||
this.question = question;
|
||||
this.answer = answer;
|
||||
this.question.set(question);
|
||||
this.answer.set(answer);
|
||||
this.questionCatalogId = questionCatalogId;
|
||||
}
|
||||
|
||||
@ -18,19 +21,28 @@ public class Question {
|
||||
return questionCatalogId;
|
||||
}
|
||||
public String getQuestion() {
|
||||
return question;
|
||||
return question.get();
|
||||
}
|
||||
|
||||
public void setQuestion(String question) {
|
||||
this.question = question;
|
||||
this.question.set(question);
|
||||
}
|
||||
|
||||
public String getAnswer() {
|
||||
public StringProperty questionProperty()
|
||||
{
|
||||
return question;
|
||||
}
|
||||
|
||||
public StringProperty answerProperty() {
|
||||
return answer;
|
||||
}
|
||||
|
||||
public String getAnswer() {
|
||||
return answer.get();
|
||||
}
|
||||
|
||||
public void setAnswer(String answer) {
|
||||
this.answer = answer;
|
||||
this.answer.set(answer);
|
||||
}
|
||||
|
||||
public void setQuestionCatalogId(int questionCatalogId) {
|
||||
@ -43,6 +55,6 @@ public class Question {
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return question;
|
||||
return question.get();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user