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