2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.AWTEvent;
24 import java.awt.ActiveEvent;
25 import java.awt.Component;
26 import java.awt.Container;
27 import java.awt.Dialog.ModalityType;
28 import java.awt.EventQueue;
29 import java.awt.HeadlessException;
30 import java.awt.MenuComponent;
31 import java.awt.Toolkit;
32 import java.awt.Window;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseMotionAdapter;
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyChangeListener;
40 import java.beans.PropertyVetoException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.concurrent.ExecutorService;
47 import java.util.concurrent.Executors;
49 import javax.swing.Icon;
50 import javax.swing.JButton;
51 import javax.swing.JDialog;
52 import javax.swing.JFrame;
53 import javax.swing.JInternalFrame;
54 import javax.swing.JLayeredPane;
55 import javax.swing.JMenu;
56 import javax.swing.JMenuBar;
57 import javax.swing.JOptionPane;
58 import javax.swing.JPanel;
59 import javax.swing.JRootPane;
60 import javax.swing.SwingUtilities;
61 import javax.swing.UIManager;
62 import javax.swing.event.InternalFrameEvent;
63 import javax.swing.event.InternalFrameListener;
65 import jalview.bin.Console;
66 import jalview.util.ChannelProperties;
67 import jalview.util.MessageManager;
68 import jalview.util.Platform;
69 import jalview.util.dialogrunner.DialogRunnerI;
71 public class JvOptionPane extends JOptionPane
72 implements DialogRunnerI, PropertyChangeListener
74 private static final long serialVersionUID = -3019167117756785229L;
76 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
78 private static boolean interactiveMode = true;
80 public static final Runnable NULLCALLABLE = () -> {
83 private Component parentComponent;
85 private ExecutorService executor = Executors.newCachedThreadPool();
87 private JDialog dialog = null;
89 private Map<Object, Runnable> callbacks = new HashMap<>();
92 * JalviewJS reports user choice in the dialog as the selected option (text);
93 * this list allows conversion to index (int)
95 List<Object> ourOptions;
97 public JvOptionPane(final Component parent)
99 this.parentComponent = Platform.isJS() ? this : parent;
103 public static int showConfirmDialog(Component parentComponent,
104 Object message) throws HeadlessException
106 // only called by test
107 return isInteractiveMode()
108 ? JOptionPane.showConfirmDialog(parentComponent, message)
109 : (int) getMockResponse();
113 * Message, title, optionType
115 * @param parentComponent
120 * @throws HeadlessException
122 public static int showConfirmDialog(Component parentComponent,
123 Object message, String title, int optionType)
124 throws HeadlessException
126 if (!isInteractiveMode())
128 return (int) getMockResponse();
132 case JvOptionPane.YES_NO_CANCEL_OPTION:
133 // FeatureRenderer amendFeatures ?? TODO ??
138 case JvOptionPane.YES_NO_OPTION:
139 // PromptUserConfig usage stats
140 // for now treated as "OK CANCEL"
142 case JvOptionPane.OK_CANCEL_OPTION:
143 // will fall back to simple HTML
144 return JOptionPane.showConfirmDialog(parentComponent, message, title,
150 * Adds a message type. Fallback is to just add it in the beginning.
152 * @param parentComponent
158 * @throws HeadlessException
160 public static int showConfirmDialog(Component parentComponent,
161 Object message, String title, int optionType, int messageType)
162 throws HeadlessException
164 // JalviewServicesChanged
165 // PromptUserConfig raiseDialog
166 return isInteractiveMode()
167 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
168 optionType, messageType)
169 : (int) getMockResponse();
175 * @param parentComponent
182 * @throws HeadlessException
184 public static int showConfirmDialog(Component parentComponent,
185 Object message, String title, int optionType, int messageType,
186 Icon icon) throws HeadlessException
188 // JvOptionPaneTest only
189 return isInteractiveMode()
190 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
191 optionType, messageType, icon)
192 : (int) getMockResponse();
196 * Internal version "OK"
198 * @param parentComponent
202 public static int showInternalConfirmDialog(Component parentComponent,
205 // JvOptionPaneTest only;
206 return isInteractiveMode()
207 ? JOptionPane.showInternalConfirmDialog(parentComponent,
209 : (int) getMockResponse();
213 * Internal version -- changed to standard version for now
215 * @param parentComponent
221 public static int showInternalConfirmDialog(Component parentComponent,
222 String message, String title, int optionType)
224 if (!isInteractiveMode())
226 return (int) getMockResponse();
230 case JvOptionPane.YES_NO_CANCEL_OPTION:
231 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
232 case JvOptionPane.YES_NO_OPTION:
233 // UserDefinedColoursSave -- relevant? TODO
236 case JvOptionPane.OK_CANCEL_OPTION:
238 // EditNameDialog --- uses panel for messsage TODO
240 // Desktop.inputURLMenuItem
242 return JOptionPane.showConfirmDialog(parentComponent, message, title,
249 * @param parentComponent
256 public static int showInternalConfirmDialog(Component parentComponent,
257 Object message, String title, int optionType, int messageType)
259 if (!isInteractiveMode())
261 return (int) getMockResponse();
265 case JvOptionPane.YES_NO_CANCEL_OPTION:
266 case JvOptionPane.YES_NO_OPTION:
267 // UserQuestionanaireCheck
271 case JvOptionPane.OK_CANCEL_OPTION:
272 // will fall back to simple HTML
273 return JOptionPane.showConfirmDialog(parentComponent, message, title,
274 optionType, messageType);
279 * adds icon; no longer internal
281 * @param parentComponent
289 public static int showInternalConfirmDialog(Component parentComponent,
290 Object message, String title, int optionType, int messageType,
293 if (!isInteractiveMode())
295 return (int) getMockResponse();
299 case JvOptionPane.YES_NO_CANCEL_OPTION:
300 case JvOptionPane.YES_NO_OPTION:
303 case JvOptionPane.OK_CANCEL_OPTION:
304 // Preferences editLink/newLink
305 return JOptionPane.showConfirmDialog(parentComponent, message, title,
306 optionType, messageType, icon);
312 * custom options full-featured
314 * @param parentComponent
321 * @param initialValue
323 * @throws HeadlessException
325 public static int showOptionDialog(Component parentComponent,
326 String message, String title, int optionType, int messageType,
327 Icon icon, Object[] options, Object initialValue)
328 throws HeadlessException
330 if (!isInteractiveMode())
332 return (int) getMockResponse();
338 // 1) AlignViewport for openLinkedAlignment
340 // Show a dialog with the option to open and link (cDNA <-> protein) as a
342 // alignment, either as a standalone alignment or in a split frame. Returns
343 // true if the new alignment was opened, false if not, because the user
344 // declined the offer.
346 // 2) UserDefinedColors warning about saving over a name already defined
348 return JOptionPane.showOptionDialog(parentComponent, message, title,
349 optionType, messageType, icon, options, initialValue);
356 * @throws HeadlessException
358 public static void showMessageDialog(Component parentComponent,
359 String message) throws HeadlessException
361 if (!isInteractiveMode())
363 outputMessage(message);
369 JOptionPane.showMessageDialog(parentComponent, message);
373 * OK with message, title, and type
375 * @param parentComponent
379 * @throws HeadlessException
381 public static void showMessageDialog(Component parentComponent,
382 String message, String title, int messageType)
383 throws HeadlessException
385 // 30 implementations -- all just fine.
387 if (!isInteractiveMode())
389 outputMessage(message);
393 JOptionPane.showMessageDialog(parentComponent,
394 getPrefix(messageType) + message, title, messageType);
398 * adds title and icon
400 * @param parentComponent
405 * @throws HeadlessException
407 public static void showMessageDialog(Component parentComponent,
408 String message, String title, int messageType, Icon icon)
409 throws HeadlessException
414 if (!isInteractiveMode())
416 outputMessage(message);
420 JOptionPane.showMessageDialog(parentComponent, message, title,
428 public static void showInternalMessageDialog(Component parentComponent,
432 // WsPreferences only
434 if (!isInteractiveMode())
436 outputMessage(message);
440 JOptionPane.showMessageDialog(parentComponent, message);
444 * Adds title and messageType
446 * @param parentComponent
451 public static void showInternalMessageDialog(Component parentComponent,
452 String message, String title, int messageType)
457 if (!isInteractiveMode())
459 outputMessage(message);
463 JOptionPane.showMessageDialog(parentComponent,
464 getPrefix(messageType) + message, title, messageType);
469 * @param parentComponent
475 public static void showInternalMessageDialog(Component parentComponent,
476 Object message, String title, int messageType, Icon icon)
481 if (!isInteractiveMode())
483 outputMessage(message);
487 JOptionPane.showMessageDialog(parentComponent, message, title,
495 * @throws HeadlessException
497 public static String showInputDialog(Object message)
498 throws HeadlessException
502 if (!isInteractiveMode())
504 return getMockResponse().toString();
507 return JOptionPane.showInputDialog(message);
511 * adds inital selection value
514 * @param initialSelectionValue
517 public static String showInputDialog(String message,
518 String initialSelectionValue)
520 if (!isInteractiveMode())
522 return getMockResponse().toString();
525 // AnnotationPanel character option
527 return JOptionPane.showInputDialog(message, initialSelectionValue);
531 * adds inital selection value
534 * @param initialSelectionValue
537 public static String showInputDialog(Object message,
538 Object initialSelectionValue)
540 if (!isInteractiveMode())
542 return getMockResponse().toString();
545 // AnnotationPanel character option
547 return JOptionPane.showInputDialog(message, initialSelectionValue);
553 * @param parentComponent
556 * @throws HeadlessException
558 public static String showInputDialog(Component parentComponent,
559 String message) throws HeadlessException
563 return isInteractiveMode()
564 ? JOptionPane.showInputDialog(parentComponent, message)
565 : getMockResponse().toString();
569 * input with initial selection
571 * @param parentComponent
573 * @param initialSelectionValue
576 public static String showInputDialog(Component parentComponent,
577 String message, String initialSelectionValue)
582 return isInteractiveMode()
583 ? JOptionPane.showInputDialog(parentComponent, message,
584 initialSelectionValue)
585 : getMockResponse().toString();
589 * input with initial selection
591 * @param parentComponent
593 * @param initialSelectionValue
596 public static String showInputDialog(Component parentComponent,
597 Object message, Object initialSelectionValue)
602 return isInteractiveMode()
603 ? JOptionPane.showInputDialog(parentComponent, message,
604 initialSelectionValue)
605 : getMockResponse().toString();
610 * @param parentComponent
615 * @throws HeadlessException
617 public static String showInputDialog(Component parentComponent,
618 String message, String title, int messageType)
619 throws HeadlessException
624 return isInteractiveMode()
625 ? JOptionPane.showInputDialog(parentComponent, message, title,
627 : getMockResponse().toString();
631 * Customized input option
633 * @param parentComponent
638 * @param selectionValues
639 * @param initialSelectionValue
641 * @throws HeadlessException
643 public static Object showInputDialog(Component parentComponent,
644 Object message, String title, int messageType, Icon icon,
645 Object[] selectionValues, Object initialSelectionValue)
646 throws HeadlessException
651 return isInteractiveMode()
652 ? JOptionPane.showInputDialog(parentComponent, message, title,
653 messageType, icon, selectionValues,
654 initialSelectionValue)
655 : getMockResponse().toString();
661 * @param parentComponent
665 public static String showInternalInputDialog(Component parentComponent,
670 return isInteractiveMode()
671 ? JOptionPane.showInternalInputDialog(parentComponent, message)
672 : getMockResponse().toString();
676 * internal with title and messageType
678 * @param parentComponent
684 public static String showInternalInputDialog(Component parentComponent,
685 String message, String title, int messageType)
688 // AlignFrame tabbedPane_mousePressed
690 return isInteractiveMode()
691 ? JOptionPane.showInternalInputDialog(parentComponent,
692 getPrefix(messageType) + message, title, messageType)
693 : getMockResponse().toString();
697 * customized internal
699 * @param parentComponent
704 * @param selectionValues
705 * @param initialSelectionValue
708 public static Object showInternalInputDialog(Component parentComponent,
709 String message, String title, int messageType, Icon icon,
710 Object[] selectionValues, Object initialSelectionValue)
714 return isInteractiveMode()
715 ? JOptionPane.showInternalInputDialog(parentComponent, message,
716 title, messageType, icon, selectionValues,
717 initialSelectionValue)
718 : getMockResponse().toString();
721 ///////////// end of options ///////////////
723 private static void outputMessage(Object message)
725 System.out.println(">>> JOption Message : " + message.toString());
728 public static Object getMockResponse()
733 public static void setMockResponse(Object mockOption)
735 JvOptionPane.mockResponse = mockOption;
738 public static void resetMock()
740 setMockResponse(JvOptionPane.CANCEL_OPTION);
741 setInteractiveMode(true);
744 public static boolean isInteractiveMode()
746 return interactiveMode;
749 public static void setInteractiveMode(boolean interactive)
751 JvOptionPane.interactiveMode = interactive;
754 private static String getPrefix(int messageType)
763 case JvOptionPane.WARNING_MESSAGE:
764 prefix = "WARNING! ";
766 case JvOptionPane.ERROR_MESSAGE:
777 * create a new option dialog that can be used to register responses - along
778 * lines of showOptionDialog
783 * @param defaultOption
784 * @param plainMessage
790 public static JvOptionPane newOptionDialog()
792 return new JvOptionPane(null);
795 public static JvOptionPane newOptionDialog(Component parentComponent)
797 return new JvOptionPane(parentComponent);
800 public void showDialog(String message, String title, int optionType,
801 int messageType, Icon icon, Object[] options, Object initialValue)
803 showDialog(message, title, optionType, messageType, icon, options,
807 public void showDialog(Object message, String title, int optionType,
808 int messageType, Icon icon, Object[] options, Object initialValue,
811 showDialog(message, title, optionType, messageType, icon, options,
812 initialValue, modal, null);
815 public void showDialog(Object message, String title, int optionType,
816 int messageType, Icon icon, Object[] options, Object initialValue,
817 boolean modal, JButton[] buttons)
819 if (!isInteractiveMode())
821 handleResponse(getMockResponse());
828 // 1) AlignViewport for openLinkedAlignment
830 // Show a dialog with the option to open and link (cDNA <-> protein) as a
832 // alignment, either as a standalone alignment or in a split frame. Returns
833 // true if the new alignment was opened, false if not, because the user
834 // declined the offer.
836 // 2) UserDefinedColors warning about saving over a name already defined
839 ourOptions = Arrays.asList(options);
843 boolean useButtons = false;
844 Object initialValueButton = null;
845 NOTNULL: if (buttons != null)
847 if (buttons.length != options.length)
849 jalview.bin.Console.error(
850 "Supplied buttons array not the same length as supplied options array.");
853 int[] buttonActions = { JOptionPane.YES_OPTION,
854 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
855 for (int i = 0; i < options.length; i++)
857 Object o = options[i];
858 jalview.bin.Console.debug(
859 "Setting button " + i + " to '" + o.toString() + "'");
860 JButton jb = buttons[i];
862 if (o.equals(initialValue))
863 initialValueButton = jb;
865 int buttonAction = buttonActions[i];
866 Runnable action = callbacks.get(buttonAction);
867 jb.setText((String) o);
868 jb.addActionListener(new ActionListener()
871 public void actionPerformed(ActionEvent e)
874 Object obj = e.getSource();
875 if (obj == null || !(obj instanceof Component))
877 jalview.bin.Console.debug(
878 "Could not find Component source of event object "
882 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
883 JOptionPane.class, (Component) obj);
884 if (joptionpaneObject == null
885 || !(joptionpaneObject instanceof JOptionPane))
887 jalview.bin.Console.debug(
888 "Could not find JOptionPane ancestor of event object "
892 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
893 joptionpane.setValue(buttonAction);
895 new Thread(action).start();
896 joptionpane.transferFocusBackward();
897 joptionpane.setVisible(false);
898 // put focus and raise parent window if possible, unless cancel or
900 boolean raiseParent = (parentComponent != null);
901 if (buttonAction == JOptionPane.CANCEL_OPTION)
903 if (optionType == JOptionPane.YES_NO_OPTION
904 && buttonAction == JOptionPane.NO_OPTION)
908 parentComponent.requestFocus();
909 if (parentComponent instanceof JInternalFrame)
911 JInternalFrame jif = (JInternalFrame) parentComponent;
916 else if (parentComponent instanceof Window)
918 Window w = (Window) parentComponent;
923 joptionpane.setVisible(false);
930 // use a JOptionPane as usual
931 int response = JOptionPane.showOptionDialog(parentComponent, message,
932 title, optionType, messageType, icon,
933 useButtons ? buttons : options,
934 useButtons ? initialValueButton : initialValue);
937 * In Java, the response is returned to this thread and handled here; (for
938 * Javascript, see propertyChange)
940 if (!Platform.isJS())
947 handleResponse(response);
953 * This is java similar to the swingjs handling, with the callbacks attached to
954 * the button press of the dialog. This means we can use a non-modal JDialog for
955 * the confirmation without blocking the GUI.
957 JOptionPane joptionpane = new JOptionPane();
958 // Make button options
959 int[] buttonActions = { JvOptionPane.YES_OPTION,
960 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
962 // we need the strings to make the buttons with actionEventListener
965 ArrayList<String> options_default = new ArrayList<>();
967 .add(UIManager.getString("OptionPane.yesButtonText"));
968 if (optionType == JvOptionPane.YES_NO_OPTION
969 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
972 .add(UIManager.getString("OptionPane.noButtonText"));
974 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
977 .add(UIManager.getString("OptionPane.cancelButtonText"));
979 options = options_default.toArray();
982 ArrayList<JButton> options_btns = new ArrayList<>();
983 Object initialValue_btn = null;
984 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
987 for (int i = 0; i < options.length && i < 3; i++)
989 Object o = options[i];
990 int buttonAction = buttonActions[i];
991 Runnable action = callbacks.get(buttonAction);
992 JButton jb = new JButton();
993 jb.setText((String) o);
994 jb.addActionListener(new ActionListener()
998 public void actionPerformed(ActionEvent e)
1000 joptionpane.setValue(buttonAction);
1002 new Thread(action).start();
1003 // joptionpane.transferFocusBackward();
1004 joptionpane.transferFocusBackward();
1005 joptionpane.setVisible(false);
1006 // put focus and raise parent window if possible, unless cancel
1008 boolean raiseParent = (parentComponent != null);
1009 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1010 raiseParent = false;
1011 if (optionType == JvOptionPane.YES_NO_OPTION
1012 && buttonAction == JvOptionPane.NO_OPTION)
1013 raiseParent = false;
1016 parentComponent.requestFocus();
1017 if (parentComponent instanceof JInternalFrame)
1019 JInternalFrame jif = (JInternalFrame) parentComponent;
1024 else if (parentComponent instanceof Window)
1026 Window w = (Window) parentComponent;
1031 joptionpane.setVisible(false);
1034 options_btns.add(jb);
1035 if (o.equals(initialValue))
1036 initialValue_btn = jb;
1039 joptionpane.setMessage(message);
1040 joptionpane.setMessageType(messageType);
1041 joptionpane.setOptionType(optionType);
1042 joptionpane.setIcon(icon);
1043 joptionpane.setOptions(
1044 Platform.isJS() ? options : options_btns.toArray());
1045 joptionpane.setInitialValue(
1046 Platform.isJS() ? initialValue : initialValue_btn);
1048 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1049 dialog.setIconImages(ChannelProperties.getIconList());
1050 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1051 : ModalityType.MODELESS);
1052 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1053 dialog.setVisible(true);
1058 public void showInternalDialog(Object mainPanel, String title,
1059 int yesNoCancelOption, int questionMessage, Icon icon,
1060 Object[] options, String initresponse)
1062 if (!isInteractiveMode())
1064 handleResponse(getMockResponse());
1067 // need to set these separately so we can set the title bar icon later
1068 this.setOptionType(yesNoCancelOption);
1069 this.setMessageType(questionMessage);
1071 this.setInitialValue(initresponse);
1072 this.setOptions(options);
1073 this.setMessage(mainPanel);
1075 ourOptions = Arrays.asList(options);
1076 if (parentComponent != this
1077 && !(parentComponent == null && Desktop.instance == null))
1079 // note the parent goes back to a JRootPane so is probably
1080 // Desktop.getDesktop()
1081 JInternalFrame jif = this.createInternalFrame(
1082 parentComponent != null ? parentComponent : Desktop.instance,
1084 // connect to the alignFrame using a map in Desktop
1085 if (parentComponent instanceof AlignFrame)
1087 Desktop.addModal((AlignFrame) parentComponent, jif);
1089 jif.setFrameIcon(null);
1090 jif.addInternalFrameListener(new InternalFrameListener()
1093 public void internalFrameActivated(InternalFrameEvent arg0)
1098 public void internalFrameClosed(InternalFrameEvent arg0)
1100 JvOptionPane.this.internalDialogHandleResponse();
1104 public void internalFrameClosing(InternalFrameEvent arg0)
1109 public void internalFrameDeactivated(InternalFrameEvent arg0)
1114 public void internalFrameDeiconified(InternalFrameEvent arg0)
1119 public void internalFrameIconified(InternalFrameEvent arg0)
1124 public void internalFrameOpened(InternalFrameEvent arg0)
1128 jif.setVisible(true);
1134 JDialog dialog = this.createDialog(parentComponent, title);
1135 dialog.setIconImages(ChannelProperties.getIconList());
1136 dialog.setVisible(true); // blocking
1137 this.internalDialogHandleResponse();
1142 private void internalDialogHandleResponse()
1144 String responseString = (String) this.getValue();
1145 int response = ourOptions.indexOf(responseString);
1147 if (!Platform.isJS())
1154 handleResponse(response);
1159 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1160 * action) { callbacks.put(response, new Callable<Void>() {
1162 * @Override public Void call() { action.run(); return null; } }); return this;
1166 public JvOptionPane setResponseHandler(Object response, Runnable action)
1170 action = NULLCALLABLE;
1172 callbacks.put(response, action);
1176 public void setDialog(JDialog d)
1181 public JDialog getDialog()
1187 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1190 public static int showDialogOnTop(String label, String actionString,
1191 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1193 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1194 JOPTIONPANE_MESSAGETYPE);
1197 public static int showDialogOnTop(Component dialogParentComponent,
1198 String label, String actionString, int JOPTIONPANE_OPTION,
1199 int JOPTIONPANE_MESSAGETYPE)
1201 if (!isInteractiveMode())
1203 return (int) getMockResponse();
1205 // Ensure Jalview window is brought to front (primarily for Quit
1206 // confirmation window to be visible)
1208 // This method of raising the Jalview window is broken in java
1209 // jalviewDesktop.setVisible(true);
1210 // jalviewDesktop.toFront();
1212 // A better hack which works is to create a new JFrame parent with
1213 // setAlwaysOnTop(true)
1214 JFrame dialogParent = new JFrame();
1215 if (dialogParentComponent == null)
1217 dialogParent.setIconImages(ChannelProperties.getIconList());
1218 dialogParent.setAlwaysOnTop(true);
1221 int answer = JOptionPane.showConfirmDialog(
1222 dialogParentComponent == null ? dialogParent
1223 : dialogParentComponent,
1224 label, actionString, JOPTIONPANE_OPTION,
1225 JOPTIONPANE_MESSAGETYPE);
1227 if (dialogParentComponent == null)
1229 dialogParent.setAlwaysOnTop(false);
1230 dialogParent.dispose();
1236 public void showDialogOnTopAsync(String label, String actionString,
1237 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1238 Object[] options, Object initialValue, boolean modal)
1240 JFrame frame = new JFrame();
1241 frame.setIconImages(ChannelProperties.getIconList());
1242 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1243 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1246 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1247 String actionString, int JOPTIONPANE_OPTION,
1248 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1249 Object initialValue, boolean modal)
1251 showDialogOnTopAsync(dialogParent, label, actionString,
1252 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1253 initialValue, modal, null);
1256 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1257 String actionString, int JOPTIONPANE_OPTION,
1258 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1259 Object initialValue, boolean modal, JButton[] buttons)
1261 if (!isInteractiveMode())
1263 handleResponse(getMockResponse());
1266 // Ensure Jalview window is brought to front (primarily for Quit
1267 // confirmation window to be visible)
1269 // This method of raising the Jalview window is broken in java
1270 // jalviewDesktop.setVisible(true);
1271 // jalviewDesktop.toFront();
1273 // A better hack which works is to create a new JFrame parent with
1274 // setAlwaysOnTop(true)
1275 dialogParent.setAlwaysOnTop(true);
1276 parentComponent = dialogParent;
1278 showDialog(label, actionString, JOPTIONPANE_OPTION,
1279 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1282 dialogParent.setAlwaysOnTop(false);
1283 dialogParent.dispose();
1287 * JalviewJS signals option selection by a property change event for the
1288 * option e.g. "OK". This methods responds to that by running the response
1289 * action that corresponds to that option.
1294 public void propertyChange(PropertyChangeEvent evt)
1296 Object newValue = evt.getNewValue();
1297 int ourOption = ourOptions.indexOf(newValue);
1300 handleResponse(ourOption);
1305 handleResponse(newValue);
1310 public void handleResponse(Object response)
1313 * this test is for NaN in Chrome
1315 if (response != null && !response.equals(response))
1319 Runnable action = callbacks.get(response);
1324 new Thread(action).start();
1326 } catch (Exception e)
1328 e.printStackTrace();
1330 if (parentComponent != null)
1331 parentComponent.requestFocus();
1336 * Create a non-modal confirm dialog
1338 public JDialog createDialog(Component parentComponent, Object message,
1339 String title, int optionType, int messageType, Icon icon,
1340 Object[] options, Object initialValue, boolean modal)
1342 return createDialog(parentComponent, message, title, optionType,
1343 messageType, icon, options, initialValue, modal, null);
1346 public JDialog createDialog(Component parentComponent, Object message,
1347 String title, int optionType, int messageType, Icon icon,
1348 Object[] options, Object initialValue, boolean modal,
1351 if (!isInteractiveMode())
1353 handleResponse(getMockResponse());
1356 JButton[] optionsButtons = null;
1357 Object initialValueButton = null;
1358 JOptionPane joptionpane = new JOptionPane();
1359 // Make button options
1360 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1361 JOptionPane.CANCEL_OPTION };
1363 // we need the strings to make the buttons with actionEventListener
1364 if (options == null)
1366 ArrayList<String> options_default = new ArrayList<>();
1367 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1368 if (optionType == JOptionPane.YES_NO_OPTION
1369 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1371 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1373 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1376 .add(UIManager.getString("OptionPane.cancelButtonText"));
1378 options = options_default.toArray();
1380 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1383 if (((optionType == JOptionPane.YES_OPTION
1384 || optionType == JOptionPane.NO_OPTION
1385 || optionType == JOptionPane.CANCEL_OPTION
1386 || optionType == JOptionPane.OK_OPTION
1387 || optionType == JOptionPane.DEFAULT_OPTION)
1388 && options.length < 1)
1389 || ((optionType == JOptionPane.YES_NO_OPTION
1390 || optionType == JOptionPane.OK_CANCEL_OPTION)
1391 && options.length < 2)
1392 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1393 && options.length < 3))
1396 .debug("JvOptionPane: not enough options for dialog type");
1398 optionsButtons = new JButton[options.length];
1399 for (int i = 0; i < options.length && i < 3; i++)
1401 Object o = options[i];
1402 int buttonAction = buttonActions[i];
1403 Runnable action = callbacks.get(buttonAction);
1405 if (buttons != null && buttons.length > i && buttons[i] != null)
1413 jb.setText((String) o);
1414 jb.addActionListener(new ActionListener()
1417 public void actionPerformed(ActionEvent e)
1419 joptionpane.setValue(buttonAction);
1421 new Thread(action).start();
1422 // joptionpane.transferFocusBackward();
1423 joptionpane.transferFocusBackward();
1424 joptionpane.setVisible(false);
1425 // put focus and raise parent window if possible, unless cancel
1427 boolean raiseParent = (parentComponent != null);
1428 if (buttonAction == JOptionPane.CANCEL_OPTION)
1429 raiseParent = false;
1430 if (optionType == JOptionPane.YES_NO_OPTION
1431 && buttonAction == JOptionPane.NO_OPTION)
1432 raiseParent = false;
1435 parentComponent.requestFocus();
1436 if (parentComponent instanceof JInternalFrame)
1438 JInternalFrame jif = (JInternalFrame) parentComponent;
1443 else if (parentComponent instanceof Window)
1445 Window w = (Window) parentComponent;
1450 joptionpane.setVisible(false);
1453 optionsButtons[i] = jb;
1454 if (o.equals(initialValue))
1455 initialValueButton = jb;
1458 joptionpane.setMessage(message);
1459 joptionpane.setMessageType(messageType);
1460 joptionpane.setOptionType(optionType);
1461 joptionpane.setIcon(icon);
1462 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1463 joptionpane.setInitialValue(
1464 Platform.isJS() ? initialValue : initialValueButton);
1466 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1467 dialog.setIconImages(ChannelProperties.getIconList());
1468 dialog.setModalityType(
1469 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1470 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1476 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1478 * returns true if button was found
1480 public static boolean clickButton(JFrame frame, int buttonType)
1487 * This helper method makes the JInternalFrame wait until it is notified by an
1488 * InternalFrameClosing event. This method also adds the given JOptionPane to
1489 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1493 * The JInternalFrame to make modal.
1495 private static void startModal(JInternalFrame f)
1497 // We need to add an additional glasspane-like component directly
1498 // below the frame, which intercepts all mouse events that are not
1499 // directed at the frame itself.
1500 JPanel modalInterceptor = new JPanel();
1501 modalInterceptor.setOpaque(false);
1502 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1503 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1504 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1505 modalInterceptor.addMouseListener(new MouseAdapter()
1508 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1511 lp.add(modalInterceptor);
1514 // disable the main menu bar if in Linux
1515 JMenuBar menubar = null;
1516 if (Platform.isLinux())
1518 JRootPane rootpane = Desktop.getDesktop().getRootPane();
1519 menubar = rootpane.getJMenuBar();
1522 // We need to explicitly dispatch events when we are blocking the event
1524 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1527 if (menubar != null)
1529 // don't allow clicks on main menu on linux due to a hanging bug.
1531 setMenusEnabled(menubar, false);
1534 while (!f.isClosed())
1536 if (EventQueue.isDispatchThread())
1538 // The getNextEventMethod() issues wait() when no
1539 // event is available, so we don't need do explicitly wait().
1540 AWTEvent ev = queue.getNextEvent();
1541 // This mimics EventQueue.dispatchEvent(). We can't use
1542 // EventQueue.dispatchEvent() directly, because it is
1543 // protected, unfortunately.
1544 if (ev instanceof ActiveEvent)
1546 ((ActiveEvent) ev).dispatch();
1548 else if (ev instanceof KeyEvent && ((KeyEvent) ev).isControlDown()
1551 // temporarily enable menus to send Ctrl+? KeyEvents
1552 setMenusEnabled(menubar, true);
1553 ((Component) ev.getSource()).dispatchEvent(ev);
1554 setMenusEnabled(menubar, false);
1556 else if (ev.getSource() instanceof MenuComponent)
1558 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1560 else if (ev.getSource() instanceof Component)
1562 ((Component) ev.getSource()).dispatchEvent(ev);
1564 // Other events are ignored as per spec in
1565 // EventQueue.dispatchEvent
1569 // Give other threads a chance to become active.
1573 } catch (InterruptedException ex)
1575 // If we get interrupted, then leave the modal state.
1578 // re-enable the main menu bar
1579 if (menubar != null)
1581 setMenusEnabled(menubar, true);
1584 // Clean up the modal interceptor.
1585 lp.remove(modalInterceptor);
1587 // unpaint the frame
1588 f.setVisible(false);
1594 } catch (PropertyVetoException e)
1596 f.doDefaultCloseAction();
1599 // Remove the internal frame from its parent, so it is no longer
1600 // lurking around and clogging memory.
1601 Container parent = f.getParent();
1609 public static JvOptionPane frameDialog(Object message, String title,
1610 int messageType, String[] buttonsTextS, String defaultButtonS,
1611 List<Runnable> handlers, boolean modal)
1613 JFrame parent = new JFrame();
1614 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1615 final String[] buttonsText;
1616 final String defaultButton;
1617 if (buttonsTextS == null)
1619 String ok = MessageManager.getString("action.ok");
1620 buttonsText = new String[] { ok };
1625 buttonsText = buttonsTextS;
1626 defaultButton = defaultButtonS;
1628 JButton[] buttons = new JButton[buttonsText.length];
1629 for (int i = 0; i < buttonsText.length; i++)
1631 buttons[i] = new JButton();
1632 buttons[i].setText(buttonsText[i]);
1633 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1634 buttons[i].setEnabled(false);
1635 buttons[i].setVisible(false);
1638 int dialogType = -1;
1639 if (buttonsText.length == 1)
1641 dialogType = JOptionPane.OK_OPTION;
1643 else if (buttonsText.length == 2)
1645 dialogType = JOptionPane.YES_NO_OPTION;
1649 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1651 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1652 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1654 if (dialogType == JOptionPane.YES_NO_OPTION
1655 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1657 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1658 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1661 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1663 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1664 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1668 final int dt = dialogType;
1670 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1671 defaultButton, modal, buttons);
1677 private static void setMenusEnabled(JMenuBar menubar, boolean b)
1679 for (int i = 0; i < menubar.getMenuCount(); i++)
1681 JMenu menu = menubar.getMenu(i);