Better JOptionPane

This commit is contained in:
Konloch 2021-07-04 20:34:37 -07:00
parent f2ada1aae1
commit d8df05b202
2 changed files with 123 additions and 1 deletions

View file

@ -32,6 +32,7 @@ import the.bytecode.club.bytecodeviewer.plugin.PluginManager;
import the.bytecode.club.bytecodeviewer.util.*;
import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
import static the.bytecode.club.bytecodeviewer.Constants.*;
import static the.bytecode.club.bytecodeviewer.Settings.addRecentPlugin;
import static the.bytecode.club.bytecodeviewer.util.MiscUtils.guessLanguage;
@ -524,7 +525,7 @@ public class BytecodeViewer
* @param message the message you need to send
*/
public static void showMessage(String message) {
JOptionPane.showMessageDialog(viewer, message);
BetterJOptionPane.showMessageDialog(viewer, message);
}
/**

View file

@ -0,0 +1,121 @@
package the.bytecode.club.bytecodeviewer.gui.components;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Method;
import static javax.swing.JOptionPane.*;
/**
* All this does is fix the bug with parentComponents being minimized.
* The bug is the JOptionPane location ends up 0,0 instead of centered.
* The fix is to center the frame manually before showing.
*
* @author Konloch
* @author James Gosling
* @author Scott Violet
* @since 7/4/2021
*/
public class BetterJOptionPane
{
public static void showMessageDialog(Component parentComponent,
Object message) throws HeadlessException
{
showMessageDialog(parentComponent, message, UIManager.getString(
"OptionPane.messageDialogTitle", parentComponent.getLocale()),
INFORMATION_MESSAGE);
}
public static void showMessageDialog(Component parentComponent,
Object message, String title, int messageType)
throws HeadlessException
{
showMessageDialog(parentComponent, message, title, messageType, null);
}
public static void showMessageDialog(Component parentComponent,
Object message, String title, int messageType, Icon icon)
throws HeadlessException
{
showOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
messageType, icon, null, null);
}
public static int showOptionDialog(Component parentComponent,
Object message, String title, int optionType, int messageType,
Icon icon, Object[] options, Object initialValue)
throws HeadlessException
{
JOptionPane pane = new JOptionPane(message, messageType,
optionType, icon,
options, initialValue);
pane.setInitialValue(initialValue);
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 selectedValue = pane.getValue();
if(selectedValue == null)
return CLOSED_OPTION;
if(options == null)
{
if(selectedValue instanceof Integer)
return (Integer) selectedValue;
return CLOSED_OPTION;
}
for(int counter = 0, maxCounter = options.length;
counter < maxCounter; counter++)
{
if(options[counter].equals(selectedValue))
return counter;
}
return CLOSED_OPTION;
}
private static int styleFromMessageType(int messageType)
{
switch (messageType)
{
case ERROR_MESSAGE:
return JRootPane.ERROR_DIALOG;
case QUESTION_MESSAGE:
return JRootPane.QUESTION_DIALOG;
case WARNING_MESSAGE:
return JRootPane.WARNING_DIALOG;
case INFORMATION_MESSAGE:
return JRootPane.INFORMATION_DIALOG;
case PLAIN_MESSAGE:
default:
return JRootPane.PLAIN_DIALOG;
}
}
}