JAL-1988 Check for setHandler capability to avoid About and Preferences menu options...
[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     FileMenu.add(quit);
408     if (!APQHandlers.setAbout)
409     {
410       HelpMenu.add(aboutMenuItem);
411     }
412     HelpMenu.add(documentationMenuItem);
413     if (!APQHandlers.setPreferences)
414     {
415       toolsMenu.add(preferences);
416     }
417     if (!Platform.isJS())
418     {
419       toolsMenu.add(showMemusage);
420       toolsMenu.add(showConsole);
421       toolsMenu.add(showNews);
422       toolsMenu.add(garbageCollect);
423       toolsMenu.add(groovyShell);
424     }
425     toolsMenu.add(experimentalFeatures);
426     // toolsMenu.add(snapShotWindow);
427     inputMenu.add(inputLocalFileMenuItem);
428     inputMenu.add(inputURLMenuItem);
429     inputMenu.add(inputTextboxMenuItem);
430     windowMenu.add(closeAll);
431     windowMenu.add(raiseRelated);
432     windowMenu.add(minimizeAssociated);
433     windowMenu.addSeparator();
434     // inputMenu.add(vamsasLoad);
435   }
436
437   protected void showExperimental_actionPerformed(boolean selected)
438   {
439   }
440
441   protected void groovyShell_actionPerformed()
442   {
443   }
444
445   protected void snapShotWindow_actionPerformed(ActionEvent e)
446   {
447     // TODO Auto-generated method stub
448
449   }
450
451   protected void showConsole_actionPerformed(ActionEvent e)
452   {
453     // TODO Auto-generated method stub
454
455   }
456
457   protected void showNews_actionPerformed(ActionEvent e)
458   {
459     // TODO Auto-generated method stub
460
461   }
462
463   protected void showMemusage_actionPerformed(ActionEvent e)
464   {
465     // TODO Auto-generated method stub
466
467   }
468
469   protected void garbageCollect_actionPerformed(ActionEvent e)
470   {
471     // TODO Auto-generated method stub
472
473   }
474
475   protected void vamsasStMenu_actionPerformed()
476   {
477   }
478
479   public void vamsasSave_actionPerformed(ActionEvent e)
480   {
481   }
482
483   /**
484    * DOCUMENT ME!
485    * 
486    * @param e
487    *          DOCUMENT ME!
488    */
489   protected void inputLocalFileMenuItem_actionPerformed(
490           jalview.gui.AlignViewport av)
491   {
492   }
493
494   /**
495    * DOCUMENT ME!
496    * 
497    * @param e
498    *          DOCUMENT ME!
499    * @throws FileFormatException
500    */
501   protected void inputURLMenuItem_actionPerformed(
502           jalview.gui.AlignViewport av) throws FileFormatException
503   {
504   }
505
506   /**
507    * DOCUMENT ME!
508    * 
509    * @param e
510    *          DOCUMENT ME!
511    */
512   protected void inputTextboxMenuItem_actionPerformed(
513           AlignmentViewPanel avp)
514   {
515   }
516
517   /**
518    * DOCUMENT ME!
519    */
520   protected void quit()
521   {
522     // System.out.println("********** GDesktop.quit()");
523   }
524
525   /**
526    * DOCUMENT ME!
527    * 
528    * @param e
529    *          DOCUMENT ME!
530    */
531   protected void aboutMenuItem_actionPerformed(ActionEvent e)
532   {
533   }
534
535   protected void documentationMenuItem_actionPerformed()
536   {
537   }
538
539   /**
540    * DOCUMENT ME!
541    * 
542    * @param e
543    *          DOCUMENT ME!
544    */
545   protected void preferences_actionPerformed(ActionEvent e)
546   {
547   }
548
549   /**
550    * DOCUMENT ME!
551    * 
552    * @param e
553    *          DOCUMENT ME!
554    */
555   public void saveState_actionPerformed()
556   {
557   }
558
559   public void saveAsState_actionPerformed(ActionEvent e)
560   {
561   }
562
563   /**
564    * DOCUMENT ME!
565    * 
566    * @param e
567    *          DOCUMENT ME!
568    */
569   public void loadState_actionPerformed()
570   {
571   }
572
573   /**
574    * DOCUMENT ME!
575    * 
576    * @param e
577    *          DOCUMENT ME!
578    */
579   public void loadJalviewAlign_actionPerformed(ActionEvent e)
580   {
581   }
582
583   public void vamsasStart_actionPerformed(ActionEvent e)
584   {
585
586   }
587
588   public void inputSequence_actionPerformed(ActionEvent e)
589   {
590
591   }
592
593   public void vamsasStop_actionPerformed(ActionEvent e)
594   {
595
596   }
597
598   public void closeAll_actionPerformed(ActionEvent e)
599   {
600
601   }
602
603   public void raiseRelated_actionPerformed(ActionEvent e)
604   {
605
606   }
607
608   public void minimizeAssociated_actionPerformed(ActionEvent e)
609   {
610
611   }
612
613   public void vamsasImport_actionPerformed(ActionEvent e)
614   {
615   }
616 }