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)
726 .outPrintln(">>> JOption Message : " + message.toString());
729 public static Object getMockResponse()
734 public static void setMockResponse(Object mockOption)
736 JvOptionPane.mockResponse = mockOption;
739 public static void resetMock()
741 setMockResponse(JvOptionPane.CANCEL_OPTION);
742 setInteractiveMode(true);
745 public static boolean isInteractiveMode()
747 return interactiveMode;
750 public static void setInteractiveMode(boolean interactive)
752 JvOptionPane.interactiveMode = interactive;
755 private static String getPrefix(int messageType)
764 case JvOptionPane.WARNING_MESSAGE:
765 prefix = "WARNING! ";
767 case JvOptionPane.ERROR_MESSAGE:
778 * create a new option dialog that can be used to register responses - along
779 * lines of showOptionDialog
784 * @param defaultOption
785 * @param plainMessage
791 public static JvOptionPane newOptionDialog()
793 return new JvOptionPane(null);
796 public static JvOptionPane newOptionDialog(Component parentComponent)
798 return new JvOptionPane(parentComponent);
801 public void showDialog(String message, String title, int optionType,
802 int messageType, Icon icon, Object[] options, Object initialValue)
804 showDialog(message, title, optionType, messageType, icon, options,
808 public void showDialog(Object message, String title, int optionType,
809 int messageType, Icon icon, Object[] options, Object initialValue,
812 showDialog(message, title, optionType, messageType, icon, options,
813 initialValue, modal, null);
816 public void showDialog(Object message, String title, int optionType,
817 int messageType, Icon icon, Object[] options, Object initialValue,
818 boolean modal, JButton[] buttons)
820 if (!isInteractiveMode())
822 handleResponse(getMockResponse());
829 // 1) AlignViewport for openLinkedAlignment
831 // Show a dialog with the option to open and link (cDNA <-> protein) as a
833 // alignment, either as a standalone alignment or in a split frame. Returns
834 // true if the new alignment was opened, false if not, because the user
835 // declined the offer.
837 // 2) UserDefinedColors warning about saving over a name already defined
840 ourOptions = Arrays.asList(options);
844 boolean useButtons = false;
845 Object initialValueButton = null;
846 NOTNULL: if (buttons != null)
848 if (buttons.length != options.length)
850 jalview.bin.Console.error(
851 "Supplied buttons array not the same length as supplied options array.");
854 int[] buttonActions = { JOptionPane.YES_OPTION,
855 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
856 for (int i = 0; i < options.length; i++)
858 Object o = options[i];
859 jalview.bin.Console.debug(
860 "Setting button " + i + " to '" + o.toString() + "'");
861 JButton jb = buttons[i];
863 if (o.equals(initialValue))
864 initialValueButton = jb;
866 int buttonAction = buttonActions[i];
867 Runnable action = callbacks.get(buttonAction);
868 jb.setText((String) o);
869 jb.addActionListener(new ActionListener()
872 public void actionPerformed(ActionEvent e)
875 Object obj = e.getSource();
876 if (obj == null || !(obj instanceof Component))
878 jalview.bin.Console.debug(
879 "Could not find Component source of event object "
883 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
884 JOptionPane.class, (Component) obj);
885 if (joptionpaneObject == null
886 || !(joptionpaneObject instanceof JOptionPane))
888 jalview.bin.Console.debug(
889 "Could not find JOptionPane ancestor of event object "
893 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
894 joptionpane.setValue(buttonAction);
896 new Thread(action).start();
897 joptionpane.transferFocusBackward();
898 joptionpane.setVisible(false);
899 // put focus and raise parent window if possible, unless cancel or
901 boolean raiseParent = (parentComponent != null);
902 if (buttonAction == JOptionPane.CANCEL_OPTION)
904 if (optionType == JOptionPane.YES_NO_OPTION
905 && buttonAction == JOptionPane.NO_OPTION)
909 parentComponent.requestFocus();
910 if (parentComponent instanceof JInternalFrame)
912 JInternalFrame jif = (JInternalFrame) parentComponent;
917 else if (parentComponent instanceof Window)
919 Window w = (Window) parentComponent;
924 joptionpane.setVisible(false);
931 // use a JOptionPane as usual
932 int response = JOptionPane.showOptionDialog(parentComponent, message,
933 title, optionType, messageType, icon,
934 useButtons ? buttons : options,
935 useButtons ? initialValueButton : initialValue);
938 * In Java, the response is returned to this thread and handled here; (for
939 * Javascript, see propertyChange)
941 if (!Platform.isJS())
948 handleResponse(response);
954 * This is java similar to the swingjs handling, with the callbacks attached to
955 * the button press of the dialog. This means we can use a non-modal JDialog for
956 * the confirmation without blocking the GUI.
958 JOptionPane joptionpane = new JOptionPane();
959 // Make button options
960 int[] buttonActions = { JvOptionPane.YES_OPTION,
961 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
963 // we need the strings to make the buttons with actionEventListener
966 ArrayList<String> options_default = new ArrayList<>();
968 .add(UIManager.getString("OptionPane.yesButtonText"));
969 if (optionType == JvOptionPane.YES_NO_OPTION
970 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
973 .add(UIManager.getString("OptionPane.noButtonText"));
975 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
978 .add(UIManager.getString("OptionPane.cancelButtonText"));
980 options = options_default.toArray();
983 ArrayList<JButton> options_btns = new ArrayList<>();
984 Object initialValue_btn = null;
985 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
988 for (int i = 0; i < options.length && i < 3; i++)
990 Object o = options[i];
991 int buttonAction = buttonActions[i];
992 Runnable action = callbacks.get(buttonAction);
993 JButton jb = new JButton();
994 jb.setText((String) o);
995 jb.addActionListener(new ActionListener()
999 public void actionPerformed(ActionEvent e)
1001 joptionpane.setValue(buttonAction);
1003 new Thread(action).start();
1004 // joptionpane.transferFocusBackward();
1005 joptionpane.transferFocusBackward();
1006 joptionpane.setVisible(false);
1007 // put focus and raise parent window if possible, unless cancel
1009 boolean raiseParent = (parentComponent != null);
1010 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1011 raiseParent = false;
1012 if (optionType == JvOptionPane.YES_NO_OPTION
1013 && buttonAction == JvOptionPane.NO_OPTION)
1014 raiseParent = false;
1017 parentComponent.requestFocus();
1018 if (parentComponent instanceof JInternalFrame)
1020 JInternalFrame jif = (JInternalFrame) parentComponent;
1025 else if (parentComponent instanceof Window)
1027 Window w = (Window) parentComponent;
1032 joptionpane.setVisible(false);
1035 options_btns.add(jb);
1036 if (o.equals(initialValue))
1037 initialValue_btn = jb;
1040 joptionpane.setMessage(message);
1041 joptionpane.setMessageType(messageType);
1042 joptionpane.setOptionType(optionType);
1043 joptionpane.setIcon(icon);
1044 joptionpane.setOptions(
1045 Platform.isJS() ? options : options_btns.toArray());
1046 joptionpane.setInitialValue(
1047 Platform.isJS() ? initialValue : initialValue_btn);
1049 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1050 dialog.setIconImages(ChannelProperties.getIconList());
1051 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1052 : ModalityType.MODELESS);
1053 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1054 dialog.setVisible(true);
1059 public void showInternalDialog(Object mainPanel, String title,
1060 int yesNoCancelOption, int questionMessage, Icon icon,
1061 Object[] options, String initresponse)
1063 if (!isInteractiveMode())
1065 handleResponse(getMockResponse());
1068 // need to set these separately so we can set the title bar icon later
1069 this.setOptionType(yesNoCancelOption);
1070 this.setMessageType(questionMessage);
1072 this.setInitialValue(initresponse);
1073 this.setOptions(options);
1074 this.setMessage(mainPanel);
1076 ourOptions = Arrays.asList(options);
1077 if (parentComponent != this
1078 && !(parentComponent == null && Desktop.instance == null))
1080 // note the parent goes back to a JRootPane so is probably
1081 // Desktop.getDesktop()
1082 JInternalFrame jif = this.createInternalFrame(
1083 parentComponent != null ? parentComponent : Desktop.instance,
1085 // connect to the alignFrame using a map in Desktop
1086 if (parentComponent instanceof AlignFrame)
1088 Desktop.addModal((AlignFrame) parentComponent, jif);
1090 jif.setFrameIcon(null);
1091 jif.addInternalFrameListener(new InternalFrameListener()
1094 public void internalFrameActivated(InternalFrameEvent arg0)
1099 public void internalFrameClosed(InternalFrameEvent arg0)
1101 JvOptionPane.this.internalDialogHandleResponse();
1105 public void internalFrameClosing(InternalFrameEvent arg0)
1110 public void internalFrameDeactivated(InternalFrameEvent arg0)
1115 public void internalFrameDeiconified(InternalFrameEvent arg0)
1120 public void internalFrameIconified(InternalFrameEvent arg0)
1125 public void internalFrameOpened(InternalFrameEvent arg0)
1129 jif.setVisible(true);
1135 JDialog dialog = this.createDialog(parentComponent, title);
1136 dialog.setIconImages(ChannelProperties.getIconList());
1137 dialog.setVisible(true); // blocking
1138 this.internalDialogHandleResponse();
1143 private void internalDialogHandleResponse()
1145 Object value = this.getValue();
1147 || (value instanceof Integer && (Integer) value == -1))
1151 String responseString = value.toString();
1152 int response = ourOptions.indexOf(responseString);
1154 if (!Platform.isJS())
1161 handleResponse(response);
1166 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1167 * action) { callbacks.put(response, new Callable<Void>() {
1169 * @Override public Void call() { action.run(); return null; } }); return this;
1173 public JvOptionPane setResponseHandler(Object response, Runnable action)
1177 action = NULLCALLABLE;
1179 callbacks.put(response, action);
1183 public void setDialog(JDialog d)
1188 public JDialog getDialog()
1194 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1197 public static int showDialogOnTop(String label, String actionString,
1198 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1200 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1201 JOPTIONPANE_MESSAGETYPE);
1204 public static int showDialogOnTop(Component dialogParentComponent,
1205 String label, String actionString, int JOPTIONPANE_OPTION,
1206 int JOPTIONPANE_MESSAGETYPE)
1208 if (!isInteractiveMode())
1210 return (int) getMockResponse();
1212 // Ensure Jalview window is brought to front (primarily for Quit
1213 // confirmation window to be visible)
1215 // This method of raising the Jalview window is broken in java
1216 // jalviewDesktop.setVisible(true);
1217 // jalviewDesktop.toFront();
1219 // A better hack which works is to create a new JFrame parent with
1220 // setAlwaysOnTop(true)
1221 JFrame dialogParent = new JFrame();
1222 if (dialogParentComponent == null)
1224 dialogParent.setIconImages(ChannelProperties.getIconList());
1225 dialogParent.setAlwaysOnTop(true);
1228 int answer = JOptionPane.showConfirmDialog(
1229 dialogParentComponent == null ? dialogParent
1230 : dialogParentComponent,
1231 label, actionString, JOPTIONPANE_OPTION,
1232 JOPTIONPANE_MESSAGETYPE);
1234 if (dialogParentComponent == null)
1236 dialogParent.setAlwaysOnTop(false);
1237 dialogParent.dispose();
1243 public void showDialogOnTopAsync(String label, String actionString,
1244 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1245 Object[] options, Object initialValue, boolean modal)
1247 JFrame frame = new JFrame();
1248 frame.setIconImages(ChannelProperties.getIconList());
1249 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1250 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1253 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1254 String actionString, int JOPTIONPANE_OPTION,
1255 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1256 Object initialValue, boolean modal)
1258 showDialogOnTopAsync(dialogParent, label, actionString,
1259 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1260 initialValue, modal, null);
1263 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1264 String actionString, int JOPTIONPANE_OPTION,
1265 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1266 Object initialValue, boolean modal, JButton[] buttons)
1268 showDialogOnTopAsync(dialogParent, label, actionString,
1269 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1270 initialValue, modal, buttons, true);
1273 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1274 String actionString, int JOPTIONPANE_OPTION,
1275 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1276 Object initialValue, boolean modal, JButton[] buttons,
1279 if (!isInteractiveMode())
1281 handleResponse(getMockResponse());
1284 // Ensure Jalview window is brought to front (primarily for Quit
1285 // confirmation window to be visible)
1287 // This method of raising the Jalview window is broken in java
1288 // jalviewDesktop.setVisible(true);
1289 // jalviewDesktop.toFront();
1291 // A better hack which works is to create a new JFrame parent with
1292 // setAlwaysOnTop(true)
1293 dialogParent.setAlwaysOnTop(true);
1294 parentComponent = dialogParent;
1296 showDialog(label, actionString, JOPTIONPANE_OPTION,
1297 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1302 dialogParent.setAlwaysOnTop(false);
1303 dialogParent.dispose();
1308 * JalviewJS signals option selection by a property change event for the
1309 * option e.g. "OK". This methods responds to that by running the response
1310 * action that corresponds to that option.
1315 public void propertyChange(PropertyChangeEvent evt)
1317 Object newValue = evt.getNewValue();
1318 int ourOption = ourOptions.indexOf(newValue);
1321 handleResponse(ourOption);
1326 handleResponse(newValue);
1331 public void handleResponse(Object response)
1334 * this test is for NaN in Chrome
1336 if (response != null && !response.equals(response))
1340 Runnable action = callbacks.get(response);
1345 new Thread(action).start();
1347 } catch (Exception e)
1349 e.printStackTrace();
1351 if (parentComponent != null)
1352 parentComponent.requestFocus();
1357 * Create a non-modal confirm dialog
1359 public JDialog createDialog(Component parentComponent, Object message,
1360 String title, int optionType, int messageType, Icon icon,
1361 Object[] options, Object initialValue, boolean modal)
1363 return createDialog(parentComponent, message, title, optionType,
1364 messageType, icon, options, initialValue, modal, null);
1367 public JDialog createDialog(Component parentComponent, Object message,
1368 String title, int optionType, int messageType, Icon icon,
1369 Object[] options, Object initialValue, boolean modal,
1372 if (!isInteractiveMode())
1374 handleResponse(getMockResponse());
1377 JButton[] optionsButtons = null;
1378 Object initialValueButton = null;
1379 JOptionPane joptionpane = new JOptionPane();
1380 // Make button options
1381 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1382 JOptionPane.CANCEL_OPTION };
1384 // we need the strings to make the buttons with actionEventListener
1385 if (options == null)
1387 ArrayList<String> options_default = new ArrayList<>();
1388 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1389 if (optionType == JOptionPane.YES_NO_OPTION
1390 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1392 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1394 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1397 .add(UIManager.getString("OptionPane.cancelButtonText"));
1399 options = options_default.toArray();
1401 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1404 if (((optionType == JOptionPane.YES_OPTION
1405 || optionType == JOptionPane.NO_OPTION
1406 || optionType == JOptionPane.CANCEL_OPTION
1407 || optionType == JOptionPane.OK_OPTION
1408 || optionType == JOptionPane.DEFAULT_OPTION)
1409 && options.length < 1)
1410 || ((optionType == JOptionPane.YES_NO_OPTION
1411 || optionType == JOptionPane.OK_CANCEL_OPTION)
1412 && options.length < 2)
1413 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1414 && options.length < 3))
1417 .debug("JvOptionPane: not enough options for dialog type");
1419 optionsButtons = new JButton[options.length];
1420 for (int i = 0; i < options.length && i < 3; i++)
1422 Object o = options[i];
1423 int buttonAction = buttonActions[i];
1424 Runnable action = callbacks.get(buttonAction);
1426 if (buttons != null && buttons.length > i && buttons[i] != null)
1434 jb.setText((String) o);
1435 jb.addActionListener(new ActionListener()
1438 public void actionPerformed(ActionEvent e)
1440 joptionpane.setValue(buttonAction);
1442 new Thread(action).start();
1443 // joptionpane.transferFocusBackward();
1444 joptionpane.transferFocusBackward();
1445 joptionpane.setVisible(false);
1446 // put focus and raise parent window if possible, unless cancel
1448 boolean raiseParent = (parentComponent != null);
1449 if (buttonAction == JOptionPane.CANCEL_OPTION)
1450 raiseParent = false;
1451 if (optionType == JOptionPane.YES_NO_OPTION
1452 && buttonAction == JOptionPane.NO_OPTION)
1453 raiseParent = false;
1456 parentComponent.requestFocus();
1457 if (parentComponent instanceof JInternalFrame)
1459 JInternalFrame jif = (JInternalFrame) parentComponent;
1464 else if (parentComponent instanceof Window)
1466 Window w = (Window) parentComponent;
1471 joptionpane.setVisible(false);
1474 optionsButtons[i] = jb;
1475 if (o.equals(initialValue))
1476 initialValueButton = jb;
1479 joptionpane.setMessage(message);
1480 joptionpane.setMessageType(messageType);
1481 joptionpane.setOptionType(optionType);
1482 joptionpane.setIcon(icon);
1483 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1484 joptionpane.setInitialValue(
1485 Platform.isJS() ? initialValue : initialValueButton);
1487 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1488 dialog.setIconImages(ChannelProperties.getIconList());
1489 dialog.setModalityType(
1490 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1491 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1497 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1499 * returns true if button was found
1501 public static boolean clickButton(JFrame frame, int buttonType)
1508 * This helper method makes the JInternalFrame wait until it is notified by an
1509 * InternalFrameClosing event. This method also adds the given JOptionPane to
1510 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1514 * The JInternalFrame to make modal.
1516 private static void startModal(JInternalFrame f)
1518 // We need to add an additional glasspane-like component directly
1519 // below the frame, which intercepts all mouse events that are not
1520 // directed at the frame itself.
1521 JPanel modalInterceptor = new JPanel();
1522 modalInterceptor.setOpaque(false);
1523 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1524 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1525 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1526 modalInterceptor.addMouseListener(new MouseAdapter()
1529 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1532 lp.add(modalInterceptor);
1535 // disable the main menu bar if in Linux
1536 JMenuBar menubar = null;
1537 if (Platform.isLinux())
1539 JRootPane rootpane = Desktop.getDesktop().getRootPane();
1540 menubar = rootpane.getJMenuBar();
1543 // We need to explicitly dispatch events when we are blocking the event
1545 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1548 if (menubar != null)
1550 // don't allow clicks on main menu on linux due to a hanging bug.
1552 setMenusEnabled(menubar, false);
1555 while (!f.isClosed())
1557 if (EventQueue.isDispatchThread())
1559 // The getNextEventMethod() issues wait() when no
1560 // event is available, so we don't need do explicitly wait().
1561 AWTEvent ev = queue.getNextEvent();
1562 // This mimics EventQueue.dispatchEvent(). We can't use
1563 // EventQueue.dispatchEvent() directly, because it is
1564 // protected, unfortunately.
1565 if (ev instanceof ActiveEvent)
1567 ((ActiveEvent) ev).dispatch();
1569 else if (ev instanceof KeyEvent && ((KeyEvent) ev).isControlDown()
1572 // temporarily enable menus to send Ctrl+? KeyEvents
1573 setMenusEnabled(menubar, true);
1574 ((Component) ev.getSource()).dispatchEvent(ev);
1575 setMenusEnabled(menubar, false);
1577 else if (ev.getSource() instanceof MenuComponent)
1579 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1581 else if (ev.getSource() instanceof Component)
1583 ((Component) ev.getSource()).dispatchEvent(ev);
1585 // Other events are ignored as per spec in
1586 // EventQueue.dispatchEvent
1590 // Give other threads a chance to become active.
1594 } catch (InterruptedException ex)
1596 // If we get interrupted, then leave the modal state.
1599 // re-enable the main menu bar
1600 if (menubar != null)
1602 setMenusEnabled(menubar, true);
1605 // Clean up the modal interceptor.
1606 lp.remove(modalInterceptor);
1608 // unpaint the frame
1609 f.setVisible(false);
1615 } catch (PropertyVetoException e)
1617 f.doDefaultCloseAction();
1620 // Remove the internal frame from its parent, so it is no longer
1621 // lurking around and clogging memory.
1622 Container parent = f.getParent();
1630 public static JvOptionPane frameDialog(Object message, String title,
1631 int messageType, String[] buttonsTextS, String defaultButtonS,
1632 List<Runnable> handlers, boolean modal)
1634 JFrame parent = new JFrame();
1635 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1636 final String[] buttonsText;
1637 final String defaultButton;
1638 if (buttonsTextS == null)
1640 String ok = MessageManager.getString("action.ok");
1641 buttonsText = new String[] { ok };
1646 buttonsText = buttonsTextS;
1647 defaultButton = defaultButtonS;
1649 JButton[] buttons = new JButton[buttonsText.length];
1650 for (int i = 0; i < buttonsText.length; i++)
1652 buttons[i] = new JButton();
1653 buttons[i].setText(buttonsText[i]);
1654 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1655 buttons[i].setEnabled(false);
1656 buttons[i].setVisible(false);
1659 int dialogType = -1;
1660 if (buttonsText.length == 1)
1662 dialogType = JOptionPane.OK_OPTION;
1664 else if (buttonsText.length == 2)
1666 dialogType = JOptionPane.YES_NO_OPTION;
1670 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1672 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1673 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1675 if (dialogType == JOptionPane.YES_NO_OPTION
1676 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1678 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1679 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1682 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1684 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1685 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1689 final int dt = dialogType;
1691 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1692 defaultButton, modal, buttons);
1698 private static void setMenusEnabled(JMenuBar menubar, boolean b)
1700 for (int i = 0; i < menubar.getMenuCount(); i++)
1702 JMenu menu = menubar.getMenu(i);