Skip to content

Commit

Permalink
Refactor JOptionPane usage for unification to JavaFX API (#99)
Browse files Browse the repository at this point in the history
* replace JOptionPane usage in ExceptionPopup class with Alert

* replace JOptionPane usage in BackupManagerController class with Alert
  • Loading branch information
hizumiaoba authored Aug 23, 2024
1 parent b61c796 commit 4755952
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -165,9 +166,14 @@ private void onErrorFindingDir() {
log.trace("Got selected item: {}",
this.backupFolderListView.getSelectionModel().getSelectedItem());
log.trace("Got backup folder list: {}", this.backupFolderListView.getItems());
JOptionPane.showMessageDialog(null,
"バックアップフォルダが見つかりませんでした。選択されていないか、内部でエラーが発生している可能性があります。",
"エラー", JOptionPane.ERROR_MESSAGE);

Alert notFoundAlert = new Alert(Alert.AlertType.ERROR);
notFoundAlert.setTitle("エラー");
notFoundAlert.setHeaderText("バックアップフォルダが見つかりませんでした");
notFoundAlert.setContentText("バックアップフォルダが選択されていないか、内部でエラーが発生している可能性があります。"
+ "\n設定タブを確認し、正しく選択出来ている場合は開発者に報告してください。");
notFoundAlert.initModality(Modality.APPLICATION_MODAL);
notFoundAlert.showAndWait();
}

@FXML
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package io.github.hizumiaoba.mctimemachine.api;

import javax.swing.JOptionPane;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ExceptionPopup {

private static final ButtonType FORCE_EXIT = new ButtonType("強制終了");
private static final ButtonType CONTINUE_ACTION = new ButtonType("続行");
private final Throwable thrown;
private final String message;
private final String occurence;
private static final String messageSkeleton = "例外:%s が %s を処理中に発生しました。\n\n%s";
private static final String messageSkeleton = "例外:%s が %s を処理中に発生しました。";


public ExceptionPopup(Throwable thrown, String message, String occurence) {
Expand All @@ -19,28 +24,29 @@ public ExceptionPopup(Throwable thrown, String message, String occurence) {
}

public void pop() {
if (isContinuable()) {
log();
} else {
forceExit();
}
Platform.runLater(() -> {
if (isContinuable()) {
log();
} else {
forceExit();
}
});
}

private int show() {
return JOptionPane.showOptionDialog(
null,
String.format(messageSkeleton, thrown.getClass().getName(), occurence, message),
String.format("%s を処理中に例外が発生しました", occurence),
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
new String[] { "続行", "強制終了" },
"強制終了"
private ButtonType show() {
Alert alert = new Alert((AlertType.ERROR));
alert.setTitle(String.format("%s を処理中に例外が発生しました", occurence));
alert.setHeaderText(String.format(messageSkeleton, thrown.getClass().getName(), occurence));
alert.setContentText(message);
alert.getButtonTypes().setAll(
CONTINUE_ACTION,
FORCE_EXIT
);
return alert.showAndWait().orElse(FORCE_EXIT);
}

private boolean isContinuable() {
return show() == JOptionPane.YES_OPTION;
return show().getText().equals("続行");
}

private void forceExit() {
Expand Down

0 comments on commit 4755952

Please sign in to comment.