|
| 1 | +package net.sf.jsignpdf.fx.preset; |
| 2 | + |
| 3 | +import static net.sf.jsignpdf.Constants.RES; |
| 4 | + |
| 5 | +import java.text.DateFormat; |
| 6 | +import java.text.MessageFormat; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import javafx.beans.property.ReadOnlyObjectWrapper; |
| 11 | +import javafx.beans.property.ReadOnlyStringWrapper; |
| 12 | +import javafx.geometry.Insets; |
| 13 | +import javafx.scene.control.Alert; |
| 14 | +import javafx.scene.control.ButtonType; |
| 15 | +import javafx.scene.control.Dialog; |
| 16 | +import javafx.scene.control.Label; |
| 17 | +import javafx.scene.control.TableCell; |
| 18 | +import javafx.scene.control.TableColumn; |
| 19 | +import javafx.scene.control.TableView; |
| 20 | +import javafx.scene.control.TextInputDialog; |
| 21 | +import javafx.scene.layout.HBox; |
| 22 | +import javafx.stage.Stage; |
| 23 | +import javafx.util.Callback; |
| 24 | +import net.sf.jsignpdf.BasicSignerOptions; |
| 25 | + |
| 26 | +/** |
| 27 | + * Dialog that lists all presets and offers per-row rename / overwrite / delete actions. |
| 28 | + */ |
| 29 | +public class ManagePresetsDialog extends Dialog<Void> { |
| 30 | + |
| 31 | + private final PresetManager manager; |
| 32 | + private final BasicSignerOptions options; |
| 33 | + |
| 34 | + public ManagePresetsDialog(PresetManager manager, BasicSignerOptions options, Stage owner) { |
| 35 | + this.manager = manager; |
| 36 | + this.options = options; |
| 37 | + |
| 38 | + setTitle(RES.get("jfx.gui.preset.manage.title")); |
| 39 | + setHeaderText(null); |
| 40 | + if (owner != null) { |
| 41 | + initOwner(owner); |
| 42 | + } |
| 43 | + |
| 44 | + TableView<Preset> table = buildTable(); |
| 45 | + table.setPrefSize(620, 320); |
| 46 | + table.setPlaceholder(new Label(RES.get("jfx.gui.preset.manage.empty"))); |
| 47 | + |
| 48 | + HBox content = new HBox(table); |
| 49 | + content.setPadding(new Insets(10)); |
| 50 | + getDialogPane().setContent(content); |
| 51 | + getDialogPane().getButtonTypes().add(ButtonType.CLOSE); |
| 52 | + ((javafx.scene.control.Button) getDialogPane().lookupButton(ButtonType.CLOSE)) |
| 53 | + .setText(RES.get("jfx.gui.preset.manage.button.close")); |
| 54 | + setResultConverter(bt -> null); |
| 55 | + } |
| 56 | + |
| 57 | + private TableView<Preset> buildTable() { |
| 58 | + TableView<Preset> table = new TableView<>(manager.getPresets()); |
| 59 | + |
| 60 | + TableColumn<Preset, String> nameCol = new TableColumn<>(RES.get("jfx.gui.preset.manage.column.name")); |
| 61 | + nameCol.setCellValueFactory(cd -> new ReadOnlyStringWrapper(cd.getValue().getDisplayName())); |
| 62 | + nameCol.setPrefWidth(220); |
| 63 | + |
| 64 | + TableColumn<Preset, Instant> createdCol = new TableColumn<>(RES.get("jfx.gui.preset.manage.column.created")); |
| 65 | + createdCol.setCellValueFactory(cd -> new ReadOnlyObjectWrapper<>(cd.getValue().getCreatedAt())); |
| 66 | + createdCol.setCellFactory(col -> new TableCell<>() { |
| 67 | + private final DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void updateItem(Instant item, boolean empty) { |
| 71 | + super.updateItem(item, empty); |
| 72 | + setText((empty || item == null) ? "" : fmt.format(java.util.Date.from(item))); |
| 73 | + } |
| 74 | + }); |
| 75 | + createdCol.setPrefWidth(150); |
| 76 | + |
| 77 | + TableColumn<Preset, Void> actionsCol = new TableColumn<>(RES.get("jfx.gui.preset.manage.column.actions")); |
| 78 | + actionsCol.setCellFactory(buildActionsCellFactory()); |
| 79 | + actionsCol.setPrefWidth(230); |
| 80 | + actionsCol.setSortable(false); |
| 81 | + actionsCol.setReorderable(false); |
| 82 | + |
| 83 | + table.getColumns().add(nameCol); |
| 84 | + table.getColumns().add(createdCol); |
| 85 | + table.getColumns().add(actionsCol); |
| 86 | + return table; |
| 87 | + } |
| 88 | + |
| 89 | + private Callback<TableColumn<Preset, Void>, TableCell<Preset, Void>> buildActionsCellFactory() { |
| 90 | + return column -> new TableCell<>() { |
| 91 | + private final javafx.scene.control.Button renameBtn = |
| 92 | + new javafx.scene.control.Button(RES.get("jfx.gui.preset.manage.button.rename")); |
| 93 | + private final javafx.scene.control.Button overwriteBtn = |
| 94 | + new javafx.scene.control.Button(RES.get("jfx.gui.preset.manage.button.overwrite")); |
| 95 | + private final javafx.scene.control.Button deleteBtn = |
| 96 | + new javafx.scene.control.Button(RES.get("jfx.gui.preset.manage.button.delete")); |
| 97 | + private final HBox box = new HBox(6, renameBtn, overwriteBtn, deleteBtn); |
| 98 | + |
| 99 | + { |
| 100 | + renameBtn.setOnAction(e -> onRename(getTableRow().getItem())); |
| 101 | + overwriteBtn.setOnAction(e -> onOverwrite(getTableRow().getItem())); |
| 102 | + deleteBtn.setOnAction(e -> onDelete(getTableRow().getItem())); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected void updateItem(Void item, boolean empty) { |
| 107 | + super.updateItem(item, empty); |
| 108 | + setGraphic((empty || getTableRow() == null || getTableRow().getItem() == null) ? null : box); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private void onRename(Preset preset) { |
| 114 | + if (preset == null) { |
| 115 | + return; |
| 116 | + } |
| 117 | + TextInputDialog dlg = new TextInputDialog(preset.getDisplayName()); |
| 118 | + dlg.setTitle(RES.get("jfx.gui.preset.dialog.rename.title")); |
| 119 | + dlg.setHeaderText(RES.get("jfx.gui.preset.dialog.rename.header")); |
| 120 | + dlg.setContentText(RES.get("jfx.gui.preset.dialog.rename.prompt")); |
| 121 | + dlg.initOwner(getDialogPane().getScene().getWindow()); |
| 122 | + |
| 123 | + while (true) { |
| 124 | + Optional<String> result = dlg.showAndWait(); |
| 125 | + if (result.isEmpty()) { |
| 126 | + return; |
| 127 | + } |
| 128 | + String name = result.get(); |
| 129 | + PresetValidation.Result validation = PresetValidation.validate(name, |
| 130 | + n -> manager.hasDisplayName(n, preset)); |
| 131 | + if (validation != PresetValidation.Result.OK) { |
| 132 | + showAlert(Alert.AlertType.ERROR, |
| 133 | + RES.get("jfx.gui.preset.dialog.rename.title"), |
| 134 | + validationMessage(validation)); |
| 135 | + dlg.getEditor().setText(PresetValidation.trim(name)); |
| 136 | + continue; |
| 137 | + } |
| 138 | + manager.rename(preset, PresetValidation.trim(name)); |
| 139 | + return; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void onOverwrite(Preset preset) { |
| 144 | + if (preset == null) { |
| 145 | + return; |
| 146 | + } |
| 147 | + Alert confirm = new Alert(Alert.AlertType.CONFIRMATION); |
| 148 | + confirm.setTitle(RES.get("jfx.gui.preset.dialog.overwrite.title")); |
| 149 | + confirm.setHeaderText(null); |
| 150 | + confirm.setContentText(MessageFormat.format( |
| 151 | + RES.get("jfx.gui.preset.dialog.overwrite.confirm"), preset.getDisplayName())); |
| 152 | + confirm.initOwner(getDialogPane().getScene().getWindow()); |
| 153 | + Optional<ButtonType> result = confirm.showAndWait(); |
| 154 | + if (result.isPresent() && result.get() == ButtonType.OK) { |
| 155 | + manager.overwrite(preset, options); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void onDelete(Preset preset) { |
| 160 | + if (preset == null) { |
| 161 | + return; |
| 162 | + } |
| 163 | + Alert confirm = new Alert(Alert.AlertType.CONFIRMATION); |
| 164 | + confirm.setTitle(RES.get("jfx.gui.preset.dialog.delete.title")); |
| 165 | + confirm.setHeaderText(null); |
| 166 | + confirm.setContentText(MessageFormat.format( |
| 167 | + RES.get("jfx.gui.preset.dialog.delete.confirm"), preset.getDisplayName())); |
| 168 | + confirm.initOwner(getDialogPane().getScene().getWindow()); |
| 169 | + Optional<ButtonType> result = confirm.showAndWait(); |
| 170 | + if (result.isPresent() && result.get() == ButtonType.OK) { |
| 171 | + manager.delete(preset); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private String validationMessage(PresetValidation.Result result) { |
| 176 | + switch (result) { |
| 177 | + case EMPTY: return RES.get("jfx.gui.preset.validation.empty"); |
| 178 | + case ILLEGAL_CHAR: return RES.get("jfx.gui.preset.validation.illegalChar"); |
| 179 | + case TOO_LONG: return RES.get("jfx.gui.preset.validation.tooLong"); |
| 180 | + case DUPLICATE: return RES.get("jfx.gui.preset.validation.duplicate"); |
| 181 | + default: return ""; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private void showAlert(Alert.AlertType type, String title, String message) { |
| 186 | + Alert alert = new Alert(type); |
| 187 | + alert.setTitle(title); |
| 188 | + alert.setHeaderText(null); |
| 189 | + alert.setContentText(message); |
| 190 | + alert.initOwner(getDialogPane().getScene().getWindow()); |
| 191 | + alert.showAndWait(); |
| 192 | + } |
| 193 | +} |
0 commit comments