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<>();
91 private int timeout = -1;
93 public void setTimeout(int i)
99 * JalviewJS reports user choice in the dialog as the selected option (text);
100 * this list allows conversion to index (int)
102 List<Object> ourOptions;
104 public JvOptionPane(final Component parent)
106 this.parentComponent = Platform.isJS() ? this : parent;
110 public static int showConfirmDialog(Component parentComponent,
111 Object message) throws HeadlessException
113 // only called by test
114 return isInteractiveMode()
115 ? JOptionPane.showConfirmDialog(parentComponent, message)
116 : (int) getMockResponse();
120 * Message, title, optionType
122 * @param parentComponent
127 * @throws HeadlessException
129 public static int showConfirmDialog(Component parentComponent,
130 Object message, String title, int optionType)
131 throws HeadlessException
133 if (!isInteractiveMode())
135 return (int) getMockResponse();
139 case JvOptionPane.YES_NO_CANCEL_OPTION:
140 // FeatureRenderer amendFeatures ?? TODO ??
145 case JvOptionPane.YES_NO_OPTION:
146 // PromptUserConfig usage stats
147 // for now treated as "OK CANCEL"
149 case JvOptionPane.OK_CANCEL_OPTION:
150 // will fall back to simple HTML
151 return JOptionPane.showConfirmDialog(parentComponent, message, title,
157 * Adds a message type. Fallback is to just add it in the beginning.
159 * @param parentComponent
165 * @throws HeadlessException
167 public static int showConfirmDialog(Component parentComponent,
168 Object message, String title, int optionType, int messageType)
169 throws HeadlessException
171 // JalviewServicesChanged
172 // PromptUserConfig raiseDialog
173 return isInteractiveMode()
174 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
175 optionType, messageType)
176 : (int) getMockResponse();
182 * @param parentComponent
189 * @throws HeadlessException
191 public static int showConfirmDialog(Component parentComponent,
192 Object message, String title, int optionType, int messageType,
193 Icon icon) throws HeadlessException
195 // JvOptionPaneTest only
196 return isInteractiveMode()
197 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
198 optionType, messageType, icon)
199 : (int) getMockResponse();
203 * Internal version "OK"
205 * @param parentComponent
209 public static int showInternalConfirmDialog(Component parentComponent,
212 // JvOptionPaneTest only;
213 return isInteractiveMode()
214 ? JOptionPane.showInternalConfirmDialog(parentComponent,
216 : (int) getMockResponse();
220 * Internal version -- changed to standard version for now
222 * @param parentComponent
228 public static int showInternalConfirmDialog(Component parentComponent,
229 String message, String title, int optionType)
231 if (!isInteractiveMode())
233 return (int) getMockResponse();
237 case JvOptionPane.YES_NO_CANCEL_OPTION:
238 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
239 case JvOptionPane.YES_NO_OPTION:
240 // UserDefinedColoursSave -- relevant? TODO
243 case JvOptionPane.OK_CANCEL_OPTION:
245 // EditNameDialog --- uses panel for messsage TODO
247 // Desktop.inputURLMenuItem
249 return JOptionPane.showConfirmDialog(parentComponent, message, title,
256 * @param parentComponent
263 public static int showInternalConfirmDialog(Component parentComponent,
264 Object message, String title, int optionType, int messageType)
266 if (!isInteractiveMode())
268 return (int) getMockResponse();
272 case JvOptionPane.YES_NO_CANCEL_OPTION:
273 case JvOptionPane.YES_NO_OPTION:
274 // UserQuestionanaireCheck
278 case JvOptionPane.OK_CANCEL_OPTION:
279 // will fall back to simple HTML
280 return JOptionPane.showConfirmDialog(parentComponent, message, title,
281 optionType, messageType);
286 * adds icon; no longer internal
288 * @param parentComponent
296 public static int showInternalConfirmDialog(Component parentComponent,
297 Object message, String title, int optionType, int messageType,
300 if (!isInteractiveMode())
302 return (int) getMockResponse();
306 case JvOptionPane.YES_NO_CANCEL_OPTION:
307 case JvOptionPane.YES_NO_OPTION:
310 case JvOptionPane.OK_CANCEL_OPTION:
311 // Preferences editLink/newLink
312 return JOptionPane.showConfirmDialog(parentComponent, message, title,
313 optionType, messageType, icon);
319 * custom options full-featured
321 * @param parentComponent
328 * @param initialValue
330 * @throws HeadlessException
332 public static int showOptionDialog(Component parentComponent,
333 String message, String title, int optionType, int messageType,
334 Icon icon, Object[] options, Object initialValue)
335 throws HeadlessException
337 if (!isInteractiveMode())
339 return (int) getMockResponse();
345 // 1) AlignViewport for openLinkedAlignment
347 // Show a dialog with the option to open and link (cDNA <-> protein) as a
349 // alignment, either as a standalone alignment or in a split frame. Returns
350 // true if the new alignment was opened, false if not, because the user
351 // declined the offer.
353 // 2) UserDefinedColors warning about saving over a name already defined
355 return JOptionPane.showOptionDialog(parentComponent, message, title,
356 optionType, messageType, icon, options, initialValue);
363 * @throws HeadlessException
365 public static void showMessageDialog(Component parentComponent,
366 String message) throws HeadlessException
368 if (!isInteractiveMode())
370 outputMessage(message);
376 JOptionPane.showMessageDialog(parentComponent, message);
380 * OK with message, title, and type
382 * @param parentComponent
386 * @throws HeadlessException
388 public static void showMessageDialog(Component parentComponent,
389 String message, String title, int messageType)
390 throws HeadlessException
392 // 30 implementations -- all just fine.
394 if (!isInteractiveMode())
396 outputMessage(message);
400 JOptionPane.showMessageDialog(parentComponent,
401 getPrefix(messageType) + message, title, messageType);
405 * adds title and icon
407 * @param parentComponent
412 * @throws HeadlessException
414 public static void showMessageDialog(Component parentComponent,
415 String message, String title, int messageType, Icon icon)
416 throws HeadlessException
421 if (!isInteractiveMode())
423 outputMessage(message);
427 JOptionPane.showMessageDialog(parentComponent, message, title,
435 public static void showInternalMessageDialog(Component parentComponent,
439 // WsPreferences only
441 if (!isInteractiveMode())
443 outputMessage(message);
447 JOptionPane.showMessageDialog(parentComponent, message);
451 * Adds title and messageType
453 * @param parentComponent
458 public static void showInternalMessageDialog(Component parentComponent,
459 String message, String title, int messageType)
464 if (!isInteractiveMode())
466 outputMessage(message);
470 JOptionPane.showMessageDialog(parentComponent,
471 getPrefix(messageType) + message, title, messageType);
476 * @param parentComponent
482 public static void showInternalMessageDialog(Component parentComponent,
483 Object message, String title, int messageType, Icon icon)
488 if (!isInteractiveMode())
490 outputMessage(message);
494 JOptionPane.showMessageDialog(parentComponent, message, title,
502 * @throws HeadlessException
504 public static String showInputDialog(Object message)
505 throws HeadlessException
509 if (!isInteractiveMode())
511 return getMockResponse().toString();
514 return JOptionPane.showInputDialog(message);
518 * adds inital selection value
521 * @param initialSelectionValue
524 public static String showInputDialog(String message,
525 String initialSelectionValue)
527 if (!isInteractiveMode())
529 return getMockResponse().toString();
532 // AnnotationPanel character option
534 return JOptionPane.showInputDialog(message, initialSelectionValue);
538 * adds inital selection value
541 * @param initialSelectionValue
544 public static String showInputDialog(Object message,
545 Object initialSelectionValue)
547 if (!isInteractiveMode())
549 return getMockResponse().toString();
552 // AnnotationPanel character option
554 return JOptionPane.showInputDialog(message, initialSelectionValue);
560 * @param parentComponent
563 * @throws HeadlessException
565 public static String showInputDialog(Component parentComponent,
566 String message) throws HeadlessException
570 return isInteractiveMode()
571 ? JOptionPane.showInputDialog(parentComponent, message)
572 : getMockResponse().toString();
576 * input with initial selection
578 * @param parentComponent
580 * @param initialSelectionValue
583 public static String showInputDialog(Component parentComponent,
584 String message, String initialSelectionValue)
589 return isInteractiveMode()
590 ? JOptionPane.showInputDialog(parentComponent, message,
591 initialSelectionValue)
592 : getMockResponse().toString();
596 * input with initial selection
598 * @param parentComponent
600 * @param initialSelectionValue
603 public static String showInputDialog(Component parentComponent,
604 Object message, Object initialSelectionValue)
609 return isInteractiveMode()
610 ? JOptionPane.showInputDialog(parentComponent, message,
611 initialSelectionValue)
612 : getMockResponse().toString();
617 * @param parentComponent
622 * @throws HeadlessException
624 public static String showInputDialog(Component parentComponent,
625 String message, String title, int messageType)
626 throws HeadlessException
631 return isInteractiveMode()
632 ? JOptionPane.showInputDialog(parentComponent, message, title,
634 : getMockResponse().toString();
638 * Customized input option
640 * @param parentComponent
645 * @param selectionValues
646 * @param initialSelectionValue
648 * @throws HeadlessException
650 public static Object showInputDialog(Component parentComponent,
651 Object message, String title, int messageType, Icon icon,
652 Object[] selectionValues, Object initialSelectionValue)
653 throws HeadlessException
658 return isInteractiveMode()
659 ? JOptionPane.showInputDialog(parentComponent, message, title,
660 messageType, icon, selectionValues,
661 initialSelectionValue)
662 : getMockResponse().toString();
668 * @param parentComponent
672 public static String showInternalInputDialog(Component parentComponent,
677 return isInteractiveMode()
678 ? JOptionPane.showInternalInputDialog(parentComponent, message)
679 : getMockResponse().toString();
683 * internal with title and messageType
685 * @param parentComponent
691 public static String showInternalInputDialog(Component parentComponent,
692 String message, String title, int messageType)
695 // AlignFrame tabbedPane_mousePressed
697 return isInteractiveMode()
698 ? JOptionPane.showInternalInputDialog(parentComponent,
699 getPrefix(messageType) + message, title, messageType)
700 : getMockResponse().toString();
704 * customized internal
706 * @param parentComponent
711 * @param selectionValues
712 * @param initialSelectionValue
715 public static Object showInternalInputDialog(Component parentComponent,
716 String message, String title, int messageType, Icon icon,
717 Object[] selectionValues, Object initialSelectionValue)
721 return isInteractiveMode()
722 ? JOptionPane.showInternalInputDialog(parentComponent, message,
723 title, messageType, icon, selectionValues,
724 initialSelectionValue)
725 : getMockResponse().toString();
728 ///////////// end of options ///////////////
730 private static void outputMessage(Object message)
733 .outPrintln(">>> JOption Message : " + message.toString());
736 public static Object getMockResponse()
741 public static void setMockResponse(Object mockOption)
743 JvOptionPane.mockResponse = mockOption;
746 public static void resetMock()
748 setMockResponse(JvOptionPane.CANCEL_OPTION);
749 setInteractiveMode(true);
752 public static boolean isInteractiveMode()
754 return interactiveMode;
757 public static void setInteractiveMode(boolean interactive)
759 JvOptionPane.interactiveMode = interactive;
762 private static String getPrefix(int messageType)
771 case JvOptionPane.WARNING_MESSAGE:
772 prefix = "WARNING! ";
774 case JvOptionPane.ERROR_MESSAGE:
785 * create a new option dialog that can be used to register responses - along
786 * lines of showOptionDialog
791 * @param defaultOption
792 * @param plainMessage
798 public static JvOptionPane newOptionDialog()
800 return new JvOptionPane(null);
803 public static JvOptionPane newOptionDialog(Component parentComponent)
805 return new JvOptionPane(parentComponent);
808 public void showDialog(String message, String title, int optionType,
809 int messageType, Icon icon, Object[] options, Object initialValue)
811 showDialog(message, title, optionType, messageType, icon, options,
815 public void showDialog(Object message, String title, int optionType,
816 int messageType, Icon icon, Object[] options, Object initialValue,
819 showDialog(message, title, optionType, messageType, icon, options,
820 initialValue, modal, null);
823 public void showDialog(Object message, String title, int optionType,
824 int messageType, Icon icon, Object[] options, Object initialValue,
825 boolean modal, JButton[] buttons)
827 if (!isInteractiveMode())
829 handleResponse(getMockResponse());
836 // 1) AlignViewport for openLinkedAlignment
838 // Show a dialog with the option to open and link (cDNA <-> protein) as a
840 // alignment, either as a standalone alignment or in a split frame. Returns
841 // true if the new alignment was opened, false if not, because the user
842 // declined the offer.
844 // 2) UserDefinedColors warning about saving over a name already defined
847 ourOptions = Arrays.asList(options);
851 boolean useButtons = false;
852 Object initialValueButton = null;
853 NOTNULL: if (buttons != null)
855 if (buttons.length != options.length)
857 jalview.bin.Console.error(
858 "Supplied buttons array not the same length as supplied options array.");
862 // run through buttons for initialValue first so can set a final
864 Thread timeoutThread = null;
865 for (int i = 0; i < options.length; i++)
867 Object o = options[i];
868 JButton jb = buttons[i];
869 if (o.equals(initialValue))
871 initialValueButton = jb;
872 if (timeout > 0 && initialValueButton != null
873 && initialValueButton instanceof JButton)
875 Runnable timeoutClose = () -> {
878 Thread.sleep(timeout);
879 } catch (InterruptedException e)
882 "Dialog timeout interrupted. Probably a button pressed.");
886 timeoutThread = new Thread(timeoutClose);
890 final Thread timeoutThreadF = timeoutThread;
891 timeoutThreadF.start();
893 int[] buttonActions = { JOptionPane.YES_OPTION,
894 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
895 for (int i = 0; i < options.length; i++)
897 Object o = options[i];
898 jalview.bin.Console.debug(
899 "Setting button " + i + " to '" + o.toString() + "'");
900 JButton jb = buttons[i];
902 int buttonAction = buttonActions[i];
903 Runnable action = callbacks.get(buttonAction);
904 jb.setText((String) o);
905 jb.addActionListener(new ActionListener()
908 public void actionPerformed(ActionEvent e)
910 if (timeoutThreadF != null)
912 timeoutThreadF.interrupt();
915 Object obj = e.getSource();
916 if (obj == null || !(obj instanceof Component))
918 jalview.bin.Console.debug(
919 "Could not find Component source of event object "
923 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
924 JOptionPane.class, (Component) obj);
925 if (joptionpaneObject == null
926 || !(joptionpaneObject instanceof JOptionPane))
928 jalview.bin.Console.debug(
929 "Could not find JOptionPane ancestor of event object "
933 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
934 joptionpane.setValue(buttonAction);
936 new Thread(action).start();
937 joptionpane.transferFocusBackward();
938 joptionpane.setVisible(false);
939 // put focus and raise parent window if possible, unless cancel or
941 boolean raiseParent = (parentComponent != null);
942 if (buttonAction == JOptionPane.CANCEL_OPTION)
944 if (optionType == JOptionPane.YES_NO_OPTION
945 && buttonAction == JOptionPane.NO_OPTION)
949 parentComponent.requestFocus();
950 if (parentComponent instanceof JInternalFrame)
952 JInternalFrame jif = (JInternalFrame) parentComponent;
957 else if (parentComponent instanceof Window)
959 Window w = (Window) parentComponent;
964 joptionpane.setVisible(false);
971 // use a JOptionPane as usual
972 int response = JOptionPane.showOptionDialog(parentComponent, message,
973 title, optionType, messageType, icon,
974 useButtons ? buttons : options,
975 useButtons ? initialValueButton : initialValue);
978 * In Java, the response is returned to this thread and handled here; (for
979 * Javascript, see propertyChange)
981 if (!Platform.isJS())
988 handleResponse(response);
994 * This is java similar to the swingjs handling, with the callbacks attached to
995 * the button press of the dialog. This means we can use a non-modal JDialog for
996 * the confirmation without blocking the GUI.
998 JOptionPane joptionpane = new JOptionPane();
999 // Make button options
1000 int[] buttonActions = { JvOptionPane.YES_OPTION,
1001 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
1003 // we need the strings to make the buttons with actionEventListener
1004 if (options == null)
1006 ArrayList<String> options_default = new ArrayList<>();
1008 .add(UIManager.getString("OptionPane.yesButtonText"));
1009 if (optionType == JvOptionPane.YES_NO_OPTION
1010 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
1013 .add(UIManager.getString("OptionPane.noButtonText"));
1015 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
1018 .add(UIManager.getString("OptionPane.cancelButtonText"));
1020 options = options_default.toArray();
1023 ArrayList<JButton> options_btns = new ArrayList<>();
1024 Object initialValue_btn = null;
1025 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1028 for (int i = 0; i < options.length && i < 3; i++)
1030 Object o = options[i];
1031 int buttonAction = buttonActions[i];
1032 Runnable action = callbacks.get(buttonAction);
1033 JButton jb = new JButton();
1034 jb.setText((String) o);
1035 jb.addActionListener(new ActionListener()
1039 public void actionPerformed(ActionEvent e)
1041 joptionpane.setValue(buttonAction);
1043 new Thread(action).start();
1044 // joptionpane.transferFocusBackward();
1045 joptionpane.transferFocusBackward();
1046 joptionpane.setVisible(false);
1047 // put focus and raise parent window if possible, unless cancel
1049 boolean raiseParent = (parentComponent != null);
1050 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1051 raiseParent = false;
1052 if (optionType == JvOptionPane.YES_NO_OPTION
1053 && buttonAction == JvOptionPane.NO_OPTION)
1054 raiseParent = false;
1057 parentComponent.requestFocus();
1058 if (parentComponent instanceof JInternalFrame)
1060 JInternalFrame jif = (JInternalFrame) parentComponent;
1065 else if (parentComponent instanceof Window)
1067 Window w = (Window) parentComponent;
1072 joptionpane.setVisible(false);
1075 options_btns.add(jb);
1076 if (o.equals(initialValue))
1077 initialValue_btn = jb;
1080 joptionpane.setMessage(message);
1081 joptionpane.setMessageType(messageType);
1082 joptionpane.setOptionType(optionType);
1083 joptionpane.setIcon(icon);
1084 joptionpane.setOptions(
1085 Platform.isJS() ? options : options_btns.toArray());
1086 joptionpane.setInitialValue(
1087 Platform.isJS() ? initialValue : initialValue_btn);
1089 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1090 dialog.setIconImages(ChannelProperties.getIconList());
1091 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1092 : ModalityType.MODELESS);
1093 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1094 dialog.setVisible(true);
1099 public void showInternalDialog(Object mainPanel, String title,
1100 int yesNoCancelOption, int questionMessage, Icon icon,
1101 Object[] options, String initresponse)
1103 if (!isInteractiveMode())
1105 handleResponse(getMockResponse());
1108 // need to set these separately so we can set the title bar icon later
1109 this.setOptionType(yesNoCancelOption);
1110 this.setMessageType(questionMessage);
1112 this.setInitialValue(initresponse);
1113 this.setOptions(options);
1114 this.setMessage(mainPanel);
1116 ourOptions = Arrays.asList(options);
1117 if (parentComponent != this
1118 && !(parentComponent == null && Desktop.instance == null))
1120 // note the parent goes back to a JRootPane so is probably
1121 // Desktop.getDesktop()
1122 JInternalFrame jif = this.createInternalFrame(
1123 parentComponent != null ? parentComponent : Desktop.instance,
1125 // connect to the alignFrame using a map in Desktop
1126 if (parentComponent instanceof AlignFrame)
1128 Desktop.addModal((AlignFrame) parentComponent, jif);
1130 jif.setFrameIcon(null);
1131 jif.addInternalFrameListener(new InternalFrameListener()
1134 public void internalFrameActivated(InternalFrameEvent arg0)
1139 public void internalFrameClosed(InternalFrameEvent arg0)
1141 JvOptionPane.this.internalDialogHandleResponse();
1145 public void internalFrameClosing(InternalFrameEvent arg0)
1150 public void internalFrameDeactivated(InternalFrameEvent arg0)
1155 public void internalFrameDeiconified(InternalFrameEvent arg0)
1160 public void internalFrameIconified(InternalFrameEvent arg0)
1165 public void internalFrameOpened(InternalFrameEvent arg0)
1169 jif.setVisible(true);
1175 JDialog dialog = this.createDialog(parentComponent, title);
1176 dialog.setIconImages(ChannelProperties.getIconList());
1177 dialog.setVisible(true); // blocking
1178 this.internalDialogHandleResponse();
1183 private void internalDialogHandleResponse()
1185 Object value = this.getValue();
1187 || (value instanceof Integer && (Integer) value == -1))
1191 String responseString = value.toString();
1192 int response = ourOptions.indexOf(responseString);
1194 if (!Platform.isJS())
1201 handleResponse(response);
1206 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1207 * action) { callbacks.put(response, new Callable<Void>() {
1209 * @Override public Void call() { action.run(); return null; } }); return this;
1213 public JvOptionPane setResponseHandler(Object response, Runnable action)
1217 action = NULLCALLABLE;
1219 callbacks.put(response, action);
1223 public void setDialog(JDialog d)
1228 public JDialog getDialog()
1234 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1237 public static int showDialogOnTop(String label, String actionString,
1238 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1240 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1241 JOPTIONPANE_MESSAGETYPE);
1244 public static int showDialogOnTop(Component dialogParentComponent,
1245 String label, String actionString, int JOPTIONPANE_OPTION,
1246 int JOPTIONPANE_MESSAGETYPE)
1248 if (!isInteractiveMode())
1250 return (int) getMockResponse();
1252 // Ensure Jalview window is brought to front (primarily for Quit
1253 // confirmation window to be visible)
1255 // This method of raising the Jalview window is broken in java
1256 // jalviewDesktop.setVisible(true);
1257 // jalviewDesktop.toFront();
1259 // A better hack which works is to create a new JFrame parent with
1260 // setAlwaysOnTop(true)
1261 JFrame dialogParent = new JFrame();
1262 if (dialogParentComponent == null)
1264 dialogParent.setIconImages(ChannelProperties.getIconList());
1265 dialogParent.setAlwaysOnTop(true);
1268 int answer = JOptionPane.showConfirmDialog(
1269 dialogParentComponent == null ? dialogParent
1270 : dialogParentComponent,
1271 label, actionString, JOPTIONPANE_OPTION,
1272 JOPTIONPANE_MESSAGETYPE);
1274 if (dialogParentComponent == null)
1276 dialogParent.setAlwaysOnTop(false);
1277 dialogParent.dispose();
1283 public void showDialogOnTopAsync(String label, String actionString,
1284 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1285 Object[] options, Object initialValue, boolean modal)
1287 JFrame frame = new JFrame();
1288 frame.setIconImages(ChannelProperties.getIconList());
1289 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1290 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1293 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1294 String actionString, int JOPTIONPANE_OPTION,
1295 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1296 Object initialValue, boolean modal)
1298 showDialogOnTopAsync(dialogParent, label, actionString,
1299 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1300 initialValue, modal, null);
1303 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1304 String actionString, int JOPTIONPANE_OPTION,
1305 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1306 Object initialValue, boolean modal, JButton[] buttons)
1308 showDialogOnTopAsync(dialogParent, label, actionString,
1309 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1310 initialValue, modal, buttons, true);
1313 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1314 String actionString, int JOPTIONPANE_OPTION,
1315 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1316 Object initialValue, boolean modal, JButton[] buttons,
1319 if (!isInteractiveMode())
1321 handleResponse(getMockResponse());
1324 // Ensure Jalview window is brought to front (primarily for Quit
1325 // confirmation window to be visible)
1327 // This method of raising the Jalview window is broken in java
1328 // jalviewDesktop.setVisible(true);
1329 // jalviewDesktop.toFront();
1331 // A better hack which works is to create a new JFrame parent with
1332 // setAlwaysOnTop(true)
1333 dialogParent.setAlwaysOnTop(true);
1334 parentComponent = dialogParent;
1336 showDialog(label, actionString, JOPTIONPANE_OPTION,
1337 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1342 dialogParent.setAlwaysOnTop(false);
1343 dialogParent.dispose();
1348 * JalviewJS signals option selection by a property change event for the
1349 * option e.g. "OK". This methods responds to that by running the response
1350 * action that corresponds to that option.
1355 public void propertyChange(PropertyChangeEvent evt)
1357 Object newValue = evt.getNewValue();
1358 int ourOption = ourOptions.indexOf(newValue);
1361 handleResponse(ourOption);
1366 handleResponse(newValue);
1371 public void handleResponse(Object response)
1374 * this test is for NaN in Chrome
1376 if (response != null && !response.equals(response))
1380 Runnable action = callbacks.get(response);
1385 new Thread(action).start();
1387 } catch (Exception e)
1389 e.printStackTrace();
1391 if (parentComponent != null)
1392 parentComponent.requestFocus();
1397 * Create a non-modal confirm dialog
1399 public JDialog createDialog(Component parentComponent, Object message,
1400 String title, int optionType, int messageType, Icon icon,
1401 Object[] options, Object initialValue, boolean modal)
1403 return createDialog(parentComponent, message, title, optionType,
1404 messageType, icon, options, initialValue, modal, null);
1407 public JDialog createDialog(Component parentComponent, Object message,
1408 String title, int optionType, int messageType, Icon icon,
1409 Object[] options, Object initialValue, boolean modal,
1412 if (!isInteractiveMode())
1414 handleResponse(getMockResponse());
1417 JButton[] optionsButtons = null;
1418 Object initialValueButton = null;
1419 JOptionPane joptionpane = new JOptionPane();
1420 // Make button options
1421 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1422 JOptionPane.CANCEL_OPTION };
1424 // we need the strings to make the buttons with actionEventListener
1425 if (options == null)
1427 ArrayList<String> options_default = new ArrayList<>();
1428 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1429 if (optionType == JOptionPane.YES_NO_OPTION
1430 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1432 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1434 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1437 .add(UIManager.getString("OptionPane.cancelButtonText"));
1439 options = options_default.toArray();
1441 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1444 if (((optionType == JOptionPane.YES_OPTION
1445 || optionType == JOptionPane.NO_OPTION
1446 || optionType == JOptionPane.CANCEL_OPTION
1447 || optionType == JOptionPane.OK_OPTION
1448 || optionType == JOptionPane.DEFAULT_OPTION)
1449 && options.length < 1)
1450 || ((optionType == JOptionPane.YES_NO_OPTION
1451 || optionType == JOptionPane.OK_CANCEL_OPTION)
1452 && options.length < 2)
1453 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1454 && options.length < 3))
1457 .debug("JvOptionPane: not enough options for dialog type");
1459 optionsButtons = new JButton[options.length];
1460 for (int i = 0; i < options.length && i < 3; i++)
1462 Object o = options[i];
1463 int buttonAction = buttonActions[i];
1464 Runnable action = callbacks.get(buttonAction);
1466 if (buttons != null && buttons.length > i && buttons[i] != null)
1474 jb.setText((String) o);
1475 jb.addActionListener(new ActionListener()
1478 public void actionPerformed(ActionEvent e)
1480 joptionpane.setValue(buttonAction);
1482 new Thread(action).start();
1483 // joptionpane.transferFocusBackward();
1484 joptionpane.transferFocusBackward();
1485 joptionpane.setVisible(false);
1486 // put focus and raise parent window if possible, unless cancel
1488 boolean raiseParent = (parentComponent != null);
1489 if (buttonAction == JOptionPane.CANCEL_OPTION)
1490 raiseParent = false;
1491 if (optionType == JOptionPane.YES_NO_OPTION
1492 && buttonAction == JOptionPane.NO_OPTION)
1493 raiseParent = false;
1496 parentComponent.requestFocus();
1497 if (parentComponent instanceof JInternalFrame)
1499 JInternalFrame jif = (JInternalFrame) parentComponent;
1504 else if (parentComponent instanceof Window)
1506 Window w = (Window) parentComponent;
1511 joptionpane.setVisible(false);
1514 optionsButtons[i] = jb;
1515 if (o.equals(initialValue))
1516 initialValueButton = jb;
1519 joptionpane.setMessage(message);
1520 joptionpane.setMessageType(messageType);
1521 joptionpane.setOptionType(optionType);
1522 joptionpane.setIcon(icon);
1523 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1524 joptionpane.setInitialValue(
1525 Platform.isJS() ? initialValue : initialValueButton);
1527 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1528 dialog.setIconImages(ChannelProperties.getIconList());
1529 dialog.setModalityType(
1530 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1531 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1537 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1539 * returns true if button was found
1541 public static boolean clickButton(JFrame frame, int buttonType)
1548 * This helper method makes the JInternalFrame wait until it is notified by an
1549 * InternalFrameClosing event. This method also adds the given JOptionPane to
1550 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1554 * The JInternalFrame to make modal.
1556 private static void startModal(JInternalFrame f)
1558 // We need to add an additional glasspane-like component directly
1559 // below the frame, which intercepts all mouse events that are not
1560 // directed at the frame itself.
1561 JPanel modalInterceptor = new JPanel();
1562 modalInterceptor.setOpaque(false);
1563 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1564 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1565 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1566 modalInterceptor.addMouseListener(new MouseAdapter()
1569 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1572 lp.add(modalInterceptor);
1575 // disable the main menu bar if in Linux
1576 JMenuBar menubar = null;
1577 if (Platform.isLinux())
1579 JRootPane rootpane = Desktop.getDesktop().getRootPane();
1580 menubar = rootpane.getJMenuBar();
1583 // We need to explicitly dispatch events when we are blocking the event
1585 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1588 if (menubar != null)
1590 // don't allow clicks on main menu on linux due to a hanging bug.
1592 setMenusEnabled(menubar, false);
1595 while (!f.isClosed())
1597 if (EventQueue.isDispatchThread())
1599 // The getNextEventMethod() issues wait() when no
1600 // event is available, so we don't need do explicitly wait().
1601 AWTEvent ev = queue.getNextEvent();
1602 // This mimics EventQueue.dispatchEvent(). We can't use
1603 // EventQueue.dispatchEvent() directly, because it is
1604 // protected, unfortunately.
1605 if (ev instanceof ActiveEvent)
1607 ((ActiveEvent) ev).dispatch();
1609 else if (ev instanceof KeyEvent && ((KeyEvent) ev).isControlDown()
1612 // temporarily enable menus to send Ctrl+? KeyEvents
1613 setMenusEnabled(menubar, true);
1614 ((Component) ev.getSource()).dispatchEvent(ev);
1615 setMenusEnabled(menubar, false);
1617 else if (ev.getSource() instanceof MenuComponent)
1619 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1621 else if (ev.getSource() instanceof Component)
1623 ((Component) ev.getSource()).dispatchEvent(ev);
1625 // Other events are ignored as per spec in
1626 // EventQueue.dispatchEvent
1630 // Give other threads a chance to become active.
1634 } catch (InterruptedException ex)
1636 // If we get interrupted, then leave the modal state.
1639 // re-enable the main menu bar
1640 if (menubar != null)
1642 setMenusEnabled(menubar, true);
1645 // Clean up the modal interceptor.
1646 lp.remove(modalInterceptor);
1648 // unpaint the frame
1649 f.setVisible(false);
1655 } catch (PropertyVetoException e)
1657 f.doDefaultCloseAction();
1660 // Remove the internal frame from its parent, so it is no longer
1661 // lurking around and clogging memory.
1662 Container parent = f.getParent();
1670 public static JvOptionPane frameDialog(Object message, String title,
1671 int messageType, String[] buttonsTextS, String defaultButtonS,
1672 List<Runnable> handlers, boolean modal)
1674 JFrame parent = new JFrame();
1675 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1676 final String[] buttonsText;
1677 final String defaultButton;
1678 if (buttonsTextS == null)
1680 String ok = MessageManager.getString("action.ok");
1681 buttonsText = new String[] { ok };
1686 buttonsText = buttonsTextS;
1687 defaultButton = defaultButtonS;
1689 JButton[] buttons = new JButton[buttonsText.length];
1690 for (int i = 0; i < buttonsText.length; i++)
1692 buttons[i] = new JButton();
1693 buttons[i].setText(buttonsText[i]);
1694 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1695 buttons[i].setEnabled(false);
1696 buttons[i].setVisible(false);
1699 int dialogType = -1;
1700 if (buttonsText.length == 1)
1702 dialogType = JOptionPane.OK_OPTION;
1704 else if (buttonsText.length == 2)
1706 dialogType = JOptionPane.YES_NO_OPTION;
1710 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1712 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1713 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1715 if (dialogType == JOptionPane.YES_NO_OPTION
1716 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1718 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1719 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1722 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1724 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1725 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1729 final int dt = dialogType;
1731 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1732 defaultButton, modal, buttons);
1738 private static void setMenusEnabled(JMenuBar menubar, boolean b)
1740 for (int i = 0; i < menubar.getMenuCount(); i++)
1742 JMenu menu = menubar.getMenu(i);