virtual printer (javaFX window) added

android-scanner
Laura Radaelli 2017-01-12 18:03:40 +02:00
parent b8418156b1
commit 72d96c4d78
17 changed files with 337 additions and 111 deletions

View File

@ -54,6 +54,9 @@ dependencies {
runtime 'ch.qos.logback:logback-classic:1.1.2'
runtime 'ch.qos.logback:logback-core:1.1.2'
// Jar that creates barcodes
compile group: 'net.sourceforge.barbecue', name: 'barbecue', version: '1.5-beta1'
// Google protobufs
compile 'com.google.protobuf:protobuf-java:3.+'

View File

@ -8,6 +8,7 @@ import meerkat.voting.controller.VotingBoothImpl;
import meerkat.voting.encryptor.VBCryptoManager;
import meerkat.voting.encryptor.VBCryptoManagerImpl;
import meerkat.voting.gui.ui.GraphicalUI;
import meerkat.voting.output.FXWindowOutputDevice;
import meerkat.voting.output.SystemConsoleOutputDevice;
import meerkat.voting.storage.StorageManager;
import meerkat.voting.storage.StorageManagerMockup;
@ -41,7 +42,7 @@ public class VotingBoothToyGraphicalRun {
DigitalSignature sig = new ToySignature("MY_SIGNER_ID");
StorageManager storageManager = new StorageManagerMockup();
SystemConsoleOutputDevice outputDevice = new SystemConsoleOutputDevice();
FXWindowOutputDevice outputDevice = new FXWindowOutputDevice();
VBCryptoManager cryptoManager = new VBCryptoManagerImpl(rand, enc, sig);
// SystemConsoleUI ui = new SystemConsoleUI ();
GraphicalUI ui = new GraphicalUI();

View File

@ -162,9 +162,6 @@ public class GraphicalUI implements VotingBoothUI, Runnable {
logger.debug("UI entered doShowWelcomeScreen");
VistaNavigator.setCurrentCommand(command);
VistaNavigator.loadVista(VistaNavigator.WELCOME_SCREEN);
// waitForEnter(null);
// ControllerCallback<Void> callback = command.getCallback();
// callback.onSuccess(null);
}
public void answerWelcomeScreen(StartSessionUICommand command) {
@ -278,30 +275,8 @@ public class GraphicalUI implements VotingBoothUI, Runnable {
*/
private void doCastOrAudit(CastOrAuditUICommand command) {
logger.debug("UI entered doCastOrAudit");
System.out.println ("Finalizing your vote. Do you wish to (C)ast or (A)udit?");
FinalizeBallotChoice fChoice;
try {
String s = readInputLine();
if (s.equals("cast") || s.equals("c")) {
fChoice = FinalizeBallotChoice.CAST;
}
else if (s.equals("audit") || s.equals("a")) {
fChoice = FinalizeBallotChoice.AUDIT;
}
else {
throw new IllegalArgumentException("UI could not understand the answer for cast/audit question '" + s + "'");
}
ControllerCallback callback = command.getCallback();
assert (callback instanceof CastOrAuditCallback);
((CastOrAuditCallback)callback).onSuccess(fChoice);
}
catch (IllegalArgumentException|IOException e) {
String errorMessage = "doCastOrAudit: some error with reading input from console. details: " + e;
logger.error(errorMessage);
command.getCallback().onFailure(e);
}
VistaNavigator.setCurrentCommand(command);
VistaNavigator.loadVista(VistaNavigator.CAST_OR_AUDIT);
}
@ -332,8 +307,7 @@ public class GraphicalUI implements VotingBoothUI, Runnable {
} else {
messageString = UIUtils.bytesToString(message.getData());
}
System.out.println(messageString);
System.out.print ("Waiting : .");
VistaNavigator.loadLoadingVista(messageString);
}
/**

View File

@ -5,10 +5,13 @@ package meerkat.voting.gui.ui;
*/
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import meerkat.voting.gui.ui.controllersFX.MainController;
import meerkat.voting.gui.ui.controllersFX.PrinterController;
import meerkat.voting.gui.ui.controllersFX.VistaNavigator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -19,6 +22,7 @@ import java.io.IOException;
public class MainFX extends Application {
private static final Logger logger = LoggerFactory.getLogger(GraphicalUI.class);
private Stage printing;
// @Override
// public void init() throws Exception {
@ -57,6 +61,9 @@ public class MainFX extends Application {
)
);
stage.show();
createPrinter(stage.getX(), stage.getY());
stage.requestFocus();
notifyLaunched();
}
@ -97,4 +104,22 @@ public class MainFX extends Application {
return scene;
}
private void createPrinter(double mainX, double mainY) throws IOException {
printing = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource(VistaNavigator.PRINTER));
Pane mainPane = null;
mainPane = (Pane) loader.load();
PrinterController printerController = loader.getController();
VistaNavigator.setPrinterController(printerController);
printing.setX(mainX - printing.getWidth());
printing.setY(mainY - printing.getHeight());
Scene scene = new Scene(mainPane);
printing.setScene(scene);
printing.show();
}
}

