From 12098b3de3f7a7858cdf50bbcc51a2afc7568e76 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 8 Nov 2022 17:47:14 +0000 Subject: [PATCH] JAL-1988 JAL-3776 Formatting JvOptionPane --- src/jalview/gui/JvOptionPane.java | 713 +++++++++++++++++++++++++------------ 1 file changed, 479 insertions(+), 234 deletions(-) diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index d805164..364d4c1 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -60,7 +60,9 @@ import javax.swing.event.InternalFrameListener; import jalview.util.Platform; import jalview.util.dialogrunner.DialogRunnerI; -public class JvOptionPane extends JOptionPane implements DialogRunnerI, PropertyChangeListener { +public class JvOptionPane extends JOptionPane + implements DialogRunnerI, PropertyChangeListener +{ private static final long serialVersionUID = -3019167117756785229L; private static Object mockResponse = JvOptionPane.CANCEL_OPTION; @@ -77,13 +79,18 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property */ List ourOptions; - public JvOptionPane(final Component parent) { + public JvOptionPane(final Component parent) + { this.parentComponent = Platform.isJS() ? this : parent; } - public static int showConfirmDialog(Component parentComponent, Object message) throws HeadlessException { + public static int showConfirmDialog(Component parentComponent, + Object message) throws HeadlessException + { // only called by test - return isInteractiveMode() ? JOptionPane.showConfirmDialog(parentComponent, message) : (int) getMockResponse(); + return isInteractiveMode() + ? JOptionPane.showConfirmDialog(parentComponent, message) + : (int) getMockResponse(); } /** @@ -96,12 +103,16 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) - throws HeadlessException { - if (!isInteractiveMode()) { + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType) + throws HeadlessException + { + if (!isInteractiveMode()) + { return (int) getMockResponse(); } - switch (optionType) { + switch (optionType) + { case JvOptionPane.YES_NO_CANCEL_OPTION: // FeatureRenderer amendFeatures ?? TODO ?? // Chimera close @@ -114,7 +125,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property // $FALL-THROUGH$ case JvOptionPane.OK_CANCEL_OPTION: // will fall back to simple HTML - return JOptionPane.showConfirmDialog(parentComponent, message, title, optionType); + return JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType); } } @@ -129,12 +141,16 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, - int messageType) throws HeadlessException { + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType) + throws HeadlessException + { // JalviewServicesChanged // PromptUserConfig raiseDialog - return isInteractiveMode() ? JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType) - : (int) getMockResponse(); + return isInteractiveMode() + ? JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType) + : (int) getMockResponse(); } /** @@ -149,12 +165,15 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, - int messageType, Icon icon) throws HeadlessException { + public static int showConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType, + Icon icon) throws HeadlessException + { // JvOptionPaneTest only return isInteractiveMode() - ? JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon) - : (int) getMockResponse(); + ? JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType, icon) + : (int) getMockResponse(); } /** @@ -164,10 +183,14 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param message * @return */ - public static int showInternalConfirmDialog(Component parentComponent, Object message) { + public static int showInternalConfirmDialog(Component parentComponent, + Object message) + { // JvOptionPaneTest only; - return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog(parentComponent, message) - : (int) getMockResponse(); + return isInteractiveMode() + ? JOptionPane.showInternalConfirmDialog(parentComponent, + message) + : (int) getMockResponse(); } /** @@ -179,11 +202,15 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param optionType * @return */ - public static int showInternalConfirmDialog(Component parentComponent, String message, String title, int optionType) { - if (!isInteractiveMode()) { + public static int showInternalConfirmDialog(Component parentComponent, + String message, String title, int optionType) + { + if (!isInteractiveMode()) + { return (int) getMockResponse(); } - switch (optionType) { + switch (optionType) + { case JvOptionPane.YES_NO_CANCEL_OPTION: // ColourMenuHelper.addMenuItmers.offerRemoval TODO case JvOptionPane.YES_NO_OPTION: @@ -196,7 +223,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property // Desktop.inputURLMenuItem // WsPreferenses - return JOptionPane.showConfirmDialog(parentComponent, message, title, optionType); + return JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType); } } @@ -209,12 +237,15 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param messageType * @return */ - public static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, - int messageType) { - if (!isInteractiveMode()) { + public static int showInternalConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType) + { + if (!isInteractiveMode()) + { return (int) getMockResponse(); } - switch (optionType) { + switch (optionType) + { case JvOptionPane.YES_NO_CANCEL_OPTION: case JvOptionPane.YES_NO_OPTION: // UserQuestionanaireCheck @@ -223,7 +254,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property default: case JvOptionPane.OK_CANCEL_OPTION: // will fall back to simple HTML - return JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType); + return JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType); } } @@ -238,19 +270,24 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param icon * @return */ - public static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, - int messageType, Icon icon) { - if (!isInteractiveMode()) { + public static int showInternalConfirmDialog(Component parentComponent, + Object message, String title, int optionType, int messageType, + Icon icon) + { + if (!isInteractiveMode()) + { return (int) getMockResponse(); } - switch (optionType) { + switch (optionType) + { case JvOptionPane.YES_NO_CANCEL_OPTION: case JvOptionPane.YES_NO_OPTION: //$FALL-THROUGH$ default: case JvOptionPane.OK_CANCEL_OPTION: // Preferences editLink/newLink - return JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon); + return JOptionPane.showConfirmDialog(parentComponent, message, title, + optionType, messageType, icon); } } @@ -269,9 +306,13 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static int showOptionDialog(Component parentComponent, String message, String title, int optionType, - int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException { - if (!isInteractiveMode()) { + public static int showOptionDialog(Component parentComponent, + String message, String title, int optionType, int messageType, + Icon icon, Object[] options, Object initialValue) + throws HeadlessException + { + if (!isInteractiveMode()) + { return (int) getMockResponse(); } // two uses: @@ -288,8 +329,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property // // 2) UserDefinedColors warning about saving over a name already defined // - return JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, - initialValue); + return JOptionPane.showOptionDialog(parentComponent, message, title, + optionType, messageType, icon, options, initialValue); } /** @@ -298,8 +339,11 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param message * @throws HeadlessException */ - public static void showMessageDialog(Component parentComponent, String message) throws HeadlessException { - if (!isInteractiveMode()) { + public static void showMessageDialog(Component parentComponent, + String message) throws HeadlessException + { + if (!isInteractiveMode()) + { outputMessage(message); return; } @@ -318,16 +362,20 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param messageType * @throws HeadlessException */ - public static void showMessageDialog(Component parentComponent, String message, String title, int messageType) - throws HeadlessException { + public static void showMessageDialog(Component parentComponent, + String message, String title, int messageType) + throws HeadlessException + { // 30 implementations -- all just fine. - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { outputMessage(message); return; } - JOptionPane.showMessageDialog(parentComponent, getPrefix(messageType) + message, title, messageType); + JOptionPane.showMessageDialog(parentComponent, + getPrefix(messageType) + message, title, messageType); } /** @@ -340,28 +388,35 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param icon * @throws HeadlessException */ - public static void showMessageDialog(Component parentComponent, String message, String title, int messageType, - Icon icon) throws HeadlessException { + public static void showMessageDialog(Component parentComponent, + String message, String title, int messageType, Icon icon) + throws HeadlessException + { // test only - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { outputMessage(message); return; } - JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon); + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType, icon); } /** * was internal * */ - public static void showInternalMessageDialog(Component parentComponent, Object message) { + public static void showInternalMessageDialog(Component parentComponent, + Object message) + { // WsPreferences only - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { outputMessage(message); return; } @@ -377,17 +432,20 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param title * @param messageType */ - public static void showInternalMessageDialog(Component parentComponent, String message, String title, - int messageType) { + public static void showInternalMessageDialog(Component parentComponent, + String message, String title, int messageType) + { // 41 references - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { outputMessage(message); return; } - JOptionPane.showMessageDialog(parentComponent, getPrefix(messageType) + message, title, messageType); + JOptionPane.showMessageDialog(parentComponent, + getPrefix(messageType) + message, title, messageType); } /** @@ -398,17 +456,20 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param messageType * @param icon */ - public static void showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, - Icon icon) { + public static void showInternalMessageDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon) + { // test only - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { outputMessage(message); return; } - JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon); + JOptionPane.showMessageDialog(parentComponent, message, title, + messageType, icon); } /** @@ -417,10 +478,13 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static String showInputDialog(Object message) throws HeadlessException { + public static String showInputDialog(Object message) + throws HeadlessException + { // test only - if (!isInteractiveMode()) { + if (!isInteractiveMode()) + { return getMockResponse().toString(); } @@ -434,8 +498,11 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param initialSelectionValue * @return */ - public static String showInputDialog(String message, String initialSelectionValue) { - if (!isInteractiveMode()) { + public static String showInputDialog(String message, + String initialSelectionValue) + { + if (!isInteractiveMode()) + { return getMockResponse().toString(); } @@ -451,8 +518,11 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param initialSelectionValue * @return */ - public static String showInputDialog(Object message, Object initialSelectionValue) { - if (!isInteractiveMode()) { + public static String showInputDialog(Object message, + Object initialSelectionValue) + { + if (!isInteractiveMode()) + { return getMockResponse().toString(); } @@ -469,10 +539,14 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static String showInputDialog(Component parentComponent, String message) throws HeadlessException { + public static String showInputDialog(Component parentComponent, + String message) throws HeadlessException + { // test only - return isInteractiveMode() ? JOptionPane.showInputDialog(parentComponent, message) : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInputDialog(parentComponent, message) + : getMockResponse().toString(); } /** @@ -483,12 +557,16 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param initialSelectionValue * @return */ - public static String showInputDialog(Component parentComponent, String message, String initialSelectionValue) { + public static String showInputDialog(Component parentComponent, + String message, String initialSelectionValue) + { // AnnotationPanel - return isInteractiveMode() ? JOptionPane.showInputDialog(parentComponent, message, initialSelectionValue) - : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInputDialog(parentComponent, message, + initialSelectionValue) + : getMockResponse().toString(); } /** @@ -499,12 +577,16 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param initialSelectionValue * @return */ - public static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) { + public static String showInputDialog(Component parentComponent, + Object message, Object initialSelectionValue) + { // AnnotationPanel - return isInteractiveMode() ? JOptionPane.showInputDialog(parentComponent, message, initialSelectionValue) - : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInputDialog(parentComponent, message, + initialSelectionValue) + : getMockResponse().toString(); } /** @@ -516,13 +598,17 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static String showInputDialog(Component parentComponent, String message, String title, int messageType) - throws HeadlessException { + public static String showInputDialog(Component parentComponent, + String message, String title, int messageType) + throws HeadlessException + { // test only - return isInteractiveMode() ? JOptionPane.showInputDialog(parentComponent, message, title, messageType) - : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInputDialog(parentComponent, message, title, + messageType) + : getMockResponse().toString(); } /** @@ -538,15 +624,19 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @return * @throws HeadlessException */ - public static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, - Icon icon, Object[] selectionValues, Object initialSelectionValue) throws HeadlessException { + public static Object showInputDialog(Component parentComponent, + Object message, String title, int messageType, Icon icon, + Object[] selectionValues, Object initialSelectionValue) + throws HeadlessException + { // test only return isInteractiveMode() - ? JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, - initialSelectionValue) - : getMockResponse().toString(); + ? JOptionPane.showInputDialog(parentComponent, message, title, + messageType, icon, selectionValues, + initialSelectionValue) + : getMockResponse().toString(); } /** @@ -556,11 +646,14 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param message * @return */ - public static String showInternalInputDialog(Component parentComponent, String message) { + public static String showInternalInputDialog(Component parentComponent, + String message) + { // test only - return isInteractiveMode() ? JOptionPane.showInternalInputDialog(parentComponent, message) - : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInternalInputDialog(parentComponent, message) + : getMockResponse().toString(); } /** @@ -572,14 +665,16 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param messageType * @return */ - public static String showInternalInputDialog(Component parentComponent, String message, String title, - int messageType) { + public static String showInternalInputDialog(Component parentComponent, + String message, String title, int messageType) + { // AlignFrame tabbedPane_mousePressed return isInteractiveMode() - ? JOptionPane.showInternalInputDialog(parentComponent, getPrefix(messageType) + message, title, messageType) - : getMockResponse().toString(); + ? JOptionPane.showInternalInputDialog(parentComponent, + getPrefix(messageType) + message, title, messageType) + : getMockResponse().toString(); } /** @@ -594,47 +689,61 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param initialSelectionValue * @return */ - public static Object showInternalInputDialog(Component parentComponent, String message, String title, int messageType, - Icon icon, Object[] selectionValues, Object initialSelectionValue) { + public static Object showInternalInputDialog(Component parentComponent, + String message, String title, int messageType, Icon icon, + Object[] selectionValues, Object initialSelectionValue) + { // test only - return isInteractiveMode() ? JOptionPane.showInternalInputDialog(parentComponent, message, title, messageType, icon, - selectionValues, initialSelectionValue) : getMockResponse().toString(); + return isInteractiveMode() + ? JOptionPane.showInternalInputDialog(parentComponent, message, + title, messageType, icon, selectionValues, + initialSelectionValue) + : getMockResponse().toString(); } ///////////// end of options /////////////// - private static void outputMessage(Object message) { + private static void outputMessage(Object message) + { System.out.println(">>> JOption Message : " + message.toString()); } - public static Object getMockResponse() { + public static Object getMockResponse() + { return mockResponse; } - public static void setMockResponse(Object mockOption) { + public static void setMockResponse(Object mockOption) + { JvOptionPane.mockResponse = mockOption; } - public static void resetMock() { + public static void resetMock() + { setMockResponse(JvOptionPane.CANCEL_OPTION); setInteractiveMode(true); } - public static boolean isInteractiveMode() { + public static boolean isInteractiveMode() + { return interactiveMode; } - public static void setInteractiveMode(boolean interactive) { + public static void setInteractiveMode(boolean interactive) + { JvOptionPane.interactiveMode = interactive; } - private static String getPrefix(int messageType) { + private static String getPrefix(int messageType) + { String prefix = ""; // JavaScript only - if (Platform.isJS()) { - switch (messageType) { + if (Platform.isJS()) + { + switch (messageType) + { case JvOptionPane.WARNING_MESSAGE: prefix = "WARNING! "; break; @@ -662,27 +771,37 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * @param string2 * @return */ - public static JvOptionPane newOptionDialog() { + public static JvOptionPane newOptionDialog() + { return new JvOptionPane(null); } - public static JvOptionPane newOptionDialog(Component parentComponent) { + public static JvOptionPane newOptionDialog(Component parentComponent) + { return new JvOptionPane(parentComponent); } - public void showDialog(String message, String title, int optionType, int messageType, Icon icon, Object[] options, - Object initialValue) { - showDialog(message, title, optionType, messageType, icon, options, initialValue, true); + public void showDialog(String message, String title, int optionType, + int messageType, Icon icon, Object[] options, Object initialValue) + { + showDialog(message, title, optionType, messageType, icon, options, + initialValue, true); } - public void showDialog(Object message, String title, int optionType, int messageType, Icon icon, Object[] options, - Object initialValue, boolean modal) { - showDialog(message, title, optionType, messageType, icon, options, initialValue, modal, null); + public void showDialog(Object message, String title, int optionType, + int messageType, Icon icon, Object[] options, Object initialValue, + boolean modal) + { + showDialog(message, title, optionType, messageType, icon, options, + initialValue, modal, null); } - public void showDialog(Object message, String title, int optionType, int messageType, Icon icon, Object[] options, - Object initialValue, boolean modal, JButton[] buttons) { - if (!isInteractiveMode()) { + public void showDialog(Object message, String title, int optionType, + int messageType, Icon icon, Object[] options, Object initialValue, + boolean modal, JButton[] buttons) + { + if (!isInteractiveMode()) + { handleResponse(getMockResponse()); } // two uses: @@ -702,18 +821,25 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property ourOptions = Arrays.asList(options); - if (modal) { + if (modal) + { boolean useButtons = false; Object initialValueButton = null; - NOTNULL: if (buttons != null) { - if (buttons.length != options.length) { - jalview.bin.Console.error("Supplied buttons array not the same length as supplied options array."); + NOTNULL: if (buttons != null) + { + if (buttons.length != options.length) + { + jalview.bin.Console.error( + "Supplied buttons array not the same length as supplied options array."); break NOTNULL; } - int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION }; - for (int i = 0; i < options.length; i++) { + int[] buttonActions = { JOptionPane.YES_OPTION, + JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION }; + for (int i = 0; i < options.length; i++) + { Object o = options[i]; - jalview.bin.Console.debug("Setting button " + i + " to '" + o.toString() + "'"); + jalview.bin.Console.debug( + "Setting button " + i + " to '" + o.toString() + "'"); JButton jb = buttons[i]; if (o.equals(initialValue)) @@ -722,18 +848,28 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property int buttonAction = buttonActions[i]; Callable action = callbacks.get(buttonAction); jb.setText((String) o); - jb.addActionListener(new ActionListener() { + jb.addActionListener(new ActionListener() + { @Override - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent e) + { Object obj = e.getSource(); - if (obj == null || !(obj instanceof Component)) { - jalview.bin.Console.debug("Could not find Component source of event object " + obj); + if (obj == null || !(obj instanceof Component)) + { + jalview.bin.Console.debug( + "Could not find Component source of event object " + + obj); return; } - Object joptionpaneObject = SwingUtilities.getAncestorOfClass(JOptionPane.class, (Component) obj); - if (joptionpaneObject == null || !(joptionpaneObject instanceof JOptionPane)) { - jalview.bin.Console.debug("Could not find JOptionPane ancestor of event object " + obj); + Object joptionpaneObject = SwingUtilities.getAncestorOfClass( + JOptionPane.class, (Component) obj); + if (joptionpaneObject == null + || !(joptionpaneObject instanceof JOptionPane)) + { + jalview.bin.Console.debug( + "Could not find JOptionPane ancestor of event object " + + obj); return; } JOptionPane joptionpane = (JOptionPane) joptionpaneObject; @@ -747,16 +883,21 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property boolean raiseParent = (parentComponent != null); if (buttonAction == JOptionPane.CANCEL_OPTION) raiseParent = false; - if (optionType == JOptionPane.YES_NO_OPTION && buttonAction == JOptionPane.NO_OPTION) + if (optionType == JOptionPane.YES_NO_OPTION + && buttonAction == JOptionPane.NO_OPTION) raiseParent = false; - if (raiseParent) { + if (raiseParent) + { parentComponent.requestFocus(); - if (parentComponent instanceof JInternalFrame) { + if (parentComponent instanceof JInternalFrame) + { JInternalFrame jif = (JInternalFrame) parentComponent; jif.show(); jif.moveToFront(); jif.grabFocus(); - } else if (parentComponent instanceof Window) { + } + else if (parentComponent instanceof Window) + { Window w = (Window) parentComponent; w.toFront(); w.requestFocus(); @@ -770,8 +911,10 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property useButtons = true; } // use a JOptionPane as usual - int response = JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, - useButtons ? buttons : options, useButtons ? initialValueButton : initialValue); + int response = JOptionPane.showOptionDialog(parentComponent, message, + title, optionType, messageType, icon, + useButtons ? buttons : options, + useButtons ? initialValueButton : initialValue); /* * In Java, the response is returned to this thread and handled here; (for @@ -786,7 +929,9 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property { handleResponse(response); } - } else { + } + else + { /* * This is java similar to the swingjs handling, with the callbacks attached to * the button press of the dialog. This means we can use a non-modal JDialog for @@ -794,34 +939,46 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property */ JOptionPane joptionpane = new JOptionPane(); // Make button options - int[] buttonActions = { JvOptionPane.YES_OPTION, JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION }; + int[] buttonActions = { JvOptionPane.YES_OPTION, + JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION }; // we need the strings to make the buttons with actionEventListener - if (options == null) { + if (options == null) + { ArrayList options_default = new ArrayList<>(); - options_default.add(UIManager.getString("OptionPane.yesButtonText")); - if (optionType == JvOptionPane.YES_NO_OPTION || optionType == JvOptionPane.YES_NO_CANCEL_OPTION) { - options_default.add(UIManager.getString("OptionPane.noButtonText")); + options_default + .add(UIManager.getString("OptionPane.yesButtonText")); + if (optionType == JvOptionPane.YES_NO_OPTION + || optionType == JvOptionPane.YES_NO_CANCEL_OPTION) + { + options_default + .add(UIManager.getString("OptionPane.noButtonText")); } - if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION) { - options_default.add(UIManager.getString("OptionPane.cancelButtonText")); + if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION) + { + options_default + .add(UIManager.getString("OptionPane.cancelButtonText")); } options = options_default.toArray(); } ArrayList options_btns = new ArrayList<>(); Object initialValue_btn = null; - if (!Platform.isJS()) // JalviewJS already uses callback, don't need to add them here + if (!Platform.isJS()) // JalviewJS already uses callback, don't need to + // add them here { - for (int i = 0; i < options.length && i < 3; i++) { + for (int i = 0; i < options.length && i < 3; i++) + { Object o = options[i]; int buttonAction = buttonActions[i]; Callable action = callbacks.get(buttonAction); JButton jb = new JButton(); jb.setText((String) o); - jb.addActionListener(new ActionListener() { + jb.addActionListener(new ActionListener() + { @Override - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent e) + { joptionpane.setValue(buttonAction); if (action != null) Executors.newSingleThreadExecutor().submit(action); @@ -833,16 +990,21 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property boolean raiseParent = (parentComponent != null); if (buttonAction == JvOptionPane.CANCEL_OPTION) raiseParent = false; - if (optionType == JvOptionPane.YES_NO_OPTION && buttonAction == JvOptionPane.NO_OPTION) + if (optionType == JvOptionPane.YES_NO_OPTION + && buttonAction == JvOptionPane.NO_OPTION) raiseParent = false; - if (raiseParent) { + if (raiseParent) + { parentComponent.requestFocus(); - if (parentComponent instanceof JInternalFrame) { + if (parentComponent instanceof JInternalFrame) + { JInternalFrame jif = (JInternalFrame) parentComponent; jif.show(); jif.moveToFront(); jif.grabFocus(); - } else if (parentComponent instanceof Window) { + } + else if (parentComponent instanceof Window) + { Window w = (Window) parentComponent; w.toFront(); w.requestFocus(); @@ -860,20 +1022,26 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property joptionpane.setMessageType(messageType); joptionpane.setOptionType(optionType); joptionpane.setIcon(icon); - joptionpane.setOptions(Platform.isJS() ? options : options_btns.toArray()); - joptionpane.setInitialValue(Platform.isJS() ? initialValue : initialValue_btn); + joptionpane.setOptions( + Platform.isJS() ? options : options_btns.toArray()); + joptionpane.setInitialValue( + Platform.isJS() ? initialValue : initialValue_btn); JDialog dialog = joptionpane.createDialog(parentComponent, title); dialog.setIconImage(WindowIcons.logoIcon.getImage()); - dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); + dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL + : ModalityType.MODELESS); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } } - public void showInternalDialog(JPanel mainPanel, String title, int yesNoCancelOption, int questionMessage, Icon icon, - Object[] options, String initresponse) { - if (!isInteractiveMode()) { + public void showInternalDialog(JPanel mainPanel, String title, + int yesNoCancelOption, int questionMessage, Icon icon, + Object[] options, String initresponse) + { + if (!isInteractiveMode()) + { handleResponse(getMockResponse()); } @@ -887,43 +1055,54 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property ourOptions = Arrays.asList(options); int response; - if (parentComponent != this) { + if (parentComponent != this) + { JInternalFrame jif = this.createInternalFrame(parentComponent, title); jif.setFrameIcon(WindowIcons.logoIcon); - jif.addInternalFrameListener(new InternalFrameListener() { + jif.addInternalFrameListener(new InternalFrameListener() + { @Override - public void internalFrameActivated(InternalFrameEvent arg0) { + public void internalFrameActivated(InternalFrameEvent arg0) + { } @Override - public void internalFrameClosed(InternalFrameEvent arg0) { + public void internalFrameClosed(InternalFrameEvent arg0) + { JvOptionPane.this.internalDialogHandleResponse(); } @Override - public void internalFrameClosing(InternalFrameEvent arg0) { + public void internalFrameClosing(InternalFrameEvent arg0) + { } @Override - public void internalFrameDeactivated(InternalFrameEvent arg0) { + public void internalFrameDeactivated(InternalFrameEvent arg0) + { } @Override - public void internalFrameDeiconified(InternalFrameEvent arg0) { + public void internalFrameDeiconified(InternalFrameEvent arg0) + { } @Override - public void internalFrameIconified(InternalFrameEvent arg0) { + public void internalFrameIconified(InternalFrameEvent arg0) + { } @Override - public void internalFrameOpened(InternalFrameEvent arg0) { + public void internalFrameOpened(InternalFrameEvent arg0) + { } }); jif.setVisible(true); startModal(jif); return; - } else { + } + else + { JDialog dialog = this.createDialog(parentComponent, title); dialog.setIconImage(WindowIcons.logoIcon.getImage()); dialog.setVisible(true); // blocking @@ -932,7 +1111,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property } } - private void internalDialogHandleResponse() { + private void internalDialogHandleResponse() + { String responseString = (String) this.getValue(); int response = ourOptions.indexOf(responseString); @@ -955,7 +1135,9 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * } */ @Override - public JvOptionPane setResponseHandler(Object response, Callable action) { + public JvOptionPane setResponseHandler(Object response, + Callable action) + { callbacks.put(response, action); return this; } @@ -964,8 +1146,9 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * showDialogOnTop will create a dialog that (attempts to) come to top of OS * desktop windows */ - public static int showDialogOnTop(String label, String actionString, int JOPTIONPANE_OPTION, - int JOPTIONPANE_MESSAGETYPE) { + public static int showDialogOnTop(String label, String actionString, + int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE) + { // Ensure Jalview window is brought to front (primarily for Quit // confirmation window to be visible) @@ -979,8 +1162,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property dialogParent.setIconImage(WindowIcons.logoIcon.getImage()); dialogParent.setAlwaysOnTop(true); - int answer = JOptionPane.showConfirmDialog(dialogParent, label, actionString, JOPTIONPANE_OPTION, - JOPTIONPANE_MESSAGETYPE); + int answer = JOptionPane.showConfirmDialog(dialogParent, label, + actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE); dialogParent.setAlwaysOnTop(false); dialogParent.dispose(); @@ -988,22 +1171,31 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property return answer; } - public void showDialogOnTopAsync(String label, String actionString, int JOPTIONPANE_OPTION, - int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal) { + public void showDialogOnTopAsync(String label, String actionString, + int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon, + Object[] options, Object initialValue, boolean modal) + { JFrame frame = new JFrame(); frame.setIconImage(WindowIcons.logoIcon.getImage()); - showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, - initialValue, modal); + showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION, + JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal); } - public void showDialogOnTopAsync(JFrame dialogParent, Object label, String actionString, int JOPTIONPANE_OPTION, - int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal) { - showDialogOnTopAsync(dialogParent, label, actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, - initialValue, modal, null); + public void showDialogOnTopAsync(JFrame dialogParent, Object label, + String actionString, int JOPTIONPANE_OPTION, + int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, + Object initialValue, boolean modal) + { + showDialogOnTopAsync(dialogParent, label, actionString, + JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, + initialValue, modal, null); } - public void showDialogOnTopAsync(JFrame dialogParent, Object label, String actionString, int JOPTIONPANE_OPTION, - int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal, JButton[] buttons) { + public void showDialogOnTopAsync(JFrame dialogParent, Object label, + String actionString, int JOPTIONPANE_OPTION, + int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, + Object initialValue, boolean modal, JButton[] buttons) + { // Ensure Jalview window is brought to front (primarily for Quit // confirmation window to be visible) @@ -1016,45 +1208,55 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property dialogParent.setAlwaysOnTop(true); parentComponent = dialogParent; - showDialog(label, actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal, - buttons); + showDialog(label, actionString, JOPTIONPANE_OPTION, + JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal, + buttons); dialogParent.setAlwaysOnTop(false); dialogParent.dispose(); } /** - * JalviewJS signals option selection by a property change event for the option - * e.g. "OK". This methods responds to that by running the response action that - * corresponds to that option. + * JalviewJS signals option selection by a property change event for the + * option e.g. "OK". This methods responds to that by running the response + * action that corresponds to that option. * * @param evt */ @Override - public void propertyChange(PropertyChangeEvent evt) { + public void propertyChange(PropertyChangeEvent evt) + { Object newValue = evt.getNewValue(); int ourOption = ourOptions.indexOf(newValue); - if (ourOption >= 0) { + if (ourOption >= 0) + { handleResponse(ourOption); - } else { + } + else + { // try our luck.. handleResponse(newValue); } } @Override - public void handleResponse(Object response) { + public void handleResponse(Object response) + { /* * this test is for NaN in Chrome */ - if (response != null && !response.equals(response)) { + if (response != null && !response.equals(response)) + { return; } Callable action = callbacks.get(response); - if (action != null) { - try { + if (action != null) + { + try + { action.call(); - } catch (Exception e) { + } catch (Exception e) + { e.printStackTrace(); } if (parentComponent != null) @@ -1065,58 +1267,82 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property /** * Create a non-modal confirm dialog */ - public JDialog createDialog(Component parentComponent, Object message, String title, int optionType, int messageType, - Icon icon, Object[] options, Object initialValue, boolean modal) { - return createDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue, modal, - null); + public JDialog createDialog(Component parentComponent, Object message, + String title, int optionType, int messageType, Icon icon, + Object[] options, Object initialValue, boolean modal) + { + return createDialog(parentComponent, message, title, optionType, + messageType, icon, options, initialValue, modal, null); } - public JDialog createDialog(Component parentComponent, Object message, String title, int optionType, int messageType, - Icon icon, Object[] options, Object initialValue, boolean modal, JButton[] buttons) { + public JDialog createDialog(Component parentComponent, Object message, + String title, int optionType, int messageType, Icon icon, + Object[] options, Object initialValue, boolean modal, + JButton[] buttons) + { JButton[] optionsButtons = null; Object initialValueButton = null; JOptionPane joptionpane = new JOptionPane(); // Make button options - int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION }; + int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, + JOptionPane.CANCEL_OPTION }; // we need the strings to make the buttons with actionEventListener - if (options == null) { + if (options == null) + { ArrayList options_default = new ArrayList<>(); options_default.add(UIManager.getString("OptionPane.yesButtonText")); - if (optionType == JOptionPane.YES_NO_OPTION || optionType == JOptionPane.YES_NO_CANCEL_OPTION) { + if (optionType == JOptionPane.YES_NO_OPTION + || optionType == JOptionPane.YES_NO_CANCEL_OPTION) + { options_default.add(UIManager.getString("OptionPane.noButtonText")); } - if (optionType == JOptionPane.YES_NO_CANCEL_OPTION) { - options_default.add(UIManager.getString("OptionPane.cancelButtonText")); + if (optionType == JOptionPane.YES_NO_CANCEL_OPTION) + { + options_default + .add(UIManager.getString("OptionPane.cancelButtonText")); } options = options_default.toArray(); } if (!Platform.isJS()) // JalviewJS already uses callback, don't need to // add them here { - if (((optionType == JOptionPane.YES_OPTION || optionType == JOptionPane.NO_OPTION - || optionType == JOptionPane.CANCEL_OPTION || optionType == JOptionPane.OK_OPTION - || optionType == JOptionPane.DEFAULT_OPTION) && options.length < 1) - || ((optionType == JOptionPane.YES_NO_OPTION || optionType == JOptionPane.OK_CANCEL_OPTION) - && options.length < 2) - || (optionType == JOptionPane.YES_NO_CANCEL_OPTION && options.length < 3)) { - jalview.bin.Console.debug("JvOptionPane: not enough options for dialog type"); + if (((optionType == JOptionPane.YES_OPTION + || optionType == JOptionPane.NO_OPTION + || optionType == JOptionPane.CANCEL_OPTION + || optionType == JOptionPane.OK_OPTION + || optionType == JOptionPane.DEFAULT_OPTION) + && options.length < 1) + || ((optionType == JOptionPane.YES_NO_OPTION + || optionType == JOptionPane.OK_CANCEL_OPTION) + && options.length < 2) + || (optionType == JOptionPane.YES_NO_CANCEL_OPTION + && options.length < 3)) + { + jalview.bin.Console + .debug("JvOptionPane: not enough options for dialog type"); } optionsButtons = new JButton[options.length]; - for (int i = 0; i < options.length && i < 3; i++) { + for (int i = 0; i < options.length && i < 3; i++) + { Object o = options[i]; int buttonAction = buttonActions[i]; Callable action = callbacks.get(buttonAction); JButton jb; - if (buttons != null && buttons.length > i && buttons[i] != null) { + if (buttons != null && buttons.length > i && buttons[i] != null) + { jb = buttons[i]; - } else { + } + else + { jb = new JButton(); } jb.setText((String) o); - jb.addActionListener(new ActionListener() { + jb.addActionListener(new ActionListener() + { @Override - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent e) + { joptionpane.setValue(buttonAction); if (action != null) Executors.newSingleThreadExecutor().submit(action); @@ -1128,16 +1354,21 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property boolean raiseParent = (parentComponent != null); if (buttonAction == JOptionPane.CANCEL_OPTION) raiseParent = false; - if (optionType == JOptionPane.YES_NO_OPTION && buttonAction == JOptionPane.NO_OPTION) + if (optionType == JOptionPane.YES_NO_OPTION + && buttonAction == JOptionPane.NO_OPTION) raiseParent = false; - if (raiseParent) { + if (raiseParent) + { parentComponent.requestFocus(); - if (parentComponent instanceof JInternalFrame) { + if (parentComponent instanceof JInternalFrame) + { JInternalFrame jif = (JInternalFrame) parentComponent; jif.show(); jif.moveToFront(); jif.grabFocus(); - } else if (parentComponent instanceof Window) { + } + else if (parentComponent instanceof Window) + { Window w = (Window) parentComponent; w.toFront(); w.requestFocus(); @@ -1156,11 +1387,13 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property joptionpane.setOptionType(optionType); joptionpane.setIcon(icon); joptionpane.setOptions(Platform.isJS() ? options : optionsButtons); - joptionpane.setInitialValue(Platform.isJS() ? initialValue : initialValueButton); + joptionpane.setInitialValue( + Platform.isJS() ? initialValue : initialValueButton); JDialog dialog = joptionpane.createDialog(parentComponent, title); dialog.setIconImage(WindowIcons.logoIcon.getImage()); - dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); + dialog.setModalityType( + modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); return dialog; } @@ -1170,7 +1403,8 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * * returns true if button was found */ - public static boolean clickButton(JFrame frame, int buttonType) { + public static boolean clickButton(JFrame frame, int buttonType) + { return false; } @@ -1181,9 +1415,11 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property * the JInternalFrame and sizes it according to the JInternalFrame's preferred * size. * - * @param f The JInternalFrame to make modal. + * @param f + * The JInternalFrame to make modal. */ - private static void startModal(JInternalFrame f) { + private static void startModal(JInternalFrame f) + { // We need to add an additional glasspane-like component directly // below the frame, which intercepts all mouse events that are not // directed at the frame itself. @@ -1192,9 +1428,11 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f); lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue()); modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight()); - modalInterceptor.addMouseListener(new MouseAdapter() { + modalInterceptor.addMouseListener(new MouseAdapter() + { }); - modalInterceptor.addMouseMotionListener(new MouseMotionAdapter() { + modalInterceptor.addMouseMotionListener(new MouseMotionAdapter() + { }); lp.add(modalInterceptor); f.toFront(); @@ -1202,9 +1440,12 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property // We need to explicitly dispatch events when we are blocking the event // dispatch thread. EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); - try { - while (!f.isClosed()) { - if (EventQueue.isDispatchThread()) { + try + { + while (!f.isClosed()) + { + if (EventQueue.isDispatchThread()) + { // The getNextEventMethod() issues wait() when no // event is available, so we don't need do explicitly wait(). AWTEvent ev = queue.getNextEvent(); @@ -1219,14 +1460,18 @@ public class JvOptionPane extends JOptionPane implements DialogRunnerI, Property ((MenuComponent) ev.getSource()).dispatchEvent(ev); // Other events are ignored as per spec in // EventQueue.dispatchEvent - } else { + } + else + { // Give other threads a chance to become active. Thread.yield(); } } - } catch (InterruptedException ex) { + } catch (InterruptedException ex) + { // If we get interrupted, then leave the modal state. - } finally { + } finally + { // Clean up the modal interceptor. lp.remove(modalInterceptor); -- 1.7.10.2