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.beans.PropertyVetoException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.List;
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 public static final Runnable NULLCALLABLE = () -> {
79 private Component parentComponent;
81 private ExecutorService executor = Executors.newCachedThreadPool();
83 private JDialog dialog = null;
85 private Map<Object, Runnable> callbacks = new HashMap<>();
88 * JalviewJS reports user choice in the dialog as the selected option (text);
89 * this list allows conversion to index (int)
91 List<Object> ourOptions;
93 public JvOptionPane(final Component parent)
95 this.parentComponent = Platform.isJS() ? this : parent;
99 public static int showConfirmDialog(Component parentComponent,
100 Object message) throws HeadlessException
102 // only called by test
103 return isInteractiveMode()
104 ? JOptionPane.showConfirmDialog(parentComponent, message)
105 : (int) getMockResponse();
109 * Message, title, optionType
111 * @param parentComponent
116 * @throws HeadlessException
118 public static int showConfirmDialog(Component parentComponent,
119 Object message, String title, int optionType)
120 throws HeadlessException
122 if (!isInteractiveMode())
124 return (int) getMockResponse();
128 case JvOptionPane.YES_NO_CANCEL_OPTION:
129 // FeatureRenderer amendFeatures ?? TODO ??
134 case JvOptionPane.YES_NO_OPTION:
135 // PromptUserConfig usage stats
136 // for now treated as "OK CANCEL"
138 case JvOptionPane.OK_CANCEL_OPTION:
139 // will fall back to simple HTML
140 return JOptionPane.showConfirmDialog(parentComponent, message, title,
146 * Adds a message type. Fallback is to just add it in the beginning.
148 * @param parentComponent
154 * @throws HeadlessException
156 public static int showConfirmDialog(Component parentComponent,
157 Object message, String title, int optionType, int messageType)
158 throws HeadlessException
160 // JalviewServicesChanged
161 // PromptUserConfig raiseDialog
162 return isInteractiveMode()
163 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
164 optionType, messageType)
165 : (int) getMockResponse();
171 * @param parentComponent
178 * @throws HeadlessException
180 public static int showConfirmDialog(Component parentComponent,
181 Object message, String title, int optionType, int messageType,
182 Icon icon) throws HeadlessException
184 // JvOptionPaneTest only
185 return isInteractiveMode()
186 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
187 optionType, messageType, icon)
188 : (int) getMockResponse();
192 * Internal version "OK"
194 * @param parentComponent
198 public static int showInternalConfirmDialog(Component parentComponent,
201 // JvOptionPaneTest only;
202 return isInteractiveMode()
203 ? JOptionPane.showInternalConfirmDialog(parentComponent,
205 : (int) getMockResponse();
209 * Internal version -- changed to standard version for now
211 * @param parentComponent
217 public static int showInternalConfirmDialog(Component parentComponent,
218 String message, String title, int optionType)
220 if (!isInteractiveMode())
222 return (int) getMockResponse();
226 case JvOptionPane.YES_NO_CANCEL_OPTION:
227 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
228 case JvOptionPane.YES_NO_OPTION:
229 // UserDefinedColoursSave -- relevant? TODO
232 case JvOptionPane.OK_CANCEL_OPTION:
234 // EditNameDialog --- uses panel for messsage TODO
236 // Desktop.inputURLMenuItem
238 return JOptionPane.showConfirmDialog(parentComponent, message, title,
245 * @param parentComponent
252 public static int showInternalConfirmDialog(Component parentComponent,
253 Object message, String title, int optionType, int messageType)
255 if (!isInteractiveMode())
257 return (int) getMockResponse();
261 case JvOptionPane.YES_NO_CANCEL_OPTION:
262 case JvOptionPane.YES_NO_OPTION:
263 // UserQuestionanaireCheck
267 case JvOptionPane.OK_CANCEL_OPTION:
268 // will fall back to simple HTML
269 return JOptionPane.showConfirmDialog(parentComponent, message, title,
270 optionType, messageType);
275 * adds icon; no longer internal
277 * @param parentComponent
285 public static int showInternalConfirmDialog(Component parentComponent,
286 Object message, String title, int optionType, int messageType,
289 if (!isInteractiveMode())
291 return (int) getMockResponse();
295 case JvOptionPane.YES_NO_CANCEL_OPTION:
296 case JvOptionPane.YES_NO_OPTION:
299 case JvOptionPane.OK_CANCEL_OPTION:
300 // Preferences editLink/newLink
301 return JOptionPane.showConfirmDialog(parentComponent, message, title,
302 optionType, messageType, icon);
308 * custom options full-featured
310 * @param parentComponent
317 * @param initialValue
319 * @throws HeadlessException
321 public static int showOptionDialog(Component parentComponent,
322 String message, String title, int optionType, int messageType,
323 Icon icon, Object[] options, Object initialValue)
324 throws HeadlessException
326 if (!isInteractiveMode())
328 return (int) getMockResponse();
334 // 1) AlignViewport for openLinkedAlignment
336 // Show a dialog with the option to open and link (cDNA <-> protein) as a
338 // alignment, either as a standalone alignment or in a split frame. Returns
339 // true if the new alignment was opened, false if not, because the user
340 // declined the offer.
342 // 2) UserDefinedColors warning about saving over a name already defined
344 return JOptionPane.showOptionDialog(parentComponent, message, title,
345 optionType, messageType, icon, options, initialValue);
352 * @throws HeadlessException
354 public static void showMessageDialog(Component parentComponent,
355 String message) throws HeadlessException
357 if (!isInteractiveMode())
359 outputMessage(message);
365 JOptionPane.showMessageDialog(parentComponent, message);
369 * OK with message, title, and type
371 * @param parentComponent
375 * @throws HeadlessException
377 public static void showMessageDialog(Component parentComponent,
378 String message, String title, int messageType)
379 throws HeadlessException
381 // 30 implementations -- all just fine.
383 if (!isInteractiveMode())
385 outputMessage(message);
389 JOptionPane.showMessageDialog(parentComponent,
390 getPrefix(messageType) + message, title, messageType);
394 * adds title and icon
396 * @param parentComponent
401 * @throws HeadlessException
403 public static void showMessageDialog(Component parentComponent,
404 String message, String title, int messageType, Icon icon)
405 throws HeadlessException
410 if (!isInteractiveMode())
412 outputMessage(message);
416 JOptionPane.showMessageDialog(parentComponent, message, title,
424 public static void showInternalMessageDialog(Component parentComponent,
428 // WsPreferences only
430 if (!isInteractiveMode())
432 outputMessage(message);
436 JOptionPane.showMessageDialog(parentComponent, message);
440 * Adds title and messageType
442 * @param parentComponent
447 public static void showInternalMessageDialog(Component parentComponent,
448 String message, String title, int messageType)
453 if (!isInteractiveMode())
455 outputMessage(message);
459 JOptionPane.showMessageDialog(parentComponent,
460 getPrefix(messageType) + message, title, messageType);
465 * @param parentComponent
471 public static void showInternalMessageDialog(Component parentComponent,
472 Object message, String title, int messageType, Icon icon)
477 if (!isInteractiveMode())
479 outputMessage(message);
483 JOptionPane.showMessageDialog(parentComponent, message, title,
491 * @throws HeadlessException
493 public static String showInputDialog(Object message)
494 throws HeadlessException
498 if (!isInteractiveMode())
500 return getMockResponse().toString();
503 return JOptionPane.showInputDialog(message);
507 * adds inital selection value
510 * @param initialSelectionValue
513 public static String showInputDialog(String message,
514 String initialSelectionValue)
516 if (!isInteractiveMode())
518 return getMockResponse().toString();
521 // AnnotationPanel character option
523 return JOptionPane.showInputDialog(message, initialSelectionValue);
527 * adds inital selection value
530 * @param initialSelectionValue
533 public static String showInputDialog(Object message,
534 Object initialSelectionValue)
536 if (!isInteractiveMode())
538 return getMockResponse().toString();
541 // AnnotationPanel character option
543 return JOptionPane.showInputDialog(message, initialSelectionValue);
549 * @param parentComponent
552 * @throws HeadlessException
554 public static String showInputDialog(Component parentComponent,
555 String message) throws HeadlessException
559 return isInteractiveMode()
560 ? JOptionPane.showInputDialog(parentComponent, message)
561 : getMockResponse().toString();
565 * input with initial selection
567 * @param parentComponent
569 * @param initialSelectionValue
572 public static String showInputDialog(Component parentComponent,
573 String message, String initialSelectionValue)
578 return isInteractiveMode()
579 ? JOptionPane.showInputDialog(parentComponent, message,
580 initialSelectionValue)
581 : getMockResponse().toString();
585 * input with initial selection
587 * @param parentComponent
589 * @param initialSelectionValue
592 public static String showInputDialog(Component parentComponent,
593 Object message, Object initialSelectionValue)
598 return isInteractiveMode()
599 ? JOptionPane.showInputDialog(parentComponent, message,
600 initialSelectionValue)
601 : getMockResponse().toString();
606 * @param parentComponent
611 * @throws HeadlessException
613 public static String showInputDialog(Component parentComponent,
614 String message, String title, int messageType)
615 throws HeadlessException
620 return isInteractiveMode()
621 ? JOptionPane.showInputDialog(parentComponent, message, title,
623 : getMockResponse().toString();
627 * Customized input option
629 * @param parentComponent
634 * @param selectionValues
635 * @param initialSelectionValue
637 * @throws HeadlessException
639 public static Object showInputDialog(Component parentComponent,
640 Object message, String title, int messageType, Icon icon,
641 Object[] selectionValues, Object initialSelectionValue)
642 throws HeadlessException
647 return isInteractiveMode()
648 ? JOptionPane.showInputDialog(parentComponent, message, title,
649 messageType, icon, selectionValues,
650 initialSelectionValue)
651 : getMockResponse().toString();
657 * @param parentComponent
661 public static String showInternalInputDialog(Component parentComponent,
666 return isInteractiveMode()
667 ? JOptionPane.showInternalInputDialog(parentComponent, message)
668 : getMockResponse().toString();
672 * internal with title and messageType
674 * @param parentComponent
680 public static String showInternalInputDialog(Component parentComponent,
681 String message, String title, int messageType)
684 // AlignFrame tabbedPane_mousePressed
686 return isInteractiveMode()
687 ? JOptionPane.showInternalInputDialog(parentComponent,
688 getPrefix(messageType) + message, title, messageType)
689 : getMockResponse().toString();
693 * customized internal
695 * @param parentComponent
700 * @param selectionValues
701 * @param initialSelectionValue
704 public static Object showInternalInputDialog(Component parentComponent,
705 String message, String title, int messageType, Icon icon,
706 Object[] selectionValues, Object initialSelectionValue)
710 return isInteractiveMode()
711 ? JOptionPane.showInternalInputDialog(parentComponent, message,
712 title, messageType, icon, selectionValues,
713 initialSelectionValue)
714 : getMockResponse().toString();
717 ///////////// end of options ///////////////
719 private static void outputMessage(Object message)
721 System.out.println(">>> JOption Message : " + message.toString());
724 public static Object getMockResponse()
729 public static void setMockResponse(Object mockOption)
731 JvOptionPane.mockResponse = mockOption;
734 public static void resetMock()
736 setMockResponse(JvOptionPane.CANCEL_OPTION);
737 setInteractiveMode(true);
740 public static boolean isInteractiveMode()
742 return interactiveMode;
745 public static void setInteractiveMode(boolean interactive)
747 JvOptionPane.interactiveMode = interactive;
750 private static String getPrefix(int messageType)
759 case JvOptionPane.WARNING_MESSAGE:
760 prefix = "WARNING! ";
762 case JvOptionPane.ERROR_MESSAGE:
773 * create a new option dialog that can be used to register responses - along
774 * lines of showOptionDialog
779 * @param defaultOption
780 * @param plainMessage
786 public static JvOptionPane newOptionDialog()
788 return new JvOptionPane(null);
791 public static JvOptionPane newOptionDialog(Component parentComponent)
793 return new JvOptionPane(parentComponent);
796 public void showDialog(String message, String title, int optionType,
797 int messageType, Icon icon, Object[] options, Object initialValue)
799 showDialog(message, title, optionType, messageType, icon, options,
803 public void showDialog(Object message, String title, int optionType,
804 int messageType, Icon icon, Object[] options, Object initialValue,
807 showDialog(message, title, optionType, messageType, icon, options,
808 initialValue, modal, null);
811 public void showDialog(Object message, String title, int optionType,
812 int messageType, Icon icon, Object[] options, Object initialValue,
813 boolean modal, JButton[] buttons)
815 if (!isInteractiveMode())
817 handleResponse(getMockResponse());
824 // 1) AlignViewport for openLinkedAlignment
826 // Show a dialog with the option to open and link (cDNA <-> protein) as a
828 // alignment, either as a standalone alignment or in a split frame. Returns
829 // true if the new alignment was opened, false if not, because the user
830 // declined the offer.
832 // 2) UserDefinedColors warning about saving over a name already defined
835 ourOptions = Arrays.asList(options);
839 boolean useButtons = false;
840 Object initialValueButton = null;
841 NOTNULL: if (buttons != null)
843 if (buttons.length != options.length)
845 jalview.bin.Console.error(
846 "Supplied buttons array not the same length as supplied options array.");
849 int[] buttonActions = { JOptionPane.YES_OPTION,
850 JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
851 for (int i = 0; i < options.length; i++)
853 Object o = options[i];
854 jalview.bin.Console.debug(
855 "Setting button " + i + " to '" + o.toString() + "'");
856 JButton jb = buttons[i];
858 if (o.equals(initialValue))
859 initialValueButton = jb;
861 int buttonAction = buttonActions[i];
862 Runnable action = callbacks.get(buttonAction);
863 jb.setText((String) o);
864 jb.addActionListener(new ActionListener()
867 public void actionPerformed(ActionEvent e)
870 Object obj = e.getSource();
871 if (obj == null || !(obj instanceof Component))
873 jalview.bin.Console.debug(
874 "Could not find Component source of event object "
878 Object joptionpaneObject = SwingUtilities.getAncestorOfClass(
879 JOptionPane.class, (Component) obj);
880 if (joptionpaneObject == null
881 || !(joptionpaneObject instanceof JOptionPane))
883 jalview.bin.Console.debug(
884 "Could not find JOptionPane ancestor of event object "
888 JOptionPane joptionpane = (JOptionPane) joptionpaneObject;
889 joptionpane.setValue(buttonAction);
891 new Thread(action).start();
892 joptionpane.transferFocusBackward();
893 joptionpane.setVisible(false);
894 // put focus and raise parent window if possible, unless cancel or
896 boolean raiseParent = (parentComponent != null);
897 if (buttonAction == JOptionPane.CANCEL_OPTION)
899 if (optionType == JOptionPane.YES_NO_OPTION
900 && buttonAction == JOptionPane.NO_OPTION)
904 parentComponent.requestFocus();
905 if (parentComponent instanceof JInternalFrame)
907 JInternalFrame jif = (JInternalFrame) parentComponent;
912 else if (parentComponent instanceof Window)
914 Window w = (Window) parentComponent;
919 joptionpane.setVisible(false);
926 // use a JOptionPane as usual
927 int response = JOptionPane.showOptionDialog(parentComponent, message,
928 title, optionType, messageType, icon,
929 useButtons ? buttons : options,
930 useButtons ? initialValueButton : initialValue);
933 * In Java, the response is returned to this thread and handled here; (for
934 * Javascript, see propertyChange)
936 if (!Platform.isJS())
943 handleResponse(response);
949 * This is java similar to the swingjs handling, with the callbacks attached to
950 * the button press of the dialog. This means we can use a non-modal JDialog for
951 * the confirmation without blocking the GUI.
953 JOptionPane joptionpane = new JOptionPane();
954 // Make button options
955 int[] buttonActions = { JvOptionPane.YES_OPTION,
956 JvOptionPane.NO_OPTION, JvOptionPane.CANCEL_OPTION };
958 // we need the strings to make the buttons with actionEventListener
961 ArrayList<String> options_default = new ArrayList<>();
963 .add(UIManager.getString("OptionPane.yesButtonText"));
964 if (optionType == JvOptionPane.YES_NO_OPTION
965 || optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
968 .add(UIManager.getString("OptionPane.noButtonText"));
970 if (optionType == JvOptionPane.YES_NO_CANCEL_OPTION)
973 .add(UIManager.getString("OptionPane.cancelButtonText"));
975 options = options_default.toArray();
978 ArrayList<JButton> options_btns = new ArrayList<>();
979 Object initialValue_btn = null;
980 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
983 for (int i = 0; i < options.length && i < 3; i++)
985 Object o = options[i];
986 int buttonAction = buttonActions[i];
987 Runnable action = callbacks.get(buttonAction);
988 JButton jb = new JButton();
989 jb.setText((String) o);
990 jb.addActionListener(new ActionListener()
994 public void actionPerformed(ActionEvent e)
996 joptionpane.setValue(buttonAction);
998 new Thread(action).start();
999 // joptionpane.transferFocusBackward();
1000 joptionpane.transferFocusBackward();
1001 joptionpane.setVisible(false);
1002 // put focus and raise parent window if possible, unless cancel
1004 boolean raiseParent = (parentComponent != null);
1005 if (buttonAction == JvOptionPane.CANCEL_OPTION)
1006 raiseParent = false;
1007 if (optionType == JvOptionPane.YES_NO_OPTION
1008 && buttonAction == JvOptionPane.NO_OPTION)
1009 raiseParent = false;
1012 parentComponent.requestFocus();
1013 if (parentComponent instanceof JInternalFrame)
1015 JInternalFrame jif = (JInternalFrame) parentComponent;
1020 else if (parentComponent instanceof Window)
1022 Window w = (Window) parentComponent;
1027 joptionpane.setVisible(false);
1030 options_btns.add(jb);
1031 if (o.equals(initialValue))
1032 initialValue_btn = jb;
1035 joptionpane.setMessage(message);
1036 joptionpane.setMessageType(messageType);
1037 joptionpane.setOptionType(optionType);
1038 joptionpane.setIcon(icon);
1039 joptionpane.setOptions(
1040 Platform.isJS() ? options : options_btns.toArray());
1041 joptionpane.setInitialValue(
1042 Platform.isJS() ? initialValue : initialValue_btn);
1044 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1045 dialog.setIconImages(ChannelProperties.getIconList());
1046 dialog.setModalityType(modal ? ModalityType.APPLICATION_MODAL
1047 : ModalityType.MODELESS);
1048 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1049 dialog.setVisible(true);
1054 public void showInternalDialog(Object mainPanel, String title,
1055 int yesNoCancelOption, int questionMessage, Icon icon,
1056 Object[] options, String initresponse)
1058 if (!isInteractiveMode())
1060 handleResponse(getMockResponse());
1063 // need to set these separately so we can set the title bar icon later
1064 this.setOptionType(yesNoCancelOption);
1065 this.setMessageType(questionMessage);
1067 this.setInitialValue(initresponse);
1068 this.setOptions(options);
1069 this.setMessage(mainPanel);
1071 ourOptions = Arrays.asList(options);
1072 if (parentComponent != this
1073 && !(parentComponent == null && Desktop.instance == null))
1075 JInternalFrame jif = this.createInternalFrame(
1076 parentComponent != null ? parentComponent : Desktop.instance,
1078 jif.setFrameIcon(null);
1079 jif.addInternalFrameListener(new InternalFrameListener()
1082 public void internalFrameActivated(InternalFrameEvent arg0)
1084 System.err.println("##### internalFrameActivated");
1088 public void internalFrameClosed(InternalFrameEvent arg0)
1090 System.err.println("##### internalFrameClosed");
1091 JvOptionPane.this.internalDialogHandleResponse();
1095 public void internalFrameClosing(InternalFrameEvent arg0)
1097 System.err.println("##### internalFrameClosing");
1101 public void internalFrameDeactivated(InternalFrameEvent arg0)
1103 System.err.println("##### internalFrameDeactivated");
1107 public void internalFrameDeiconified(InternalFrameEvent arg0)
1109 System.err.println("##### internalFrameDeiconified");
1113 public void internalFrameIconified(InternalFrameEvent arg0)
1115 System.err.println("##### internalFrameIconified");
1119 public void internalFrameOpened(InternalFrameEvent arg0)
1121 System.err.println("##### internalFrameOpened");
1124 jif.setVisible(true);
1130 JDialog dialog = this.createDialog(parentComponent, title);
1131 dialog.setIconImages(ChannelProperties.getIconList());
1132 dialog.setVisible(true); // blocking
1133 this.internalDialogHandleResponse();
1138 private void internalDialogHandleResponse()
1140 String responseString = (String) this.getValue();
1141 int response = ourOptions.indexOf(responseString);
1143 if (!Platform.isJS())
1150 handleResponse(response);
1155 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1156 * action) { callbacks.put(response, new Callable<Void>() {
1158 * @Override public Void call() { action.run(); return null; } }); return this;
1162 public JvOptionPane setResponseHandler(Object response, Runnable action)
1166 action = NULLCALLABLE;
1168 callbacks.put(response, action);
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 Runnable action = callbacks.get(response);
1320 new Thread(action).start();
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 Runnable 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 new Thread(action).start();
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 boolean stillModal = true;
1516 while (!f.isClosed() && stillModal)
1518 if (EventQueue.isDispatchThread())
1520 // The getNextEventMethod() issues wait() when no
1521 // event is available, so we don't need do explicitly wait().
1522 AWTEvent ev = queue.getNextEvent();
1523 // This mimics EventQueue.dispatchEvent(). We can't use
1524 // EventQueue.dispatchEvent() directly, because it is
1525 // protected, unfortunately.
1527 "##### ev source=" + ev.getSource().getClass() + "");
1528 if (ev instanceof ActiveEvent)
1530 System.err.println("##### 1");
1531 ((ActiveEvent) ev).dispatch();
1533 else if (ev.getSource() instanceof MenuComponent)
1535 System.err.println("##### 2");
1536 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1538 else if (ev.getSource() instanceof Component)
1540 System.err.println("##### 3");
1541 if (ev.getSource().equals(Desktop.getDesktop().getRootPane()))
1547 ((Component) ev.getSource()).dispatchEvent(ev);
1550 // Other events are ignored as per spec in
1551 // EventQueue.dispatchEvent
1552 System.err.println("##### 4");
1556 // Give other threads a chance to become active.
1557 System.err.println("##### 5");
1561 } catch (InterruptedException ex)
1563 // If we get interrupted, then leave the modal state.
1564 System.err.println("##### 6");
1567 System.err.println("##### 7");
1568 // Clean up the modal interceptor.
1569 lp.remove(modalInterceptor);
1571 f.setVisible(false);
1576 } catch (PropertyVetoException e)
1578 f.doDefaultCloseAction();
1581 // Remove the internal frame from its parent, so it is no longer
1582 // lurking around and clogging memory.
1583 Container parent = f.getParent();
1591 public static JvOptionPane frameDialog(Object message, String title,
1592 int messageType, String[] buttonsTextS, String defaultButtonS,
1593 List<Runnable> handlers, boolean modal)
1595 JFrame parent = new JFrame();
1596 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1597 final String[] buttonsText;
1598 final String defaultButton;
1599 if (buttonsTextS == null)
1601 String ok = MessageManager.getString("action.ok");
1602 buttonsText = new String[] { ok };
1607 buttonsText = buttonsTextS;
1608 defaultButton = defaultButtonS;
1610 JButton[] buttons = new JButton[buttonsText.length];
1611 for (int i = 0; i < buttonsText.length; i++)
1613 buttons[i] = new JButton();
1614 buttons[i].setText(buttonsText[i]);
1615 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1616 buttons[i].setEnabled(false);
1617 buttons[i].setVisible(false);
1620 int dialogType = -1;
1621 if (buttonsText.length == 1)
1623 dialogType = JOptionPane.OK_OPTION;
1625 else if (buttonsText.length == 2)
1627 dialogType = JOptionPane.YES_NO_OPTION;
1631 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1633 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1634 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1636 if (dialogType == JOptionPane.YES_NO_OPTION
1637 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1639 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1640 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1643 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1645 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1646 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1650 final int dt = dialogType;
1652 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1653 defaultButton, modal, buttons);