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;
38 public class JvOptionPane extends JOptionPane
39 implements DialogRunnerI, PropertyChangeListener
41 // BH 2018 no changes needed here.
43 private static final long serialVersionUID = -3019167117756785229L;
45 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
47 private static boolean interactiveMode = true;
49 private Component parentComponent;
51 public JvOptionPane(final Component parentComponent)
54 this.parentComponent = parentComponent;
57 public static int showConfirmDialog(Component parentComponent,
58 Object message) throws HeadlessException
60 // only called by test
61 return isInteractiveMode()
62 ? JOptionPane.showConfirmDialog(parentComponent, message)
63 : (int) getMockResponse();
67 * Message, title, optionType
69 * @param parentComponent
74 * @throws HeadlessException
76 public static int showConfirmDialog(Component parentComponent,
77 Object message, String title, int optionType)
78 throws HeadlessException
80 if (!isInteractiveMode())
82 return (int) getMockResponse();
86 case JvOptionPane.YES_NO_CANCEL_OPTION:
87 // FeatureRenderer amendFeatures ?? TODO ??
92 case JvOptionPane.YES_NO_OPTION:
93 // PromptUserConfig usage stats
94 // for now treated as "OK CANCEL"
96 case JvOptionPane.OK_CANCEL_OPTION:
97 // will fall back to simple HTML
98 return JOptionPane.showConfirmDialog(parentComponent, message, title,
104 * Adds a message type. Fallback is to just add it in the beginning.
106 * @param parentComponent
112 * @throws HeadlessException
114 public static int showConfirmDialog(Component parentComponent,
115 Object message, String title, int optionType, int messageType)
116 throws HeadlessException
118 // JalviewServicesChanged
119 // PromptUserConfig raiseDialog
120 return isInteractiveMode()
121 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
122 optionType, messageType)
123 : (int) getMockResponse();
129 * @param parentComponent
136 * @throws HeadlessException
138 public static int showConfirmDialog(Component parentComponent,
139 Object message, String title, int optionType, int messageType,
140 Icon icon) throws HeadlessException
142 // JvOptionPaneTest only
143 return isInteractiveMode()
144 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
145 optionType, messageType, icon)
146 : (int) getMockResponse();
150 * Internal version "OK"
152 * @param parentComponent
156 public static int showInternalConfirmDialog(Component parentComponent,
159 // JvOptionPaneTest only;
160 return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog(
161 parentComponent, message) : (int) getMockResponse();
165 * Internal version -- changed to standard version for now
167 * @param parentComponent
173 public static int showInternalConfirmDialog(Component parentComponent,
174 String message, String title, int optionType)
176 if (!isInteractiveMode())
178 return (int) getMockResponse();
182 case JvOptionPane.YES_NO_CANCEL_OPTION:
183 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
184 case JvOptionPane.YES_NO_OPTION:
185 // UserDefinedColoursSave -- relevant? TODO
188 case JvOptionPane.OK_CANCEL_OPTION:
190 // EditNameDialog --- uses panel for messsage TODO
192 // Desktop.inputURLMenuItem
194 return JOptionPane.showConfirmDialog(parentComponent, message, title,
201 * @param parentComponent
208 public static int showInternalConfirmDialog(Component parentComponent,
209 Object message, String title, int optionType, int messageType)
211 if (!isInteractiveMode())
213 return (int) getMockResponse();
217 case JvOptionPane.YES_NO_CANCEL_OPTION:
218 case JvOptionPane.YES_NO_OPTION:
219 // UserQuestionanaireCheck
223 case JvOptionPane.OK_CANCEL_OPTION:
224 // will fall back to simple HTML
225 return JOptionPane.showConfirmDialog(parentComponent, message, title,
226 optionType, messageType);
231 * adds icon; no longer internal
233 * @param parentComponent
241 public static int showInternalConfirmDialog(Component parentComponent,
242 Object message, String title, int optionType, int messageType,
245 if (!isInteractiveMode())
247 return (int) getMockResponse();
251 case JvOptionPane.YES_NO_CANCEL_OPTION:
252 case JvOptionPane.YES_NO_OPTION:
255 case JvOptionPane.OK_CANCEL_OPTION:
256 // Preferences editLink/newLink
257 return JOptionPane.showConfirmDialog(parentComponent, message, title,
258 optionType, messageType, icon);
264 * custom options full-featured
266 * @param parentComponent
273 * @param initialValue
275 * @throws HeadlessException
277 public static int showOptionDialog(Component parentComponent,
278 String message, String title, int optionType, int messageType,
279 Icon icon, Object[] options, Object initialValue)
280 throws HeadlessException
282 if (!isInteractiveMode())
284 return (int) getMockResponse();
290 // 1) AlignViewport for openLinkedAlignment
292 // Show a dialog with the option to open and link (cDNA <-> protein) as a
294 // alignment, either as a standalone alignment or in a split frame. Returns
295 // true if the new alignment was opened, false if not, because the user
296 // declined the offer.
298 // 2) UserDefinedColors warning about saving over a name already defined
300 return JOptionPane.showOptionDialog(parentComponent, message, title,
301 optionType, messageType, icon, options, initialValue);
308 * @throws HeadlessException
310 public static void showMessageDialog(Component parentComponent,
311 String message) throws HeadlessException
313 if (!isInteractiveMode())
315 outputMessage(message);
321 JOptionPane.showMessageDialog(parentComponent, message);
325 * OK with message, title, and type
327 * @param parentComponent
331 * @throws HeadlessException
333 public static void showMessageDialog(Component parentComponent,
334 String message, String title, int messageType)
335 throws HeadlessException
337 // 30 implementations -- all just fine.
339 if (!isInteractiveMode())
341 outputMessage(message);
345 JOptionPane.showMessageDialog(parentComponent,
346 getPrefix(messageType) + message, title, messageType);
350 * adds title and icon
352 * @param parentComponent
357 * @throws HeadlessException
359 public static void showMessageDialog(Component parentComponent,
360 String message, String title, int messageType, Icon icon)
361 throws HeadlessException
366 if (!isInteractiveMode())
368 outputMessage(message);
372 JOptionPane.showMessageDialog(parentComponent, message, title,
380 public static void showInternalMessageDialog(Component parentComponent,
384 // WsPreferences only
386 if (!isInteractiveMode())
388 outputMessage(message);
392 JOptionPane.showMessageDialog(parentComponent, message);
397 * Adds title and messageType
399 * @param parentComponent
404 public static void showInternalMessageDialog(Component parentComponent,
405 String message, String title, int messageType)
410 if (!isInteractiveMode())
412 outputMessage(message);
416 JOptionPane.showMessageDialog(parentComponent,
417 getPrefix(messageType) + message, title, messageType);
422 * @param parentComponent
428 public static void showInternalMessageDialog(Component parentComponent,
429 Object message, String title, int messageType, Icon icon)
434 if (!isInteractiveMode())
436 outputMessage(message);
440 JOptionPane.showMessageDialog(parentComponent, message, title,
448 * @throws HeadlessException
450 public static String showInputDialog(Object message)
451 throws HeadlessException
455 if (!isInteractiveMode())
457 return getMockResponse().toString();
460 return JOptionPane.showInputDialog(message);
464 * adds inital selection value
467 * @param initialSelectionValue
470 public static String showInputDialog(String message,
471 String initialSelectionValue)
473 if (!isInteractiveMode())
475 return getMockResponse().toString();
478 // AnnotationPanel character option
480 return JOptionPane.showInputDialog(message, initialSelectionValue);
486 * @param parentComponent
489 * @throws HeadlessException
491 public static String showInputDialog(Component parentComponent,
492 String message) throws HeadlessException
496 return isInteractiveMode()
497 ? JOptionPane.showInputDialog(parentComponent, message)
498 : getMockResponse().toString();
502 * input with initial selection
504 * @param parentComponent
506 * @param initialSelectionValue
509 public static String showInputDialog(Component parentComponent,
510 String message, String initialSelectionValue)
515 return isInteractiveMode()
516 ? JOptionPane.showInputDialog(parentComponent, message,
517 initialSelectionValue)
518 : getMockResponse().toString();
523 * @param parentComponent
528 * @throws HeadlessException
530 public static String showInputDialog(Component parentComponent,
531 String message, String title, int messageType)
532 throws HeadlessException
537 return isInteractiveMode() ? JOptionPane
538 .showInputDialog(parentComponent, message, title, messageType)
539 : getMockResponse().toString();
543 * Customized input option
545 * @param parentComponent
550 * @param selectionValues
551 * @param initialSelectionValue
553 * @throws HeadlessException
555 public static Object showInputDialog(Component parentComponent,
556 Object message, String title, int messageType, Icon icon,
557 Object[] selectionValues, Object initialSelectionValue)
558 throws HeadlessException
563 return isInteractiveMode()
564 ? JOptionPane.showInputDialog(parentComponent, message, title,
565 messageType, icon, selectionValues,
566 initialSelectionValue)
567 : getMockResponse().toString();
575 * @param parentComponent
579 public static String showInternalInputDialog(Component parentComponent,
584 return isInteractiveMode()
585 ? JOptionPane.showInternalInputDialog(parentComponent, message)
586 : getMockResponse().toString();
591 * internal with title and messageType
593 * @param parentComponent
599 public static String showInternalInputDialog(Component parentComponent,
600 String message, String title, int messageType)
603 // AlignFrame tabbedPane_mousePressed
605 return isInteractiveMode()
606 ? JOptionPane.showInternalInputDialog(parentComponent,
607 getPrefix(messageType) + message, title, messageType)
608 : getMockResponse().toString();
612 * customized internal
614 * @param parentComponent
619 * @param selectionValues
620 * @param initialSelectionValue
623 public static Object showInternalInputDialog(Component parentComponent,
624 String message, String title, int messageType, Icon icon,
625 Object[] selectionValues, Object initialSelectionValue)
629 return isInteractiveMode()
630 ? JOptionPane.showInternalInputDialog(parentComponent, message,
631 title, messageType, icon, selectionValues,
632 initialSelectionValue)
633 : getMockResponse().toString();
637 ///////////// end of options ///////////////
640 private static void outputMessage(Object message)
642 System.out.println(">>> JOption Message : " + message.toString());
645 public static Object getMockResponse()
650 public static void setMockResponse(Object mockOption)
652 JvOptionPane.mockResponse = mockOption;
655 public static void resetMock()
657 setMockResponse(JvOptionPane.CANCEL_OPTION);
658 setInteractiveMode(true);
661 public static boolean isInteractiveMode()
663 return interactiveMode;
666 public static void setInteractiveMode(boolean interactiveMode)
668 JvOptionPane.interactiveMode = interactiveMode;
671 @SuppressWarnings("unused")
672 private static String getPrefix(int messageType)
674 String prefix = ""; // JavaScript only
676 if (/** @j2sNative true || */
682 case JvOptionPane.INFORMATION_MESSAGE:
685 case JvOptionPane.WARNING_MESSAGE:
686 prefix = "WARNING! ";
688 case JvOptionPane.ERROR_MESSAGE:
696 DialogRunner<JvOptionPane> runner = new DialogRunner(this);
698 private List<Object> ourOptions;
700 * create a new option dialog that can be used to register responses - along
701 * lines of showOptionDialog
706 * @param defaultOption
707 * @param plainMessage
713 public static JvOptionPane newOptionDialog(Component parentComponent)
715 return new JvOptionPane(parentComponent);
718 public void showDialog(
719 String message, String title, int optionType, int messageType,
720 Icon icon, Object[] options, Object initialValue)
723 if (!isInteractiveMode())
725 runner.firstRun((int) getMockResponse());
731 // 1) AlignViewport for openLinkedAlignment
733 // Show a dialog with the option to open and link (cDNA <-> protein) as a
735 // alignment, either as a standalone alignment or in a split frame. Returns
736 // true if the new alignment was opened, false if not, because the user
737 // declined the offer.
739 // 2) UserDefinedColors warning about saving over a name already defined
747 parent = parentComponent;
750 ourOptions = Arrays.asList(options);
751 int response = JOptionPane.showOptionDialog(parent, message, title,
752 optionType, messageType, icon, options, initialValue);
757 runner.firstRun(response);
763 public JvOptionPane response(RunResponse action)
766 runner.response(action);
770 public JvOptionPane defaultResponse(Runnable runnable)
772 runner.setDefaultResponse(runnable);
777 public void propertyChange(PropertyChangeEvent evt)
779 int ourOption = ourOptions.indexOf(evt.getNewValue());
783 runner.run(evt.getNewValue());
787 runner.run(ourOption);