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