// Main command preview flow private ChangeListener<String> createUserInputListener() { return (observable, oldValue, newValue) -> { if (newValue.trim().isEmpty()) { ui.setPreviewStatus(FEEDBACK_READY); } else { ui.setPreviewStatus(previewCommand(newValue)); } }; }
// Listen for exit invoked by close button or system tray private ChangeListener<Boolean> createIsExitListener() { return (observable, oldValue, newValue) -> { if (newValue.booleanValue()) { if (!exit()) { ui.resetIsExit(); ui.setPreviewStatus(Feedback.FEEDBACK_TRY_AGAIN); } } }; }
// Main handle public void initUi(Stage stage) { ui = new UI(stage); ui.attachHandlersAndListeners( createKeyPressHandler(), createUserInputListener(), createIsExitListener()); if (hasStartupError) { ui.createErrorDialog(ERROR_STARTUP_HEADER, ERROR_STARTUP_MESSAGE); exit(); } initUiTaskList(); ui.setPreviewStatus(FEEDBACK_READY); }
// Process key press events private EventHandler<KeyEvent> createKeyPressHandler() { return (keyEvent) -> { // Remove initial splash overlay ui.hideSplashOverlay(); switch (keyEvent.getCode()) { // Main command execution flow case ENTER: { // Whitespace command if (ui.getInput().trim().isEmpty()) { ui.clearInput(); ui.hideHelpOverlay(); return; } // Hide help unless it's a help or exit command if (!lastPreviewedCommand.getType().equals(CommandType.HELP) && !lastPreviewedCommand.getType().equals(CommandType.EXIT)) { ui.hideHelpOverlay(); } // All the work happens here! String feedback = executeLastPreviewedCommand(); // Clear input box and display feedback // ClearInput must come before setStatus as user input listener // resets status when input is cleared ui.clearInput(); ui.setExecuteStatus(feedback); return; } // Edit description autocompletion case TAB: { if (!hasLastPreviewedCommand()) { return; } Command command = lastPreviewedCommand; if (command.getType() != CommandType.EDIT_PARTIAL) { return; } int lineNumber = lastPreviewedCommand.getLineNumber(); if (!isValidLineNumber(lineNumber)) { return; } ui.setInput( ui.getInput().trim() + " " + getTaskFromLineNumber(lineNumber).getDescription()); return; } // Scrolling case UP: { ui.scrollUpScreen(); return; } case DOWN: { ui.scrollDownScreen(); return; } // Show help case F1: { ui.showHelpOverlay(); if (ui.getInput().isEmpty()) { ui.setPreviewStatus(FeedbackHelp.HELP); } return; } // Activate next help page using left/right keys // (but only when the input box is empty) case LEFT: case RIGHT: { if (ui.getInput().isEmpty()) { ui.nextHelpPage(); } return; } // Hide help case ESCAPE: { ui.hideHelpOverlay(); if (ui.getInput().trim().isEmpty()) { ui.setPreviewStatus(FEEDBACK_READY); } return; } default: break; } }; }