2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.AWTEvent;
24 import java.awt.ActiveEvent;
25 import java.awt.Component;
26 import java.awt.Container;
27 import java.awt.Dialog.ModalityType;
28 import java.awt.EventQueue;
29 import java.awt.HeadlessException;
30 import java.awt.MenuComponent;
31 import java.awt.Toolkit;
32 import java.awt.Window;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseMotionAdapter;
37 import java.beans.PropertyChangeEvent;
38 import java.beans.PropertyChangeListener;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.concurrent.ExecutorService;
45 import java.util.concurrent.Executors;
47 import javax.swing.Icon;
48 import javax.swing.JButton;
49 import javax.swing.JDialog;
50 import javax.swing.JFrame;
51 import javax.swing.JInternalFrame;
52 import javax.swing.JLayeredPane;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.SwingUtilities;
56 import javax.swing.UIManager;
57 import javax.swing.event.InternalFrameEvent;
58 import javax.swing.event.InternalFrameListener;
60 import jalview.bin.Console;
61 import jalview.util.ChannelProperties;
62 import jalview.util.MessageManager;
63 import jalview.util.Platform;
64 import jalview.util.dialogrunner.DialogRunnerI;
66 public class JvOptionPane extends JOptionPane
67 implements DialogRunnerI, PropertyChangeListener
69 private static final long serialVersionUID = -3019167117756785229L;
71 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
73 private static boolean interactiveMode = true;
75 public static final Runnable NULLCALLABLE = () -> {
78 private Component parentComponent;
80 private ExecutorService executor = Executors.newCachedThreadPool();
82 private JDialog dialog = null;
84 private Map<Object, Runnable> callbacks = new HashMap<>();
87 * JalviewJS reports user choice in the dialog as the selected option (text);
88 * this list allows conversion to index (int)
90 List<Object> ourOptions;
92 public JvOptionPane(final Component parent)
94 this.parentComponent = Platform.isJS() ? this : parent;
98 public static int showConfirmDialog(Component parentComponent,
99 Object message) throws HeadlessException
101 // only called by test
102 return isInteractiveMode()
103 ? JOptionPane.showConfirmDialog(parentComponent, message)
104 : (int) getMockResponse();
108 * Message, title, optionType
110 * @param parentComponent
115 * @throws HeadlessException
117 public static int showConfirmDialog(Component parentComponent,
118 Object message, String title, int optionType)
119 throws HeadlessException
121 if (!isInteractiveMode())
123 return (int) getMockResponse();
127 case JvOptionPane.YES_NO_CANCEL_OPTION:
128 // FeatureRenderer amendFeatures ?? TODO ??
133 case JvOptionPane.YES_NO_OPTION:
134 // PromptUserConfig usage stats
135 // for now treated as "OK CANCEL"
137 case JvOptionPane.OK_CANCEL_OPTION:
138 // will fall back to simple HTML
139 return JOptionPane.showConfirmDialog(parentComponent, message, title,
145 * Adds a message type. Fallback is to just add it in the beginning.
147 * @param parentComponent
153 * @throws HeadlessException
155 public static int showConfirmDialog(Component parentComponent,
156 Object message, String title, int optionType, int messageType)
157 throws HeadlessException
159 // JalviewServicesChanged
160 // PromptUserConfig raiseDialog
161 return isInteractiveMode()
162 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
163 optionType, messageType)
164 : (int) getMockResponse();
170 * @param parentComponent
177 * @throws HeadlessException
179 public static int showConfirmDialog(Component parentComponent,
180 Object message, String title, int optionType, int messageType,
181 Icon icon) throws HeadlessException
183 // JvOptionPaneTest only
184 return isInteractiveMode()
185 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
186 optionType, messageType, icon)
187 : (int) getMockResponse();
191 * Internal version "OK"
193 * @param parentComponent
197 public static int showInternalConfirmDialog(Component parentComponent,
200 // JvOptionPaneTest only;
201 return isInteractiveMode()
202 ? JOptionPane.showInternalConfirmDialog(parentComponent,
204 : (int) getMockResponse();
208 * Internal version -- changed to standard version for now
210 * @param parentComponent
216 public static int showInternalConfirmDialog(Component parentComponent,
217 String message, String title, int optionType)
219 if (!isInteractiveMode())
221 return (int) getMockResponse();
225 case JvOptionPane.YES_NO_CANCEL_OPTION:
226 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
227 case JvOptionPane.YES_NO_OPTION:
228 // UserDefinedColoursSave -- relevant? TODO
231 case JvOptionPane.OK_CANCEL_OPTION:
233 // EditNameDialog --- uses panel for messsage TODO
235 // Desktop.inputURLMenuItem
237 return JOptionPane.showConfirmDialog(parentComponent, message, title,
244 * @param parentComponent
251 public static int showInternalConfirmDialog(Component parentComponent,
252 Object message, String title, int optionType, int messageType)
254 if (!isInteractiveMode())
256 return (int) getMockResponse();
260 case JvOptionPane.YES_NO_CANCEL_OPTION:
261 case JvOptionPane.YES_NO_OPTION:
262 // UserQuestionanaireCheck
266 case JvOptionPane.OK_CANCEL_OPTION:
267 // will fall back to simple HTML
268 return JOptionPane.showConfirmDialog(parentComponent, message, title,
269 optionType, messageType);
274 * adds icon; no longer internal
276 * @param parentComponent
284 public static int showInternalConfirmDialog(Component parentComponent,
285 Object message, String title, int optionType, int messageType,
288 if (!isInteractiveMode())
290 return (int) getMockResponse();
294 case JvOptionPane.YES_NO_CANCEL_OPTION:
295 case JvOptionPane.YES_NO_OPTION:
298 case JvOptionPane.OK_CANCEL_OPTION:
299 // Preferences editLink/newLink
300 return JOptionPane.showConfirmDialog(parentComponent, message, title,
301 optionType, messageType, icon);
307 * custom options full-featured
309 * @param parentComponent
316 * @param initialValue
318 * @throws HeadlessException
320 public static int showOptionDialog(Component parentComponent,
321 String message, String title, int optionType, int messageType,
322 Icon icon, Object[] options, Object initialValue)
323 throws HeadlessException
325 if (!isInteractiveMode())
327 return (int) getMockResponse();
333 // 1) AlignViewport for openLinkedAlignment
335 // Show a dialog with the option to open and link (cDNA <-> protein) as a
337 // alignment, either as a standalone alignment or in a split frame. Returns
338 // true if the new alignment was opened, false if not, because the user
339 // declined the offer.
341 // 2) UserDefinedColors warning about saving over a name already defined
343 return JOptionPane.showOptionDialog(parentComponent, message, title,
344 optionType, messageType, icon, options, initialValue);
351 * @throws HeadlessException
353 public static void showMessageDialog(Component parentComponent,
354 String message) throws HeadlessException
356 if (!isInteractiveMode())
358 outputMessage(message);
364 JOptionPane.showMessageDialog(parentComponent, message);
368 * OK with message, title, and type
370 * @param parentComponent
374 * @throws HeadlessException
376 public static void showMessageDialog(Component parentComponent,
377 String message, String title, int messageType)
378 throws HeadlessException
380 // 30 implementations -- all just fine.
382 if (!isInteractiveMode())
384 outputMessage(message);
388 JOptionPane.showMessageDialog(parentComponent,
389 getPrefix(messageType) + message, title, messageType);
393 * adds title and icon
395 * @param parentComponent
400 * @throws HeadlessException
402 public static void showMessageDialog(Component parentComponent,
403 String message, String title, int messageType, Icon icon)
404 throws HeadlessException
409 if (!isInteractiveMode())
411 outputMessage(message);
415 JOptionPane.showMessageDialog(parentComponent, message, title,
423 public static void showInternalMessageDialog(Component parentComponent,
427 // WsPreferences only
429 if (!isInteractiveMode())
431 outputMessage(message);
435 JOptionPane.showMessageDialog(parentComponent, message);
439 * Adds title and messageType
441 * @param parentComponent
446 public static void showInternalMessageDialog(Component parentComponent,
447 String message, String title, int messageType)
452 if (!isInteractiveMode())
454 outputMessage(message);
458 JOptionPane.showMessageDialog(parentComponent,
459 getPrefix(messageType) + message, title, messageType);
464 * @param parentComponent
470 public static void showInternalMessageDialog(Component parentComponent,
471 Object message, String title, int messageType, Icon icon)
476 if (!isInteractiveMode())
478 outputMessage(message);
482 JOptionPane.showMessageDialog(parentComponent, message, title,
490 * @throws HeadlessException
492 public static String showInputDialog(Object message)
493 throws HeadlessException
497 if (!isInteractiveMode())
499 return getMockResponse().toString();
502 return JOptionPane.showInputDialog(message);
506 * adds inital selection value
509 * @param initialSelectionValue
512 public static String showInputDialog(String message,
513 String initialSelectionValue)
515 if (!isInteractiveMode())
517 return getMockResponse().toString();
520 // AnnotationPanel character option
522 return JOptionPane.showInputDialog(message, initialSelectionValue);
526 * adds inital selection value
529 * @param initialSelectionValue
532 public static String showInputDialog(Object message,
533 Object initialSelectionValue)
535 if (!isInteractiveMode())
537 return getMockResponse().toString();
540 // AnnotationPanel character option
542 return JOptionPane.showInputDialog(message, initialSelectionValue);
548 * @param parentComponent
551 * @throws HeadlessException
553 public static String showInputDialog(Component parentComponent,
554 String message) throws HeadlessException
558 return isInteractiveMode()
559 ? JOptionPane.showInputDialog(parentComponent, message)
560 : getMockResponse().toString();
564 * input with initial selection
566 * @param parentComponent
568 * @param initialSelectionValue
571 public static String showInputDialog(Component parentComponent,
572 String message, String initialSelectionValue)
577 return isInteractiveMode()
578 ? JOptionPane.showInputDialog(parentComponent, message,
579 initialSelectionValue)
580 : getMockResponse().toString();
584 * input with initial selection
586 * @param parentComponent
588 * @param initialSelectionValue
591 public static String showInputDialog(Component parentComponent,
592 Object message, Object initialSelectionValue)
597 return isInteractiveMode()
598 ? JOptionPane.showInputDialog(parentComponent, message,
599 initialSelectionValue)
600 : getMockResponse().toString();
605 * @param parentComponent
610 * @throws HeadlessException
612 public static String showInputDialog(Component parentComponent,
613 String message, String title, int messageType)
614 throws HeadlessException
619 return isInteractiveMode()
620 ? JOptionPane.showInputDialog(parentComponent, message, title,
622 : getMockResponse().toString();
626 * Customized input option
628 * @param parentComponent
633 * @param selectionValues
634 * @param initialSelectionValue
636 * @throws HeadlessException
638 public static Object showInputDialog(Component parentComponent,
639 Object message, String title, int messageType, Icon icon,
640 Object[] selectionValues, Object initialSelectionValue)
641 throws HeadlessException
646 return isInteractiveMode()
647 ? JOptionPane.showInputDialog(parentComponent, message, title,
648 messageType, icon, selectionValues,
649 initialSelectionValue)
650 : getMockResponse().toString();
656 * @param parentComponent
660 public static String showInternalInputDialog(Component parentComponent,
665 return isInteractiveMode()
666 ? JOptionPane.showInternalInputDialog(parentComponent, message)
667 : getMockResponse().toString();
671 * internal with title and messageType
673 * @param parentComponent
679 public static String showInternalInputDialog(Component parentComponent,
680 String message, String title, int messageType)
683 // AlignFrame tabbedPane_mousePressed
685 return isInteractiveMode()
686 ? JOptionPane.showInternalInputDialog(parentComponent,
687 getPrefix(messageType) + message, title, messageType)
688 : getMockResponse().toString();
692 * customized internal
694 * @param parentComponent
699 * @param selectionValues
700 * @param initialSelectionValue
703 public static Object showInternalInputDialog(Component parentComponent,
704 String message, String title, int messageType, Icon icon,
705 Object[] selectionValues, Object initialSelectionValue)
709 return isInteractiveMode()
710 ? JOptionPane.showInternalInputDialog(parentComponent, message,
711 title, messageType, icon, selectionValues,
712 initialSelectionValue)
713 : getMockResponse().toString();
716 ///////////// end of options ///////////////
718 private static void outputMessage(Object message)
720 System.out.println(">>> JOption Message : " + message.toString());
723 public static Object getMockResponse()
728 public static void setMockResponse(Object mockOption)
730 JvOptionPane.mockResponse = mockOption;
733 public static void resetMock()
735 setMockResponse(JvOptionPane.CANCEL_OPTION);
736 setInteractiveMode(true);
739 public static boolean isInteractiveMode()
741 return interactiveMode;
744 public static void setInteractiveMode(boolean interactive)
746 JvOptionPane.interactiveMode = interactive;
749 private static String getPrefix(int messageType)
758 case JvOptionPane.WARNING_MESSAGE:
759 prefix = "WARNING! ";
761 case JvOptionPane.ERROR_MESSAGE:
772 * create a new option dialog that can be used to register responses - along
773 * lines of showOptionDialog
778 * @param defaultOption
779 * @param plainMessage
785 public static JvOptionPane newOptionDialog()
787 return new JvOptionPane(null);
790 public static JvOptionPane newOptionDialog(Component parentComponent)
792 return new JvOptionPane(parentComponent);
795 public void showDialog(String message, String title, int optionType,
796 int messageType, Icon icon, Object[] options, Object initialValue)
798 showDialog(message, title, optionType, messageType, icon, options,
802 public void showDialog(Object message, String title, int optionType,
803 int messageType, Icon icon, Object[] options, Object initialValue,
806 showDialog(message, title, optionType, messageType, icon, options,
807 initialValue, modal, null);
810 public void showDialog(Object message, String title, int optionType,
811 int messageType, Icon icon, Object[] options, Object initialValue,
812 boolean modal, JButton[] buttons)
814 if (!isInteractiveMode())
816 handleResponse(getMockResponse());
823 // 1) AlignViewport for openLinkedAlignment
825 // Show a dialog with the option to open and link (cDNA <-> protein) as a
827 // alignment, either as a standalone alignment or in a split frame. Returns
828 // true if the new alignment was opened, false if not, because the user
829 // declined the offer.
831 // 2) UserDefinedColors warning about saving over a name already defined
834 ourOptions = Arrays.asList(options);
838 boolean useButtons = false;
839 Object initialValueButton = null;
840 NOTNULL: if (buttons != null)
842 if (buttons.length != options.length)
844 jalview.bin.Console.error(
845 "Supplied buttons array not the same length as supplied options array.");
848 int[] buttonActions = { JOptionPane.YES_OPTION,
849 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
850 for (int i = 0; i < options.length; i++)
852 Object o = options[i];
853 jalview.bin.Console.debug(
854 "Setting button " + i + " to '" + o.toString() + "'");
855 JButton jb = buttons[i];
857 if (o.equals(initialValue))
858 initialValueButton = jb;
860 int buttonAction = buttonActions[i];
861 Runnable action = callbacks.get(buttonAction);
862 jb.setText((String) o);
863 jb.addActionListener(new ActionListener()
866 public void actionPerformed(ActionEvent e)
869 Object obj = e.getSource();
870 if (obj == null || !(obj instanceof Component))
872 jalview.bin.Console.debug(
873 "Could not find Component source of event object "
877 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
878 JOptionPane.class, (Component) obj);
879 if (joptionpaneObject == null
880 || !(joptionpaneObject instanceof JOptionPane))
882 jalview.bin.Console.debug(
883 "Could not find JOptionPane ancestor of event object "
887 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
888 joptionpane.setValue(buttonAction);
890 new Thread(action).start();
891 joptionpane.transferFocusBackward();
892 joptionpane.setVisible(false);
893 // put focus and raise parent window if possible, unless cancel or
895 boolean raiseParent = (parentComponent != null);
896 if (buttonAction == JOptionPane.CANCEL_OPTION)
898 if (optionType == JOptionPane.YES_NO_OPTION
899 && buttonAction == JOptionPane.NO_OPTION)
903 parentComponent.requestFocus();
904 if (parentComponent instanceof JInternalFrame)
906 JInternalFrame jif = (JInternalFrame) parentComponent;
911 else if (parentComponent instanceof Window)
913 Window w = (Window) parentComponent;
918 joptionpane.setVisible(false);
925 // use a JOptionPane as usual
926 int response = JOptionPane.showOptionDialog(parentComponent, message,
927 title, optionType, messageType, icon,
928 useButtons ? buttons : options,
929 useButtons ? initialValueButton : initialValue);
932 * In Java, the response is returned to this thread and handled here; (for
933 * Javascript, see propertyChange)
935 if (!Platform.isJS())
942 handleResponse(response);
948 * This is java similar to the swingjs handling, with the callbacks attached to
949 * the button press of the dialog. This means we can use a non-modal JDialog for
950 * the confirmation without blocking the GUI.
952 JOptionPane joptionpane = new JOptionPane();
953 // Make button options
954 int[] buttonActions = { JvOptionPane.YES_OPTION,
955 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
957 // we need the strings to make the buttons with actionEventListener
960 ArrayList<String> options_default = new ArrayList<>();
962 .add(UIManager.getString("OptionPane.yesButtonText"));
963 if (optionType == JvOptionPane.YES_NO_OPTION
964 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
967 .add(UIManager.getString("OptionPane.noButtonText"));
969 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
972 .add(UIManager.getString("OptionPane.cancelButtonText"));
974 options = options_default.toArray();
977 ArrayList<JButton> options_btns = new ArrayList<>();
978 Object initialValue_btn = null;
979 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
982 for (int i = 0; i < options.length && i < 3; i++)
984 Object o = options[i];
985 int buttonAction = buttonActions[i];
986 Runnable action = callbacks.get(buttonAction);
987 JButton jb = new JButton();
988 jb.setText((String) o);
989 jb.addActionListener(new ActionListener()
993 public void actionPerformed(ActionEvent e)
995 joptionpane.setValue(buttonAction);
997 new Thread(action).start();
998 // joptionpane.transferFocusBackward();
999 joptionpane.transferFocusBackward();
1000 joptionpane.setVisible(false);
1001 // put focus and raise parent window if possible, unless cancel
1003 boolean raiseParent = (parentComponent != null);
1004 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1005 raiseParent = false;
1006 if (optionType == JvOptionPane.YES_NO_OPTION
1007 && buttonAction == JvOptionPane.NO_OPTION)
1008 raiseParent = false;
1011 parentComponent.requestFocus();
1012 if (parentComponent instanceof JInternalFrame)
1014 JInternalFrame jif = (JInternalFrame) parentComponent;
1019 else if (parentComponent instanceof Window)
1021 Window w = (Window) parentComponent;
1026 joptionpane.setVisible(false);
1029 options_btns.add(jb);
1030 if (o.equals(initialValue))
1031 initialValue_btn = jb;
1034 joptionpane.setMessage(message);
1035 joptionpane.setMessageType(messageType);
1036 joptionpane.setOptionType(optionType);
1037 joptionpane.setIcon(icon);
1038 joptionpane.setOptions(
1039 Platform.isJS() ? options : options_btns.toArray());
1040 joptionpane.setInitialValue(
1041 Platform.isJS() ? initialValue : initialValue_btn);
1043 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1044 dialog.setIconImages(ChannelProperties.getIconList());
1045 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1046 : ModalityType.MODELESS);
1047 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1048 dialog.setVisible(true);
1053 public void showInternalDialog(Object mainPanel, String title,
1054 int yesNoCancelOption, int questionMessage, Icon icon,
1055 Object[] options, String initresponse)
1057 if (!isInteractiveMode())
1059 handleResponse(getMockResponse());
1062 // need to set these separately so we can set the title bar icon later
1063 this.setOptionType(yesNoCancelOption);
1064 this.setMessageType(questionMessage);
1066 this.setInitialValue(initresponse);
1067 this.setOptions(options);
1068 this.setMessage(mainPanel);
1070 ourOptions = Arrays.asList(options);
1071 if (parentComponent != this
1072 && !(parentComponent == null && Desktop.instance == null))
1074 JInternalFrame jif = this.createInternalFrame(
1075 parentComponent != null ? parentComponent : Desktop.instance,
1077 jif.setFrameIcon(null);
1078 jif.addInternalFrameListener(new InternalFrameListener()
1081 public void internalFrameActivated(InternalFrameEvent arg0)
1086 public void internalFrameClosed(InternalFrameEvent arg0)
1088 JvOptionPane.this.internalDialogHandleResponse();
1092 public void internalFrameClosing(InternalFrameEvent arg0)
1097 public void internalFrameDeactivated(InternalFrameEvent arg0)
1102 public void internalFrameDeiconified(InternalFrameEvent arg0)
1107 public void internalFrameIconified(InternalFrameEvent arg0)
1112 public void internalFrameOpened(InternalFrameEvent arg0)
1116 jif.setVisible(true);
1122 JDialog dialog = this.createDialog(parentComponent, title);
1123 dialog.setIconImages(ChannelProperties.getIconList());
1124 dialog.setVisible(true); // blocking
1125 this.internalDialogHandleResponse();
1130 private void internalDialogHandleResponse()
1132 Object value = this.getValue();
1134 || (value instanceof Integer && (Integer) value == -1))
1138 String responseString = value.toString();
1139 int response = ourOptions.indexOf(responseString);
1141 if (!Platform.isJS())
1148 handleResponse(response);
1153 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1154 * action) { callbacks.put(response, new Callable<Void>() {
1156 * @Override public Void call() { action.run(); return null; } }); return this;
1160 public JvOptionPane setResponseHandler(Object response, Runnable action)
1164 action = NULLCALLABLE;
1166 callbacks.put(response, action);
1170 public void setDialog(JDialog d)
1175 public JDialog getDialog()
1181 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1184 public static int showDialogOnTop(String label, String actionString,
1185 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1187 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1188 JOPTIONPANE_MESSAGETYPE);
1191 public static int showDialogOnTop(Component dialogParentComponent,
1192 String label, String actionString, int JOPTIONPANE_OPTION,
1193 int JOPTIONPANE_MESSAGETYPE)
1195 if (!isInteractiveMode())
1197 return (int) getMockResponse();
1199 // Ensure Jalview window is brought to front (primarily for Quit
1200 // confirmation window to be visible)
1202 // This method of raising the Jalview window is broken in java
1203 // jalviewDesktop.setVisible(true);
1204 // jalviewDesktop.toFront();
1206 // A better hack which works is to create a new JFrame parent with
1207 // setAlwaysOnTop(true)
1208 JFrame dialogParent = new JFrame();
1209 if (dialogParentComponent == null)
1211 dialogParent.setIconImages(ChannelProperties.getIconList());
1212 dialogParent.setAlwaysOnTop(true);
1215 int answer = JOptionPane.showConfirmDialog(
1216 dialogParentComponent == null ? dialogParent
1217 : dialogParentComponent,
1218 label, actionString, JOPTIONPANE_OPTION,
1219 JOPTIONPANE_MESSAGETYPE);
1221 if (dialogParentComponent == null)
1223 dialogParent.setAlwaysOnTop(false);
1224 dialogParent.dispose();
1230 public void showDialogOnTopAsync(String label, String actionString,
1231 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1232 Object[] options, Object initialValue, boolean modal)
1234 JFrame frame = new JFrame();
1235 frame.setIconImages(ChannelProperties.getIconList());
1236 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1237 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1240 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1241 String actionString, int JOPTIONPANE_OPTION,
1242 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1243 Object initialValue, boolean modal)
1245 showDialogOnTopAsync(dialogParent, label, actionString,
1246 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1247 initialValue, modal, null);
1250 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1251 String actionString, int JOPTIONPANE_OPTION,
1252 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1253 Object initialValue, boolean modal, JButton[] buttons)
1255 if (!isInteractiveMode())
1257 handleResponse(getMockResponse());
1260 // Ensure Jalview window is brought to front (primarily for Quit
1261 // confirmation window to be visible)
1263 // This method of raising the Jalview window is broken in java
1264 // jalviewDesktop.setVisible(true);
1265 // jalviewDesktop.toFront();
1267 // A better hack which works is to create a new JFrame parent with
1268 // setAlwaysOnTop(true)
1269 dialogParent.setAlwaysOnTop(true);
1270 parentComponent = dialogParent;
1272 showDialog(label, actionString, JOPTIONPANE_OPTION,
1273 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1276 dialogParent.setAlwaysOnTop(false);
1277 dialogParent.dispose();
1281 * JalviewJS signals option selection by a property change event for the
1282 * option e.g. "OK". This methods responds to that by running the response
1283 * action that corresponds to that option.
1288 public void propertyChange(PropertyChangeEvent evt)
1290 Object newValue = evt.getNewValue();
1291 int ourOption = ourOptions.indexOf(newValue);
1294 handleResponse(ourOption);
1299 handleResponse(newValue);
1304 public void handleResponse(Object response)
1307 * this test is for NaN in Chrome
1309 if (response != null && !response.equals(response))
1313 Runnable action = callbacks.get(response);
1318 new Thread(action).start();
1320 } catch (Exception e)
1322 e.printStackTrace();
1324 if (parentComponent != null)
1325 parentComponent.requestFocus();
1330 * Create a non-modal confirm dialog
1332 public JDialog createDialog(Component parentComponent, Object message,
1333 String title, int optionType, int messageType, Icon icon,
1334 Object[] options, Object initialValue, boolean modal)
1336 return createDialog(parentComponent, message, title, optionType,
1337 messageType, icon, options, initialValue, modal, null);
1340 public JDialog createDialog(Component parentComponent, Object message,
1341 String title, int optionType, int messageType, Icon icon,
1342 Object[] options, Object initialValue, boolean modal,
1345 if (!isInteractiveMode())
1347 handleResponse(getMockResponse());
1350 JButton[] optionsButtons = null;
1351 Object initialValueButton = null;
1352 JOptionPane joptionpane = new JOptionPane();
1353 // Make button options
1354 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1355 JOptionPane.CANCEL_OPTION };
1357 // we need the strings to make the buttons with actionEventListener
1358 if (options == null)
1360 ArrayList<String> options_default = new ArrayList<>();
1361 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1362 if (optionType == JOptionPane.YES_NO_OPTION
1363 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1365 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1367 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1370 .add(UIManager.getString("OptionPane.cancelButtonText"));
1372 options = options_default.toArray();
1374 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1377 if (((optionType == JOptionPane.YES_OPTION
1378 || optionType == JOptionPane.NO_OPTION
1379 || optionType == JOptionPane.CANCEL_OPTION
1380 || optionType == JOptionPane.OK_OPTION
1381 || optionType == JOptionPane.DEFAULT_OPTION)
1382 && options.length < 1)
1383 || ((optionType == JOptionPane.YES_NO_OPTION
1384 || optionType == JOptionPane.OK_CANCEL_OPTION)
1385 && options.length < 2)
1386 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1387 && options.length < 3))
1390 .debug("JvOptionPane: not enough options for dialog type");
1392 optionsButtons = new JButton[options.length];
1393 for (int i = 0; i < options.length && i < 3; i++)
1395 Object o = options[i];
1396 int buttonAction = buttonActions[i];
1397 Runnable action = callbacks.get(buttonAction);
1399 if (buttons != null && buttons.length > i && buttons[i] != null)
1407 jb.setText((String) o);
1408 jb.addActionListener(new ActionListener()
1411 public void actionPerformed(ActionEvent e)
1413 joptionpane.setValue(buttonAction);
1415 new Thread(action).start();
1416 // joptionpane.transferFocusBackward();
1417 joptionpane.transferFocusBackward();
1418 joptionpane.setVisible(false);
1419 // put focus and raise parent window if possible, unless cancel
1421 boolean raiseParent = (parentComponent != null);
1422 if (buttonAction == JOptionPane.CANCEL_OPTION)
1423 raiseParent = false;
1424 if (optionType == JOptionPane.YES_NO_OPTION
1425 && buttonAction == JOptionPane.NO_OPTION)
1426 raiseParent = false;
1429 parentComponent.requestFocus();
1430 if (parentComponent instanceof JInternalFrame)
1432 JInternalFrame jif = (JInternalFrame) parentComponent;
1437 else if (parentComponent instanceof Window)
1439 Window w = (Window) parentComponent;
1444 joptionpane.setVisible(false);
1447 optionsButtons[i] = jb;
1448 if (o.equals(initialValue))
1449 initialValueButton = jb;
1452 joptionpane.setMessage(message);
1453 joptionpane.setMessageType(messageType);
1454 joptionpane.setOptionType(optionType);
1455 joptionpane.setIcon(icon);
1456 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1457 joptionpane.setInitialValue(
1458 Platform.isJS() ? initialValue : initialValueButton);
1460 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1461 dialog.setIconImages(ChannelProperties.getIconList());
1462 dialog.setModalityType(
1463 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1464 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1470 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1472 * returns true if button was found
1474 public static boolean clickButton(JFrame frame, int buttonType)
1481 * This helper method makes the JInternalFrame wait until it is notified by an
1482 * InternalFrameClosing event. This method also adds the given JOptionPane to
1483 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1487 * The JInternalFrame to make modal.
1489 private static void startModal(JInternalFrame f)
1491 // We need to add an additional glasspane-like component directly
1492 // below the frame, which intercepts all mouse events that are not
1493 // directed at the frame itself.
1494 JPanel modalInterceptor = new JPanel();
1495 modalInterceptor.setOpaque(false);
1496 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1497 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1498 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1499 modalInterceptor.addMouseListener(new MouseAdapter()
1502 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1505 lp.add(modalInterceptor);
1508 // We need to explicitly dispatch events when we are blocking the event
1510 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1513 while (!f.isClosed())
1515 if (EventQueue.isDispatchThread())
1517 // The getNextEventMethod() issues wait() when no
1518 // event is available, so we don't need do explicitly wait().
1519 AWTEvent ev = queue.getNextEvent();
1520 // This mimics EventQueue.dispatchEvent(). We can't use
1521 // EventQueue.dispatchEvent() directly, because it is
1522 // protected, unfortunately.
1523 if (ev instanceof ActiveEvent)
1524 ((ActiveEvent) ev).dispatch();
1525 else if (ev.getSource() instanceof Component)
1526 ((Component) ev.getSource()).dispatchEvent(ev);
1527 else if (ev.getSource() instanceof MenuComponent)
1528 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1529 // Other events are ignored as per spec in
1530 // EventQueue.dispatchEvent
1534 // Give other threads a chance to become active.
1538 } catch (InterruptedException ex)
1540 // If we get interrupted, then leave the modal state.
1543 // Clean up the modal interceptor.
1544 lp.remove(modalInterceptor);
1546 // Remove the internal frame from its parent, so it is no longer
1547 // lurking around and clogging memory.
1548 Container parent = f.getParent();
1554 public static JvOptionPane frameDialog(Object message, String title,
1555 int messageType, String[] buttonsTextS, String defaultButtonS,
1556 List<Runnable> handlers, boolean modal)
1558 JFrame parent = new JFrame();
1559 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1560 final String[] buttonsText;
1561 final String defaultButton;
1562 if (buttonsTextS == null)
1564 String ok = MessageManager.getString("action.ok");
1565 buttonsText = new String[] { ok };
1570 buttonsText = buttonsTextS;
1571 defaultButton = defaultButtonS;
1573 JButton[] buttons = new JButton[buttonsText.length];
1574 for (int i = 0; i < buttonsText.length; i++)
1576 buttons[i] = new JButton();
1577 buttons[i].setText(buttonsText[i]);
1578 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1579 buttons[i].setEnabled(false);
1580 buttons[i].setVisible(false);
1583 int dialogType = -1;
1584 if (buttonsText.length == 1)
1586 dialogType = JOptionPane.OK_OPTION;
1588 else if (buttonsText.length == 2)
1590 dialogType = JOptionPane.YES_NO_OPTION;
1594 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1596 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1597 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1599 if (dialogType == JOptionPane.YES_NO_OPTION
1600 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1602 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1603 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1606 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1608 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1609 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1613 final int dt = dialogType;
1615 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1616 defaultButton, modal, buttons);