From 26cd0e575e86884dee2d8cafab1effa05c3842d3 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Fri, 4 Nov 2022 17:54:14 +0000 Subject: [PATCH] JAL-3416 Added logoIcon to quit (and potentially other) dialogs --- src/jalview/gui/JvOptionPane.java | 759 +++++++++++++------------------------ 1 file changed, 254 insertions(+), 505 deletions(-) diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index 752f25c..d805164 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -18,7 +18,6 @@ * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ - package jalview.gui; import java.awt.AWTEvent; @@ -61,9 +60,7 @@ 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; @@ -75,23 +72,18 @@ public class JvOptionPane extends JOptionPane private Map> callbacks = new HashMap<>(); /* - * JalviewJS reports user choice in the dialog as the selected - * option (text); this list allows conversion to index (int) + * JalviewJS reports user choice in the dialog as the selected option (text); + * this list allows conversion to index (int) */ 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(); } /** @@ -104,16 +96,12 @@ public class JvOptionPane extends JOptionPane * @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 @@ -126,8 +114,7 @@ public class JvOptionPane extends JOptionPane // $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); } } @@ -142,16 +129,12 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -166,15 +149,12 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -184,14 +164,10 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -203,15 +179,11 @@ public class JvOptionPane extends JOptionPane * @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: @@ -224,8 +196,7 @@ public class JvOptionPane extends JOptionPane // Desktop.inputURLMenuItem // WsPreferenses - return JOptionPane.showConfirmDialog(parentComponent, message, title, - optionType); + return JOptionPane.showConfirmDialog(parentComponent, message, title, optionType); } } @@ -238,15 +209,12 @@ public class JvOptionPane extends JOptionPane * @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 @@ -255,8 +223,7 @@ public class JvOptionPane extends JOptionPane 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); } } @@ -271,24 +238,19 @@ public class JvOptionPane extends JOptionPane * @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); } } @@ -307,13 +269,9 @@ public class JvOptionPane extends JOptionPane * @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: @@ -330,8 +288,8 @@ public class JvOptionPane extends JOptionPane // // 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); } /** @@ -340,11 +298,8 @@ public class JvOptionPane extends JOptionPane * @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; } @@ -363,20 +318,16 @@ public class JvOptionPane extends JOptionPane * @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); } /** @@ -389,35 +340,28 @@ public class JvOptionPane extends JOptionPane * @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; } @@ -433,20 +377,17 @@ public class JvOptionPane extends JOptionPane * @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); } /** @@ -457,20 +398,17 @@ public class JvOptionPane extends JOptionPane * @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); } /** @@ -479,13 +417,10 @@ public class JvOptionPane extends JOptionPane * @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(); } @@ -499,11 +434,8 @@ public class JvOptionPane extends JOptionPane * @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(); } @@ -519,11 +451,8 @@ public class JvOptionPane extends JOptionPane * @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(); } @@ -540,14 +469,10 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -558,16 +483,12 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -578,16 +499,12 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -599,17 +516,13 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -625,19 +538,15 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -647,14 +556,11 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -666,16 +572,14 @@ public class JvOptionPane extends JOptionPane * @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(); } /** @@ -690,61 +594,47 @@ public class JvOptionPane extends JOptionPane * @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; @@ -772,37 +662,27 @@ public class JvOptionPane extends JOptionPane * @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: @@ -822,25 +702,18 @@ public class JvOptionPane extends JOptionPane 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)) @@ -849,28 +722,18 @@ public class JvOptionPane extends JOptionPane 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; @@ -884,21 +747,16 @@ public class JvOptionPane extends JOptionPane 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(); @@ -912,14 +770,12 @@ public class JvOptionPane extends JOptionPane 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 Javascript, see propertyChange) + * In Java, the response is returned to this thread and handled here; (for + * Javascript, see propertyChange) */ if (!Platform.isJS()) /** @@ -930,35 +786,25 @@ public class JvOptionPane extends JOptionPane { 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 the confirmation without blocking the GUI. + * 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 + * the confirmation without blocking the GUI. */ 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(); } @@ -967,21 +813,18 @@ public class JvOptionPane extends JOptionPane Object initialValue_btn = null; 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]; - Runnable action = callbacks.get(buttonAction); + 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.defaultThreadFactory().newThread(action).start(); + Executors.newSingleThreadExecutor().submit(action); // joptionpane.transferFocusBackward(); joptionpane.transferFocusBackward(); joptionpane.setVisible(false); @@ -990,21 +833,16 @@ public class JvOptionPane extends JOptionPane 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(); @@ -1022,26 +860,20 @@ public class JvOptionPane extends JOptionPane 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()); } @@ -1055,54 +887,43 @@ public class JvOptionPane extends JOptionPane 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 @@ -1111,8 +932,7 @@ public class JvOptionPane extends JOptionPane } } - private void internalDialogHandleResponse() - { + private void internalDialogHandleResponse() { String responseString = (String) this.getValue(); int response = ourOptions.indexOf(responseString); @@ -1128,25 +948,14 @@ public class JvOptionPane extends JOptionPane } /* + * @Override public JvOptionPane setResponseHandler(Object response, Runnable + * action) { callbacks.put(response, new Callable() { + * + * @Override public Void call() { action.run(); return null; } }); return this; + * } + */ @Override - public JvOptionPane setResponseHandler(Object response, Runnable action) - { - callbacks.put(response, new Callable() - { - @Override - public Void call() - { - action.run(); - return null; - } - }); - return this; - } - */ - @Override - public JvOptionPane setResponseHandler(Object response, - Callable action) - { + public JvOptionPane setResponseHandler(Object response, Callable action) { callbacks.put(response, action); return this; } @@ -1155,9 +964,8 @@ public class JvOptionPane extends JOptionPane * 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) @@ -1168,10 +976,11 @@ public class JvOptionPane extends JOptionPane // A better hack which works is to create a new JFrame parent with // setAlwaysOnTop(true) JFrame dialogParent = new JFrame(); + 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(); @@ -1179,30 +988,22 @@ public class JvOptionPane extends JOptionPane return answer; } - public void showDialogOnTopAsync(String label, String actionString, - int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon, - Object[] options, Object initialValue, boolean modal) - { - showDialogOnTopAsync(new JFrame(), label, actionString, - JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, - initialValue, 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); } - 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) @@ -1215,55 +1016,45 @@ public class JvOptionPane extends JOptionPane 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)) - { + * this test is for NaN in Chrome + */ + 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) @@ -1274,82 +1065,58 @@ public class JvOptionPane extends JOptionPane /** * 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); @@ -1361,21 +1128,16 @@ public class JvOptionPane extends JOptionPane 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(); @@ -1394,12 +1156,11 @@ public class JvOptionPane extends JOptionPane 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.setModalityType( - modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); + dialog.setIconImage(WindowIcons.logoIcon.getImage()); + dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); return dialog; } @@ -1409,8 +1170,7 @@ public class JvOptionPane extends JOptionPane * * returns true if button was found */ - public static boolean clickButton(JFrame frame, int buttonType) - { + public static boolean clickButton(JFrame frame, int buttonType) { return false; } @@ -1421,11 +1181,9 @@ public class JvOptionPane extends JOptionPane * 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. @@ -1434,11 +1192,9 @@ public class JvOptionPane extends JOptionPane 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(); @@ -1446,12 +1202,9 @@ public class JvOptionPane extends JOptionPane // 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(); @@ -1466,18 +1219,14 @@ public class JvOptionPane extends JOptionPane ((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