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 java.awt.Component;
25 import java.awt.HeadlessException;
27 import javax.swing.Icon;
28 import javax.swing.JOptionPane;
30 public class JvOptionPane extends JOptionPane
32 // BH 2018 no changes needed here.
34 private static final long serialVersionUID = -3019167117756785229L;
36 private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
38 private static boolean interactiveMode = true;
40 public static int showConfirmDialog(Component parentComponent,
41 Object message) throws HeadlessException
43 // only called by test
44 return isInteractiveMode()
45 ? JOptionPane.showConfirmDialog(parentComponent, message)
46 : (int) getMockResponse();
50 * Message, title, optionType
52 * @param parentComponent
57 * @throws HeadlessException
59 public static int showConfirmDialog(Component parentComponent,
60 Object message, String title, int optionType)
61 throws HeadlessException
63 if (!isInteractiveMode())
64 return (int) getMockResponse();
67 case JvOptionPane.YES_NO_CANCEL_OPTION:
68 // FeatureRenderer amendFeatures ?? TODO ??
73 case JvOptionPane.YES_NO_OPTION:
74 // PromptUserConfig usage stats
75 // for now treated as "OK CANCEL"
77 case JvOptionPane.OK_CANCEL_OPTION:
78 // will fall back to simple HTML
79 return JOptionPane.showConfirmDialog(parentComponent, message, title,
85 * Adds a message type. Fallback is to just add it in the beginning.
87 * @param parentComponent
93 * @throws HeadlessException
95 public static int showConfirmDialog(Component parentComponent,
96 Object message, String title, int optionType, int messageType)
97 throws HeadlessException
99 // JalviewServicesChanged
100 // PromptUserConfig raiseDialog
101 return isInteractiveMode()
102 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
103 optionType, messageType)
104 : (int) getMockResponse();
110 * @param parentComponent
117 * @throws HeadlessException
119 public static int showConfirmDialog(Component parentComponent,
120 Object message, String title, int optionType, int messageType,
121 Icon icon) throws HeadlessException
123 // JvOptionPaneTest only
124 return isInteractiveMode()
125 ? JOptionPane.showConfirmDialog(parentComponent, message, title,
126 optionType, messageType, icon)
127 : (int) getMockResponse();
131 * Internal version "OK"
133 * @param parentComponent
137 public static int showInternalConfirmDialog(Component parentComponent,
140 // JvOptionPaneTest only;
141 return isInteractiveMode() ? JOptionPane.showInternalConfirmDialog(
142 parentComponent, message) : (int) getMockResponse();
146 * Internal version -- changed to standard version for now
148 * @param parentComponent
154 public static int showInternalConfirmDialog(Component parentComponent,
155 String message, String title, int optionType)
157 if (!isInteractiveMode())
158 return (int) getMockResponse();
161 case JvOptionPane.YES_NO_CANCEL_OPTION:
162 // ColourMenuHelper.addMenuItmers.offerRemoval TODO
163 case JvOptionPane.YES_NO_OPTION:
164 // UserDefinedColoursSave -- relevant? TODO
167 case JvOptionPane.OK_CANCEL_OPTION:
169 // EditNameDialog --- uses panel for messsage TODO
171 // Desktop.inputURLMenuItem
173 return JOptionPane.showConfirmDialog(parentComponent, message, title,
180 * @param parentComponent
187 public static int showInternalConfirmDialog(Component parentComponent,
188 Object message, String title, int optionType, int messageType)
190 if (!isInteractiveMode())
191 return (int) getMockResponse();
194 case JvOptionPane.YES_NO_CANCEL_OPTION:
195 case JvOptionPane.YES_NO_OPTION:
196 // UserQuestionanaireCheck
200 case JvOptionPane.OK_CANCEL_OPTION:
201 // will fall back to simple HTML
202 return JOptionPane.showConfirmDialog(parentComponent, message, title,
203 optionType, messageType);
208 * adds icon; no longer internal
210 * @param parentComponent
218 public static int showInternalConfirmDialog(Component parentComponent,
219 Object message, String title, int optionType, int messageType,
222 if (!isInteractiveMode())
223 return (int) getMockResponse();
226 case JvOptionPane.YES_NO_CANCEL_OPTION:
227 case JvOptionPane.YES_NO_OPTION:
230 case JvOptionPane.OK_CANCEL_OPTION:
231 // Preferences editLink/newLink
232 return JOptionPane.showConfirmDialog(parentComponent, message, title,
233 optionType, messageType, icon);
239 * custom options full-featured
241 * @param parentComponent
248 * @param initialValue
250 * @throws HeadlessException
252 public static int showOptionDialog(Component parentComponent,
253 String message, String title, int optionType, int messageType,
254 Icon icon, Object[] options, Object initialValue)
255 throws HeadlessException
257 if (!isInteractiveMode())
258 return (int) getMockResponse();
263 // 1) AlignViewport for openLinkedAlignment
265 // Show a dialog with the option to open and link (cDNA <-> protein) as a
267 // alignment, either as a standalone alignment or in a split frame. Returns
268 // true if the new alignment was opened, false if not, because the user
269 // declined the offer.
271 // 2) UserDefinedColors warning about saving over a name already defined
273 return JOptionPane.showOptionDialog(parentComponent, message, title,
274 optionType, messageType, icon, options, initialValue);
281 * @throws HeadlessException
283 public static void showMessageDialog(Component parentComponent,
284 String message) throws HeadlessException
286 if (!isInteractiveMode())
288 outputMessage(message);
294 JOptionPane.showMessageDialog(parentComponent, message);
298 * OK with message, title, and type
300 * @param parentComponent
304 * @throws HeadlessException
306 public static void showMessageDialog(Component parentComponent,
307 String message, String title, int messageType)
308 throws HeadlessException
310 // 30 implementations -- all just fine.
312 if (!isInteractiveMode())
314 outputMessage(message);
318 JOptionPane.showMessageDialog(parentComponent,
319 getPrefix(messageType) + message, title, messageType);
323 * adds title and icon
325 * @param parentComponent
330 * @throws HeadlessException
332 public static void showMessageDialog(Component parentComponent,
333 String message, String title, int messageType, Icon icon)
334 throws HeadlessException
339 if (!isInteractiveMode())
341 outputMessage(message);
345 JOptionPane.showMessageDialog(parentComponent, message, title,
353 public static void showInternalMessageDialog(Component parentComponent,
357 // WsPreferences only
359 if (!isInteractiveMode())
361 outputMessage(message);
365 JOptionPane.showMessageDialog(parentComponent, message);
370 * Adds title and messageType
372 * @param parentComponent
377 public static void showInternalMessageDialog(Component parentComponent,
378 String message, String title, int messageType)
383 if (!isInteractiveMode())
385 outputMessage(message);
389 JOptionPane.showMessageDialog(parentComponent,
390 getPrefix(messageType) + message, title, messageType);
395 * @param parentComponent
401 public static void showInternalMessageDialog(Component parentComponent,
402 Object message, String title, int messageType, Icon icon)
407 if (!isInteractiveMode())
409 outputMessage(message);
413 JOptionPane.showMessageDialog(parentComponent, message, title,
421 * @throws HeadlessException
423 public static String showInputDialog(Object message)
424 throws HeadlessException
428 if (!isInteractiveMode())
430 return getMockResponse().toString();
433 return JOptionPane.showInputDialog(message);
437 * adds inital selection value
440 * @param initialSelectionValue
443 public static String showInputDialog(String message,
444 String initialSelectionValue)
446 if (!isInteractiveMode())
448 return getMockResponse().toString();
451 // AnnotationPanel character option
453 return JOptionPane.showInputDialog(message, initialSelectionValue);
459 * @param parentComponent
462 * @throws HeadlessException
464 public static String showInputDialog(Component parentComponent,
465 String message) throws HeadlessException
469 return isInteractiveMode()
470 ? JOptionPane.showInputDialog(parentComponent, message)
471 : getMockResponse().toString();
475 * input with initial selection
477 * @param parentComponent
479 * @param initialSelectionValue
482 public static String showInputDialog(Component parentComponent,
483 String message, String initialSelectionValue)
488 return isInteractiveMode()
489 ? JOptionPane.showInputDialog(parentComponent, message,
490 initialSelectionValue)
491 : getMockResponse().toString();
496 * @param parentComponent
501 * @throws HeadlessException
503 public static String showInputDialog(Component parentComponent,
504 String message, String title, int messageType)
505 throws HeadlessException
510 return isInteractiveMode() ? JOptionPane
511 .showInputDialog(parentComponent, message, title, messageType)
512 : getMockResponse().toString();
516 * Customized input option
518 * @param parentComponent
523 * @param selectionValues
524 * @param initialSelectionValue
526 * @throws HeadlessException
528 public static Object showInputDialog(Component parentComponent,
529 Object message, String title, int messageType, Icon icon,
530 Object[] selectionValues, Object initialSelectionValue)
531 throws HeadlessException
536 return isInteractiveMode()
537 ? JOptionPane.showInputDialog(parentComponent, message, title,
538 messageType, icon, selectionValues,
539 initialSelectionValue)
540 : getMockResponse().toString();
548 * @param parentComponent
552 public static String showInternalInputDialog(Component parentComponent,
557 return isInteractiveMode()
558 ? JOptionPane.showInternalInputDialog(parentComponent, message)
559 : getMockResponse().toString();
564 * internal with title and messageType
566 * @param parentComponent
572 public static String showInternalInputDialog(Component parentComponent,
573 String message, String title, int messageType)
576 // AlignFrame tabbedPane_mousePressed
578 return isInteractiveMode()
579 ? JOptionPane.showInternalInputDialog(parentComponent,
580 getPrefix(messageType) + message, title, messageType)
581 : getMockResponse().toString();
585 * customized internal
587 * @param parentComponent
592 * @param selectionValues
593 * @param initialSelectionValue
596 public static Object showInternalInputDialog(Component parentComponent,
597 String message, String title, int messageType, Icon icon,
598 Object[] selectionValues, Object initialSelectionValue)
602 return isInteractiveMode()
603 ? JOptionPane.showInternalInputDialog(parentComponent, message,
604 title, messageType, icon, selectionValues,
605 initialSelectionValue)
606 : getMockResponse().toString();
610 ///////////// end of options ///////////////
613 private static void outputMessage(Object message)
615 System.out.println(">>> JOption Message : " + message.toString());
618 public static Object getMockResponse()
623 public static void setMockResponse(Object mockOption)
625 JvOptionPane.mockResponse = mockOption;
628 public static void resetMock()
630 setMockResponse(JvOptionPane.CANCEL_OPTION);
631 setInteractiveMode(true);
634 public static boolean isInteractiveMode()
636 return interactiveMode;
639 public static void setInteractiveMode(boolean interactiveMode)
641 JvOptionPane.interactiveMode = interactiveMode;
644 @SuppressWarnings("unused")
645 private static String getPrefix(int messageType)
647 String prefix = ""; // JavaScript only
649 if (/** @j2sNative true || */
655 case JvOptionPane.INFORMATION_MESSAGE:
658 case JvOptionPane.WARNING_MESSAGE:
659 prefix = "WARNING! ";
661 case JvOptionPane.ERROR_MESSAGE: