/** Gets called when an action is undone */ public void undoCallback() { if (undoBuffer.isPresent()) { TurboIssue issue = undoBuffer.get().getKey(); Action<TurboIssue> action = undoBuffer.get().getValue(); action .undo(issue) .thenApply(isSuccessful -> handleActionResult(issue, action, isSuccessful, true)); undoBuffer = Optional.empty(); } }
/** * Adds an action to the undo buffer. * * @param issue The TurboIssue to be acted on * @param action The Action that acts on the above TurboIssue */ public void addAction(TurboIssue issue, Action<TurboIssue> action) { undoBuffer = Optional.of(new Pair<>(issue, action)); action .act(issue) .thenApply(success -> handleActionResult(issue, action, success, false)) .exceptionally((e) -> handleActionException(issue, action, e)); }
/** * Shows an error dialog with a summary of the attempted action * * @param issue * @param action * @param errorMessage */ private void showErrorDialog(TurboIssue issue, Action action, String errorMessage) { Platform.runLater( () -> DialogMessage.showErrorDialog( "Error Requesting GitHub", String.format( "An error occurred while attempting to %s on:\n\n%s\n\n" + errorMessage, action.getDescription(), issue))); }
/** * Possibly shows a notification or an error dialog depending on {@code success} and {@code * isUndo}. * * @param issue the TurboIssue acted on * @param action the Action that acted on the issue * @param success whether the action was successful * @param isUndo whether action is an undo * @return {@code success} */ private boolean handleActionResult( TurboIssue issue, Action<TurboIssue> action, Boolean success, boolean isUndo) { if (!success) { String errorMessage = "Please check if you have write permissions to " + issue.getRepoId(); showErrorDialog(issue, action, errorMessage); return success; } if (!isUndo) { showNotification(issue.getId(), issue.getTitle(), action.getDescription()); return success; } return success; }