diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java index 0bd17d3..cc77b6e 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java @@ -5,9 +5,11 @@ import javafx.embed.swing.SwingFXUtils; import javafx.event.Event; import javafx.event.EventHandler; import javafx.geometry.Insets; +import javafx.scene.Node; import javafx.scene.control.CheckBox; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import meerkat.protobuf.BallotQuestionUIElementOuterClass; @@ -15,6 +17,9 @@ import meerkat.protobuf.BallotQuestionUIElementOuterClass; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; /** @@ -24,23 +29,27 @@ import java.io.IOException; class PicturesAnswersUpdater implements EventHandler{ private Stage currentStage; private int columIndex; + private List allAvailableAnswers; + private ByteString answer; PicturesAnswersUpdater(Stage primaryStage) { this.currentStage = primaryStage; this.columIndex = 0; // The lookup works only after the css have been randered this.currentStage.getScene().getRoot().applyCss(); + this.allAvailableAnswers = new ArrayList<>(); } /** * Gets all the binaryDatas from the ballot ui question and puts them into the binaryDatas container * - * @param question + * @param question the ballot question that contains all the pictures that are the answers */ void UpdateAnswers(BallotQuestionUIElementOuterClass.BallotQuestionUIElement question) { this.RemoveAllAnswers(); for (ByteString bytesAnswer : question.getAnswers().getAnswers().getAnswersList()) { try { + this.allAvailableAnswers.add(bytesAnswer); this.AddAnswer(bytesAnswer); } catch (IOException e) { e.printStackTrace(); @@ -69,9 +78,10 @@ class PicturesAnswersUpdater implements EventHandler{ * Creates binaryData element which is grid pane with the binaryData and check box * * @param binaryData string binaryData to show to the voter + * @param location the location of the picture * @return GridPane which contains string and check box */ - private GridPane GetAnswerElement(ByteString binaryData) throws IOException { + private GridPane GetAnswerElement(ByteString binaryData, int location) throws IOException { GridPane gridPane = new GridPane(); BufferedImage bufferedImage = ImageIO.read(binaryData.newInput()); @@ -80,11 +90,14 @@ class PicturesAnswersUpdater implements EventHandler{ ImageView imageView = new ImageView(); imageView.setImage(image); + BorderPane borderPane = new BorderPane(); CheckBox checkBox = new CheckBox(); + checkBox.setId(location+"-checkbox"); checkBox.setOnAction(this); + borderPane.setCenter(checkBox); gridPane.add(imageView, 2 , 1); - gridPane.add(checkBox, 2, 2); + gridPane.add(borderPane, 2, 2); gridPane.setPrefSize(100, 100); gridPane.setPadding(new Insets(10)); @@ -100,16 +113,45 @@ class PicturesAnswersUpdater implements EventHandler{ private void AddAnswer(ByteString binaryData) throws IOException { GridPane container = this.GetAnswersContainer(); - GridPane newAnswer = this.GetAnswerElement(binaryData); + this.columIndex++; + GridPane newAnswer = this.GetAnswerElement(binaryData, this.columIndex); container.addColumn(1); - this.columIndex++; container.add(newAnswer, this.columIndex, 0); this.currentStage.show(); } - @Override - public void handle(Event event) { + /** + * Unchecks all the check boxes that are not target box + * @param target + */ + private void uncheckBoxes(CheckBox target){ + GridPane answersContainer = this.GetAnswersContainer(); + for (Node child : answersContainer.getChildren()){ + GridPane answer = (GridPane)child; + BorderPane borderPane = (BorderPane) answer.getChildren().get(1); + CheckBox checkBox = (CheckBox)borderPane.getCenter(); + if (!Objects.equals(checkBox.getId(), target.getId()) && checkBox.isSelected()){ + checkBox.fire(); + } + } } + + @Override + /** + * Saves the user chose + */ + public void handle(Event event) { + if (event.getSource() instanceof CheckBox) { + CheckBox target = ((CheckBox)event.getSource()); + + if (target.isSelected()) { + String answerIndex = target.getId().split("-")[0]; + this.answer = this.allAvailableAnswers.get(Integer.parseInt(answerIndex) - 1); + this.uncheckBoxes(target); + } + } + } + } diff --git a/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml b/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml index 6452ce0..096b1f7 100644 --- a/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml +++ b/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml @@ -81,9 +81,8 @@ - - - + + @@ -121,27 +120,27 @@ - + - - + + - +
- + - + - + - +