implemented the question edit
This commit is contained in:
parent
a424c04937
commit
af87631ff1
@ -14,6 +14,7 @@ import javafx.scene.control.*;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class ViewController {
|
public class ViewController {
|
||||||
@FXML
|
@FXML
|
||||||
@ -32,6 +33,7 @@ public class ViewController {
|
|||||||
private Question currentQuestion = null;
|
private Question currentQuestion = null;
|
||||||
private ObservableList<QuestionCatalog> catalogs;
|
private ObservableList<QuestionCatalog> catalogs;
|
||||||
private ObservableList<Question> questions;
|
private ObservableList<Question> questions;
|
||||||
|
private boolean isInEditState = false;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void initialize() throws SQLException {
|
private void initialize() throws SQLException {
|
||||||
@ -43,8 +45,9 @@ public class ViewController {
|
|||||||
|
|
||||||
private void addListenerToListView() {
|
private void addListenerToListView() {
|
||||||
questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
|
questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
|
||||||
currentQuestion = (Question) newValue;
|
|
||||||
if (newValue != null) {
|
if (newValue != null) {
|
||||||
|
currentQuestion = (Question) newValue;
|
||||||
|
System.out.println("changed " + currentQuestion);
|
||||||
Question q = (Question) newValue;
|
Question q = (Question) newValue;
|
||||||
questionField.setText(q.getQuestion());
|
questionField.setText(q.getQuestion());
|
||||||
answerField.setText(q.getAnswer());
|
answerField.setText(q.getAnswer());
|
||||||
@ -74,14 +77,60 @@ public class ViewController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void deleteQuestion(ActionEvent actionEvent) {
|
private void deleteQuestion(ActionEvent actionEvent) {
|
||||||
questions.remove(currentQuestion);
|
if(!showConfirmationButton("Do you want to proceed with deletion of the question: \n" + currentQuestion.getQuestion()))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QuestionService.DeleteQuestion(currentQuestion.getId());
|
QuestionService.DeleteQuestion(currentQuestion.getId());
|
||||||
|
questions.remove(currentQuestion);
|
||||||
this.questionListView.refresh();
|
this.questionListView.refresh();
|
||||||
this.currentQuestion = null;
|
this.currentQuestion = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void editQuestion(ActionEvent actionEvent) {
|
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);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.setEditableAndSetDisabled(true);
|
||||||
|
this.editButton.setText("Save");
|
||||||
|
this.isInEditState = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setEditableAndSetDisabled(boolean value)
|
||||||
|
{
|
||||||
|
questionField.setEditable(value);
|
||||||
|
answerField.setEditable(value);
|
||||||
|
this.questionListView.setDisable(value);
|
||||||
|
this.deleteButton.setDisable(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean showConfirmationButton(String content) {
|
||||||
|
|
||||||
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
|
alert.setTitle("Confirmation Dialog");
|
||||||
|
alert.setHeaderText("Are you sure?");
|
||||||
|
alert.setContentText(content);
|
||||||
|
|
||||||
|
ButtonType yesButton = new ButtonType("Yes");
|
||||||
|
ButtonType cancelButton = new ButtonType("Cancel");
|
||||||
|
|
||||||
|
alert.getButtonTypes().setAll(yesButton, cancelButton);
|
||||||
|
|
||||||
|
Optional<ButtonType> result = alert.showAndWait();
|
||||||
|
|
||||||
|
return result.filter(buttonType -> buttonType == yesButton).isPresent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,4 +66,23 @@ public class QuestionService {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public static void UpdateQuestion(Question question) throws SQLException {
|
||||||
|
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||||
|
String insertStmt = "update QUESTION set " +
|
||||||
|
"QUESTION = ?," +
|
||||||
|
"ANSWER=?," +
|
||||||
|
"QUESTIONCATALOGID=?" +
|
||||||
|
"where ID = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
|
||||||
|
statement.setInt(3, question.getQuestionCatalogId());
|
||||||
|
statement.setInt(4, question.getId());
|
||||||
|
statement.setString(1, question.getQuestion());
|
||||||
|
statement.setString(2, question.getAnswer());
|
||||||
|
statement.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user