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.Callable;
45 import java.util.concurrent.ExecutorService;
46 import java.util.concurrent.Executors;
48 import javax.swing.Icon;
49 import javax.swing.JButton;
50 import javax.swing.JDialog;
51 import javax.swing.JFrame;
52 import javax.swing.JInternalFrame;
53 import javax.swing.JLayeredPane;
54 import javax.swing.JOptionPane;
55 import javax.swing.JPanel;
56 import javax.swing.SwingUtilities;
57 import javax.swing.UIManager;
58 import javax.swing.event.InternalFrameEvent;
59 import javax.swing.event.InternalFrameListener;
61 import jalview.bin.Console;
62 import jalview.util.ChannelProperties;
63 import jalview.util.MessageManager;
64 import jalview.util.Platform;
65 import jalview.util.dialogrunner.DialogRunnerI;
67 public class JvOptionPane extends JOptionPane
68 implements DialogRunnerI, PropertyChangeListener
70 private static final long serialVersionUID = -3019167117756785229L;
72 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
74 private static boolean interactiveMode = true;
76 private Component parentComponent;
78 private ExecutorService executor = Executors.newCachedThreadPool();
80 private JDialog dialog = null;
82 private Map<Object, Callable<Void>> callbacks = new HashMap<>();
85 * JalviewJS reports user choice in the dialog as the selected option (text);
86 * this list allows conversion to index (int)
88 List<Object> ourOptions;
90 public JvOptionPane(final Component parent)
92 this.parentComponent = Platform.isJS() ? this : parent;
96 public static int showConfirmDialog(Component parentComponent,
97 Object message) throws HeadlessException
99 // only called by test
100 return isInteractiveMode()
101 ? JOptionPane.showConfirmDialog(parentComponent, message)
102 : (int) getMockResponse();
106 * Message, title, optionType
108 * @param parentComponent
113 * @throws HeadlessException
115 public static int showConfirmDialog(Component parentComponent,
116 Object message, String title, int optionType)
117 throws HeadlessException
119 if (!isInteractiveMode())
121 return (int) getMockResponse();
125 case JvOptionPane.YES_NO_CANCEL_OPTION:
126 // FeatureRenderer amendFeatures ?? TODO ??
131 case JvOptionPane.YES_NO_OPTION:
132 // PromptUserConfig usage stats
133 // for now treated as "OK CANCEL"
135 case JvOptionPane.OK_CANCEL_OPTION:
136 // will fall back to simple HTML
137 return JOptionPane.showConfirmDialog(parentComponent, message, title,
143 * Adds a message type. Fallback is to just add it in the beginning.
145 * @param parentComponent
151 * @throws HeadlessException
153 public static int showConfirmDialog(Component parentComponent,
154 Object message, String title, int optionType, int messageType)
155 throws HeadlessException
157 // JalviewServicesChanged
158 // PromptUserConfig raiseDialog
159 return isInteractiveMode()
160 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
161 optionType, messageType)
162 : (int) getMockResponse();
168 * @param parentComponent
175 * @throws HeadlessException
177 public static int showConfirmDialog(Component parentComponent,
178 Object message, String title, int optionType, int messageType,
179 Icon icon) throws HeadlessException
181 // JvOptionPaneTest only
182 return isInteractiveMode()
183 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
184 optionType, messageType, icon)
185 : (int) getMockResponse();
189 * Internal version "OK"
191 * @param parentComponent
195 public static int showInternalConfirmDialog(Component parentComponent,
198 // JvOptionPaneTest only;
199 return isInteractiveMode()
200 ? JOptionPane.showInternalConfirmDialog(parentComponent,
202 : (int) getMockResponse();
206 * Internal version -- changed to standard version for now
208 * @param parentComponent
214 public static int showInternalConfirmDialog(Component parentComponent,
215 String message, String title, int optionType)
217 if (!isInteractiveMode())
219 return (int) getMockResponse();
223 case JvOptionPane.YES_NO_CANCEL_OPTION:
224 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
225 case JvOptionPane.YES_NO_OPTION:
226 // UserDefinedColoursSave -- relevant? TODO
229 case JvOptionPane.OK_CANCEL_OPTION:
231 // EditNameDialog --- uses panel for messsage TODO
233 // Desktop.inputURLMenuItem
235 return JOptionPane.showConfirmDialog(parentComponent, message, title,
242 * @param parentComponent
249 public static int showInternalConfirmDialog(Component parentComponent,
250 Object message, String title, int optionType, int messageType)
252 if (!isInteractiveMode())
254 return (int) getMockResponse();
258 case JvOptionPane.YES_NO_CANCEL_OPTION:
259 case JvOptionPane.YES_NO_OPTION:
260 // UserQuestionanaireCheck
264 case JvOptionPane.OK_CANCEL_OPTION:
265 // will fall back to simple HTML
266 return JOptionPane.showConfirmDialog(parentComponent, message, title,
267 optionType, messageType);
272 * adds icon; no longer internal
274 * @param parentComponent
282 public static int showInternalConfirmDialog(Component parentComponent,
283 Object message, String title, int optionType, int messageType,
286 if (!isInteractiveMode())
288 return (int) getMockResponse();
292 case JvOptionPane.YES_NO_CANCEL_OPTION:
293 case JvOptionPane.YES_NO_OPTION:
296 case JvOptionPane.OK_CANCEL_OPTION:
297 // Preferences editLink/newLink
298 return JOptionPane.showConfirmDialog(parentComponent, message, title,
299 optionType, messageType, icon);
305 * custom options full-featured
307 * @param parentComponent
314 * @param initialValue
316 * @throws HeadlessException
318 public static int showOptionDialog(Component parentComponent,
319 String message, String title, int optionType, int messageType,
320 Icon icon, Object[] options, Object initialValue)
321 throws HeadlessException
323 if (!isInteractiveMode())
325 return (int) getMockResponse();
331 // 1) AlignViewport for openLinkedAlignment
333 // Show a dialog with the option to open and link (cDNA <-> protein) as a
335 // alignment, either as a standalone alignment or in a split frame. Returns
336 // true if the new alignment was opened, false if not, because the user
337 // declined the offer.
339 // 2) UserDefinedColors warning about saving over a name already defined
341 return JOptionPane.showOptionDialog(parentComponent, message, title,
342 optionType, messageType, icon, options, initialValue);
349 * @throws HeadlessException
351 public static void showMessageDialog(Component parentComponent,
352 String message) throws HeadlessException
354 if (!isInteractiveMode())
356 outputMessage(message);
362 JOptionPane.showMessageDialog(parentComponent, message);
366 * OK with message, title, and type
368 * @param parentComponent
372 * @throws HeadlessException
374 public static void showMessageDialog(Component parentComponent,
375 String message, String title, int messageType)
376 throws HeadlessException
378 // 30 implementations -- all just fine.
380 if (!isInteractiveMode())
382 outputMessage(message);
386 JOptionPane.showMessageDialog(parentComponent,
387 getPrefix(messageType) + message, title, messageType);
391 * adds title and icon
393 * @param parentComponent
398 * @throws HeadlessException
400 public static void showMessageDialog(Component parentComponent,
401 String message, String title, int messageType, Icon icon)
402 throws HeadlessException
407 if (!isInteractiveMode())
409 outputMessage(message);
413 JOptionPane.showMessageDialog(parentComponent, message, title,
421 public static void showInternalMessageDialog(Component parentComponent,
425 // WsPreferences only
427 if (!isInteractiveMode())
429 outputMessage(message);
433 JOptionPane.showMessageDialog(parentComponent, message);
437 * Adds title and messageType
439 * @param parentComponent
444 public static void showInternalMessageDialog(Component parentComponent,
445 String message, String title, int messageType)
450 if (!isInteractiveMode())
452 outputMessage(message);
456 JOptionPane.showMessageDialog(parentComponent,
457 getPrefix(messageType) + message, title, messageType);
462 * @param parentComponent
468 public static void showInternalMessageDialog(Component parentComponent,
469 Object message, String title, int messageType, Icon icon)
474 if (!isInteractiveMode())
476 outputMessage(message);
480 JOptionPane.showMessageDialog(parentComponent, message, title,
488 * @throws HeadlessException
490 public static String showInputDialog(Object message)
491 throws HeadlessException
495 if (!isInteractiveMode())
497 return getMockResponse().toString();
500 return JOptionPane.showInputDialog(message);
504 * adds inital selection value
507 * @param initialSelectionValue
510 public static String showInputDialog(String message,
511 String initialSelectionValue)
513 if (!isInteractiveMode())
515 return getMockResponse().toString();
518 // AnnotationPanel character option
520 return JOptionPane.showInputDialog(message, initialSelectionValue);
524 * adds inital selection value
527 * @param initialSelectionValue
530 public static String showInputDialog(Object message,
531 Object initialSelectionValue)
533 if (!isInteractiveMode())
535 return getMockResponse().toString();
538 // AnnotationPanel character option
540 return JOptionPane.showInputDialog(message, initialSelectionValue);
546 * @param parentComponent
549 * @throws HeadlessException
551 public static String showInputDialog(Component parentComponent,
552 String message) throws HeadlessException
556 return isInteractiveMode()
557 ? JOptionPane.showInputDialog(parentComponent, message)
558 : getMockResponse().toString();
562 * input with initial selection
564 * @param parentComponent
566 * @param initialSelectionValue
569 public static String showInputDialog(Component parentComponent,
570 String message, String initialSelectionValue)
575 return isInteractiveMode()
576 ? JOptionPane.showInputDialog(parentComponent, message,
577 initialSelectionValue)
578 : getMockResponse().toString();
582 * input with initial selection
584 * @param parentComponent
586 * @param initialSelectionValue
589 public static String showInputDialog(Component parentComponent,
590 Object message, Object initialSelectionValue)
595 return isInteractiveMode()
596 ? JOptionPane.showInputDialog(parentComponent, message,
597 initialSelectionValue)
598 : getMockResponse().toString();
603 * @param parentComponent
608 * @throws HeadlessException
610 public static String showInputDialog(Component parentComponent,
611 String message, String title, int messageType)
612 throws HeadlessException
617 return isInteractiveMode()
618 ? JOptionPane.showInputDialog(parentComponent, message, title,
620 : getMockResponse().toString();
624 * Customized input option
626 * @param parentComponent
631 * @param selectionValues
632 * @param initialSelectionValue
634 * @throws HeadlessException
636 public static Object showInputDialog(Component parentComponent,
637 Object message, String title, int messageType, Icon icon,
638 Object[] selectionValues, Object initialSelectionValue)
639 throws HeadlessException
644 return isInteractiveMode()
645 ? JOptionPane.showInputDialog(parentComponent, message, title,
646 messageType, icon, selectionValues,
647 initialSelectionValue)
648 : getMockResponse().toString();
654 * @param parentComponent
658 public static String showInternalInputDialog(Component parentComponent,
663 return isInteractiveMode()
664 ? JOptionPane.showInternalInputDialog(parentComponent, message)
665 : getMockResponse().toString();
669 * internal with title and messageType
671 * @param parentComponent
677 public static String showInternalInputDialog(Component parentComponent,
678 String message, String title, int messageType)
681 // AlignFrame tabbedPane_mousePressed
683 return isInteractiveMode()
684 ? JOptionPane.showInternalInputDialog(parentComponent,
685 getPrefix(messageType) + message, title, messageType)
686 : getMockResponse().toString();
690 * customized internal
692 * @param parentComponent
697 * @param selectionValues
698 * @param initialSelectionValue
701 public static Object showInternalInputDialog(Component parentComponent,
702 String message, String title, int messageType, Icon icon,
703 Object[] selectionValues, Object initialSelectionValue)
707 return isInteractiveMode()
708 ? JOptionPane.showInternalInputDialog(parentComponent, message,
709 title, messageType, icon, selectionValues,
710 initialSelectionValue)
711 : getMockResponse().toString();
714 ///////////// end of options ///////////////
716 private static void outputMessage(Object message)
718 System.out.println(">>> JOption Message : " + message.toString());
721 public static Object getMockResponse()
726 public static void setMockResponse(Object mockOption)
728 JvOptionPane.mockResponse = mockOption;
731 public static void resetMock()
733 setMockResponse(JvOptionPane.CANCEL_OPTION);
734 setInteractiveMode(true);
737 public static boolean isInteractiveMode()
739 return interactiveMode;
742 public static void setInteractiveMode(boolean interactive)
744 JvOptionPane.interactiveMode = interactive;
747 private static String getPrefix(int messageType)
756 case JvOptionPane.WARNING_MESSAGE:
757 prefix = "WARNING! ";
759 case JvOptionPane.ERROR_MESSAGE:
770 * create a new option dialog that can be used to register responses - along
771 * lines of showOptionDialog
776 * @param defaultOption
777 * @param plainMessage
783 public static JvOptionPane newOptionDialog()
785 return new JvOptionPane(null);
788 public static JvOptionPane newOptionDialog(Component parentComponent)
790 return new JvOptionPane(parentComponent);
793 public void showDialog(String message, String title, int optionType,
794 int messageType, Icon icon, Object[] options, Object initialValue)
796 showDialog(message, title, optionType, messageType, icon, options,
800 public void showDialog(Object message, String title, int optionType,
801 int messageType, Icon icon, Object[] options, Object initialValue,
804 showDialog(message, title, optionType, messageType, icon, options,
805 initialValue, modal, null);
808 public void showDialog(Object message, String title, int optionType,
809 int messageType, Icon icon, Object[] options, Object initialValue,
810 boolean modal, JButton[] buttons)
812 if (!isInteractiveMode())
814 handleResponse(getMockResponse());
821 // 1) AlignViewport for openLinkedAlignment
823 // Show a dialog with the option to open and link (cDNA <-> protein) as a
825 // alignment, either as a standalone alignment or in a split frame. Returns
826 // true if the new alignment was opened, false if not, because the user
827 // declined the offer.
829 // 2) UserDefinedColors warning about saving over a name already defined
832 ourOptions = Arrays.asList(options);
836 boolean useButtons = false;
837 Object initialValueButton = null;
838 NOTNULL: if (buttons != null)
840 if (buttons.length != options.length)
842 jalview.bin.Console.error(
843 "Supplied buttons array not the same length as supplied options array.");
846 int[] buttonActions = { JOptionPane.YES_OPTION,
847 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
848 for (int i = 0; i < options.length; i++)
850 Object o = options[i];
851 jalview.bin.Console.debug(
852 "Setting button " + i + " to '" + o.toString() + "'");
853 JButton jb = buttons[i];
855 if (o.equals(initialValue))
856 initialValueButton = jb;
858 int buttonAction = buttonActions[i];
859 Callable<Void> action = callbacks.get(buttonAction);
860 jb.setText((String) o);
861 jb.addActionListener(new ActionListener()
864 public void actionPerformed(ActionEvent e)
867 Object obj = e.getSource();
868 if (obj == null || !(obj instanceof Component))
870 jalview.bin.Console.debug(
871 "Could not find Component source of event object "
875 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
876 JOptionPane.class, (Component) obj);
877 if (joptionpaneObject == null
878 || !(joptionpaneObject instanceof JOptionPane))
880 jalview.bin.Console.debug(
881 "Could not find JOptionPane ancestor of event object "
885 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
886 joptionpane.setValue(buttonAction);
888 getExecutor().submit(action);
889 joptionpane.transferFocusBackward();
890 joptionpane.setVisible(false);
891 // put focus and raise parent window if possible, unless cancel or
893 boolean raiseParent = (parentComponent != null);
894 if (buttonAction == JOptionPane.CANCEL_OPTION)
896 if (optionType == JOptionPane.YES_NO_OPTION
897 && buttonAction == JOptionPane.NO_OPTION)
901 parentComponent.requestFocus();
902 if (parentComponent instanceof JInternalFrame)
904 JInternalFrame jif = (JInternalFrame) parentComponent;
909 else if (parentComponent instanceof Window)
911 Window w = (Window) parentComponent;
916 joptionpane.setVisible(false);
923 // use a JOptionPane as usual
924 int response = JOptionPane.showOptionDialog(parentComponent, message,
925 title, optionType, messageType, icon,
926 useButtons ? buttons : options,
927 useButtons ? initialValueButton : initialValue);
930 * In Java, the response is returned to this thread and handled here; (for
931 * Javascript, see propertyChange)
933 if (!Platform.isJS())
940 handleResponse(response);
946 * This is java similar to the swingjs handling, with the callbacks attached to
947 * the button press of the dialog. This means we can use a non-modal JDialog for
948 * the confirmation without blocking the GUI.
950 JOptionPane joptionpane = new JOptionPane();
951 // Make button options
952 int[] buttonActions = { JvOptionPane.YES_OPTION,
953 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
955 // we need the strings to make the buttons with actionEventListener
958 ArrayList<String> options_default = new ArrayList<>();
960 .add(UIManager.getString("OptionPane.yesButtonText"));
961 if (optionType == JvOptionPane.YES_NO_OPTION
962 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
965 .add(UIManager.getString("OptionPane.noButtonText"));
967 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
970 .add(UIManager.getString("OptionPane.cancelButtonText"));
972 options = options_default.toArray();
975 ArrayList<JButton> options_btns = new ArrayList<>();
976 Object initialValue_btn = null;
977 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
980 for (int i = 0; i < options.length && i < 3; i++)
982 Object o = options[i];
983 int buttonAction = buttonActions[i];
984 Callable<Void> action = callbacks.get(buttonAction);
985 JButton jb = new JButton();
986 jb.setText((String) o);
987 jb.addActionListener(new ActionListener()
991 public void actionPerformed(ActionEvent e)
993 joptionpane.setValue(buttonAction);
995 getExecutor().submit(action);
996 // joptionpane.transferFocusBackward();
997 joptionpane.transferFocusBackward();
998 joptionpane.setVisible(false);
999 // put focus and raise parent window if possible, unless cancel
1001 boolean raiseParent = (parentComponent != null);
1002 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1003 raiseParent = false;
1004 if (optionType == JvOptionPane.YES_NO_OPTION
1005 && buttonAction == JvOptionPane.NO_OPTION)
1006 raiseParent = false;
1009 parentComponent.requestFocus();
1010 if (parentComponent instanceof JInternalFrame)
1012 JInternalFrame jif = (JInternalFrame) parentComponent;
1017 else if (parentComponent instanceof Window)
1019 Window w = (Window) parentComponent;
1024 joptionpane.setVisible(false);
1027 options_btns.add(jb);
1028 if (o.equals(initialValue))
1029 initialValue_btn = jb;
1032 joptionpane.setMessage(message);
1033 joptionpane.setMessageType(messageType);
1034 joptionpane.setOptionType(optionType);
1035 joptionpane.setIcon(icon);
1036 joptionpane.setOptions(
1037 Platform.isJS() ? options : options_btns.toArray());
1038 joptionpane.setInitialValue(
1039 Platform.isJS() ? initialValue : initialValue_btn);
1041 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1042 dialog.setIconImages(ChannelProperties.getIconList());
1043 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1044 : ModalityType.MODELESS);
1045 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1046 dialog.setVisible(true);
1051 public void showInternalDialog(JPanel mainPanel, String title,
1052 int yesNoCancelOption, int questionMessage, Icon icon,
1053 Object[] options, String initresponse)
1055 if (!isInteractiveMode())
1057 handleResponse(getMockResponse());
1060 // need to set these separately so we can set the title bar icon later
1061 this.setOptionType(yesNoCancelOption);
1062 this.setMessageType(questionMessage);
1064 this.setInitialValue(initresponse);
1065 this.setOptions(options);
1066 this.setMessage(mainPanel);
1068 ourOptions = Arrays.asList(options);
1070 if (parentComponent != this
1071 && !(parentComponent == null && Desktop.instance == null))
1073 JInternalFrame jif = this.createInternalFrame(
1074 parentComponent != null ? parentComponent : Desktop.instance,
1076 jif.setFrameIcon(null);
1077 jif.addInternalFrameListener(new InternalFrameListener()
1080 public void internalFrameActivated(InternalFrameEvent arg0)
1085 public void internalFrameClosed(InternalFrameEvent arg0)
1087 JvOptionPane.this.internalDialogHandleResponse();
1091 public void internalFrameClosing(InternalFrameEvent arg0)
1096 public void internalFrameDeactivated(InternalFrameEvent arg0)
1101 public void internalFrameDeiconified(InternalFrameEvent arg0)
1106 public void internalFrameIconified(InternalFrameEvent arg0)
1111 public void internalFrameOpened(InternalFrameEvent arg0)
1115 jif.setVisible(true);
1121 JDialog dialog = this.createDialog(parentComponent, title);
1122 dialog.setIconImages(ChannelProperties.getIconList());
1123 dialog.setVisible(true); // blocking
1124 this.internalDialogHandleResponse();
1129 private void internalDialogHandleResponse()
1131 String responseString = (String) this.getValue();
1132 int response = ourOptions.indexOf(responseString);
1134 if (!Platform.isJS())
1141 handleResponse(response);
1146 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1147 * action) { callbacks.put(response, new Callable<Void>() {
1149 * @Override public Void call() { action.run(); return null; } }); return this;
1153 public JvOptionPane setResponseHandler(Object response,
1154 Callable<Void> action)
1156 callbacks.put(response, action);
1160 public ExecutorService getExecutor()
1162 if (executor == null)
1163 executor = Executors.newSingleThreadExecutor();
1167 public void setExecutor(ExecutorService es)
1172 public void setDialog(JDialog d)
1177 public JDialog getDialog()
1183 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1186 public static int showDialogOnTop(String label, String actionString,
1187 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1189 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1190 JOPTIONPANE_MESSAGETYPE);
1193 public static int showDialogOnTop(Component dialogParentComponent,
1194 String label, String actionString, int JOPTIONPANE_OPTION,
1195 int JOPTIONPANE_MESSAGETYPE)
1197 if (!isInteractiveMode())
1199 return (int) getMockResponse();
1201 // Ensure Jalview window is brought to front (primarily for Quit
1202 // confirmation window to be visible)
1204 // This method of raising the Jalview window is broken in java
1205 // jalviewDesktop.setVisible(true);
1206 // jalviewDesktop.toFront();
1208 // A better hack which works is to create a new JFrame parent with
1209 // setAlwaysOnTop(true)
1210 JFrame dialogParent = new JFrame();
1211 if (dialogParentComponent == null)
1213 dialogParent.setIconImages(ChannelProperties.getIconList());
1214 dialogParent.setAlwaysOnTop(true);
1217 int answer = JOptionPane.showConfirmDialog(
1218 dialogParentComponent == null ? dialogParent
1219 : dialogParentComponent,
1220 label, actionString, JOPTIONPANE_OPTION,
1221 JOPTIONPANE_MESSAGETYPE);
1223 if (dialogParentComponent == null)
1225 dialogParent.setAlwaysOnTop(false);
1226 dialogParent.dispose();
1232 public void showDialogOnTopAsync(String label, String actionString,
1233 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1234 Object[] options, Object initialValue, boolean modal)
1236 JFrame frame = new JFrame();
1237 frame.setIconImages(ChannelProperties.getIconList());
1238 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1239 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1242 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1243 String actionString, int JOPTIONPANE_OPTION,
1244 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1245 Object initialValue, boolean modal)
1247 showDialogOnTopAsync(dialogParent, label, actionString,
1248 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1249 initialValue, modal, null);
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, JButton[] buttons)
1257 if (!isInteractiveMode())
1259 handleResponse(getMockResponse());
1262 // Ensure Jalview window is brought to front (primarily for Quit
1263 // confirmation window to be visible)
1265 // This method of raising the Jalview window is broken in java
1266 // jalviewDesktop.setVisible(true);
1267 // jalviewDesktop.toFront();
1269 // A better hack which works is to create a new JFrame parent with
1270 // setAlwaysOnTop(true)
1271 dialogParent.setAlwaysOnTop(true);
1272 parentComponent = dialogParent;
1274 showDialog(label, actionString, JOPTIONPANE_OPTION,
1275 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1278 dialogParent.setAlwaysOnTop(false);
1279 dialogParent.dispose();
1283 * JalviewJS signals option selection by a property change event for the
1284 * option e.g. "OK". This methods responds to that by running the response
1285 * action that corresponds to that option.
1290 public void propertyChange(PropertyChangeEvent evt)
1292 Object newValue = evt.getNewValue();
1293 int ourOption = ourOptions.indexOf(newValue);
1296 handleResponse(ourOption);
1301 handleResponse(newValue);
1306 public void handleResponse(Object response)
1309 * this test is for NaN in Chrome
1311 if (response != null && !response.equals(response))
1315 Callable<Void> action = callbacks.get(response);
1320 getExecutor().submit(action).get();
1322 } catch (Exception e)
1324 e.printStackTrace();
1326 if (parentComponent != null)
1327 parentComponent.requestFocus();
1332 * Create a non-modal confirm dialog
1334 public JDialog createDialog(Component parentComponent, Object message,
1335 String title, int optionType, int messageType, Icon icon,
1336 Object[] options, Object initialValue, boolean modal)
1338 return createDialog(parentComponent, message, title, optionType,
1339 messageType, icon, options, initialValue, modal, null);
1342 public JDialog createDialog(Component parentComponent, Object message,
1343 String title, int optionType, int messageType, Icon icon,
1344 Object[] options, Object initialValue, boolean modal,
1347 if (!isInteractiveMode())
1349 handleResponse(getMockResponse());
1352 JButton[] optionsButtons = null;
1353 Object initialValueButton = null;
1354 JOptionPane joptionpane = new JOptionPane();
1355 // Make button options
1356 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1357 JOptionPane.CANCEL_OPTION };
1359 // we need the strings to make the buttons with actionEventListener
1360 if (options == null)
1362 ArrayList<String> options_default = new ArrayList<>();
1363 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1364 if (optionType == JOptionPane.YES_NO_OPTION
1365 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1367 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1369 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1372 .add(UIManager.getString("OptionPane.cancelButtonText"));
1374 options = options_default.toArray();
1376 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1379 if (((optionType == JOptionPane.YES_OPTION
1380 || optionType == JOptionPane.NO_OPTION
1381 || optionType == JOptionPane.CANCEL_OPTION
1382 || optionType == JOptionPane.OK_OPTION
1383 || optionType == JOptionPane.DEFAULT_OPTION)
1384 && options.length < 1)
1385 || ((optionType == JOptionPane.YES_NO_OPTION
1386 || optionType == JOptionPane.OK_CANCEL_OPTION)
1387 && options.length < 2)
1388 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1389 && options.length < 3))
1392 .debug("JvOptionPane: not enough options for dialog type");
1394 optionsButtons = new JButton[options.length];
1395 for (int i = 0; i < options.length && i < 3; i++)
1397 Object o = options[i];
1398 int buttonAction = buttonActions[i];
1399 Callable<Void> action = callbacks.get(buttonAction);
1401 if (buttons != null && buttons.length > i && buttons[i] != null)
1409 jb.setText((String) o);
1410 jb.addActionListener(new ActionListener()
1413 public void actionPerformed(ActionEvent e)
1415 joptionpane.setValue(buttonAction);
1417 getExecutor().submit(action);
1418 // joptionpane.transferFocusBackward();
1419 joptionpane.transferFocusBackward();
1420 joptionpane.setVisible(false);
1421 // put focus and raise parent window if possible, unless cancel
1423 boolean raiseParent = (parentComponent != null);
1424 if (buttonAction == JOptionPane.CANCEL_OPTION)
1425 raiseParent = false;
1426 if (optionType == JOptionPane.YES_NO_OPTION
1427 && buttonAction == JOptionPane.NO_OPTION)
1428 raiseParent = false;
1431 parentComponent.requestFocus();
1432 if (parentComponent instanceof JInternalFrame)
1434 JInternalFrame jif = (JInternalFrame) parentComponent;
1439 else if (parentComponent instanceof Window)
1441 Window w = (Window) parentComponent;
1446 joptionpane.setVisible(false);
1449 optionsButtons[i] = jb;
1450 if (o.equals(initialValue))
1451 initialValueButton = jb;
1454 joptionpane.setMessage(message);
1455 joptionpane.setMessageType(messageType);
1456 joptionpane.setOptionType(optionType);
1457 joptionpane.setIcon(icon);
1458 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1459 joptionpane.setInitialValue(
1460 Platform.isJS() ? initialValue : initialValueButton);
1462 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1463 dialog.setIconImages(ChannelProperties.getIconList());
1464 dialog.setModalityType(
1465 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1466 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1472 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1474 * returns true if button was found
1476 public static boolean clickButton(JFrame frame, int buttonType)
1483 * This helper method makes the JInternalFrame wait until it is notified by an
1484 * InternalFrameClosing event. This method also adds the given JOptionPane to
1485 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1489 * The JInternalFrame to make modal.
1491 private static void startModal(JInternalFrame f)
1493 // We need to add an additional glasspane-like component directly
1494 // below the frame, which intercepts all mouse events that are not
1495 // directed at the frame itself.
1496 JPanel modalInterceptor = new JPanel();
1497 modalInterceptor.setOpaque(false);
1498 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1499 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1500 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1501 modalInterceptor.addMouseListener(new MouseAdapter()
1504 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1507 lp.add(modalInterceptor);
1510 // We need to explicitly dispatch events when we are blocking the event
1512 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1515 while (!f.isClosed())
1517 if (EventQueue.isDispatchThread())
1519 // The getNextEventMethod() issues wait() when no
1520 // event is available, so we don't need do explicitly wait().
1521 AWTEvent ev = queue.getNextEvent();
1522 // This mimics EventQueue.dispatchEvent(). We can't use
1523 // EventQueue.dispatchEvent() directly, because it is
1524 // protected, unfortunately.
1525 if (ev instanceof ActiveEvent)
1526 ((ActiveEvent) ev).dispatch();
1527 else if (ev.getSource() instanceof Component)
1528 ((Component) ev.getSource()).dispatchEvent(ev);
1529 else if (ev.getSource() instanceof MenuComponent)
1530 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1531 // Other events are ignored as per spec in
1532 // EventQueue.dispatchEvent
1536 // Give other threads a chance to become active.
1540 } catch (InterruptedException ex)
1542 // If we get interrupted, then leave the modal state.
1545 // Clean up the modal interceptor.
1546 lp.remove(modalInterceptor);
1548 // Remove the internal frame from its parent, so it is no longer
1549 // lurking around and clogging memory.
1550 Container parent = f.getParent();
1556 public static JvOptionPane frameDialog(String message, String title,
1557 int messageType, String[] buttonsTextS, String defaultButtonS,
1558 Callable<Void>[] handlers, boolean modal)
1560 JFrame parent = new JFrame();
1561 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1562 final String[] buttonsText;
1563 final String defaultButton;
1564 if (buttonsTextS == null)
1566 String ok = MessageManager.getString("action.ok");
1567 buttonsText = new String[] { ok };
1572 buttonsText = buttonsTextS;
1573 defaultButton = defaultButtonS;
1575 JButton[] buttons = new JButton[buttonsText.length];
1576 for (int i = 0; i < buttonsText.length; i++)
1578 buttons[i] = new JButton();
1579 buttons[i].setText(buttonsText[i]);
1580 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1581 buttons[i].setEnabled(false);
1582 buttons[i].setVisible(false);
1585 int dialogType = -1;
1586 if (buttonsText.length == 1)
1588 dialogType = JOptionPane.OK_OPTION;
1590 else if (buttonsText.length == 2)
1592 dialogType = JOptionPane.YES_NO_OPTION;
1596 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1598 Callable<Void> nullCallable = () -> {
1601 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1602 (handlers != null && handlers.length > 0) ? handlers[0]
1604 if (dialogType == JOptionPane.YES_NO_OPTION
1605 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1607 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1608 (handlers != null && handlers.length > 1) ? handlers[1]
1611 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1613 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1614 (handlers != null && handlers.length > 2) ? handlers[2]
1618 final int dt = dialogType;
1619 jvop.getExecutor().execute(() -> {
1620 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1621 defaultButton, modal, buttons);