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.MouseAdapter;
36 import java.awt.event.MouseMotionAdapter;
37 import java.beans.PropertyChangeEvent;
38 import java.beans.PropertyChangeListener;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.concurrent.ExecutorService;
45 import java.util.concurrent.Executors;
47 import javax.swing.Icon;
48 import javax.swing.JButton;
49 import javax.swing.JDialog;
50 import javax.swing.JFrame;
51 import javax.swing.JInternalFrame;
52 import javax.swing.JLayeredPane;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.SwingUtilities;
56 import javax.swing.UIManager;
57 import javax.swing.event.InternalFrameEvent;
58 import javax.swing.event.InternalFrameListener;
60 import jalview.bin.Console;
61 import jalview.util.ChannelProperties;
62 import jalview.util.MessageManager;
63 import jalview.util.Platform;
64 import jalview.util.dialogrunner.DialogRunnerI;
66 public class JvOptionPane extends JOptionPane
67 implements DialogRunnerI, PropertyChangeListener
69 private static final long serialVersionUID = -3019167117756785229L;
71 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
73 private static boolean interactiveMode = true;
75 public static final Runnable NULLCALLABLE = () -> {
78 private Component parentComponent;
80 private ExecutorService executor = Executors.newCachedThreadPool();
82 private JDialog dialog = null;
84 private Map<Object, Runnable> callbacks = new HashMap<>();
87 * JalviewJS reports user choice in the dialog as the selected option (text);
88 * this list allows conversion to index (int)
90 List<Object> ourOptions;
92 public JvOptionPane(final Component parent)
94 this.parentComponent = Platform.isJS() ? this : parent;
98 public static int showConfirmDialog(Component parentComponent,
99 Object message) throws HeadlessException
101 // only called by test
102 return isInteractiveMode()
103 ? JOptionPane.showConfirmDialog(parentComponent, message)
104 : (int) getMockResponse();
108 * Message, title, optionType
110 * @param parentComponent
115 * @throws HeadlessException
117 public static int showConfirmDialog(Component parentComponent,
118 Object message, String title, int optionType)
119 throws HeadlessException
121 if (!isInteractiveMode())
123 return (int) getMockResponse();
127 case JvOptionPane.YES_NO_CANCEL_OPTION:
128 // FeatureRenderer amendFeatures ?? TODO ??
133 case JvOptionPane.YES_NO_OPTION:
134 // PromptUserConfig usage stats
135 // for now treated as "OK CANCEL"
137 case JvOptionPane.OK_CANCEL_OPTION:
138 // will fall back to simple HTML
139 return JOptionPane.showConfirmDialog(parentComponent, message, title,
145 * Adds a message type. Fallback is to just add it in the beginning.
147 * @param parentComponent
153 * @throws HeadlessException
155 public static int showConfirmDialog(Component parentComponent,
156 Object message, String title, int optionType, int messageType)
157 throws HeadlessException
159 // JalviewServicesChanged
160 // PromptUserConfig raiseDialog
161 return isInteractiveMode()
162 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
163 optionType, messageType)
164 : (int) getMockResponse();
170 * @param parentComponent
177 * @throws HeadlessException
179 public static int showConfirmDialog(Component parentComponent,
180 Object message, String title, int optionType, int messageType,
181 Icon icon) throws HeadlessException
183 // JvOptionPaneTest only
184 return isInteractiveMode()
185 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
186 optionType, messageType, icon)
187 : (int) getMockResponse();
191 * Internal version "OK"
193 * @param parentComponent
197 public static int showInternalConfirmDialog(Component parentComponent,
200 // JvOptionPaneTest only;
201 return isInteractiveMode()
202 ? JOptionPane.showInternalConfirmDialog(parentComponent,
204 : (int) getMockResponse();
208 * Internal version -- changed to standard version for now
210 * @param parentComponent
216 public static int showInternalConfirmDialog(Component parentComponent,
217 String message, String title, int optionType)
219 if (!isInteractiveMode())
221 return (int) getMockResponse();
225 case JvOptionPane.YES_NO_CANCEL_OPTION:
226 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
227 case JvOptionPane.YES_NO_OPTION:
228 // UserDefinedColoursSave -- relevant? TODO
231 case JvOptionPane.OK_CANCEL_OPTION:
233 // EditNameDialog --- uses panel for messsage TODO
235 // Desktop.inputURLMenuItem
237 return JOptionPane.showConfirmDialog(parentComponent, message, title,
244 * @param parentComponent
251 public static int showInternalConfirmDialog(Component parentComponent,
252 Object message, String title, int optionType, int messageType)
254 if (!isInteractiveMode())
256 return (int) getMockResponse();
260 case JvOptionPane.YES_NO_CANCEL_OPTION:
261 case JvOptionPane.YES_NO_OPTION:
262 // UserQuestionanaireCheck
266 case JvOptionPane.OK_CANCEL_OPTION:
267 // will fall back to simple HTML
268 return JOptionPane.showConfirmDialog(parentComponent, message, title,
269 optionType, messageType);
274 * adds icon; no longer internal
276 * @param parentComponent
284 public static int showInternalConfirmDialog(Component parentComponent,
285 Object message, String title, int optionType, int messageType,
288 if (!isInteractiveMode())
290 return (int) getMockResponse();
294 case JvOptionPane.YES_NO_CANCEL_OPTION:
295 case JvOptionPane.YES_NO_OPTION:
298 case JvOptionPane.OK_CANCEL_OPTION:
299 // Preferences editLink/newLink
300 return JOptionPane.showConfirmDialog(parentComponent, message, title,
301 optionType, messageType, icon);
307 * custom options full-featured
309 * @param parentComponent
316 * @param initialValue
318 * @throws HeadlessException
320 public static int showOptionDialog(Component parentComponent,
321 String message, String title, int optionType, int messageType,
322 Icon icon, Object[] options, Object initialValue)
323 throws HeadlessException
325 if (!isInteractiveMode())
327 return (int) getMockResponse();
333 // 1) AlignViewport for openLinkedAlignment
335 // Show a dialog with the option to open and link (cDNA <-> protein) as a
337 // alignment, either as a standalone alignment or in a split frame. Returns
338 // true if the new alignment was opened, false if not, because the user
339 // declined the offer.
341 // 2) UserDefinedColors warning about saving over a name already defined
343 return JOptionPane.showOptionDialog(parentComponent, message, title,
344 optionType, messageType, icon, options, initialValue);
351 * @throws HeadlessException
353 public static void showMessageDialog(Component parentComponent,
354 String message) throws HeadlessException
356 if (!isInteractiveMode())
358 outputMessage(message);
364 JOptionPane.showMessageDialog(parentComponent, message);
368 * OK with message, title, and type
370 * @param parentComponent
374 * @throws HeadlessException
376 public static void showMessageDialog(Component parentComponent,
377 String message, String title, int messageType)
378 throws HeadlessException
380 // 30 implementations -- all just fine.
382 if (!isInteractiveMode())
384 outputMessage(message);
388 JOptionPane.showMessageDialog(parentComponent,
389 getPrefix(messageType) + message, title, messageType);
393 * adds title and icon
395 * @param parentComponent
400 * @throws HeadlessException
402 public static void showMessageDialog(Component parentComponent,
403 String message, String title, int messageType, Icon icon)
404 throws HeadlessException
409 if (!isInteractiveMode())
411 outputMessage(message);
415 JOptionPane.showMessageDialog(parentComponent, message, title,
423 public static void showInternalMessageDialog(Component parentComponent,
427 // WsPreferences only
429 if (!isInteractiveMode())
431 outputMessage(message);
435 JOptionPane.showMessageDialog(parentComponent, message);
439 * Adds title and messageType
441 * @param parentComponent
446 public static void showInternalMessageDialog(Component parentComponent,
447 String message, String title, int messageType)
452 if (!isInteractiveMode())
454 outputMessage(message);
458 JOptionPane.showMessageDialog(parentComponent,
459 getPrefix(messageType) + message, title, messageType);
464 * @param parentComponent
470 public static void showInternalMessageDialog(Component parentComponent,
471 Object message, String title, int messageType, Icon icon)
476 if (!isInteractiveMode())
478 outputMessage(message);
482 JOptionPane.showMessageDialog(parentComponent, message, title,
490 * @throws HeadlessException
492 public static String showInputDialog(Object message)
493 throws HeadlessException
497 if (!isInteractiveMode())
499 return getMockResponse().toString();
502 return JOptionPane.showInputDialog(message);
506 * adds inital selection value
509 * @param initialSelectionValue
512 public static String showInputDialog(String message,
513 String initialSelectionValue)
515 if (!isInteractiveMode())
517 return getMockResponse().toString();
520 // AnnotationPanel character option
522 return JOptionPane.showInputDialog(message, initialSelectionValue);
526 * adds inital selection value
529 * @param initialSelectionValue
532 public static String showInputDialog(Object message,
533 Object initialSelectionValue)
535 if (!isInteractiveMode())
537 return getMockResponse().toString();
540 // AnnotationPanel character option
542 return JOptionPane.showInputDialog(message, initialSelectionValue);
548 * @param parentComponent
551 * @throws HeadlessException
553 public static String showInputDialog(Component parentComponent,
554 String message) throws HeadlessException
558 return isInteractiveMode()
559 ? JOptionPane.showInputDialog(parentComponent, message)
560 : getMockResponse().toString();
564 * input with initial selection
566 * @param parentComponent
568 * @param initialSelectionValue
571 public static String showInputDialog(Component parentComponent,
572 String message, String initialSelectionValue)
577 return isInteractiveMode()
578 ? JOptionPane.showInputDialog(parentComponent, message,
579 initialSelectionValue)
580 : getMockResponse().toString();
584 * input with initial selection
586 * @param parentComponent
588 * @param initialSelectionValue
591 public static String showInputDialog(Component parentComponent,
592 Object message, Object initialSelectionValue)
597 return isInteractiveMode()
598 ? JOptionPane.showInputDialog(parentComponent, message,
599 initialSelectionValue)
600 : getMockResponse().toString();
605 * @param parentComponent
610 * @throws HeadlessException
612 public static String showInputDialog(Component parentComponent,
613 String message, String title, int messageType)
614 throws HeadlessException
619 return isInteractiveMode()
620 ? JOptionPane.showInputDialog(parentComponent, message, title,
622 : getMockResponse().toString();
626 * Customized input option
628 * @param parentComponent
633 * @param selectionValues
634 * @param initialSelectionValue
636 * @throws HeadlessException
638 public static Object showInputDialog(Component parentComponent,
639 Object message, String title, int messageType, Icon icon,
640 Object[] selectionValues, Object initialSelectionValue)
641 throws HeadlessException
646 return isInteractiveMode()
647 ? JOptionPane.showInputDialog(parentComponent, message, title,
648 messageType, icon, selectionValues,
649 initialSelectionValue)
650 : getMockResponse().toString();
656 * @param parentComponent
660 public static String showInternalInputDialog(Component parentComponent,
665 return isInteractiveMode()
666 ? JOptionPane.showInternalInputDialog(parentComponent, message)
667 : getMockResponse().toString();
671 * internal with title and messageType
673 * @param parentComponent
679 public static String showInternalInputDialog(Component parentComponent,
680 String message, String title, int messageType)
683 // AlignFrame tabbedPane_mousePressed
685 return isInteractiveMode()
686 ? JOptionPane.showInternalInputDialog(parentComponent,
687 getPrefix(messageType) + message, title, messageType)
688 : getMockResponse().toString();
692 * customized internal
694 * @param parentComponent
699 * @param selectionValues
700 * @param initialSelectionValue
703 public static Object showInternalInputDialog(Component parentComponent,
704 String message, String title, int messageType, Icon icon,
705 Object[] selectionValues, Object initialSelectionValue)
709 return isInteractiveMode()
710 ? JOptionPane.showInternalInputDialog(parentComponent, message,
711 title, messageType, icon, selectionValues,
712 initialSelectionValue)
713 : getMockResponse().toString();
716 ///////////// end of options ///////////////
718 private static void outputMessage(Object message)
720 System.out.println(">>> JOption Message : " + message.toString());
723 public static Object getMockResponse()
728 public static void setMockResponse(Object mockOption)
730 JvOptionPane.mockResponse = mockOption;
733 public static void resetMock()
735 setMockResponse(JvOptionPane.CANCEL_OPTION);
736 setInteractiveMode(true);
739 public static boolean isInteractiveMode()
741 return interactiveMode;
744 public static void setInteractiveMode(boolean interactive)
746 JvOptionPane.interactiveMode = interactive;
749 private static String getPrefix(int messageType)
758 case JvOptionPane.WARNING_MESSAGE:
759 prefix = "WARNING! ";
761 case JvOptionPane.ERROR_MESSAGE:
772 * create a new option dialog that can be used to register responses - along
773 * lines of showOptionDialog
778 * @param defaultOption
779 * @param plainMessage
785 public static JvOptionPane newOptionDialog()
787 return new JvOptionPane(null);
790 public static JvOptionPane newOptionDialog(Component parentComponent)
792 return new JvOptionPane(parentComponent);
795 public void showDialog(String message, String title, int optionType,
796 int messageType, Icon icon, Object[] options, Object initialValue)
798 showDialog(message, title, optionType, messageType, icon, options,
802 public void showDialog(Object message, String title, int optionType,
803 int messageType, Icon icon, Object[] options, Object initialValue,
806 showDialog(message, title, optionType, messageType, icon, options,
807 initialValue, modal, null);
810 public void showDialog(Object message, String title, int optionType,
811 int messageType, Icon icon, Object[] options, Object initialValue,
812 boolean modal, JButton[] buttons)
814 if (!isInteractiveMode())
816 handleResponse(getMockResponse());
823 // 1) AlignViewport for openLinkedAlignment
825 // Show a dialog with the option to open and link (cDNA <-> protein) as a
827 // alignment, either as a standalone alignment or in a split frame. Returns
828 // true if the new alignment was opened, false if not, because the user
829 // declined the offer.
831 // 2) UserDefinedColors warning about saving over a name already defined
834 ourOptions = Arrays.asList(options);
838 boolean useButtons = false;
839 Object initialValueButton = null;
840 NOTNULL: if (buttons != null)
842 if (buttons.length != options.length)
844 jalview.bin.Console.error(
845 "Supplied buttons array not the same length as supplied options array.");
848 int[] buttonActions = { JOptionPane.YES_OPTION,
849 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
850 for (int i = 0; i < options.length; i++)
852 Object o = options[i];
853 jalview.bin.Console.debug(
854 "Setting button " + i + " to '" + o.toString() + "'");
855 JButton jb = buttons[i];
857 if (o.equals(initialValue))
858 initialValueButton = jb;
860 int buttonAction = buttonActions[i];
861 Runnable action = callbacks.get(buttonAction);
862 jb.setText((String) o);
863 jb.addActionListener(new ActionListener()
866 public void actionPerformed(ActionEvent e)
869 Object obj = e.getSource();
870 if (obj == null || !(obj instanceof Component))
872 jalview.bin.Console.debug(
873 "Could not find Component source of event object "
877 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
878 JOptionPane.class, (Component) obj);
879 if (joptionpaneObject == null
880 || !(joptionpaneObject instanceof JOptionPane))
882 jalview.bin.Console.debug(
883 "Could not find JOptionPane ancestor of event object "
887 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
888 joptionpane.setValue(buttonAction);
890 new Thread(action).start();
891 joptionpane.transferFocusBackward();
892 joptionpane.setVisible(false);
893 // put focus and raise parent window if possible, unless cancel or
895 boolean raiseParent = (parentComponent != null);
896 if (buttonAction == JOptionPane.CANCEL_OPTION)
898 if (optionType == JOptionPane.YES_NO_OPTION
899 && buttonAction == JOptionPane.NO_OPTION)
903 parentComponent.requestFocus();
904 if (parentComponent instanceof JInternalFrame)
906 JInternalFrame jif = (JInternalFrame) parentComponent;
911 else if (parentComponent instanceof Window)
913 Window w = (Window) parentComponent;
918 joptionpane.setVisible(false);
925 // use a JOptionPane as usual
926 int response = JOptionPane.showOptionDialog(parentComponent, message,
927 title, optionType, messageType, icon,
928 useButtons ? buttons : options,
929 useButtons ? initialValueButton : initialValue);
932 * In Java, the response is returned to this thread and handled here; (for
933 * Javascript, see propertyChange)
935 if (!Platform.isJS())
942 handleResponse(response);
948 * This is java similar to the swingjs handling, with the callbacks attached to
949 * the button press of the dialog. This means we can use a non-modal JDialog for
950 * the confirmation without blocking the GUI.
952 JOptionPane joptionpane = new JOptionPane();
953 // Make button options
954 int[] buttonActions = { JvOptionPane.YES_OPTION,
955 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
957 // we need the strings to make the buttons with actionEventListener
960 ArrayList<String> options_default = new ArrayList<>();
962 .add(UIManager.getString("OptionPane.yesButtonText"));
963 if (optionType == JvOptionPane.YES_NO_OPTION
964 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
967 .add(UIManager.getString("OptionPane.noButtonText"));
969 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
972 .add(UIManager.getString("OptionPane.cancelButtonText"));
974 options = options_default.toArray();
977 ArrayList<JButton> options_btns = new ArrayList<>();
978 Object initialValue_btn = null;
979 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
982 for (int i = 0; i < options.length && i < 3; i++)
984 Object o = options[i];
985 int buttonAction = buttonActions[i];
986 Runnable action = callbacks.get(buttonAction);
987 JButton jb = new JButton();
988 jb.setText((String) o);
989 jb.addActionListener(new ActionListener()
993 public void actionPerformed(ActionEvent e)
995 joptionpane.setValue(buttonAction);
997 new Thread(action).start();
998 // joptionpane.transferFocusBackward();
999 joptionpane.transferFocusBackward();
1000 joptionpane.setVisible(false);
1001 // put focus and raise parent window if possible, unless cancel
1003 boolean raiseParent = (parentComponent != null);
1004 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1005 raiseParent = false;
1006 if (optionType == JvOptionPane.YES_NO_OPTION
1007 && buttonAction == JvOptionPane.NO_OPTION)
1008 raiseParent = false;
1011 parentComponent.requestFocus();
1012 if (parentComponent instanceof JInternalFrame)
1014 JInternalFrame jif = (JInternalFrame) parentComponent;
1019 else if (parentComponent instanceof Window)
1021 Window w = (Window) parentComponent;
1026 joptionpane.setVisible(false);
1029 options_btns.add(jb);
1030 if (o.equals(initialValue))
1031 initialValue_btn = jb;
1034 joptionpane.setMessage(message);
1035 joptionpane.setMessageType(messageType);
1036 joptionpane.setOptionType(optionType);
1037 joptionpane.setIcon(icon);
1038 joptionpane.setOptions(
1039 Platform.isJS() ? options : options_btns.toArray());
1040 joptionpane.setInitialValue(
1041 Platform.isJS() ? initialValue : initialValue_btn);
1043 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1044 dialog.setIconImages(ChannelProperties.getIconList());
1045 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1046 : ModalityType.MODELESS);
1047 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1048 dialog.setVisible(true);
1053 public void showInternalDialog(Object mainPanel, String title,
1054 int yesNoCancelOption, int questionMessage, Icon icon,
1055 Object[] options, String initresponse)
1057 if (!isInteractiveMode())
1059 handleResponse(getMockResponse());
1062 // need to set these separately so we can set the title bar icon later
1063 this.setOptionType(yesNoCancelOption);
1064 this.setMessageType(questionMessage);
1066 this.setInitialValue(initresponse);
1067 this.setOptions(options);
1068 this.setMessage(mainPanel);
1070 ourOptions = Arrays.asList(options);
1071 if (parentComponent != this
1072 && !(parentComponent == null && Desktop.instance == null))
1074 JInternalFrame jif = this.createInternalFrame(
1075 parentComponent != null ? parentComponent : Desktop.instance,
1077 jif.setFrameIcon(null);
1078 jif.addInternalFrameListener(new InternalFrameListener()
1081 public void internalFrameActivated(InternalFrameEvent arg0)
1086 public void internalFrameClosed(InternalFrameEvent arg0)
1088 JvOptionPane.this.internalDialogHandleResponse();
1092 public void internalFrameClosing(InternalFrameEvent arg0)
1097 public void internalFrameDeactivated(InternalFrameEvent arg0)
1102 public void internalFrameDeiconified(InternalFrameEvent arg0)
1107 public void internalFrameIconified(InternalFrameEvent arg0)
1112 public void internalFrameOpened(InternalFrameEvent arg0)
1116 jif.setVisible(true);
1122 JDialog dialog = this.createDialog(parentComponent, title);
1123 dialog.setIconImages(ChannelProperties.getIconList());
1124 dialog.setVisible(true); // blocking
1125 this.internalDialogHandleResponse();
1130 private void internalDialogHandleResponse()
1132 String responseString = (String) this.getValue();
1133 int response = ourOptions.indexOf(responseString);
1135 if (!Platform.isJS())
1142 handleResponse(response);
1147 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1148 * action) { callbacks.put(response, new Callable<Void>() {
1150 * @Override public Void call() { action.run(); return null; } }); return this;
1154 public JvOptionPane setResponseHandler(Object response, Runnable action)
1158 action = NULLCALLABLE;
1160 callbacks.put(response, action);
1164 public void setDialog(JDialog d)
1169 public JDialog getDialog()
1175 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1178 public static int showDialogOnTop(String label, String actionString,
1179 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1181 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1182 JOPTIONPANE_MESSAGETYPE);
1185 public static int showDialogOnTop(Component dialogParentComponent,
1186 String label, String actionString, int JOPTIONPANE_OPTION,
1187 int JOPTIONPANE_MESSAGETYPE)
1189 if (!isInteractiveMode())
1191 return (int) getMockResponse();
1193 // Ensure Jalview window is brought to front (primarily for Quit
1194 // confirmation window to be visible)
1196 // This method of raising the Jalview window is broken in java
1197 // jalviewDesktop.setVisible(true);
1198 // jalviewDesktop.toFront();
1200 // A better hack which works is to create a new JFrame parent with
1201 // setAlwaysOnTop(true)
1202 JFrame dialogParent = new JFrame();
1203 if (dialogParentComponent == null)
1205 dialogParent.setIconImages(ChannelProperties.getIconList());
1206 dialogParent.setAlwaysOnTop(true);
1209 int answer = JOptionPane.showConfirmDialog(
1210 dialogParentComponent == null ? dialogParent
1211 : dialogParentComponent,
1212 label, actionString, JOPTIONPANE_OPTION,
1213 JOPTIONPANE_MESSAGETYPE);
1215 if (dialogParentComponent == null)
1217 dialogParent.setAlwaysOnTop(false);
1218 dialogParent.dispose();
1224 public void showDialogOnTopAsync(String label, String actionString,
1225 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1226 Object[] options, Object initialValue, boolean modal)
1228 JFrame frame = new JFrame();
1229 frame.setIconImages(ChannelProperties.getIconList());
1230 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1231 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1234 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1235 String actionString, int JOPTIONPANE_OPTION,
1236 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1237 Object initialValue, boolean modal)
1239 showDialogOnTopAsync(dialogParent, label, actionString,
1240 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1241 initialValue, modal, null);
1244 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1245 String actionString, int JOPTIONPANE_OPTION,
1246 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1247 Object initialValue, boolean modal, JButton[] buttons)
1249 if (!isInteractiveMode())
1251 handleResponse(getMockResponse());
1254 // Ensure Jalview window is brought to front (primarily for Quit
1255 // confirmation window to be visible)
1257 // This method of raising the Jalview window is broken in java
1258 // jalviewDesktop.setVisible(true);
1259 // jalviewDesktop.toFront();
1261 // A better hack which works is to create a new JFrame parent with
1262 // setAlwaysOnTop(true)
1263 dialogParent.setAlwaysOnTop(true);
1264 parentComponent = dialogParent;
1266 showDialog(label, actionString, JOPTIONPANE_OPTION,
1267 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1270 dialogParent.setAlwaysOnTop(false);
1271 dialogParent.dispose();
1275 * JalviewJS signals option selection by a property change event for the
1276 * option e.g. "OK". This methods responds to that by running the response
1277 * action that corresponds to that option.
1282 public void propertyChange(PropertyChangeEvent evt)
1284 Object newValue = evt.getNewValue();
1285 int ourOption = ourOptions.indexOf(newValue);
1288 handleResponse(ourOption);
1293 handleResponse(newValue);
1298 public void handleResponse(Object response)
1301 * this test is for NaN in Chrome
1303 if (response != null && !response.equals(response))
1307 Runnable action = callbacks.get(response);
1312 new Thread(action).start();
1314 } catch (Exception e)
1316 e.printStackTrace();
1318 if (parentComponent != null)
1319 parentComponent.requestFocus();
1324 * Create a non-modal confirm dialog
1326 public JDialog createDialog(Component parentComponent, Object message,
1327 String title, int optionType, int messageType, Icon icon,
1328 Object[] options, Object initialValue, boolean modal)
1330 return createDialog(parentComponent, message, title, optionType,
1331 messageType, icon, options, initialValue, modal, null);
1334 public JDialog createDialog(Component parentComponent, Object message,
1335 String title, int optionType, int messageType, Icon icon,
1336 Object[] options, Object initialValue, boolean modal,
1339 if (!isInteractiveMode())
1341 handleResponse(getMockResponse());
1344 JButton[] optionsButtons = null;
1345 Object initialValueButton = null;
1346 JOptionPane joptionpane = new JOptionPane();
1347 // Make button options
1348 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1349 JOptionPane.CANCEL_OPTION };
1351 // we need the strings to make the buttons with actionEventListener
1352 if (options == null)
1354 ArrayList<String> options_default = new ArrayList<>();
1355 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1356 if (optionType == JOptionPane.YES_NO_OPTION
1357 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1359 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1361 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1364 .add(UIManager.getString("OptionPane.cancelButtonText"));
1366 options = options_default.toArray();
1368 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1371 if (((optionType == JOptionPane.YES_OPTION
1372 || optionType == JOptionPane.NO_OPTION
1373 || optionType == JOptionPane.CANCEL_OPTION
1374 || optionType == JOptionPane.OK_OPTION
1375 || optionType == JOptionPane.DEFAULT_OPTION)
1376 && options.length < 1)
1377 || ((optionType == JOptionPane.YES_NO_OPTION
1378 || optionType == JOptionPane.OK_CANCEL_OPTION)
1379 && options.length < 2)
1380 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1381 && options.length < 3))
1384 .debug("JvOptionPane: not enough options for dialog type");
1386 optionsButtons = new JButton[options.length];
1387 for (int i = 0; i < options.length && i < 3; i++)
1389 Object o = options[i];
1390 int buttonAction = buttonActions[i];
1391 Runnable action = callbacks.get(buttonAction);
1393 if (buttons != null && buttons.length > i && buttons[i] != null)
1401 jb.setText((String) o);
1402 jb.addActionListener(new ActionListener()
1405 public void actionPerformed(ActionEvent e)
1407 joptionpane.setValue(buttonAction);
1409 new Thread(action).start();
1410 // joptionpane.transferFocusBackward();
1411 joptionpane.transferFocusBackward();
1412 joptionpane.setVisible(false);
1413 // put focus and raise parent window if possible, unless cancel
1415 boolean raiseParent = (parentComponent != null);
1416 if (buttonAction == JOptionPane.CANCEL_OPTION)
1417 raiseParent = false;
1418 if (optionType == JOptionPane.YES_NO_OPTION
1419 && buttonAction == JOptionPane.NO_OPTION)
1420 raiseParent = false;
1423 parentComponent.requestFocus();
1424 if (parentComponent instanceof JInternalFrame)
1426 JInternalFrame jif = (JInternalFrame) parentComponent;
1431 else if (parentComponent instanceof Window)
1433 Window w = (Window) parentComponent;
1438 joptionpane.setVisible(false);
1441 optionsButtons[i] = jb;
1442 if (o.equals(initialValue))
1443 initialValueButton = jb;
1446 joptionpane.setMessage(message);
1447 joptionpane.setMessageType(messageType);
1448 joptionpane.setOptionType(optionType);
1449 joptionpane.setIcon(icon);
1450 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1451 joptionpane.setInitialValue(
1452 Platform.isJS() ? initialValue : initialValueButton);
1454 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1455 dialog.setIconImages(ChannelProperties.getIconList());
1456 dialog.setModalityType(
1457 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1458 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1464 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1466 * returns true if button was found
1468 public static boolean clickButton(JFrame frame, int buttonType)
1475 * This helper method makes the JInternalFrame wait until it is notified by an
1476 * InternalFrameClosing event. This method also adds the given JOptionPane to
1477 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1481 * The JInternalFrame to make modal.
1483 private static void startModal(JInternalFrame f)
1485 // We need to add an additional glasspane-like component directly
1486 // below the frame, which intercepts all mouse events that are not
1487 // directed at the frame itself.
1488 JPanel modalInterceptor = new JPanel();
1489 modalInterceptor.setOpaque(false);
1490 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1491 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1492 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1493 modalInterceptor.addMouseListener(new MouseAdapter()
1496 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1499 lp.add(modalInterceptor);
1502 // We need to explicitly dispatch events when we are blocking the event
1504 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1507 while (!f.isClosed())
1509 if (EventQueue.isDispatchThread())
1511 // The getNextEventMethod() issues wait() when no
1512 // event is available, so we don't need do explicitly wait().
1513 AWTEvent ev = queue.getNextEvent();
1514 // This mimics EventQueue.dispatchEvent(). We can't use
1515 // EventQueue.dispatchEvent() directly, because it is
1516 // protected, unfortunately.
1517 if (ev instanceof ActiveEvent)
1518 ((ActiveEvent) ev).dispatch();
1519 else if (ev.getSource() instanceof Component)
1520 ((Component) ev.getSource()).dispatchEvent(ev);
1521 else if (ev.getSource() instanceof MenuComponent)
1522 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1523 // Other events are ignored as per spec in
1524 // EventQueue.dispatchEvent
1528 // Give other threads a chance to become active.
1532 } catch (InterruptedException ex)
1534 // If we get interrupted, then leave the modal state.
1537 // Clean up the modal interceptor.
1538 lp.remove(modalInterceptor);
1540 // Remove the internal frame from its parent, so it is no longer
1541 // lurking around and clogging memory.
1542 Container parent = f.getParent();
1548 public static JvOptionPane frameDialog(Object message, String title,
1549 int messageType, String[] buttonsTextS, String defaultButtonS,
1550 List<Runnable> handlers, boolean modal)
1552 JFrame parent = new JFrame();
1553 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1554 final String[] buttonsText;
1555 final String defaultButton;
1556 if (buttonsTextS == null)
1558 String ok = MessageManager.getString("action.ok");
1559 buttonsText = new String[] { ok };
1564 buttonsText = buttonsTextS;
1565 defaultButton = defaultButtonS;
1567 JButton[] buttons = new JButton[buttonsText.length];
1568 for (int i = 0; i < buttonsText.length; i++)
1570 buttons[i] = new JButton();
1571 buttons[i].setText(buttonsText[i]);
1572 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1573 buttons[i].setEnabled(false);
1574 buttons[i].setVisible(false);
1577 int dialogType = -1;
1578 if (buttonsText.length == 1)
1580 dialogType = JOptionPane.OK_OPTION;
1582 else if (buttonsText.length == 2)
1584 dialogType = JOptionPane.YES_NO_OPTION;
1588 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1590 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1591 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1593 if (dialogType == JOptionPane.YES_NO_OPTION
1594 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1596 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1597 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1600 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1602 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1603 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1607 final int dt = dialogType;
1609 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1610 defaultButton, modal, buttons);