なんとなくダイアログボックス
ダイアログボックスって出すの面倒だよなー、とか思って。
public enum DialogBox { MESSAGE("メッセージ", ButtonType.DEFAULT) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), PLAIN_MESSAGE); return new DialogResult(result); } }, INFORMATION("情報", ButtonType.DEFAULT) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), INFORMATION_MESSAGE); return new DialogResult(result); } }, QUESTION("確認", ButtonType.YES_NO) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), QUESTION_MESSAGE); return new DialogResult(result); } }, WARNING("警告", ButtonType.DEFAULT) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), WARNING_MESSAGE); return new DialogResult(result); } }, ERROR("エラー", ButtonType.DEFAULT) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), ERROR_MESSAGE); return new DialogResult(result); } }, FATAL("致命的なエラー", ButtonType.DEFAULT) { public DialogResult show(Component p, Object msg, String ttl, ButtonType t) { int result = showConfirmDialog(p, msg, ttl, t.toInt(), ERROR_MESSAGE); return new DialogResult(result); } }; private final String DEFAULT_TITLE; private final ButtonType DEFAULT_BTN_TYPE; private DialogBox(String defaultTitle, ButtonType defaultBtnType) { DEFAULT_TITLE = defaultTitle; DEFAULT_BTN_TYPE = defaultBtnType; } public static enum ButtonType { DEFAULT(DEFAULT_OPTION), YES_NO(YES_NO_OPTION), YES_NO_CANCEL(YES_NO_CANCEL_OPTION), OK_CANCEL(OK_CANCEL_OPTION); private final int type; private ButtonType(int type) { this.type = type; } public int toInt() { return type; } } public abstract DialogResult show( Component parent, Object message, String title, ButtonType type); public DialogResult show(Component parent, Object message, ButtonType type) { return show(parent, message, DEFAULT_TITLE, type); } public DialogResult show(Component parent, Object message, String title) { return show(parent, message, title, DEFAULT_BTN_TYPE); } public DialogResult show(Component parent, Object message) { return show(parent, message, DEFAULT_TITLE, DEFAULT_BTN_TYPE); } public DialogResult showGlobal(Object message, String title, ButtonType type) { return show(null, message, title, type); } public DialogResult showGlobal(Object message, ButtonType type) { return show(null, message, type); } public DialogResult showGlobal(Object message, String title) { return show(null, message, title); } public DialogResult showGlobal(Object message) { return show(null, message); } }
DialogResultはまぁ、適当な感じで。enumでもいいけど上のコードではclassにしている。いや、やっぱりenumのほうが良かったかも。
使い方はこんな感じ。
DialogBox.MESSAGE.showGlobal("Hoge, world!");
DialogBox自体をenumにせずに、classにして、show/showGlobalメソッドで戻り値voidでgetDialogResultメソッドで結果取ってくるとかの方がいいかも。
ま、Swingにあわせるという意味ではこれでもいいか。
とりあえず、凝ったダイアログボックスを出したいとかじゃなければこんなんでも使える、はず。