JAL-1988 JAL-3772 Quit confirmation dialog boxes with saving files check and wait
[jalview.git] / src / jalview / jbgui / GDesktop.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 package jalview.jbgui;
22
23 import java.awt.FlowLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26
27 import javax.swing.JCheckBoxMenuItem;
28 import javax.swing.JFrame;
29 import javax.swing.JMenu;
30 import javax.swing.JMenuBar;
31 import javax.swing.JMenuItem;
32
33 import jalview.api.AlignmentViewPanel;
34 import jalview.bin.Cache;
35 import jalview.gui.Desktop;
36 import jalview.io.FileFormatException;
37 import jalview.jbgui.QuitHandler.QResponse;
38 import jalview.util.MessageManager;
39 import jalview.util.Platform;
40
41 /**
42  * DOCUMENT ME!
43  * 
44  * @author $author$
45  * @version $Revision$
46  */
47 @SuppressWarnings("serial")
48 public class GDesktop extends JFrame
49 {
50
51   protected static JMenu windowMenu = new JMenu();
52
53   JMenuBar desktopMenubar = new JMenuBar();
54
55   JMenu FileMenu = new JMenu();
56
57   JMenu HelpMenu = new JMenu();
58
59   JMenuItem inputLocalFileMenuItem = new JMenuItem();
60
61   JMenuItem inputURLMenuItem = new JMenuItem();
62
63   JMenuItem inputTextboxMenuItem = new JMenuItem();
64
65   JMenuItem quit = new JMenuItem();
66
67   JMenuItem aboutMenuItem = new JMenuItem();
68
69   JMenuItem documentationMenuItem = new JMenuItem();
70
71   FlowLayout flowLayout1 = new FlowLayout();
72
73   protected JMenu toolsMenu = new JMenu();
74
75   JMenuItem preferences = new JMenuItem();
76
77   JMenuItem saveState = new JMenuItem();
78
79   JMenuItem saveAsState = new JMenuItem();
80
81   JMenuItem loadState = new JMenuItem();
82
83   JMenu inputMenu = new JMenu();
84
85   JMenuItem inputSequence = new JMenuItem();
86
87   JMenuItem closeAll = new JMenuItem();
88
89   JMenuItem raiseRelated = new JMenuItem();
90
91   JMenuItem minimizeAssociated = new JMenuItem();
92
93   protected JCheckBoxMenuItem showMemusage = new JCheckBoxMenuItem();
94
95   JMenuItem garbageCollect = new JMenuItem();
96
97   protected JMenuItem groovyShell;
98
99   protected JCheckBoxMenuItem experimentalFeatures;
100
101   protected JCheckBoxMenuItem showConsole = new JCheckBoxMenuItem();
102
103   protected JCheckBoxMenuItem showNews = new JCheckBoxMenuItem();
104
105   protected JMenuItem snapShotWindow = new JMenuItem();
106
107   /**
108    * Creates a new GDesktop object.
109    */
110   public GDesktop()
111   {
112     super();
113     try
114     {
115       jbInit();
116       this.setJMenuBar(desktopMenubar);
117     } catch (Exception e)
118     {
119       e.printStackTrace();
120     }
121
122     if (Platform.allowMnemonics())
123     {
124       // BH was !Platform.isAMacAndNotJS()) i.e. "JS or not Mac"
125       // but here we want just not a Mac, period, right?
126       FileMenu.setMnemonic('F');
127       inputLocalFileMenuItem.setMnemonic('L');
128       inputURLMenuItem.setMnemonic('U');
129       inputTextboxMenuItem.setMnemonic('C');
130       quit.setMnemonic('Q');
131       saveState.setMnemonic('S');
132       loadState.setMnemonic('L');
133       inputMenu.setMnemonic('I');
134     }
135   }
136
137   /**
138    * DOCUMENT ME!
139    * 
140    * @throws Exception
141    *           DOCUMENT ME!
142    */
143   private void jbInit() throws Exception
144   {
145     boolean apqHandlersSet = false;
146     /**
147      * APQHandlers sets handlers for About, Preferences and Quit actions
148      * peculiar to macOS's application menu. APQHandlers will check to see if a
149      * handler is supported before setting it.
150      */
151     try
152     {
153       apqHandlersSet = APQHandlers.setAPQHandlers(this);
154     } catch (Exception e)
155     {
156       System.out.println("Cannot set APQHandlers");
157       // e.printStackTrace();
158     } catch (Throwable t)
159     {
160       jalview.bin.Console
161               .warn("Error setting APQHandlers: " + t.toString());
162       jalview.bin.Console.trace(Cache.getStackTraceString(t));
163     }
164
165     setName("jalview-desktop");
166     FileMenu.setText(MessageManager.getString("action.file"));
167     HelpMenu.setText(MessageManager.getString("action.help"));
168     inputLocalFileMenuItem
169             .setText(MessageManager.getString("label.load_tree_from_file"));
170     inputLocalFileMenuItem
171             .setAccelerator(
172                     javax.swing.KeyStroke
173                             .getKeyStroke(java.awt.event.KeyEvent.VK_O,
174                                     jalview.util.ShortcutKeyMaskExWrapper
175                                             .getMenuShortcutKeyMaskEx(),
176                                     false));
177     inputLocalFileMenuItem
178             .addActionListener(new java.awt.event.ActionListener()
179             {
180               @Override
181               public void actionPerformed(ActionEvent e)
182               {
183                 inputLocalFileMenuItem_actionPerformed(null);
184               }
185             });
186     inputURLMenuItem.setText(MessageManager.getString("label.from_url"));
187     inputURLMenuItem.addActionListener(new java.awt.event.ActionListener()
188     {
189       @Override
190       public void actionPerformed(ActionEvent e)
191       {
192         try
193         {
194           inputURLMenuItem_actionPerformed(null);
195         } catch (FileFormatException e1)
196         {
197           System.err.println("Error loading from URL: " + e1.getMessage());
198         }
199       }
200     });
201     inputTextboxMenuItem
202             .setText(MessageManager.getString("label.from_textbox"));
203     inputTextboxMenuItem
204             .addActionListener(new java.awt.event.ActionListener()
205             {
206               @Override
207               public void actionPerformed(ActionEvent e)
208               {
209                 inputTextboxMenuItem_actionPerformed(null);
210               }
211             });
212     quit.setText(MessageManager.getString("action.quit"));
213     quit.addActionListener(new java.awt.event.ActionListener()
214     {
215       @Override
216       public void actionPerformed(ActionEvent e)
217       {
218         QResponse qresponse = Desktop.instance != null
219                 ? Desktop.instance.desktopQuit()
220                 : QResponse.QUIT;
221         if (qresponse == QResponse.CANCEL_QUIT)
222         {
223           jalview.bin.Console
224                   .debug("GDesktop: Quit action cancelled by user");
225         }
226       }
227     });
228     aboutMenuItem.setText(MessageManager.getString("label.about"));
229     aboutMenuItem.addActionListener(new java.awt.event.ActionListener()
230     {
231       @Override
232       public void actionPerformed(ActionEvent e)
233       {
234         aboutMenuItem_actionPerformed(e);
235       }
236     });
237     documentationMenuItem
238             .setText(MessageManager.getString("label.documentation"));
239     documentationMenuItem.setAccelerator(javax.swing.KeyStroke
240             .getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0, false));
241     documentationMenuItem
242             .addActionListener(new java.awt.event.ActionListener()
243             {
244               @Override
245               public void actionPerformed(ActionEvent e)
246               {
247                 documentationMenuItem_actionPerformed();
248               }
249             });
250     this.getContentPane().setLayout(flowLayout1);
251     windowMenu.setText(MessageManager.getString("label.window"));
252     preferences.setText(MessageManager.getString("label.preferences"));
253     preferences.addActionListener(new ActionListener()
254     {
255       @Override
256       public void actionPerformed(ActionEvent e)
257       {
258         preferences_actionPerformed(e);
259       }
260     });
261     toolsMenu.setText(MessageManager.getString("label.tools"));
262     saveState.setText(MessageManager.getString("action.save_project"));
263     saveState.addActionListener(new ActionListener()
264     {
265       @Override
266       public void actionPerformed(ActionEvent e)
267       {
268         saveState_actionPerformed();
269       }
270     });
271     saveAsState.setText(MessageManager.getString("action.save_project_as"));
272     saveAsState.addActionListener(new ActionListener()
273     {
274       @Override
275       public void actionPerformed(ActionEvent e)
276       {
277         saveAsState_actionPerformed(e);
278       }
279     });
280     loadState.setText(MessageManager.getString("action.load_project"));
281     loadState.addActionListener(new ActionListener()
282     {
283       @Override
284       public void actionPerformed(ActionEvent e)
285       {
286         loadState_actionPerformed();
287       }
288     });
289     inputMenu.setText(MessageManager.getString("label.input_alignment"));
290     inputSequence
291             .setText(MessageManager.getString("action.fetch_sequences"));
292     inputSequence.addActionListener(new ActionListener()
293     {
294       @Override
295       public void actionPerformed(ActionEvent e)
296       {
297         inputSequence_actionPerformed(e);
298       }
299     });
300     closeAll.setText(MessageManager.getString("action.close_all"));
301     closeAll.addActionListener(new ActionListener()
302     {
303       @Override
304       public void actionPerformed(ActionEvent e)
305       {
306         closeAll_actionPerformed(e);
307       }
308     });
309     raiseRelated.setText(
310             MessageManager.getString("action.raise_associated_windows"));
311     raiseRelated.addActionListener(new ActionListener()
312     {
313       @Override
314       public void actionPerformed(ActionEvent e)
315       {
316         raiseRelated_actionPerformed(e);
317       }
318     });
319     minimizeAssociated.setText(
320             MessageManager.getString("action.minimize_associated_windows"));
321     minimizeAssociated.addActionListener(new ActionListener()
322     {
323       @Override
324       public void actionPerformed(ActionEvent e)
325       {
326         minimizeAssociated_actionPerformed(e);
327       }
328     });
329     garbageCollect
330             .setText(MessageManager.getString("label.collect_garbage"));
331     garbageCollect.addActionListener(new ActionListener()
332     {
333       @Override
334       public void actionPerformed(ActionEvent e)
335       {
336         garbageCollect_actionPerformed(e);
337       }
338     });
339     showMemusage
340             .setText(MessageManager.getString("label.show_memory_usage"));
341     showMemusage.addActionListener(new ActionListener()
342     {
343       @Override
344       public void actionPerformed(ActionEvent e)
345       {
346         showMemusage_actionPerformed(e);
347       }
348     });
349     showConsole
350             .setText(MessageManager.getString("label.show_java_console"));
351     showConsole.addActionListener(new ActionListener()
352     {
353       @Override
354       public void actionPerformed(ActionEvent e)
355       {
356         showConsole_actionPerformed(e);
357       }
358     });
359     showNews.setText(MessageManager.getString("label.show_jalview_news"));
360     showNews.addActionListener(new ActionListener()
361     {
362       @Override
363       public void actionPerformed(ActionEvent e)
364       {
365         showNews_actionPerformed(e);
366       }
367     });
368     groovyShell = new JMenuItem();
369     groovyShell.setText(MessageManager.getString("label.groovy_console"));
370     groovyShell.addActionListener(new ActionListener()
371     {
372       @Override
373       public void actionPerformed(ActionEvent e)
374       {
375         groovyShell_actionPerformed();
376       }
377     });
378     experimentalFeatures = new JCheckBoxMenuItem();
379     experimentalFeatures
380             .setText(MessageManager.getString("label.show_experimental"));
381     experimentalFeatures.setToolTipText(
382             MessageManager.getString("label.show_experimental_tip"));
383     experimentalFeatures.addActionListener(new ActionListener()
384     {
385       @Override
386       public void actionPerformed(ActionEvent e)
387       {
388         showExperimental_actionPerformed(experimentalFeatures.isSelected());
389       }
390     });
391
392     snapShotWindow.setText(MessageManager.getString("label.take_snapshot"));
393     snapShotWindow.addActionListener(new ActionListener()
394     {
395       @Override
396       public void actionPerformed(ActionEvent e)
397       {
398         snapShotWindow_actionPerformed(e);
399       }
400     });
401
402     desktopMenubar.add(FileMenu);
403     desktopMenubar.add(toolsMenu);
404     desktopMenubar.add(HelpMenu);
405     desktopMenubar.add(windowMenu);
406     FileMenu.add(inputMenu);
407     FileMenu.add(inputSequence);
408     FileMenu.addSeparator();
409     // FileMenu.add(saveState);
410     FileMenu.add(saveAsState);
411     FileMenu.add(loadState);
412     FileMenu.addSeparator();
413     if (!APQHandlers.setQuit)
414     {
415       FileMenu.add(quit);
416     }
417     if (!APQHandlers.setAbout)
418     {
419       HelpMenu.add(aboutMenuItem);
420     }
421     HelpMenu.add(documentationMenuItem);
422     if (!APQHandlers.setPreferences)
423     {
424       toolsMenu.add(preferences);
425     }
426     if (!Platform.isJS())
427     {
428       toolsMenu.add(showMemusage);
429       toolsMenu.add(showConsole);
430       toolsMenu.add(showNews);
431       toolsMenu.add(garbageCollect);
432       toolsMenu.add(groovyShell);
433     }
434     toolsMenu.add(experimentalFeatures);
435     // toolsMenu.add(snapShotWindow);
436     inputMenu.add(inputLocalFileMenuItem);
437     inputMenu.add(inputURLMenuItem);
438     inputMenu.add(inputTextboxMenuItem);
439     windowMenu.add(closeAll);
440     windowMenu.add(raiseRelated);
441     windowMenu.add(minimizeAssociated);
442     windowMenu.addSeparator();
443     // inputMenu.add(vamsasLoad);
444   }
445
446   protected void showExperimental_actionPerformed(boolean selected)
447   {
448   }
449
450   protected void groovyShell_actionPerformed()
451   {
452   }
453
454   protected void snapShotWindow_actionPerformed(ActionEvent e)
455   {
456     // TODO Auto-generated method stub
457
458   }
459
460   protected void showConsole_actionPerformed(ActionEvent e)
461   {
462     // TODO Auto-generated method stub
463
464   }
465
466   protected void showNews_actionPerformed(ActionEvent e)
467   {
468     // TODO Auto-generated method stub
469
470   }
471
472   protected void showMemusage_actionPerformed(ActionEvent e)
473   {
474     // TODO Auto-generated method stub
475
476   }
477
478   protected void garbageCollect_actionPerformed(ActionEvent e)
479   {
480     // TODO Auto-generated method stub
481
482   }
483
484   protected void vamsasStMenu_actionPerformed()
485   {
486   }
487
488   public void vamsasSave_actionPerformed(ActionEvent e)
489   {
490   }
491
492   /**
493    * DOCUMENT ME!
494    * 
495    * @param e
496    *          DOCUMENT ME!
497    */
498   protected void inputLocalFileMenuItem_actionPerformed(
499           jalview.gui.AlignViewport av)
500   {
501   }
502
503   /**
504    * DOCUMENT ME!
505    * 
506    * @param e
507    *          DOCUMENT ME!
508    * @throws FileFormatException
509    */
510   protected void inputURLMenuItem_actionPerformed(
511           jalview.gui.AlignViewport av) throws FileFormatException
512   {
513   }
514
515   /**
516    * DOCUMENT ME!
517    * 
518    * @param e
519    *          DOCUMENT ME!
520    */
521   protected void inputTextboxMenuItem_actionPerformed(
522           AlignmentViewPanel avp)
523   {
524   }
525
526   /**
527    * DOCUMENT ME!
528    */
529   protected void quit()
530   {
531     // System.out.println("********** GDesktop.quit()");
532   }
533
534   /**
535    * DOCUMENT ME!
536    * 
537    * @param e
538    *          DOCUMENT ME!
539    */
540   protected void aboutMenuItem_actionPerformed(ActionEvent e)
541   {
542   }
543
544   protected void documentationMenuItem_actionPerformed()
545   {
546   }
547
548   /**
549    * DOCUMENT ME!
550    * 
551    * @param e
552    *          DOCUMENT ME!
553    */
554   protected void preferences_actionPerformed(ActionEvent e)
555   {
556   }
557
558   /**
559    * DOCUMENT ME!
560    * 
561    * @param e
562    *          DOCUMENT ME!
563    */
564   public void saveState_actionPerformed()
565   {
566   }
567
568   public void saveAsState_actionPerformed(ActionEvent e)
569   {
570   }
571
572   /**
573    * DOCUMENT ME!
574    * 
575    * @param e
576    *          DOCUMENT ME!
577    */
578   public void loadState_actionPerformed()
579   {
580   }
581
582   /**
583    * DOCUMENT ME!
584    * 
585    * @param e
586    *          DOCUMENT ME!
587    */
588   public void loadJalviewAlign_actionPerformed(ActionEvent e)
589   {
590   }
591
592   public void vamsasStart_actionPerformed(ActionEvent e)
593   {
594
595   }
596
597   public void inputSequence_actionPerformed(ActionEvent e)
598   {
599
600   }
601
602   public void vamsasStop_actionPerformed(ActionEvent e)
603   {
604
605   }
606
607   public void closeAll_actionPerformed(ActionEvent e)
608   {
609
610   }
611
612   public void raiseRelated_actionPerformed(ActionEvent e)
613   {
614
615   }
616
617   public void minimizeAssociated_actionPerformed(ActionEvent e)
618   {
619
620   }
621
622   public void vamsasImport_actionPerformed(ActionEvent e)
623   {
624   }
625 }