Added pictures showing

Now from given configuration the the pictures showed two,
android-scanner
VladimirEliTokarev 2016-10-03 20:30:43 +03:00
parent 21867fb5da
commit 6e47a4df98
2 changed files with 59 additions and 18 deletions

View File

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

View File

@ -81,9 +81,8 @@
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="102.0" minHeight="0.0" prefHeight="53.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="214.0" minHeight="10.0" prefHeight="161.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="102.0" minHeight="0.0" prefHeight="55.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="276.0" minHeight="10.0" prefHeight="261.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
@ -121,27 +120,27 @@
</BorderPane>
</children>
</GridPane>
<GridPane GridPane.rowIndex="1">
<GridPane prefHeight="250.0" prefWidth="600.0" GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="25.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="552.0" minWidth="10.0" prefWidth="548.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="25.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="166.0" minHeight="10.0" prefHeight="166.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="9.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="213.0" minHeight="10.0" prefHeight="213.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="41.0" minHeight="5.0" prefHeight="5.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<BorderPane prefHeight="250.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<center>
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<ScrollPane prefHeight="250.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="151.0" prefWidth="1000.0">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="199.0" prefWidth="1000.0">
<children>
<Pane prefHeight="151.0" prefWidth="1000.0">
<Pane prefHeight="199.0" prefWidth="1000.0">
<children>
<GridPane fx:id="AnswersGridPane" prefHeight="151.0" prefWidth="1000.0">
<GridPane fx:id="AnswersGridPane" layoutY="-2.0" prefHeight="199.0" prefWidth="1000.0">
</GridPane>
</children>
</Pane>