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.Event;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBox;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import meerkat.protobuf.BallotQuestionUIElementOuterClass; import meerkat.protobuf.BallotQuestionUIElementOuterClass;
@ -15,6 +17,9 @@ import meerkat.protobuf.BallotQuestionUIElementOuterClass;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; 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{ class PicturesAnswersUpdater implements EventHandler{
private Stage currentStage; private Stage currentStage;
private int columIndex; private int columIndex;
private List<ByteString> allAvailableAnswers;
private ByteString answer;
PicturesAnswersUpdater(Stage primaryStage) { PicturesAnswersUpdater(Stage primaryStage) {
this.currentStage = primaryStage; this.currentStage = primaryStage;
this.columIndex = 0; this.columIndex = 0;
// The lookup works only after the css have been randered // The lookup works only after the css have been randered
this.currentStage.getScene().getRoot().applyCss(); 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 * 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) { void UpdateAnswers(BallotQuestionUIElementOuterClass.BallotQuestionUIElement question) {
this.RemoveAllAnswers(); this.RemoveAllAnswers();
for (ByteString bytesAnswer : question.getAnswers().getAnswers().getAnswersList()) { for (ByteString bytesAnswer : question.getAnswers().getAnswers().getAnswersList()) {
try { try {
this.allAvailableAnswers.add(bytesAnswer);
this.AddAnswer(bytesAnswer); this.AddAnswer(bytesAnswer);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -69,9 +78,10 @@ class PicturesAnswersUpdater implements EventHandler{
* Creates binaryData element which is grid pane with the binaryData and check box * Creates binaryData element which is grid pane with the binaryData and check box
* *
* @param binaryData string binaryData to show to the voter * @param binaryData string binaryData to show to the voter
* @param location the location of the picture
* @return GridPane which contains string and check box * @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(); GridPane gridPane = new GridPane();
BufferedImage bufferedImage = ImageIO.read(binaryData.newInput()); BufferedImage bufferedImage = ImageIO.read(binaryData.newInput());
@ -80,11 +90,14 @@ class PicturesAnswersUpdater implements EventHandler{
ImageView imageView = new ImageView(); ImageView imageView = new ImageView();
imageView.setImage(image); imageView.setImage(image);
BorderPane borderPane = new BorderPane();
CheckBox checkBox = new CheckBox(); CheckBox checkBox = new CheckBox();
checkBox.setId(location+"-checkbox");
checkBox.setOnAction(this); checkBox.setOnAction(this);
borderPane.setCenter(checkBox);
gridPane.add(imageView, 2 , 1); gridPane.add(imageView, 2 , 1);
gridPane.add(checkBox, 2, 2); gridPane.add(borderPane, 2, 2);
gridPane.setPrefSize(100, 100); gridPane.setPrefSize(100, 100);
gridPane.setPadding(new Insets(10)); gridPane.setPadding(new Insets(10));
@ -100,16 +113,45 @@ class PicturesAnswersUpdater implements EventHandler{
private void AddAnswer(ByteString binaryData) throws IOException { private void AddAnswer(ByteString binaryData) throws IOException {
GridPane container = this.GetAnswersContainer(); GridPane container = this.GetAnswersContainer();
GridPane newAnswer = this.GetAnswerElement(binaryData); this.columIndex++;
GridPane newAnswer = this.GetAnswerElement(binaryData, this.columIndex);
container.addColumn(1); container.addColumn(1);
this.columIndex++;
container.add(newAnswer, this.columIndex, 0); container.add(newAnswer, this.columIndex, 0);
this.currentStage.show(); 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 hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="102.0" minHeight="0.0" prefHeight="53.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="102.0" minHeight="0.0" prefHeight="55.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="214.0" minHeight="10.0" prefHeight="161.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="276.0" minHeight="10.0" prefHeight="261.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<GridPane> <GridPane>
@ -121,27 +120,27 @@
</BorderPane> </BorderPane>
</children> </children>
</GridPane> </GridPane>
<GridPane GridPane.rowIndex="1"> <GridPane prefHeight="250.0" prefWidth="600.0" GridPane.rowIndex="1">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="25.0" /> <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="552.0" minWidth="10.0" prefWidth="548.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="25.0" /> <ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="25.0" />
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="9.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="166.0" minHeight="10.0" prefHeight="166.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 maxHeight="41.0" minHeight="5.0" prefHeight="5.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <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> <center>
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <ScrollPane prefHeight="250.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<content> <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> <children>
<Pane prefHeight="151.0" prefWidth="1000.0"> <Pane prefHeight="199.0" prefWidth="1000.0">
<children> <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> </GridPane>
</children> </children>
</Pane> </Pane>