Example usage for javafx.scene.control TextInputDialog getEditor

List of usage examples for javafx.scene.control TextInputDialog getEditor

Introduction

In this page you can find the example usage for javafx.scene.control TextInputDialog getEditor.

Prototype

public final TextField getEditor() 

Source Link

Document

Returns the TextField used within this dialog.

Usage

From source file:org.sleuthkit.autopsy.timeline.actions.SaveSnapshotAsReport.java

/**
 * Constructor// w w w  .ja  v a  2 s . c o  m
 *
 * @param controller   The controller for this timeline action
 * @param nodeSupplier The Supplier of the node to snapshot.
 */
@NbBundle.Messages({ "Timeline.ModuleName=Timeline", "SaveSnapShotAsReport.action.dialogs.title=Timeline",
        "SaveSnapShotAsReport.action.name.text=Snapshot Report",
        "SaveSnapShotAsReport.action.longText=Save a screen capture of the current view of the timeline as a report.",
        "# {0} - report file path", "SaveSnapShotAsReport.ReportSavedAt=Report saved at [{0}]",
        "SaveSnapShotAsReport.Success=Success",
        "SaveSnapShotAsReport.FailedToAddReport=Failed to add snaphot to case as a report.",
        "# {0} - report path", "SaveSnapShotAsReport.ErrorWritingReport=Error writing report to disk at {0}.",
        "# {0} - generated default report name",
        "SaveSnapShotAsReport.reportName.prompt=leave empty for default report name: {0}.",
        "SaveSnapShotAsReport.reportName.header=Enter a report name for the Timeline Snapshot Report.",
        "SaveSnapShotAsReport.duplicateReportNameError.text=A report with that name already exists." })
public SaveSnapshotAsReport(TimeLineController controller, Supplier<Node> nodeSupplier) {
    super(Bundle.SaveSnapShotAsReport_action_name_text());
    setLongText(Bundle.SaveSnapShotAsReport_action_longText());
    setGraphic(new ImageView(SNAP_SHOT));

    this.controller = controller;
    this.currentCase = controller.getAutopsyCase();

    setEventHandler(actionEvent -> {
        //capture generation date and use to make default report name
        Date generationDate = new Date();
        final String defaultReportName = FileUtil.escapeFileName(currentCase.getName() + " "
                + new SimpleDateFormat("MM-dd-yyyy-HH-mm-ss").format(generationDate)); //NON_NLS
        BufferedImage snapshot = SwingFXUtils.fromFXImage(nodeSupplier.get().snapshot(null, null), null);

        //prompt user to pick report name
        TextInputDialog textInputDialog = new TextInputDialog();
        PromptDialogManager.setDialogIcons(textInputDialog);
        textInputDialog.setTitle(Bundle.SaveSnapShotAsReport_action_dialogs_title());
        textInputDialog.getEditor()
                .setPromptText(Bundle.SaveSnapShotAsReport_reportName_prompt(defaultReportName));
        textInputDialog.setHeaderText(Bundle.SaveSnapShotAsReport_reportName_header());

        //keep prompt even if text field has focus, until user starts typing.
        textInputDialog.getEditor()
                .setStyle("-fx-prompt-text-fill: derive(-fx-control-inner-background, -30%);");//NON_NLS 

        /*
         * Create a ValidationSupport to validate that a report with the
         * entered name doesn't exist on disk already. Disable ok button if
         * report name is not validated.
         */
        ValidationSupport validationSupport = new ValidationSupport();
        validationSupport.registerValidator(textInputDialog.getEditor(), false, new Validator<String>() {
            @Override
            public ValidationResult apply(Control textField, String enteredReportName) {
                String reportName = StringUtils.defaultIfBlank(enteredReportName, defaultReportName);
                boolean exists = Files.exists(Paths.get(currentCase.getReportDirectory(), reportName));
                return ValidationResult.fromErrorIf(textField,
                        Bundle.SaveSnapShotAsReport_duplicateReportNameError_text(), exists);
            }
        });
        textInputDialog.getDialogPane().lookupButton(ButtonType.OK).disableProperty()
                .bind(validationSupport.invalidProperty());

        //show dialog and handle result
        textInputDialog.showAndWait().ifPresent(enteredReportName -> {
            //reportName defaults to case name + timestamp if left blank
            String reportName = StringUtils.defaultIfBlank(enteredReportName, defaultReportName);
            Path reportFolderPath = Paths.get(currentCase.getReportDirectory(), reportName,
                    "Timeline Snapshot"); //NON_NLS
            Path reportMainFilePath;

            try {
                //generate and write report
                reportMainFilePath = new SnapShotReportWriter(currentCase, reportFolderPath, reportName,
                        controller.getEventsModel().getZoomParamaters(), generationDate, snapshot)
                                .writeReport();
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, "Error writing report to disk at " + reportFolderPath, ex); //NON_NLS
                new Alert(Alert.AlertType.ERROR,
                        Bundle.SaveSnapShotAsReport_ErrorWritingReport(reportFolderPath)).show();
                return;
            }

            try {
                //add main file as report to case
                Case.getCurrentCase().addReport(reportMainFilePath.toString(), Bundle.Timeline_ModuleName(),
                        reportName);
            } catch (TskCoreException ex) {
                LOGGER.log(Level.WARNING,
                        "Failed to add " + reportMainFilePath.toString() + " to case as a report", ex); //NON_NLS
                new Alert(Alert.AlertType.ERROR, Bundle.SaveSnapShotAsReport_FailedToAddReport()).show();
                return;
            }

            //notify user of report location
            final Alert alert = new Alert(Alert.AlertType.INFORMATION, null, OPEN, OK);
            alert.setTitle(Bundle.SaveSnapShotAsReport_action_dialogs_title());
            alert.setHeaderText(Bundle.SaveSnapShotAsReport_Success());

            //make action to open report, and hyperlinklable to invoke action
            final OpenReportAction openReportAction = new OpenReportAction(reportMainFilePath);
            HyperlinkLabel hyperlinkLabel = new HyperlinkLabel(
                    Bundle.SaveSnapShotAsReport_ReportSavedAt(reportMainFilePath.toString()));
            hyperlinkLabel.setOnAction(openReportAction);
            alert.getDialogPane().setContent(hyperlinkLabel);

            alert.showAndWait().filter(OPEN::equals).ifPresent(buttonType -> openReportAction.handle(null));
        });
    });
}