Skip to content

Commit

Permalink
Handle CVR Read Failure in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Jun 30, 2024
1 parent 04a3835 commit cd23150
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions src/main/java/network/brightspots/rcv/GuiTabulateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void nameUpdated(KeyEvent keyEvent) {
public void buttonLoadCvrsClicked(ActionEvent actionEvent) {
String configPath = getConfigPathOrCreateTempFile();

enableButtonsUpTo(null);
setButtonsEnabled(false, false, false);
tabulateButton.setText("Tabulate");
Service<LoadedCvrData> service = guiConfigController.parseAndCountCastVoteRecords(configPath);

Expand Down Expand Up @@ -209,7 +209,7 @@ private void watchTabulatorServiceProgress(Service<Boolean> service) {
} else {
openResultsButton.setText("Close and View Errors");
}
enableButtonsUpTo(openResultsButton);
setButtonsEnabled(true, true, true);
stage.setOnCloseRequest(null);
long endTime = System.currentTimeMillis();
Logger.info("Tabulation took " + (endTime - startTime) / 1000.0 + " seconds.");
Expand All @@ -220,21 +220,29 @@ private void watchTabulatorServiceProgress(Service<Boolean> service) {
private void watchParseCvrServiceProgress(Service<LoadedCvrData> service) {
EventHandler<WorkerStateEvent> onSucceededEvent =
workerStateEvent -> {
enableButtonsUpTo(tabulateButton);
LoadedCvrData data = service.getValue();
tabulateButton.setText("Tabulate " + String.format("%,d", data.numCvrs()) + " ballots");

// Populate the per-source CVR count table, and calculate the width of the filename column
perSourceCvrCountTable.getItems().clear();
int maxFilenameLength = 0;
for (ResultsWriter.CvrSourceData sourceData : data.getCvrSourcesData()) {
String countString = String.format("%,d", sourceData.getNumCvrs());
String fileString = new File(sourceData.source.getFilePath()).getName();
perSourceCvrCountTable.getItems().add(new Pair<>(fileString, countString));
maxFilenameLength = Math.max(maxFilenameLength, fileString.length());
if (data == null || !data.successfullyReadAll) {
openResultsButton.setText("Close and View Errors");
setButtonsEnabled(false, false, true);
} else {
openResultsButton.setText("Check Ballot Counts");
setButtonsEnabled(true, true, false);
tabulateButton.setText("Tabulate " + String.format("%,d", data.numCvrs()) + " ballots");

// Populate the per-source CVR count table,
// and calculate the width of the filename column
perSourceCvrCountTable.getItems().clear();
int maxFilenameLength = 0;
for (ResultsWriter.CvrSourceData sourceData : data.getCvrSourcesData()) {
String countString = String.format("%,d", sourceData.getNumCvrs());
String fileString = new File(sourceData.source.getFilePath()).getName();
perSourceCvrCountTable.getItems().add(new Pair<>(fileString, countString));
maxFilenameLength = Math.max(maxFilenameLength, fileString.length());
}
float estWidth = getEstWidthForStringInPixels(maxFilenameLength);
perSourceCvrColumnFilepath.setPrefWidth(estWidth);
perSourceCvrCountTable.setVisible(true);
}
perSourceCvrColumnFilepath.setPrefWidth(getEstWidthForStringInPixels(maxFilenameLength));
perSourceCvrCountTable.setVisible(true);

data.discard();
lastLoadedCvrData = data;
Expand All @@ -250,7 +258,7 @@ private float getEstWidthForStringInPixels(int stringLength) {
private <T> void watchGenericService(
Service<T> service, EventHandler<WorkerStateEvent> onSuccessCallback) {
progressBar.progressProperty().bind(service.progressProperty());
enableButtonsUpTo(null);
setButtonsEnabled(false, false, false);

// Don't let user close the modal while the service is running
Window window = tabulateButton.getScene().getWindow();
Expand All @@ -274,31 +282,17 @@ private <T> void watchGenericService(
originalCallback.handle(workerStateEvent);
onSuccessCallback.handle(workerStateEvent);
});
service.setOnFailed(
workerStateEvent -> {
openResultsButton.setText("Close Close clos");
});
}

/**
* In the list of three buttons, Read > Tabulate > Open, sets the buttons up until this button as
* enabled.
*
* @param button Last button to be enabled, or null to disable all buttons
*/
private void enableButtonsUpTo(Button button) {
loadCvrButton.setDisable(true);
tabulateButton.setDisable(true);
openResultsButton.setDisable(true);

if (button == loadCvrButton) {
loadCvrButton.setDisable(false);
} else if (button == tabulateButton) {
loadCvrButton.setDisable(false);
tabulateButton.setDisable(false);
} else if (button == openResultsButton) {
loadCvrButton.setDisable(false);
tabulateButton.setDisable(false);
openResultsButton.setDisable(false);
} else if (button != null) {
throw new IllegalArgumentException("Invalid button");
}
private void setButtonsEnabled(
boolean loadCvrEnabled, boolean tabulateEnabled, boolean openResultsEnabled) {
loadCvrButton.setDisable(!loadCvrEnabled);
tabulateButton.setDisable(!tabulateEnabled);
openResultsButton.setDisable(!openResultsEnabled);
}

/**
Expand Down Expand Up @@ -357,9 +351,9 @@ private void setTabulationButtonStatus() {
}

if (isConfigSavedOrTempFileReady() && !userNameField.getText().isBlank()) {
enableButtonsUpTo(loadCvrButton);
setButtonsEnabled(true, false, false);
} else {
enableButtonsUpTo(null);
setButtonsEnabled(false, false, false);
}
}

Expand Down

0 comments on commit cd23150

Please sign in to comment.