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