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 jalview.bin.Console.outPrintln(">>> 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 Object value = this.getValue();
1142 || (value instanceof Integer && (Integer) value == -1))
1146 String responseString = value.toString();
1147 int response = ourOptions.indexOf(responseString);
1149 if (!Platform.isJS())
1156 handleResponse(response);
1161 * @Override public JvOptionPane setResponseHandler(Object response, Runnable
1162 * action) { callbacks.put(response, new Callable<Void>() {
1164 * @Override public Void call() { action.run(); return null; } }); return this;
1168 public JvOptionPane setResponseHandler(Object response, Runnable action)
1172 action = NULLCALLABLE;
1174 callbacks.put(response, action);
1178 public void setDialog(JDialog d)
1183 public JDialog getDialog()
1189 * showDialogOnTop will create a dialog that (attempts to) come to top of OS
1192 public static int showDialogOnTop(String label, String actionString,
1193 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
1195 return showDialogOnTop(null, label, actionString, JOPTIONPANE_OPTION,
1196 JOPTIONPANE_MESSAGETYPE);
1199 public static int showDialogOnTop(Component dialogParentComponent,
1200 String label, String actionString, int JOPTIONPANE_OPTION,
1201 int JOPTIONPANE_MESSAGETYPE)
1203 if (!isInteractiveMode())
1205 return (int) getMockResponse();
1207 // Ensure Jalview window is brought to front (primarily for Quit
1208 // confirmation window to be visible)
1210 // This method of raising the Jalview window is broken in java
1211 // jalviewDesktop.setVisible(true);
1212 // jalviewDesktop.toFront();
1214 // A better hack which works is to create a new JFrame parent with
1215 // setAlwaysOnTop(true)
1216 JFrame dialogParent = new JFrame();
1217 if (dialogParentComponent == null)
1219 dialogParent.setIconImages(ChannelProperties.getIconList());
1220 dialogParent.setAlwaysOnTop(true);
1223 int answer = JOptionPane.showConfirmDialog(
1224 dialogParentComponent == null ? dialogParent
1225 : dialogParentComponent,
1226 label, actionString, JOPTIONPANE_OPTION,
1227 JOPTIONPANE_MESSAGETYPE);
1229 if (dialogParentComponent == null)
1231 dialogParent.setAlwaysOnTop(false);
1232 dialogParent.dispose();
1238 public void showDialogOnTopAsync(String label, String actionString,
1239 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon,
1240 Object[] options, Object initialValue, boolean modal)
1242 JFrame frame = new JFrame();
1243 frame.setIconImages(ChannelProperties.getIconList());
1244 showDialogOnTopAsync(frame, label, actionString, JOPTIONPANE_OPTION,
1245 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal);
1248 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1249 String actionString, int JOPTIONPANE_OPTION,
1250 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1251 Object initialValue, boolean modal)
1253 showDialogOnTopAsync(dialogParent, label, actionString,
1254 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
1255 initialValue, modal, null);
1258 public void showDialogOnTopAsync(JFrame dialogParent, Object label,
1259 String actionString, int JOPTIONPANE_OPTION,
1260 int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
1261 Object initialValue, boolean modal, JButton[] buttons)
1263 if (!isInteractiveMode())
1265 handleResponse(getMockResponse());
1268 // Ensure Jalview window is brought to front (primarily for Quit
1269 // confirmation window to be visible)
1271 // This method of raising the Jalview window is broken in java
1272 // jalviewDesktop.setVisible(true);
1273 // jalviewDesktop.toFront();
1275 // A better hack which works is to create a new JFrame parent with
1276 // setAlwaysOnTop(true)
1277 dialogParent.setAlwaysOnTop(true);
1278 parentComponent = dialogParent;
1280 showDialog(label, actionString, JOPTIONPANE_OPTION,
1281 JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
1284 dialogParent.setAlwaysOnTop(false);
1285 dialogParent.dispose();
1289 * JalviewJS signals option selection by a property change event for the
1290 * option e.g. "OK". This methods responds to that by running the response
1291 * action that corresponds to that option.
1296 public void propertyChange(PropertyChangeEvent evt)
1298 Object newValue = evt.getNewValue();
1299 int ourOption = ourOptions.indexOf(newValue);
1302 handleResponse(ourOption);
1307 handleResponse(newValue);
1312 public void handleResponse(Object response)
1315 * this test is for NaN in Chrome
1317 if (response != null && !response.equals(response))
1321 Runnable action = callbacks.get(response);
1326 new Thread(action).start();
1328 } catch (Exception e)
1330 e.printStackTrace();
1332 if (parentComponent != null)
1333 parentComponent.requestFocus();
1338 * Create a non-modal confirm dialog
1340 public JDialog createDialog(Component parentComponent, Object message,
1341 String title, int optionType, int messageType, Icon icon,
1342 Object[] options, Object initialValue, boolean modal)
1344 return createDialog(parentComponent, message, title, optionType,
1345 messageType, icon, options, initialValue, modal, null);
1348 public JDialog createDialog(Component parentComponent, Object message,
1349 String title, int optionType, int messageType, Icon icon,
1350 Object[] options, Object initialValue, boolean modal,
1353 if (!isInteractiveMode())
1355 handleResponse(getMockResponse());
1358 JButton[] optionsButtons = null;
1359 Object initialValueButton = null;
1360 JOptionPane joptionpane = new JOptionPane();
1361 // Make button options
1362 int[] buttonActions = { JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
1363 JOptionPane.CANCEL_OPTION };
1365 // we need the strings to make the buttons with actionEventListener
1366 if (options == null)
1368 ArrayList<String> options_default = new ArrayList<>();
1369 options_default.add(UIManager.getString("OptionPane.yesButtonText"));
1370 if (optionType == JOptionPane.YES_NO_OPTION
1371 || optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1373 options_default.add(UIManager.getString("OptionPane.noButtonText"));
1375 if (optionType == JOptionPane.YES_NO_CANCEL_OPTION)
1378 .add(UIManager.getString("OptionPane.cancelButtonText"));
1380 options = options_default.toArray();
1382 if (!Platform.isJS()) // JalviewJS already uses callback, don't need to
1385 if (((optionType == JOptionPane.YES_OPTION
1386 || optionType == JOptionPane.NO_OPTION
1387 || optionType == JOptionPane.CANCEL_OPTION
1388 || optionType == JOptionPane.OK_OPTION
1389 || optionType == JOptionPane.DEFAULT_OPTION)
1390 && options.length < 1)
1391 || ((optionType == JOptionPane.YES_NO_OPTION
1392 || optionType == JOptionPane.OK_CANCEL_OPTION)
1393 && options.length < 2)
1394 || (optionType == JOptionPane.YES_NO_CANCEL_OPTION
1395 && options.length < 3))
1398 .debug("JvOptionPane: not enough options for dialog type");
1400 optionsButtons = new JButton[options.length];
1401 for (int i = 0; i < options.length && i < 3; i++)
1403 Object o = options[i];
1404 int buttonAction = buttonActions[i];
1405 Runnable action = callbacks.get(buttonAction);
1407 if (buttons != null && buttons.length > i && buttons[i] != null)
1415 jb.setText((String) o);
1416 jb.addActionListener(new ActionListener()
1419 public void actionPerformed(ActionEvent e)
1421 joptionpane.setValue(buttonAction);
1423 new Thread(action).start();
1424 // joptionpane.transferFocusBackward();
1425 joptionpane.transferFocusBackward();
1426 joptionpane.setVisible(false);
1427 // put focus and raise parent window if possible, unless cancel
1429 boolean raiseParent = (parentComponent != null);
1430 if (buttonAction == JOptionPane.CANCEL_OPTION)
1431 raiseParent = false;
1432 if (optionType == JOptionPane.YES_NO_OPTION
1433 && buttonAction == JOptionPane.NO_OPTION)
1434 raiseParent = false;
1437 parentComponent.requestFocus();
1438 if (parentComponent instanceof JInternalFrame)
1440 JInternalFrame jif = (JInternalFrame) parentComponent;
1445 else if (parentComponent instanceof Window)
1447 Window w = (Window) parentComponent;
1452 joptionpane.setVisible(false);
1455 optionsButtons[i] = jb;
1456 if (o.equals(initialValue))
1457 initialValueButton = jb;
1460 joptionpane.setMessage(message);
1461 joptionpane.setMessageType(messageType);
1462 joptionpane.setOptionType(optionType);
1463 joptionpane.setIcon(icon);
1464 joptionpane.setOptions(Platform.isJS() ? options : optionsButtons);
1465 joptionpane.setInitialValue(
1466 Platform.isJS() ? initialValue : initialValueButton);
1468 JDialog dialog = joptionpane.createDialog(parentComponent, title);
1469 dialog.setIconImages(ChannelProperties.getIconList());
1470 dialog.setModalityType(
1471 modal ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
1472 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1478 * Utility to programmatically click a button on a JOptionPane (as a JFrame)
1480 * returns true if button was found
1482 public static boolean clickButton(JFrame frame, int buttonType)
1489 * This helper method makes the JInternalFrame wait until it is notified by an
1490 * InternalFrameClosing event. This method also adds the given JOptionPane to
1491 * the JInternalFrame and sizes it according to the JInternalFrame's preferred
1495 * The JInternalFrame to make modal.
1497 private static void startModal(JInternalFrame f)
1499 // We need to add an additional glasspane-like component directly
1500 // below the frame, which intercepts all mouse events that are not
1501 // directed at the frame itself.
1502 JPanel modalInterceptor = new JPanel();
1503 modalInterceptor.setOpaque(false);
1504 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1505 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1506 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1507 modalInterceptor.addMouseListener(new MouseAdapter()
1510 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter()
1513 lp.add(modalInterceptor);
1516 // We need to explicitly dispatch events when we are blocking the event
1518 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1521 boolean stillModal = true;
1522 while (!f.isClosed() && stillModal)
1524 if (EventQueue.isDispatchThread())
1526 // The getNextEventMethod() issues wait() when no
1527 // event is available, so we don't need do explicitly wait().
1528 AWTEvent ev = queue.getNextEvent();
1529 // This mimics EventQueue.dispatchEvent(). We can't use
1530 // EventQueue.dispatchEvent() directly, because it is
1531 // protected, unfortunately.
1533 "##### ev source=" + ev.getSource().getClass() + "");
1534 if (ev instanceof ActiveEvent)
1536 System.err.println("##### 1");
1537 ((ActiveEvent) ev).dispatch();
1539 else if (ev.getSource() instanceof MenuComponent)
1541 System.err.println("##### 2");
1542 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1544 else if (ev.getSource() instanceof Component)
1546 System.err.println("##### 3");
1547 if (ev.getSource().equals(Desktop.getDesktop().getRootPane()))
1553 ((Component) ev.getSource()).dispatchEvent(ev);
1556 // Other events are ignored as per spec in
1557 // EventQueue.dispatchEvent
1558 System.err.println("##### 4");
1562 // Give other threads a chance to become active.
1563 System.err.println("##### 5");
1567 } catch (InterruptedException ex)
1569 // If we get interrupted, then leave the modal state.
1570 System.err.println("##### 6");
1573 System.err.println("##### 7");
1574 // Clean up the modal interceptor.
1575 lp.remove(modalInterceptor);
1577 f.setVisible(false);
1582 } catch (PropertyVetoException e)
1584 f.doDefaultCloseAction();
1587 // Remove the internal frame from its parent, so it is no longer
1588 // lurking around and clogging memory.
1589 Container parent = f.getParent();
1597 public static JvOptionPane frameDialog(Object message, String title,
1598 int messageType, String[] buttonsTextS, String defaultButtonS,
1599 List<Runnable> handlers, boolean modal)
1601 JFrame parent = new JFrame();
1602 JvOptionPane jvop = JvOptionPane.newOptionDialog();
1603 final String[] buttonsText;
1604 final String defaultButton;
1605 if (buttonsTextS == null)
1607 String ok = MessageManager.getString("action.ok");
1608 buttonsText = new String[] { ok };
1613 buttonsText = buttonsTextS;
1614 defaultButton = defaultButtonS;
1616 JButton[] buttons = new JButton[buttonsText.length];
1617 for (int i = 0; i < buttonsText.length; i++)
1619 buttons[i] = new JButton();
1620 buttons[i].setText(buttonsText[i]);
1621 Console.debug("DISABLING BUTTON " + buttons[i].getText());
1622 buttons[i].setEnabled(false);
1623 buttons[i].setVisible(false);
1626 int dialogType = -1;
1627 if (buttonsText.length == 1)
1629 dialogType = JOptionPane.OK_OPTION;
1631 else if (buttonsText.length == 2)
1633 dialogType = JOptionPane.YES_NO_OPTION;
1637 dialogType = JOptionPane.YES_NO_CANCEL_OPTION;
1639 jvop.setResponseHandler(JOptionPane.YES_OPTION,
1640 (handlers != null && handlers.size() > 0) ? handlers.get(0)
1642 if (dialogType == JOptionPane.YES_NO_OPTION
1643 || dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1645 jvop.setResponseHandler(JOptionPane.NO_OPTION,
1646 (handlers != null && handlers.size() > 1) ? handlers.get(1)
1649 if (dialogType == JOptionPane.YES_NO_CANCEL_OPTION)
1651 jvop.setResponseHandler(JOptionPane.CANCEL_OPTION,
1652 (handlers != null && handlers.size() > 2) ? handlers.get(2)
1656 final int dt = dialogType;
1658 jvop.showDialog(message, title, dt, messageType, null, buttonsText,
1659 defaultButton, modal, buttons);