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.
24 import jalview.util.dialogrunner.DialogRunner;
25 import jalview.util.dialogrunner.DialogRunnerI;
26 import jalview.util.dialogrunner.RunResponse;
28 import java.awt.Component;
29 import java.awt.HeadlessException;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32 import java.util.Arrays;
33 import java.util.List;
35 import javax.swing.Icon;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
39 public class JvOptionPane extends JOptionPane
40 implements DialogRunnerI, PropertyChangeListener
42 // BH 2018 no changes needed here.
44 private static final long serialVersionUID = -3019167117756785229L;
46 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
48 private static boolean interactiveMode = true;
50 private Component parentComponent;
52 public JvOptionPane(final Component parentComponent)
55 this.parentComponent = parentComponent;
58 public static int showConfirmDialog(Component parentComponent,
59 Object message) throws HeadlessException
61 // only called by test
62 return isInteractiveMode()
63 ? JOptionPane.showConfirmDialog(parentComponent, message)
64 : (int) getMockResponse();
68 * Message, title, optionType
70 * @param parentComponent
75 * @throws HeadlessException
77 public static int showConfirmDialog(Component parentComponent,
78 Object message, String title, int optionType)
79 throws HeadlessException
81 if (!isInteractiveMode())
83 return (int) getMockResponse();
87 case JvOptionPane.YES_NO_CANCEL_OPTION:
88 // FeatureRenderer amendFeatures ?? TODO ??
93 case JvOptionPane.YES_NO_OPTION:
94 // PromptUserConfig usage stats
95 // for now treated as "OK CANCEL"
97 case JvOptionPane.OK_CANCEL_OPTION:
98 // will fall back to simple HTML
99 return JOptionPane.showConfirmDialog(parentComponent, message, title,
105 * Adds a message type. Fallback is to just add it in the beginning.
107 * @param parentComponent
113 * @throws HeadlessException
115 public static int showConfirmDialog(Component parentComponent,
116 Object message, String title, int optionType, int messageType)
117 throws HeadlessException
119 // JalviewServicesChanged
120 // PromptUserConfig raiseDialog
121 return isInteractiveMode()
122 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
123 optionType, messageType)
124 : (int) getMockResponse();
130 * @param parentComponent
137 * @throws HeadlessException
139 public static int showConfirmDialog(Component parentComponent,
140 Object message, String title, int optionType, int messageType,
141 Icon icon) throws HeadlessException
143 // JvOptionPaneTest only
144 return isInteractiveMode()
145 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
146 optionType, messageType, icon)
147 : (int) getMockResponse();
151 * Internal version "OK"
153 * @param parentComponent
157 public static int showInternalConfirmDialog(Component parentComponent,
160 // JvOptionPaneTest only;
161 return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog(
162 parentComponent, message) : (int) getMockResponse();
166 * Internal version -- changed to standard version for now
168 * @param parentComponent
174 public static int showInternalConfirmDialog(Component parentComponent,
175 String message, String title, int optionType)
177 if (!isInteractiveMode())
179 return (int) getMockResponse();
183 case JvOptionPane.YES_NO_CANCEL_OPTION:
184 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
185 case JvOptionPane.YES_NO_OPTION:
186 // UserDefinedColoursSave -- relevant? TODO
189 case JvOptionPane.OK_CANCEL_OPTION:
191 // EditNameDialog --- uses panel for messsage TODO
193 // Desktop.inputURLMenuItem
195 return JOptionPane.showConfirmDialog(parentComponent, message, title,
202 * @param parentComponent
209 public static int showInternalConfirmDialog(Component parentComponent,
210 Object message, String title, int optionType, int messageType)
212 if (!isInteractiveMode())
214 return (int) getMockResponse();
218 case JvOptionPane.YES_NO_CANCEL_OPTION:
219 case JvOptionPane.YES_NO_OPTION:
220 // UserQuestionanaireCheck
224 case JvOptionPane.OK_CANCEL_OPTION:
225 // will fall back to simple HTML
226 return JOptionPane.showConfirmDialog(parentComponent, message, title,
227 optionType, messageType);
232 * adds icon; no longer internal
234 * @param parentComponent
242 public static int showInternalConfirmDialog(Component parentComponent,
243 Object message, String title, int optionType, int messageType,
246 if (!isInteractiveMode())
248 return (int) getMockResponse();
252 case JvOptionPane.YES_NO_CANCEL_OPTION:
253 case JvOptionPane.YES_NO_OPTION:
256 case JvOptionPane.OK_CANCEL_OPTION:
257 // Preferences editLink/newLink
258 return JOptionPane.showConfirmDialog(parentComponent, message, title,
259 optionType, messageType, icon);
265 * custom options full-featured
267 * @param parentComponent
274 * @param initialValue
276 * @throws HeadlessException
278 public static int showOptionDialog(Component parentComponent,
279 String message, String title, int optionType, int messageType,
280 Icon icon, Object[] options, Object initialValue)
281 throws HeadlessException
283 if (!isInteractiveMode())
285 return (int) getMockResponse();
291 // 1) AlignViewport for openLinkedAlignment
293 // Show a dialog with the option to open and link (cDNA <-> protein) as a
295 // alignment, either as a standalone alignment or in a split frame. Returns
296 // true if the new alignment was opened, false if not, because the user
297 // declined the offer.
299 // 2) UserDefinedColors warning about saving over a name already defined
301 return JOptionPane.showOptionDialog(parentComponent, message, title,
302 optionType, messageType, icon, options, initialValue);
309 * @throws HeadlessException
311 public static void showMessageDialog(Component parentComponent,
312 String message) throws HeadlessException
314 if (!isInteractiveMode())
316 outputMessage(message);
322 JOptionPane.showMessageDialog(parentComponent, message);
326 * OK with message, title, and type
328 * @param parentComponent
332 * @throws HeadlessException
334 public static void showMessageDialog(Component parentComponent,
335 String message, String title, int messageType)
336 throws HeadlessException
338 // 30 implementations -- all just fine.
340 if (!isInteractiveMode())
342 outputMessage(message);
346 JOptionPane.showMessageDialog(parentComponent,
347 getPrefix(messageType) + message, title, messageType);
351 * adds title and icon
353 * @param parentComponent
358 * @throws HeadlessException
360 public static void showMessageDialog(Component parentComponent,
361 String message, String title, int messageType, Icon icon)
362 throws HeadlessException
367 if (!isInteractiveMode())
369 outputMessage(message);
373 JOptionPane.showMessageDialog(parentComponent, message, title,
381 public static void showInternalMessageDialog(Component parentComponent,
385 // WsPreferences only
387 if (!isInteractiveMode())
389 outputMessage(message);
393 JOptionPane.showMessageDialog(parentComponent, message);
398 * Adds title and messageType
400 * @param parentComponent
405 public static void showInternalMessageDialog(Component parentComponent,
406 String message, String title, int messageType)
411 if (!isInteractiveMode())
413 outputMessage(message);
417 JOptionPane.showMessageDialog(parentComponent,
418 getPrefix(messageType) + message, title, messageType);
423 * @param parentComponent
429 public static void showInternalMessageDialog(Component parentComponent,
430 Object message, String title, int messageType, Icon icon)
435 if (!isInteractiveMode())
437 outputMessage(message);
441 JOptionPane.showMessageDialog(parentComponent, message, title,
449 * @throws HeadlessException
451 public static String showInputDialog(Object message)
452 throws HeadlessException
456 if (!isInteractiveMode())
458 return getMockResponse().toString();
461 return JOptionPane.showInputDialog(message);
465 * adds inital selection value
468 * @param initialSelectionValue
471 public static String showInputDialog(String message,
472 String initialSelectionValue)
474 if (!isInteractiveMode())
476 return getMockResponse().toString();
479 // AnnotationPanel character option
481 return JOptionPane.showInputDialog(message, initialSelectionValue);
485 * adds inital selection value
488 * @param initialSelectionValue
491 public static String showInputDialog(Object message,
492 Object initialSelectionValue)
494 if (!isInteractiveMode())
496 return getMockResponse().toString();
499 // AnnotationPanel character option
501 return JOptionPane.showInputDialog(message, initialSelectionValue);
506 * @param parentComponent
509 * @throws HeadlessException
511 public static String showInputDialog(Component parentComponent,
512 String message) throws HeadlessException
516 return isInteractiveMode()
517 ? JOptionPane.showInputDialog(parentComponent, message)
518 : getMockResponse().toString();
522 * input with initial selection
524 * @param parentComponent
526 * @param initialSelectionValue
529 public static String showInputDialog(Component parentComponent,
530 String message, String initialSelectionValue)
535 return isInteractiveMode()
536 ? JOptionPane.showInputDialog(parentComponent, message,
537 initialSelectionValue)
538 : getMockResponse().toString();
543 * input with initial selection
545 * @param parentComponent
547 * @param initialSelectionValue
550 public static String showInputDialog(Component parentComponent,
551 Object message, Object initialSelectionValue)
556 return isInteractiveMode()
557 ? JOptionPane.showInputDialog(parentComponent, message,
558 initialSelectionValue)
559 : getMockResponse().toString();
564 * @param parentComponent
569 * @throws HeadlessException
571 public static String showInputDialog(Component parentComponent,
572 String message, String title, int messageType)
573 throws HeadlessException
578 return isInteractiveMode() ? JOptionPane
579 .showInputDialog(parentComponent, message, title, messageType)
580 : getMockResponse().toString();
584 * Customized input option
586 * @param parentComponent
591 * @param selectionValues
592 * @param initialSelectionValue
594 * @throws HeadlessException
596 public static Object showInputDialog(Component parentComponent,
597 Object message, String title, int messageType, Icon icon,
598 Object[] selectionValues, Object initialSelectionValue)
599 throws HeadlessException
604 return isInteractiveMode()
605 ? JOptionPane.showInputDialog(parentComponent, message, title,
606 messageType, icon, selectionValues,
607 initialSelectionValue)
608 : getMockResponse().toString();
616 * @param parentComponent
620 public static String showInternalInputDialog(Component parentComponent,
625 return isInteractiveMode()
626 ? JOptionPane.showInternalInputDialog(parentComponent, message)
627 : getMockResponse().toString();
632 * internal with title and messageType
634 * @param parentComponent
640 public static String showInternalInputDialog(Component parentComponent,
641 String message, String title, int messageType)
644 // AlignFrame tabbedPane_mousePressed
646 return isInteractiveMode()
647 ? JOptionPane.showInternalInputDialog(parentComponent,
648 getPrefix(messageType) + message, title, messageType)
649 : getMockResponse().toString();
653 * customized internal
655 * @param parentComponent
660 * @param selectionValues
661 * @param initialSelectionValue
664 public static Object showInternalInputDialog(Component parentComponent,
665 String message, String title, int messageType, Icon icon,
666 Object[] selectionValues, Object initialSelectionValue)
670 return isInteractiveMode()
671 ? JOptionPane.showInternalInputDialog(parentComponent, message,
672 title, messageType, icon, selectionValues,
673 initialSelectionValue)
674 : getMockResponse().toString();
678 ///////////// end of options ///////////////
681 private static void outputMessage(Object message)
683 System.out.println(">>> JOption Message : " + message.toString());
686 public static Object getMockResponse()
691 public static void setMockResponse(Object mockOption)
693 JvOptionPane.mockResponse = mockOption;
696 public static void resetMock()
698 setMockResponse(JvOptionPane.CANCEL_OPTION);
699 setInteractiveMode(true);
702 public static boolean isInteractiveMode()
704 return interactiveMode;
707 public static void setInteractiveMode(boolean interactiveMode)
709 JvOptionPane.interactiveMode = interactiveMode;
712 @SuppressWarnings("unused")
713 private static String getPrefix(int messageType)
715 String prefix = ""; // JavaScript only
717 if (/** @j2sNative true || */
723 case JvOptionPane.INFORMATION_MESSAGE:
726 case JvOptionPane.WARNING_MESSAGE:
727 prefix = "WARNING! ";
729 case JvOptionPane.ERROR_MESSAGE:
737 DialogRunner<JvOptionPane> runner = new DialogRunner(this);
739 private List<Object> ourOptions;
741 * create a new option dialog that can be used to register responses - along
742 * lines of showOptionDialog
747 * @param defaultOption
748 * @param plainMessage
754 public static JvOptionPane newOptionDialog(Component parentComponent)
756 return new JvOptionPane(parentComponent);
759 public void showDialog(
760 String message, String title, int optionType, int messageType,
761 Icon icon, Object[] options, Object initialValue)
764 if (!isInteractiveMode())
766 runner.firstRun((int) getMockResponse());
772 // 1) AlignViewport for openLinkedAlignment
774 // Show a dialog with the option to open and link (cDNA <-> protein) as a
776 // alignment, either as a standalone alignment or in a split frame. Returns
777 // true if the new alignment was opened, false if not, because the user
778 // declined the offer.
780 // 2) UserDefinedColors warning about saving over a name already defined
788 parent = parentComponent;
791 ourOptions = Arrays.asList(options);
792 int response = JOptionPane.showOptionDialog(parent, message, title,
793 optionType, messageType, icon, options, initialValue);
798 runner.firstRun(response);
803 public void showInternalDialog(JPanel mainPanel, String title,
804 int yesNoCancelOption, int questionMessage, Icon icon,
805 Object[] options, String initresponse)
807 if (!isInteractiveMode())
809 runner.firstRun((int) getMockResponse());
813 * @j2sNative parent = this;
816 parent = parentComponent;
819 ourOptions = Arrays.asList(options);
824 response = JOptionPane.showInternalOptionDialog(parent, mainPanel,
825 title, yesNoCancelOption, questionMessage, icon, options,
830 response = JOptionPane.showOptionDialog(parent, mainPanel, title,
831 yesNoCancelOption, questionMessage, icon, options,
838 runner.firstRun(response);
843 public JvOptionPane response(RunResponse action)
846 runner.response(action);
850 public JvOptionPane defaultResponse(Runnable runnable)
852 runner.setDefaultResponse(runnable);
857 public void propertyChange(PropertyChangeEvent evt)
859 int ourOption = ourOptions.indexOf(evt.getNewValue());
863 runner.run(evt.getNewValue());
867 runner.run(ourOption);