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 jalview.bin.Console.outPrintln(">>> 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 Object value = this.getValue();
1146 || (value instanceof Integer && (Integer) value == -1))
1150 String responseString = value.toString();
1151 int response = ourOptions.indexOf(responseString);
1153 if (!Platform.isJS())
1160 handleResponse(response);
1165 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1166 * action) { callbacks.put(response, new Callable<Void>() {
1168 * @Override public Void call() { action.run(); return null; } }); return this;
1172 public JvOptionPane setResponseHandler(Object response, Runnable action)
1176 action = NULLCALLABLE;
1178 callbacks.put(response, action);
1182 public void setDialog(JDialog d)
1187 public JDialog getDialog()
1193 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1196 public static int showDialogOnTop(String label, String actionString,
1197 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1199 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1200 JOPTIONPANE_MESSAGETYPE);
1203 public static int showDialogOnTop(Component dialogParentComponent,
1204 String label, String actionString, int JOPTIONPANE_OPTION,
1205 int JOPTIONPANE_MESSAGETYPE)
1207 if (!isInteractiveMode())
1209 return (int) getMockResponse();
1211 // Ensure Jalview window is brought to front (primarily for Quit
1212 // confirmation window to be visible)
1214 // This method of raising the Jalview window is broken in java
1215 // jalviewDesktop.setVisible(true);
1216 // jalviewDesktop.toFront();
1218 // A better hack which works is to create a new JFrame parent with
1219 // setAlwaysOnTop(true)
1220 JFrame dialogParent = new JFrame();
1221 if (dialogParentComponent == null)
1223 dialogParent.setIconImages(ChannelProperties.getIconList());
1224 dialogParent.setAlwaysOnTop(true);
1227 int answer = JOptionPane.showConfirmDialog(
1228 dialogParentComponent == null ? dialogParent
1229 : dialogParentComponent,
1230 label, actionString, JOPTIONPANE_OPTION,
1231 JOPTIONPANE_MESSAGETYPE);
1233 if (dialogParentComponent == null)
1235 dialogParent.setAlwaysOnTop(false);
1236 dialogParent.dispose();
1242 public void showDialogOnTopAsync(String label, String actionString,
1243 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1244 Object[] options, Object initialValue, boolean modal)
1246 JFrame frame = new JFrame();
1247 frame.setIconImages(ChannelProperties.getIconList());
1248 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1249 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1252 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1253 String actionString, int JOPTIONPANE_OPTION,
1254 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1255 Object initialValue, boolean modal)
1257 showDialogOnTopAsync(dialogParent, label, actionString,
1258 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1259 initialValue, modal, null);
1262 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1263 String actionString, int JOPTIONPANE_OPTION,
1264 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1265 Object initialValue, boolean modal, JButton[] buttons)
1267 if (!isInteractiveMode())
1269 handleResponse(getMockResponse());
1272 // Ensure Jalview window is brought to front (primarily for Quit
1273 // confirmation window to be visible)
1275 // This method of raising the Jalview window is broken in java
1276 // jalviewDesktop.setVisible(true);
1277 // jalviewDesktop.toFront();
1279 // A better hack which works is to create a new JFrame parent with
1280 // setAlwaysOnTop(true)
1281 dialogParent.setAlwaysOnTop(true);
1282 parentComponent = dialogParent;
1284 showDialog(label, actionString, JOPTIONPANE_OPTION,
1285 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1288 dialogParent.setAlwaysOnTop(false);
1289 dialogParent.dispose();
1293 * JalviewJS signals option selection by a property change event for the
1294 * option e.g. "OK". This methods responds to that by running the response
1295 * action that corresponds to that option.
1300 public void propertyChange(PropertyChangeEvent evt)
1302 Object newValue = evt.getNewValue();
1303 int ourOption = ourOptions.indexOf(newValue);
1306 handleResponse(ourOption);
1311 handleResponse(newValue);
1316 public void handleResponse(Object response)
1319 * this test is for NaN in Chrome
1321 if (response != null && !response.equals(response))
1325 Runnable action = callbacks.get(response);
1330 new Thread(action).start();
1332 } catch (Exception e)
1334 e.printStackTrace();
1336 if (parentComponent != null)
1337 parentComponent.requestFocus();
1342 * Create a non-modal confirm dialog
1344 public JDialog createDialog(Component parentComponent, Object message,
1345 String title, int optionType, int messageType, Icon icon,
1346 Object[] options, Object initialValue, boolean modal)
1348 return createDialog(parentComponent, message, title, optionType,
1349 messageType, icon, options, initialValue, modal, null);
1352 public JDialog createDialog(Component parentComponent, Object message,
1353 String title, int optionType, int messageType, Icon icon,
1354 Object[] options, Object initialValue, boolean modal,
1357 if (!isInteractiveMode())
1359 handleResponse(getMockResponse());
1362 JButton[] optionsButtons = null;
1363 Object initialValueButton = null;
1364 JOptionPane joptionpane = new JOptionPane();
1365 // Make button options
1366 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1367 JOptionPane.CANCEL_OPTION };
1369 // we need the strings to make the buttons with actionEventListener
1370 if (options == null)
1372 ArrayList<String> options_default = new ArrayList<>();
1373 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1374 if (optionType == JOptionPane.YES_NO_OPTION
1375 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1377 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1379 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1382 .add(UIManager.getString("OptionPane.cancelButtonText"));
1384 options = options_default.toArray();
1386 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1389 if (((optionType == JOptionPane.YES_OPTION
1390 || optionType == JOptionPane.NO_OPTION
1391 || optionType == JOptionPane.CANCEL_OPTION
1392 || optionType == JOptionPane.OK_OPTION
1393 || optionType == JOptionPane.DEFAULT_OPTION)
1394 && options.length < 1)
1395 || ((optionType == JOptionPane.YES_NO_OPTION
1396 || optionType == JOptionPane.OK_CANCEL_OPTION)
1397 && options.length < 2)
1398 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1399 && options.length < 3))
1402 .debug("JvOptionPane: not enough options for dialog type");
1404 optionsButtons = new JButton[options.length];
1405 for (int i = 0; i < options.length && i < 3; i++)
1407 Object o = options[i];
1408 int buttonAction = buttonActions[i];
1409 Runnable action = callbacks.get(buttonAction);
1411 if (buttons != null && buttons.length > i && buttons[i] != null)
1419 jb.setText((String) o);
1420 jb.addActionListener(new ActionListener()
1423 public void actionPerformed(ActionEvent e)
1425 joptionpane.setValue(buttonAction);
1427 new Thread(action).start();
1428 // joptionpane.transferFocusBackward();
1429 joptionpane.transferFocusBackward();
1430 joptionpane.setVisible(false);
1431 // put focus and raise parent window if possible, unless cancel
1433 boolean raiseParent = (parentComponent != null);
1434 if (buttonAction == JOptionPane.CANCEL_OPTION)
1435 raiseParent = false;
1436 if (optionType == JOptionPane.YES_NO_OPTION
1437 && buttonAction == JOptionPane.NO_OPTION)
1438 raiseParent = false;
1441 parentComponent.requestFocus();
1442 if (parentComponent instanceof JInternalFrame)
1444 JInternalFrame jif = (JInternalFrame) parentComponent;
1449 else if (parentComponent instanceof Window)
1451 Window w = (Window) parentComponent;
1456 joptionpane.setVisible(false);
1459 optionsButtons[i] = jb;
1460 if (o.equals(initialValue))
1461 initialValueButton = jb;
1464 joptionpane.setMessage(message);
1465 joptionpane.setMessageType(messageType);
1466 joptionpane.setOptionType(optionType);
1467 joptionpane.setIcon(icon);
1468 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1469 joptionpane.setInitialValue(
1470 Platform.isJS() ? initialValue : initialValueButton);
1472 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1473 dialog.setIconImages(ChannelProperties.getIconList());
1474 dialog.setModalityType(
1475 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1476 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1482 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1484 * returns true if button was found
1486 public static boolean clickButton(JFrame frame, int buttonType)
1493 * This helper method makes the JInternalFrame wait until it is notified by an
1494 * InternalFrameClosing event. This method also adds the given JOptionPane to
1495 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1499 * The JInternalFrame to make modal.
1501 private static void startModal(JInternalFrame f)
1503 // We need to add an additional glasspane-like component directly
1504 // below the frame, which intercepts all mouse events that are not
1505 // directed at the frame itself.
1506 JPanel modalInterceptor = new JPanel();
1507 modalInterceptor.setOpaque(false);
1508 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1509 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1510 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1511 modalInterceptor.addMouseListener(new MouseAdapter()
1514 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1517 lp.add(modalInterceptor);
1520 // disable the main menu bar if in Linux
1521 JMenuBar menubar = null;
1522 if (Platform.isLinux())
1524 JRootPane rootpane = Desktop.getDesktop().getRootPane();
1525 menubar = rootpane.getJMenuBar();
1528 // We need to explicitly dispatch events when we are blocking the event
1530 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1533 if (menubar != null)
1535 // don't allow clicks on main menu on linux due to a hanging bug.
1537 setMenusEnabled(menubar, false);
1540 while (!f.isClosed())
1542 if (EventQueue.isDispatchThread())
1544 // The getNextEventMethod() issues wait() when no
1545 // event is available, so we don't need do explicitly wait().
1546 AWTEvent ev = queue.getNextEvent();
1547 // This mimics EventQueue.dispatchEvent(). We can't use
1548 // EventQueue.dispatchEvent() directly, because it is
1549 // protected, unfortunately.
1550 if (ev instanceof ActiveEvent)
1552 ((ActiveEvent) ev).dispatch();
1554 else if (ev instanceof KeyEvent && ((KeyEvent) ev).isControlDown()
1557 // temporarily enable menus to send Ctrl+? KeyEvents
1558 setMenusEnabled(menubar, true);
1559 ((Component) ev.getSource()).dispatchEvent(ev);
1560 setMenusEnabled(menubar, false);
1562 else if (ev.getSource() instanceof MenuComponent)
1564 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1566 else if (ev.getSource() instanceof Component)
1568 ((Component) ev.getSource()).dispatchEvent(ev);
1570 // Other events are ignored as per spec in
1571 // EventQueue.dispatchEvent
1575 // Give other threads a chance to become active.
1579 } catch (InterruptedException ex)
1581 // If we get interrupted, then leave the modal state.
1584 // re-enable the main menu bar
1585 if (menubar != null)
1587 setMenusEnabled(menubar, true);
1590 // Clean up the modal interceptor.
1591 lp.remove(modalInterceptor);
1593 // unpaint the frame
1594 f.setVisible(false);
1600 } catch (PropertyVetoException e)
1602 f.doDefaultCloseAction();
1605 // Remove the internal frame from its parent, so it is no longer
1606 // lurking around and clogging memory.
1607 Container parent = f.getParent();
1615 public static JvOptionPane frameDialog(Object message, String title,
1616 int messageType, String[] buttonsTextS, String defaultButtonS,
1617 List<Runnable> handlers, boolean modal)
1619 JFrame parent = new JFrame();
1620 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1621 final String[] buttonsText;
1622 final String defaultButton;
1623 if (buttonsTextS == null)
1625 String ok = MessageManager.getString("action.ok");
1626 buttonsText = new String[] { ok };
1631 buttonsText = buttonsTextS;
1632 defaultButton = defaultButtonS;
1634 JButton[] buttons = new JButton[buttonsText.length];
1635 for (int i = 0; i < buttonsText.length; i++)
1637 buttons[i] = new JButton();
1638 buttons[i].setText(buttonsText[i]);
1639 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1640 buttons[i].setEnabled(false);
1641 buttons[i].setVisible(false);
1644 int dialogType = -1;
1645 if (buttonsText.length == 1)
1647 dialogType = JOptionPane.OK_OPTION;
1649 else if (buttonsText.length == 2)
1651 dialogType = JOptionPane.YES_NO_OPTION;
1655 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1657 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1658 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1660 if (dialogType == JOptionPane.YES_NO_OPTION
1661 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1663 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1664 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1667 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1669 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1670 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1674 final int dt = dialogType;
1676 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1677 defaultButton, modal, buttons);
1683 private static void setMenusEnabled(JMenuBar menubar, boolean b)
1685 for (int i = 0; i < menubar.getMenuCount(); i++)
1687 JMenu menu = menubar.getMenu(i);