Skip to content

Commit

Permalink
improve alert box message and add ability to refresh listview (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
hizumiaoba authored Oct 1, 2024
1 parent e12e7ed commit f6456c0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ void initialize() throws IOException {
}
}

private void updateListView() {
ObservableList<String> newItems = FXCollections.observableArrayList();
try {
newItems.addAll(MainController.backupUtils.getBackupDirPaths().parallelStream().map(p -> p.getFileName().toString()).toList());
} catch (IOException e) {
log.error("Failed to get backup directory names", e);
ExceptionPopup p = new ExceptionPopup(e, "バックアップフォルダ名の取得に失敗しました",
"BackupManagerController#updateListView");
p.pop();
} finally {
this.backupFolderListView.setItems(newItems);
}
}

@FXML
private void onDeleteButtonClicked() {
List<Path> dirList;
Expand All @@ -144,14 +158,28 @@ private void onDeleteButtonClicked() {
.getSelectedItem()))
.findFirst()
.ifPresentOrElse(d -> {
try {
MainController.backupUtils.deleteBackupRecursively(d);
} catch (IOException ex) {
log.error("Failed to delete backup", ex);
ExceptionPopup p = new ExceptionPopup(ex, "バックアップの削除に失敗しました",
"BackupManagerController#onDeleteButtonClicked$lambda-1");
p.pop();
}
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("バックアップの削除");
alert.setHeaderText("バックアップ:" + d.getFileName() + "を削除しますか?");
alert.setContentText("この操作は元に戻せません。");
alert.getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL);
alert.initModality(Modality.APPLICATION_MODAL);
alert.showAndWait().ifPresentOrElse(response -> {
if(response == ButtonType.OK) {
try {
MainController.backupUtils.deleteBackupRecursively(d);
} catch (IOException ex) {
log.error("Failed to delete backup", ex);
ExceptionPopup p = new ExceptionPopup(ex, "バックアップの削除に失敗しました",
"BackupManagerController#onDeleteButtonClicked$lambda-1");
p.pop();
} finally {
updateListView();
}
} else {
log.info("Cancelled deleting backup");
}
}, () -> log.warn("Neither OK nor CANCEL button clicked"));
}, this::onErrorFindingDir);
}

Expand Down Expand Up @@ -199,14 +227,28 @@ private void onCopyDirectoryButtonClicked() {
.getSelectedItem()))
.findFirst()
.ifPresentOrElse(d -> {
try {
MainController.backupUtils.duplicate(d);
} catch (IOException ex) {
log.error("Failed to copy backup", ex);
ExceptionPopup p = new ExceptionPopup(ex, "バックアップのコピーに失敗しました",
"BackupManagerController#onCopyDirectoryButtonClicked$lambda-1");
p.pop();
}
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("バックアップの複製");
alert.setHeaderText("バックアップ:" + d.getFileName() + "を複製しますか?");
alert.setContentText("新しいフォルダ名は%s_copyとなります。".formatted(d.getFileName()));
alert.getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL);
alert.initModality(Modality.APPLICATION_MODAL);
alert.showAndWait().ifPresentOrElse(response -> {
if(response == ButtonType.OK) {
try {
MainController.backupUtils.duplicate(d);
} catch (IOException ex) {
log.error("Failed to copy backup", ex);
ExceptionPopup p = new ExceptionPopup(ex, "バックアップのコピーに失敗しました",
"BackupManagerController#onCopyDirectoryButtonClicked$lambda-1");
p.pop();
} finally {
updateListView();
}
} else {
log.info("Cancelled copying backup");
}
}, () -> log.warn("Neither OK nor CANCEL button clicked"));
}, this::onErrorFindingDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void createBackupDir() {

public void duplicate(Path d) throws IOException {
log.info("Commencing duplication...");
Path targetDir = this.backupPath.resolve(String.format("Copy of %s", d.getFileName()));
Path targetDir = this.backupPath.resolve(String.format("%s_copy", d.getFileName()));
try (Stream<Path> s = Files.walk(d).parallel()) {
s.forEach(source -> {
Path target = targetDir.resolve(d.relativize(source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
</Label>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0" VBox.vgrow="ALWAYS">
<children>
<Button disable="true" mnemonicParsing="false" onAction="#onDeleteButtonClicked" text="削除" />
<Button disable="true" mnemonicParsing="false" onAction="#onCopyDirectoryButtonClicked" text="コピー" />
<Button mnemonicParsing="false" onAction="#onDeleteButtonClicked" text="削除" />
<Button mnemonicParsing="false" onAction="#onCopyDirectoryButtonClicked" text="コピー" />
<Button mnemonicParsing="false" onAction="#onRestoreWorldButtonClicked" text="復元" />
<Button mnemonicParsing="false" onAction="#onCloseButtonClicked" text="閉じる" />
</children>
Expand Down

0 comments on commit f6456c0

Please sign in to comment.