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);
487 * @param parentComponent
490 * @throws HeadlessException
492 public static String showInputDialog(Component parentComponent,
493 String message) throws HeadlessException
497 return isInteractiveMode()
498 ? JOptionPane.showInputDialog(parentComponent, message)
499 : getMockResponse().toString();
503 * input with initial selection
505 * @param parentComponent
507 * @param initialSelectionValue
510 public static String showInputDialog(Component parentComponent,
511 String message, String initialSelectionValue)
516 return isInteractiveMode()
517 ? JOptionPane.showInputDialog(parentComponent, message,
518 initialSelectionValue)
519 : getMockResponse().toString();
524 * @param parentComponent
529 * @throws HeadlessException
531 public static String showInputDialog(Component parentComponent,
532 String message, String title, int messageType)
533 throws HeadlessException
538 return isInteractiveMode() ? JOptionPane
539 .showInputDialog(parentComponent, message, title, messageType)
540 : getMockResponse().toString();
544 * Customized input option
546 * @param parentComponent
551 * @param selectionValues
552 * @param initialSelectionValue
554 * @throws HeadlessException
556 public static Object showInputDialog(Component parentComponent,
557 Object message, String title, int messageType, Icon icon,
558 Object[] selectionValues, Object initialSelectionValue)
559 throws HeadlessException
564 return isInteractiveMode()
565 ? JOptionPane.showInputDialog(parentComponent, message, title,
566 messageType, icon, selectionValues,
567 initialSelectionValue)
568 : getMockResponse().toString();
576 * @param parentComponent
580 public static String showInternalInputDialog(Component parentComponent,
585 return isInteractiveMode()
586 ? JOptionPane.showInternalInputDialog(parentComponent, message)
587 : getMockResponse().toString();
592 * internal with title and messageType
594 * @param parentComponent
600 public static String showInternalInputDialog(Component parentComponent,
601 String message, String title, int messageType)
604 // AlignFrame tabbedPane_mousePressed
606 return isInteractiveMode()
607 ? JOptionPane.showInternalInputDialog(parentComponent,
608 getPrefix(messageType) + message, title, messageType)
609 : getMockResponse().toString();
613 * customized internal
615 * @param parentComponent
620 * @param selectionValues
621 * @param initialSelectionValue
624 public static Object showInternalInputDialog(Component parentComponent,
625 String message, String title, int messageType, Icon icon,
626 Object[] selectionValues, Object initialSelectionValue)
630 return isInteractiveMode()
631 ? JOptionPane.showInternalInputDialog(parentComponent, message,
632 title, messageType, icon, selectionValues,
633 initialSelectionValue)
634 : getMockResponse().toString();
638 ///////////// end of options ///////////////
641 private static void outputMessage(Object message)
643 System.out.println(">>> JOption Message : " + message.toString());
646 public static Object getMockResponse()
651 public static void setMockResponse(Object mockOption)
653 JvOptionPane.mockResponse = mockOption;
656 public static void resetMock()
658 setMockResponse(JvOptionPane.CANCEL_OPTION);
659 setInteractiveMode(true);
662 public static boolean isInteractiveMode()
664 return interactiveMode;
667 public static void setInteractiveMode(boolean interactiveMode)
669 JvOptionPane.interactiveMode = interactiveMode;
672 @SuppressWarnings("unused")
673 private static String getPrefix(int messageType)
675 String prefix = ""; // JavaScript only
677 if (/** @j2sNative true || */
683 case JvOptionPane.INFORMATION_MESSAGE:
686 case JvOptionPane.WARNING_MESSAGE:
687 prefix = "WARNING! ";
689 case JvOptionPane.ERROR_MESSAGE:
697 DialogRunner<JvOptionPane> runner = new DialogRunner(this);
699 private List<Object> ourOptions;
701 * create a new option dialog that can be used to register responses - along
702 * lines of showOptionDialog
707 * @param defaultOption
708 * @param plainMessage
714 public static JvOptionPane newOptionDialog(Component parentComponent)
716 return new JvOptionPane(parentComponent);
719 public void showDialog(
720 String message, String title, int optionType, int messageType,
721 Icon icon, Object[] options, Object initialValue)
724 if (!isInteractiveMode())
726 runner.firstRun((int) getMockResponse());
732 // 1) AlignViewport for openLinkedAlignment
734 // Show a dialog with the option to open and link (cDNA <-> protein) as a
736 // alignment, either as a standalone alignment or in a split frame. Returns
737 // true if the new alignment was opened, false if not, because the user
738 // declined the offer.
740 // 2) UserDefinedColors warning about saving over a name already defined
748 parent = parentComponent;
751 ourOptions = Arrays.asList(options);
752 int response = JOptionPane.showOptionDialog(parent, message, title,
753 optionType, messageType, icon, options, initialValue);
758 runner.firstRun(response);
763 public void showInternalDialog(JPanel mainPanel, String title,
764 int yesNoCancelOption, int questionMessage, Icon icon,
765 Object[] options, String initresponse)
767 if (!isInteractiveMode())
769 runner.firstRun((int) getMockResponse());
773 * @j2sNative parent = this;
776 parent = parentComponent;
779 ourOptions = Arrays.asList(options);
784 response = JOptionPane.showInternalOptionDialog(parent, mainPanel,
785 title, yesNoCancelOption, questionMessage, icon, options,
790 response = JOptionPane.showOptionDialog(parent, mainPanel, title,
791 yesNoCancelOption, questionMessage, icon, options,
798 runner.firstRun(response);
803 public JvOptionPane response(RunResponse action)
806 runner.response(action);
810 public JvOptionPane defaultResponse(Runnable runnable)
812 runner.setDefaultResponse(runnable);
817 public void propertyChange(PropertyChangeEvent evt)
819 int ourOption = ourOptions.indexOf(evt.getNewValue());
823 runner.run(evt.getNewValue());
827 runner.run(ourOption);