JAL-3032 comments only - starting work on dialogs
[jalview.git] / src / jalview / gui / JvOptionPane.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21
22 package jalview.gui;
23
24 import java.awt.Component;
25 import java.awt.HeadlessException;
26
27 import javax.swing.Icon;
28 import javax.swing.JOptionPane;
29
30 public class JvOptionPane extends JOptionPane
31 {
32   // BH 2018 no changes needed here.
33
34   private static final long serialVersionUID = -3019167117756785229L;
35
36   private static Object mockResponse = JvOptionPane.CANCEL_OPTION;
37
38   private static boolean interactiveMode = true;
39
40   public static int showConfirmDialog(Component parentComponent,
41           Object message) throws HeadlessException
42   {
43     // only called by test
44     return isInteractiveMode()
45             ? JOptionPane.showConfirmDialog(parentComponent, message)
46             : (int) getMockResponse();
47   }
48
49   public static int showConfirmDialog(Component parentComponent,
50           Object message, String title, int optionType)
51           throws HeadlessException
52   {
53     return isInteractiveMode()
54             ? JOptionPane.showConfirmDialog(parentComponent, message, title,
55                     optionType)
56             : (int) getMockResponse();
57   }
58
59   public static int showConfirmDialog(Component parentComponent,
60           Object message, String title, int optionType, int messageType)
61           throws HeadlessException
62   {
63     return isInteractiveMode()
64             ? JOptionPane.showConfirmDialog(parentComponent, message, title,
65                     optionType, messageType)
66             : (int) getMockResponse();
67   }
68
69   public static int showConfirmDialog(Component parentComponent,
70           Object message, String title, int optionType, int messageType,
71           Icon icon) throws HeadlessException
72   {
73     return isInteractiveMode()
74             ? JOptionPane.showConfirmDialog(parentComponent, message, title,
75                     optionType, messageType, icon)
76             : (int) getMockResponse();
77   }
78
79   public static int showInternalConfirmDialog(Component parentComponent,
80           Object message)
81   {
82     return isInteractiveMode()
83             ? JOptionPane.showInternalConfirmDialog(parentComponent,
84                     message)
85             : (int) getMockResponse();
86   }
87
88   public static int showInternalConfirmDialog(Component parentComponent,
89           Object message, String title, int optionType)
90   {
91     return isInteractiveMode()
92             ? JOptionPane.showConfirmDialog(parentComponent, message, title,
93                     optionType)
94             : (int) getMockResponse();
95   }
96
97   public static int showInternalConfirmDialog(Component parentComponent,
98           Object message, String title, int optionType, int messageType)
99   {
100     return isInteractiveMode()
101             ? JOptionPane.showConfirmDialog(parentComponent, message, title,
102                     optionType, messageType)
103             : (int) getMockResponse();
104   }
105
106   public static int showInternalConfirmDialog(Component parentComponent,
107           Object message, String title, int optionType, int messageType,
108           Icon icon)
109   {
110     return isInteractiveMode()
111             ? JOptionPane.showInternalConfirmDialog(parentComponent,
112                     message, title, optionType, messageType, icon)
113             : (int) getMockResponse();
114   }
115
116   public static int showOptionDialog(Component parentComponent,
117           Object message, String title, int optionType, int messageType,
118           Icon icon, Object[] options, Object initialValue)
119           throws HeadlessException
120   {
121     return isInteractiveMode()
122             ? JOptionPane.showOptionDialog(parentComponent, message, title,
123                     optionType, messageType, icon, options, initialValue)
124             : (int) getMockResponse();
125   }
126
127   public static void showMessageDialog(Component parentComponent,
128           Object message) throws HeadlessException
129   {
130     if (isInteractiveMode())
131     {
132       JOptionPane.showMessageDialog(parentComponent, message);
133     }
134     else
135     {
136       outputMessage(message);
137     }
138   }
139
140   public static void showMessageDialog(Component parentComponent,
141           Object message, String title, int messageType)
142           throws HeadlessException
143   {
144     if (isInteractiveMode())
145     {
146       JOptionPane.showMessageDialog(parentComponent, message, title,
147               messageType);
148     }
149     else
150     {
151       outputMessage(message);
152     }
153   }
154
155   public static void showMessageDialog(Component parentComponent,
156           Object message, String title, int messageType, Icon icon)
157           throws HeadlessException
158   {
159     if (isInteractiveMode())
160     {
161       JOptionPane.showMessageDialog(parentComponent, message, title,
162               messageType, icon);
163     }
164     else
165     {
166       outputMessage(message);
167     }
168   }
169
170   public static void showInternalMessageDialog(Component parentComponent,
171           Object message)
172   {
173     if (isInteractiveMode())
174     {
175       JOptionPane.showMessageDialog(parentComponent, message);
176     }
177     else
178     {
179       outputMessage(message);
180     }
181   }
182
183   public static void showInternalMessageDialog(Component parentComponent,
184           Object message, String title, int messageType)
185   {
186     if (isInteractiveMode())
187     {
188       JOptionPane.showMessageDialog(parentComponent, message, title,
189               messageType);
190     }
191     else
192     {
193       outputMessage(message);
194     }
195   }
196
197   public static void showInternalMessageDialog(Component parentComponent,
198           Object message, String title, int messageType, Icon icon)
199   {
200     if (isInteractiveMode())
201     {
202       JOptionPane.showMessageDialog(parentComponent, message, title,
203               messageType, icon);
204     }
205     else
206     {
207       outputMessage(message);
208     }
209   }
210
211   public static String showInputDialog(Object message)
212           throws HeadlessException
213   {
214     return isInteractiveMode() ? JOptionPane.showInputDialog(message)
215             : getMockResponse().toString();
216   }
217
218   public static String showInputDialog(Object message,
219           Object initialSelectionValue)
220   {
221     return isInteractiveMode()
222             ? JOptionPane.showInputDialog(message, initialSelectionValue)
223             : getMockResponse().toString();
224   }
225
226   public static String showInputDialog(Component parentComponent,
227           Object message) throws HeadlessException
228   {
229     return isInteractiveMode()
230             ? JOptionPane.showInputDialog(parentComponent, message)
231             : getMockResponse().toString();
232   }
233
234   public static String showInputDialog(Component parentComponent,
235           Object message, Object initialSelectionValue)
236   {
237     return isInteractiveMode()
238             ? JOptionPane.showInputDialog(parentComponent, message,
239                     initialSelectionValue)
240             : getMockResponse().toString();
241   }
242
243   public static String showInputDialog(Component parentComponent,
244           Object message, String title, int messageType)
245           throws HeadlessException
246   {
247     return isInteractiveMode()
248             ? JOptionPane.showInputDialog(parentComponent, message, title,
249                     messageType)
250             : getMockResponse().toString();
251   }
252
253   public static Object showInputDialog(Component parentComponent,
254           Object message, String title, int messageType, Icon icon,
255           Object[] selectionValues, Object initialSelectionValue)
256           throws HeadlessException
257   {
258     return isInteractiveMode()
259             ? JOptionPane.showInputDialog(parentComponent, message, title,
260                     messageType, icon, selectionValues,
261                     initialSelectionValue)
262             : getMockResponse().toString();
263   }
264
265   public static String showInternalInputDialog(Component parentComponent,
266           Object message)
267   {
268     return isInteractiveMode()
269             ? JOptionPane.showInternalInputDialog(parentComponent, message)
270             : getMockResponse().toString();
271   }
272
273   public static String showInternalInputDialog(Component parentComponent,
274           Object message, String title, int messageType)
275   {
276     return isInteractiveMode()
277             ? JOptionPane.showInternalInputDialog(parentComponent, message,
278                     title, messageType)
279             : getMockResponse().toString();
280   }
281
282   public static Object showInternalInputDialog(Component parentComponent,
283           Object message, String title, int messageType, Icon icon,
284           Object[] selectionValues, Object initialSelectionValue)
285   {
286     return isInteractiveMode()
287             ? JOptionPane.showInternalInputDialog(parentComponent, message,
288                     title, messageType, icon, selectionValues,
289                     initialSelectionValue)
290             : getMockResponse().toString();
291   }
292
293   private static void outputMessage(Object message)
294   {
295     System.out.println(">>> JOption Message : " + message.toString());
296   }
297
298   public static Object getMockResponse()
299   {
300     return mockResponse;
301   }
302
303   public static void setMockResponse(Object mockOption)
304   {
305     JvOptionPane.mockResponse = mockOption;
306   }
307
308   public static void resetMock()
309   {
310     setMockResponse(JvOptionPane.CANCEL_OPTION);
311     setInteractiveMode(true);
312   }
313
314   public static boolean isInteractiveMode()
315   {
316     return interactiveMode;
317   }
318
319   public static void setInteractiveMode(boolean interactiveMode)
320   {
321     JvOptionPane.interactiveMode = interactiveMode;
322   }
323
324 }