View File

@ -33,11 +33,13 @@ public class CastOrAuditController {
@FXML
private void cast() {
fChoice = VotingBoothUI.FinalizeBallotChoice.CAST;
nextPane();
}
@FXML
private void audit() {
fChoice = VotingBoothUI.FinalizeBallotChoice.AUDIT;
nextPane();
}
private void nextPane() {

View File

@ -0,0 +1,25 @@
package meerkat.voting.gui.ui.controllersFX;
/**
* Created by Laura on 01/12/2017.
*/
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
/**
* Main controller class for the entire layout.
*/
public class LoadingController {
/** Holder of a switchable vista. */
@FXML
private Text loading_text;
public void setContent(String s) {
loading_text.setText(s);
}
}

View File

@ -0,0 +1,42 @@
package meerkat.voting.gui.ui.controllersFX;
/**
* Created by Laura on 01/12/2017.
*/
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import meerkat.voting.gui.ui.CommandPend;
import meerkat.voting.gui.ui.uicommands.UICommand;
/**
* Main controller class for the entire layout.
*/
public class PrinterController {
/** Holder of a switchable vista. */
@FXML
private StackPane printerHolder;
@FXML
private Text content;
/**
* Replaces the vista displayed in the vista holder with a new vista.
*
* @param node the vista node to be swapped in.
*/
public void setVista(Node node) {
printerHolder.getChildren().setAll(node);
}
public void setContent(String s) {
content.setText(s);
}
public void addContent(String s) {
String old_content = content.getText();
content.setText(old_content+"\n"+s);
}
}

View File

@ -1,23 +0,0 @@
package meerkat.voting.gui.ui.controllersFX;
/**
* Created by Laura on 12/16/2016.
*/
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the first vista.
*/
public class Vista1Controller {
/**
* Event handler fired when the user requests a new vista.
*
*/
@FXML
void nextPane() {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}

View File

@ -1,24 +0,0 @@
package meerkat.voting.gui.ui.controllersFX;
/**
* Created by Laura on 12/16/2016.
*/
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the second vista.
*/
public class Vista2Controller {
/**
* Event handler fired when the user requests a previous vista.
*
* @param event the event that triggered the handler.
*/
@FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.EMPTY_START);
}
}

View File

@ -10,8 +10,6 @@ import meerkat.voting.gui.ui.GraphicalUI;
import meerkat.voting.gui.ui.uicommands.UICommand;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* Utility class for controlling navigation between vistas.
@ -25,15 +23,17 @@ public class VistaNavigator {
* Convenience constants for fxml layouts managed by the navigator.
*/
public static final String MAIN = "/views/main.fxml";
public static final String PRINTER = "/views/printer.fxml";
public static final String LOADING = "/views/loading.fxml";
public static final String EMPTY_START = "/views/empty_start.fxml";
public static final String WELCOME_SCREEN = "/views/welcome_screen.fxml";
public static final String VISTA_2 = "/views/vista2.fxml";
public static final String CHANNEL_CHOICE = "/views/channel_choice.fxml";
public static final String VOTING = "/views/voting.fxml";
public static final String CAST_OR_AUDIT = "/views/castOrAudit.fxml";
/** The main application layout controller. */
private static MainController mainController;
private static PrinterController printerController;
public static GraphicalUI uiThread;
@ -50,6 +50,19 @@ public class VistaNavigator {
return VistaNavigator.mainController;
}
/**
* Stores the printer controller for later use in printing tasks.
*
* @param printerController the printer application layout controller.
*/
public static void setPrinterController(PrinterController printerController) {
VistaNavigator.printerController = printerController;
}
public static PrinterController getPrinterController() {
return VistaNavigator.printerController;
}
/**
* Stores the thread that controls the UI.
*
@ -111,4 +124,53 @@ public class VistaNavigator {
}
}
/**
* Prints the content into the printer application layout.
*
* @param content a String with the content that should be printed.
* @param mode if value 'a' means append, any other character will erase current content of printer
*/
public static void printFX(String content, char mode) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (mode=='a') {
printerController.addContent(content);
} else {
printerController.setContent(content);
}
}
}
);
}
/**
* Loads the 'LOADING' vista
* vistaHolder pane of the main application layout.
*
* The parameter allows to set some special text to show in the new vista.
*
* @param text the extra text to show instead of 'loading...'
*/
public static void loadLoadingVista(String text) {
try {
FXMLLoader loader = new FXMLLoader(VistaNavigator.class.getResource(VistaNavigator.LOADING));
Node node = (Node) loader.load();
LoadingController loadingController = loader.getController();
loadingController.setContent(text);
Platform.runLater(new Runnable() {
@Override
public void run() {
mainController.setVista(node);
}
}
);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,131 @@
package meerkat.voting.output;
import com.google.protobuf.ByteString;
import meerkat.protobuf.Crypto.EncryptionRandomness;
import meerkat.protobuf.Crypto.RandomnessGenerationProof;
import meerkat.protobuf.Voting.BallotSecrets;
import meerkat.protobuf.Voting.PlaintextBallot;
import meerkat.protobuf.Voting.SignedEncryptedBallot;
import meerkat.voting.gui.ui.controllersFX.VistaNavigator;
import meerkat.voting.output.outputcommands.AuditOutputCommand;
import meerkat.voting.output.outputcommands.CancelOutputCommand;
import meerkat.voting.output.outputcommands.CastOutputCommand;
import meerkat.voting.output.outputcommands.CommitOutputCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A toy OutputDevice class
* outputs everything simply to the System console
*/
public class FXWindowOutputDevice extends AsyncRunnableOutputDevice {
private static final Logger logger = LoggerFactory.getLogger(FXWindowOutputDevice.class);
public FXWindowOutputDevice() {
super();
logger.info("A FXWindowOutputDevice is constructed");
}
/**
* Committing to the ballot.
* Simply prints to the output stream all the details in the CommitOutputCommand.
* @param command details to commit to, and the callback to call when finished
*/
public void doCommitToBallot(CommitOutputCommand command) {
logger.debug("entered method doCommitToBallot");
PlaintextBallot plaintextBallot = command.getPlaintext();
long plaintextSerialNumber = plaintextBallot.getSerialNumber();
String toPrint = "";
toPrint+="Commitment of Ballot #" + plaintextSerialNumber;
toPrint+="\n";
toPrint+="(channel): ";
toPrint+="\n";
toPrint+=bytesToString(command.getChannelIdentifierByteString());
toPrint+="\n";
toPrint+="(plaintext): ";
toPrint+="\n";
toPrint+=plaintextBallot;
SignedEncryptedBallot signedEncryptedBallot = command.getSignedEncryptedBallot();
long encryptedSerialNumber = signedEncryptedBallot.getEncryptedBallot().getSerialNumber();
toPrint+="Commitment of Ballot #" + encryptedSerialNumber + " (ciphertext):";
toPrint+="\n";
if (plaintextSerialNumber != encryptedSerialNumber) {
logger.error("plaintext and encryption serial numbers do not match!! plaintext# = " +
plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber);
}
ByteString encryptedData = signedEncryptedBallot.getEncryptedBallot().getData().getData();
toPrint+=bytesToString(encryptedData);
toPrint+="\n";
VistaNavigator.printFX(toPrint, 'w');
command.getCallback().onSuccess(null);
}
/**
* auditing the ballot.
* prints to the output stream the ballot secrets (the encryption randomness and its proof of random generation)
* @param command An auditing command with the callback to finally call
*/
public void doAudit(AuditOutputCommand command) {
logger.debug("entered method doAudit");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
String msg = "Auditing";
VistaNavigator.printFX(msg, 'a');
BallotSecrets ballotSecrets = command.getBallotSecrets();
printEncryptionRandomness(ballotSecrets.getEncryptionRandomness());
printRandomnessGenerationProof (ballotSecrets.getProof());
command.getCallback().onSuccess(null);
}
/**
* Casting the ballot (actually does nothing new)
* @param command a CastOutputCommand with the details and the callback
*/
public void doCastBallot(CastOutputCommand command) {
logger.debug("entered method doCastBallot");
String msg = "Ballot finalized for casting!";
VistaNavigator.printFX(msg, 'a');
command.getCallback().onSuccess(null);
}
/**
* Canceling the ballot (actually does nothing new)
* @param command a CancelOutputCommand with the details and the callback
*/
public void doCancel(CancelOutputCommand command) {
logger.debug("entered method doCancel");
System.out.println("Ballot cancelled!");
command.getCallback().onSuccess(null);
}
private void printEncryptionRandomness (EncryptionRandomness encryptionRandomness) {
String msg = "Encryption Randomness = ";
ByteString data = encryptionRandomness.getData();
msg+=bytesToString(data);
VistaNavigator.printFX(msg, 'a');
}
private void printRandomnessGenerationProof (RandomnessGenerationProof proof) {
String msg = "Proof of randomness generation:";
ByteString data = proof.getData();
msg+=bytesToString(data);
VistaNavigator.printFX(msg, 'a');
}
/*
* Returns the UTF8 decoding of byte-string data
*/
private static String bytesToString(ByteString data) {
return data.toStringUtf8();
}
}

View File

@ -7,8 +7,7 @@
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.CastOrAuditController">
<children>
<Text fx:id="question" layoutX="84.0" layoutY="89.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Cast or Audit?" textAlignment="CENTER" wrappingWidth="431.3046875" />
<Pane fx:id="questionPane" layoutX="84.0" layoutY="97.0" prefHeight="234.0" prefWidth="422.0" />
<Button layoutX="84.0" layoutY="343.0" mnemonicParsing="false" onMousePressed="#cast" text="Cast" />
<Button layoutX="469.0" layoutY="343.0" mnemonicParsing="false" onMousePressed="#audit" text="Audit" />
<Button layoutX="150.0" layoutY="200.0" mnemonicParsing="false" onMousePressed="#cast" prefHeight="50.0" prefWidth="90.0" text="Cast" />
<Button layoutX="360.0" layoutY="200.0" mnemonicParsing="false" onMousePressed="#audit" prefHeight="50.0" prefWidth="90.0" text="Audit" />
</children>
</Pane>

View File

@ -3,9 +3,8 @@
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Text?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.LoadingController">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Loading..." />
<Text fx:id="loading_text" strokeType="OUTSIDE" strokeWidth="0.0" text="Loading..." />
</children>
</StackPane>

View File

@ -1,13 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?scenebuilder-stylesheet vista.css?>
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.MainController">
<VBox prefHeight="709.0" prefWidth="1259.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.MainController">
<children>
<Label fx:id="headerLabel" maxWidth="1.7976931348623157E308" text="Header" VBox.vgrow="NEVER" />
<StackPane fx:id="vistaHolder" prefHeight="404.0" prefWidth="524.0" VBox.vgrow="ALWAYS" />
<Label fx:id="headerLabel" maxWidth="1.7976931348623157E308" prefHeight="70.0" prefWidth="1162.0" text="Header of Election" textFill="#939090" VBox.vgrow="NEVER">
<font>
<Font size="25.0" />
</font>
<VBox.margin>
<Insets left="100.0" />
</VBox.margin></Label>
<StackPane fx:id="vistaHolder" prefHeight="553.0" prefWidth="1259.0" style="-fx-background-color: #eeeeee;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="70.0" />
</VBox.margin></StackPane>
</children>
</VBox>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?scenebuilder-stylesheet vista.css?>
<VBox prefHeight="472.0" prefWidth="354.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="meerkat.voting.gui.ui.controllersFX.PrinterController">
<children>
<ScrollPane prefHeight="595.0" prefWidth="354.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="830.0" prefWidth="337.0">
<children>
<Text fx:id="content" layoutX="23.0" layoutY="32.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Printer will print here..." wrappingWidth="256.13671875" />
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</VBox>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?scenebuilder-stylesheet vista.css?>
<StackPane fx:id="vista1" xmlns:fx="http://javafx.com/fxml" fx:controller="meerkat.voting.gui.ui.controllersFX.Vista1Controller">
<children>
<Button mnemonicParsing="false" onAction="#nextPane" text="Next Pane" />
</children>
</StackPane>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?scenebuilder-stylesheet vista.css?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="vista2" xmlns:fx="http://javafx.com/fxml" fx:controller="meerkat.voting.gui.ui.controllersFX.Vista2Controller">
<children>
<Button mnemonicParsing="false" onAction="#previousPane" text="Previous Pane" />
</children>
</StackPane>