From 33ec3f3fdbffee1b8f68e582d1d595b5a2744427 Mon Sep 17 00:00:00 2001 From: Konloch Date: Tue, 13 Jul 2021 03:36:39 -0700 Subject: [PATCH] Extended BetterJOptionPane This adds the showInputDialogue functions as well as the fixes for them --- .../gui/components/BetterJOptionPane.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/BetterJOptionPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/BetterJOptionPane.java index aa9e7065..4d9db63d 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/BetterJOptionPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/BetterJOptionPane.java @@ -41,6 +41,36 @@ public class BetterJOptionPane messageType, icon, null, null); } + public static String showInputDialog(Object message) + throws HeadlessException { + return showInputDialog(null, message); + } + + public static String showInputDialog(Object message, Object initialSelectionValue) { + return showInputDialog(null, message, initialSelectionValue); + } + + public static String showInputDialog(Component parentComponent, + Object message) throws HeadlessException { + return showInputDialog(parentComponent, message, UIManager.getString( + "OptionPane.inputDialogTitle", parentComponent.getLocale()), QUESTION_MESSAGE); + } + + public static String showInputDialog(Component parentComponent, Object message, + Object initialSelectionValue) { + return (String)showInputDialog(parentComponent, message, + UIManager.getString("OptionPane.inputDialogTitle", + parentComponent.getLocale()), QUESTION_MESSAGE, null, null, + initialSelectionValue); + } + + public static String showInputDialog(Component parentComponent, + Object message, String title, int messageType) + throws HeadlessException { + return (String)showInputDialog(parentComponent, message, title, + messageType, null, null, null); + } + public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) @@ -101,6 +131,52 @@ public class BetterJOptionPane return CLOSED_OPTION; } + public static Object showInputDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon, + Object[] selectionValues, Object initialSelectionValue) + throws HeadlessException { + JOptionPane pane = new JOptionPane(message, messageType, + OK_CANCEL_OPTION, icon, + null, null); + + pane.setWantsInput(true); + pane.setSelectionValues(selectionValues); + pane.setInitialSelectionValue(initialSelectionValue); + pane.setComponentOrientation(((parentComponent == null) ? + getRootFrame() : parentComponent).getComponentOrientation()); + + int style = styleFromMessageType(messageType); + //reflection to cheat our way around the + // private createDialog(Component parentComponent, String title, int style) + JDialog dialog = null; + try + { + Method createDialog = pane.getClass().getDeclaredMethod("createDialog", Component.class, String.class, int.class); + createDialog.setAccessible(true); + dialog = (JDialog) createDialog.invoke(pane, parentComponent, title, style); + } + catch(Exception e) + { + e.printStackTrace(); + } + + pane.selectInitialValue(); + + //check if the dialogue is in a poor location, attempt to correct + if(dialog.getLocation().getY() == 0) + dialog.setLocationRelativeTo(null); + + dialog.show(); + dialog.dispose(); + + Object value = pane.getInputValue(); + + if (value == UNINITIALIZED_VALUE) { + return null; + } + return value; + } + private static int styleFromMessageType(int messageType) { switch (messageType)