JAL-1988 This fixes the Quit response when user cancels the quit
[jalview.git] / src / jalview / gui / Desktop.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.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.bin.Cache;
26 import jalview.bin.Jalview;
27 import jalview.io.DataSourceType;
28 import jalview.io.FileFormat;
29 import jalview.io.FileFormatException;
30 import jalview.io.FileFormatI;
31 import jalview.io.FileFormats;
32 import jalview.io.FileLoader;
33 import jalview.io.FormatAdapter;
34 import jalview.io.IdentifyFile;
35 import jalview.io.JalviewFileChooser;
36 import jalview.io.JalviewFileView;
37 import jalview.jbgui.GSplitFrame;
38 import jalview.jbgui.GStructureViewer;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.urls.IdOrgSettings;
41 import jalview.util.ImageMaker;
42 import jalview.util.MessageManager;
43 import jalview.util.Platform;
44 import jalview.util.UrlConstants;
45 import jalview.viewmodel.AlignmentViewport;
46 import jalview.ws.params.ParamManager;
47 import jalview.ws.utils.UrlDownloadClient;
48
49 import java.awt.BorderLayout;
50 import java.awt.Color;
51 import java.awt.Dimension;
52 import java.awt.FontMetrics;
53 import java.awt.Graphics;
54 import java.awt.GridLayout;
55 import java.awt.Point;
56 import java.awt.Rectangle;
57 import java.awt.Toolkit;
58 import java.awt.Window;
59 import java.awt.datatransfer.Clipboard;
60 import java.awt.datatransfer.ClipboardOwner;
61 import java.awt.datatransfer.DataFlavor;
62 import java.awt.datatransfer.Transferable;
63 import java.awt.desktop.AboutEvent;
64 import java.awt.desktop.AboutHandler;
65 import java.awt.desktop.PreferencesEvent;
66 import java.awt.desktop.PreferencesHandler;
67 import java.awt.desktop.QuitEvent;
68 import java.awt.desktop.QuitHandler;
69 import java.awt.desktop.QuitResponse;
70 import java.awt.desktop.QuitStrategy;
71 import java.awt.dnd.DnDConstants;
72 import java.awt.dnd.DropTargetDragEvent;
73 import java.awt.dnd.DropTargetDropEvent;
74 import java.awt.dnd.DropTargetEvent;
75 import java.awt.dnd.DropTargetListener;
76 import java.awt.event.ActionEvent;
77 import java.awt.event.ActionListener;
78 import java.awt.event.InputEvent;
79 import java.awt.event.KeyEvent;
80 import java.awt.event.MouseAdapter;
81 import java.awt.event.MouseEvent;
82 import java.awt.event.WindowAdapter;
83 import java.awt.event.WindowEvent;
84 import java.beans.PropertyChangeEvent;
85 import java.beans.PropertyChangeListener;
86 import java.io.BufferedInputStream;
87 import java.io.File;
88 import java.io.FileOutputStream;
89 import java.io.FileWriter;
90 import java.io.IOException;
91 import java.net.URL;
92 import java.util.ArrayList;
93 import java.util.HashMap;
94 import java.util.Hashtable;
95 import java.util.List;
96 import java.util.ListIterator;
97 import java.util.StringTokenizer;
98 import java.util.Vector;
99 import java.util.concurrent.ExecutorService;
100 import java.util.concurrent.Executors;
101 import java.util.concurrent.Semaphore;
102
103 import javax.swing.AbstractAction;
104 import javax.swing.Action;
105 import javax.swing.ActionMap;
106 import javax.swing.Box;
107 import javax.swing.BoxLayout;
108 import javax.swing.DefaultDesktopManager;
109 import javax.swing.DesktopManager;
110 import javax.swing.InputMap;
111 import javax.swing.JButton;
112 import javax.swing.JCheckBox;
113 import javax.swing.JComboBox;
114 import javax.swing.JComponent;
115 import javax.swing.JDesktopPane;
116 import javax.swing.JInternalFrame;
117 import javax.swing.JLabel;
118 import javax.swing.JMenuItem;
119 import javax.swing.JOptionPane;
120 import javax.swing.JPanel;
121 import javax.swing.JPopupMenu;
122 import javax.swing.JProgressBar;
123 import javax.swing.KeyStroke;
124 import javax.swing.SwingUtilities;
125 import javax.swing.event.HyperlinkEvent;
126 import javax.swing.event.HyperlinkEvent.EventType;
127 import javax.swing.event.InternalFrameAdapter;
128 import javax.swing.event.InternalFrameEvent;
129 import javax.swing.event.MenuEvent;
130 import javax.swing.event.MenuListener;
131
132 import org.stackoverflowusers.file.WindowsShortcut;
133
134 /**
135  * Jalview Desktop
136  * 
137  * 
138  * @author $author$
139  * @version $Revision: 1.155 $
140  */
141 public class Desktop extends jalview.jbgui.GDesktop
142         implements DropTargetListener, ClipboardOwner, IProgressIndicator,
143         jalview.api.StructureSelectionManagerProvider
144 {
145   private static int DEFAULT_MIN_WIDTH = 300;
146
147   private static int DEFAULT_MIN_HEIGHT = 250;
148
149   private static int ALIGN_FRAME_DEFAULT_MIN_WIDTH = 600;
150
151   private static int ALIGN_FRAME_DEFAULT_MIN_HEIGHT = 70;
152
153   private static final String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES";
154
155   public static HashMap<String, FileWriter> savingFiles = new HashMap<>();
156
157   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
158
159   /**
160    * news reader - null if it was never started.
161    */
162   private BlogReader jvnews = null;
163
164   private File projectFile;
165
166   /**
167    * @param listener
168    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
169    */
170   public void addJalviewPropertyChangeListener(
171           PropertyChangeListener listener)
172   {
173     changeSupport.addJalviewPropertyChangeListener(listener);
174   }
175
176   /**
177    * @param propertyName
178    * @param listener
179    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.lang.String,
180    *      java.beans.PropertyChangeListener)
181    */
182   public void addJalviewPropertyChangeListener(String propertyName,
183           PropertyChangeListener listener)
184   {
185     changeSupport.addJalviewPropertyChangeListener(propertyName, listener);
186   }
187
188   /**
189    * @param propertyName
190    * @param listener
191    * @see jalview.gui.JalviewChangeSupport#removeJalviewPropertyChangeListener(java.lang.String,
192    *      java.beans.PropertyChangeListener)
193    */
194   public void removeJalviewPropertyChangeListener(String propertyName,
195           PropertyChangeListener listener)
196   {
197     changeSupport.removeJalviewPropertyChangeListener(propertyName,
198             listener);
199   }
200
201   /** Singleton Desktop instance */
202   public static Desktop instance;
203
204   public static MyDesktopPane desktop;
205
206   static int openFrameCount = 0;
207
208   static final int xOffset = 30;
209
210   static final int yOffset = 30;
211
212   public static jalview.ws.jws1.Discoverer discoverer;
213
214   public static Object[] jalviewClipboard;
215
216   public static boolean internalCopy = false;
217
218   static int fileLoadingCount = 0;
219
220   class MyDesktopManager implements DesktopManager
221   {
222
223     private DesktopManager delegate;
224
225     public MyDesktopManager(DesktopManager delegate)
226     {
227       this.delegate = delegate;
228     }
229
230     @Override
231     public void activateFrame(JInternalFrame f)
232     {
233       try
234       {
235         delegate.activateFrame(f);
236       } catch (NullPointerException npe)
237       {
238         Point p = getMousePosition();
239         instance.showPasteMenu(p.x, p.y);
240       }
241     }
242
243     @Override
244     public void beginDraggingFrame(JComponent f)
245     {
246       delegate.beginDraggingFrame(f);
247     }
248
249     @Override
250     public void beginResizingFrame(JComponent f, int direction)
251     {
252       delegate.beginResizingFrame(f, direction);
253     }
254
255     @Override
256     public void closeFrame(JInternalFrame f)
257     {
258       delegate.closeFrame(f);
259     }
260
261     @Override
262     public void deactivateFrame(JInternalFrame f)
263     {
264       delegate.deactivateFrame(f);
265     }
266
267     @Override
268     public void deiconifyFrame(JInternalFrame f)
269     {
270       delegate.deiconifyFrame(f);
271     }
272
273     @Override
274     public void dragFrame(JComponent f, int newX, int newY)
275     {
276       if (newY < 0)
277       {
278         newY = 0;
279       }
280       delegate.dragFrame(f, newX, newY);
281     }
282
283     @Override
284     public void endDraggingFrame(JComponent f)
285     {
286       delegate.endDraggingFrame(f);
287       desktop.repaint();
288     }
289
290     @Override
291     public void endResizingFrame(JComponent f)
292     {
293       delegate.endResizingFrame(f);
294       desktop.repaint();
295     }
296
297     @Override
298     public void iconifyFrame(JInternalFrame f)
299     {
300       delegate.iconifyFrame(f);
301     }
302
303     @Override
304     public void maximizeFrame(JInternalFrame f)
305     {
306       delegate.maximizeFrame(f);
307     }
308
309     @Override
310     public void minimizeFrame(JInternalFrame f)
311     {
312       delegate.minimizeFrame(f);
313     }
314
315     @Override
316     public void openFrame(JInternalFrame f)
317     {
318       delegate.openFrame(f);
319     }
320
321     @Override
322     public void resizeFrame(JComponent f, int newX, int newY, int newWidth,
323             int newHeight)
324     {
325       if (newY < 0)
326       {
327         newY = 0;
328       }
329       delegate.resizeFrame(f, newX, newY, newWidth, newHeight);
330     }
331
332     @Override
333     public void setBoundsForFrame(JComponent f, int newX, int newY,
334             int newWidth, int newHeight)
335     {
336       delegate.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
337     }
338
339     // All other methods, simply delegate
340
341   }
342
343   /**
344    * Creates a new Desktop object.
345    */
346   public Desktop()
347   {
348     /**
349      * A note to implementors. It is ESSENTIAL that any activities that might
350      * block are spawned off as threads rather than waited for during this
351      * constructor.
352      */
353     instance = this;
354     doVamsasClientCheck();
355
356     doConfigureStructurePrefs();
357     setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
358     /*
359     if (!Platform.isAMac())
360     {
361       // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
362     }
363     else
364     */
365     // {
366     // this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
367     java.awt.Desktop hdesktop = java.awt.Desktop.getDesktop();
368
369     hdesktop.setAboutHandler(new AboutHandler()
370     {
371       @Override
372       public void handleAbout(AboutEvent e)
373       {
374         aboutMenuItem_actionPerformed(null);
375       }
376     });
377     hdesktop.setPreferencesHandler(new PreferencesHandler()
378     {
379       @Override
380       public void handlePreferences(PreferencesEvent e)
381       {
382         preferences_actionPerformed(null);
383       }
384     });
385     hdesktop.setQuitHandler(new QuitHandler()
386     {
387       @Override
388       public void handleQuitRequestWith(QuitEvent e, QuitResponse r)
389       {
390         int n = JOptionPane.showConfirmDialog(null,
391                 MessageManager.getString("label.quit_jalview"),
392                 MessageManager.getString("action.quit"),
393                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
394                 null);
395         if (n == JOptionPane.OK_OPTION)
396         {
397           System.out.println("Shortcut Quit confirmed by user");
398           quit();
399           r.performQuit(); // probably won't reach this line, but just in case
400         }
401         else
402         {
403           r.cancelQuit();
404           System.out.println("Shortcut Quit cancelled by user");
405         }
406       }
407     });
408     hdesktop.setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
409
410     // }
411
412     addWindowListener(new WindowAdapter()
413     {
414
415       @Override
416       public void windowClosing(WindowEvent ev)
417       {
418         quit();
419       }
420     });
421
422     boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",
423             false);
424
425     boolean showjconsole = jalview.bin.Cache.getDefault("SHOW_JAVA_CONSOLE",
426             false);
427     desktop = new MyDesktopPane(selmemusage);
428     showMemusage.setSelected(selmemusage);
429     desktop.setBackground(Color.white);
430
431     getContentPane().setLayout(new BorderLayout());
432     // alternate config - have scrollbars - see notes in JAL-153
433     // JScrollPane sp = new JScrollPane();
434     // sp.getViewport().setView(desktop);
435     // getContentPane().add(sp, BorderLayout.CENTER);
436     getContentPane().add(desktop, BorderLayout.CENTER);
437     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
438
439     // This line prevents Windows Look&Feel resizing all new windows to maximum
440     // if previous window was maximised
441     desktop.setDesktopManager(new MyDesktopManager(
442             (Platform.isWindows() ? new DefaultDesktopManager()
443                     : Platform.isAMac()
444                             ? new AquaInternalFrameManager(
445                                     desktop.getDesktopManager())
446                             : desktop.getDesktopManager())));
447
448     Rectangle dims = getLastKnownDimensions("");
449     if (dims != null)
450     {
451       setBounds(dims);
452     }
453     else
454     {
455       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
456       setBounds((screenSize.width - 900) / 2, (screenSize.height - 650) / 2,
457               900, 650);
458     }
459     jconsole = new Console(this, showjconsole);
460     // add essential build information
461     jconsole.setHeader(
462             "Jalview Version: " + jalview.bin.Cache.getProperty("VERSION")
463                     + "\n" + "Jalview Installation: "
464                     + jalview.bin.Cache.getDefault("INSTALLATION",
465                             "unknown")
466                     + "\n" + "Build Date: "
467                     + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")
468                     + "\n" + "Java version: "
469                     + System.getProperty("java.version") + "\n"
470                     + System.getProperty("os.arch") + " "
471                     + System.getProperty("os.name") + " "
472                     + System.getProperty("os.version"));
473
474     showConsole(showjconsole);
475
476     showNews.setVisible(false);
477
478     experimentalFeatures.setSelected(showExperimental());
479
480     getIdentifiersOrgData();
481
482     checkURLLinks();
483
484     this.addWindowListener(new WindowAdapter()
485     {
486       @Override
487       public void windowClosing(WindowEvent evt)
488       {
489         quit();
490       }
491     });
492
493     MouseAdapter ma;
494     this.addMouseListener(ma = new MouseAdapter()
495     {
496       @Override
497       public void mousePressed(MouseEvent evt)
498       {
499         if (evt.isPopupTrigger()) // Mac
500         {
501           showPasteMenu(evt.getX(), evt.getY());
502         }
503       }
504
505       @Override
506       public void mouseReleased(MouseEvent evt)
507       {
508         if (evt.isPopupTrigger()) // Windows
509         {
510           showPasteMenu(evt.getX(), evt.getY());
511         }
512       }
513     });
514     desktop.addMouseListener(ma);
515
516     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
517     // Spawn a thread that shows the splashscreen
518     SwingUtilities.invokeLater(new Runnable()
519     {
520       @Override
521       public void run()
522       {
523         new SplashScreen();
524       }
525     });
526
527     // Thread off a new instance of the file chooser - this reduces the time it
528     // takes to open it later on.
529     new Thread(new Runnable()
530     {
531       @Override
532       public void run()
533       {
534         Cache.log.debug("Filechooser init thread started.");
535         String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
536         JalviewFileChooser.forRead(Cache.getProperty("LAST_DIRECTORY"),
537                 fileFormat);
538         Cache.log.debug("Filechooser init thread finished.");
539       }
540     }).start();
541     // Add the service change listener
542     changeSupport.addJalviewPropertyChangeListener("services",
543             new PropertyChangeListener()
544             {
545
546               @Override
547               public void propertyChange(PropertyChangeEvent evt)
548               {
549                 Cache.log.debug("Firing service changed event for "
550                         + evt.getNewValue());
551                 JalviewServicesChanged(evt);
552               }
553
554             });
555   }
556
557   /**
558    * Answers true if user preferences to enable experimental features is True
559    * (on), else false
560    * 
561    * @return
562    */
563   public boolean showExperimental()
564   {
565     String experimental = Cache.getDefault(EXPERIMENTAL_FEATURES,
566             Boolean.FALSE.toString());
567     return Boolean.valueOf(experimental).booleanValue();
568   }
569
570   public void doConfigureStructurePrefs()
571   {
572     // configure services
573     StructureSelectionManager ssm = StructureSelectionManager
574             .getStructureSelectionManager(this);
575     if (jalview.bin.Cache.getDefault(Preferences.ADD_SS_ANN, true))
576     {
577       ssm.setAddTempFacAnnot(jalview.bin.Cache
578               .getDefault(Preferences.ADD_TEMPFACT_ANN, true));
579       ssm.setProcessSecondaryStructure(jalview.bin.Cache
580               .getDefault(Preferences.STRUCT_FROM_PDB, true));
581       ssm.setSecStructServices(
582               jalview.bin.Cache.getDefault(Preferences.USE_RNAVIEW, true));
583     }
584     else
585     {
586       ssm.setAddTempFacAnnot(false);
587       ssm.setProcessSecondaryStructure(false);
588       ssm.setSecStructServices(false);
589     }
590   }
591
592   public void checkForNews()
593   {
594     final Desktop me = this;
595     // Thread off the news reader, in case there are connection problems.
596     new Thread(new Runnable()
597     {
598       @Override
599       public void run()
600       {
601         Cache.log.debug("Starting news thread.");
602
603         jvnews = new BlogReader(me);
604         showNews.setVisible(true);
605         Cache.log.debug("Completed news thread.");
606       }
607     }).start();
608   }
609
610   public void getIdentifiersOrgData()
611   {
612     // Thread off the identifiers fetcher
613     new Thread(new Runnable()
614     {
615       @Override
616       public void run()
617       {
618         Cache.log.debug("Downloading data from identifiers.org");
619         UrlDownloadClient client = new UrlDownloadClient();
620         try
621         {
622           client.download(IdOrgSettings.getUrl(),
623                   IdOrgSettings.getDownloadLocation());
624         } catch (IOException e)
625         {
626           Cache.log.debug("Exception downloading identifiers.org data"
627                   + e.getMessage());
628         }
629       }
630     }).start();
631     ;
632   }
633
634   @Override
635   protected void showNews_actionPerformed(ActionEvent e)
636   {
637     showNews(showNews.isSelected());
638   }
639
640   void showNews(boolean visible)
641   {
642     {
643       Cache.log.debug((visible ? "Showing" : "Hiding") + " news.");
644       showNews.setSelected(visible);
645       if (visible && !jvnews.isVisible())
646       {
647         new Thread(new Runnable()
648         {
649           @Override
650           public void run()
651           {
652             long now = System.currentTimeMillis();
653             Desktop.instance.setProgressBar(
654                     MessageManager.getString("status.refreshing_news"),
655                     now);
656             jvnews.refreshNews();
657             Desktop.instance.setProgressBar(null, now);
658             jvnews.showNews();
659           }
660         }).start();
661       }
662     }
663   }
664
665   /**
666    * recover the last known dimensions for a jalview window
667    * 
668    * @param windowName
669    *          - empty string is desktop, all other windows have unique prefix
670    * @return null or last known dimensions scaled to current geometry (if last
671    *         window geom was known)
672    */
673   Rectangle getLastKnownDimensions(String windowName)
674   {
675     // TODO: lock aspect ratio for scaling desktop Bug #0058199
676     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
677     String x = jalview.bin.Cache.getProperty(windowName + "SCREEN_X");
678     String y = jalview.bin.Cache.getProperty(windowName + "SCREEN_Y");
679     String width = jalview.bin.Cache
680             .getProperty(windowName + "SCREEN_WIDTH");
681     String height = jalview.bin.Cache
682             .getProperty(windowName + "SCREEN_HEIGHT");
683     if ((x != null) && (y != null) && (width != null) && (height != null))
684     {
685       int ix = Integer.parseInt(x), iy = Integer.parseInt(y),
686               iw = Integer.parseInt(width), ih = Integer.parseInt(height);
687       if (jalview.bin.Cache.getProperty("SCREENGEOMETRY_WIDTH") != null)
688       {
689         // attempt #1 - try to cope with change in screen geometry - this
690         // version doesn't preserve original jv aspect ratio.
691         // take ratio of current screen size vs original screen size.
692         double sw = ((1f * screenSize.width) / (1f * Integer.parseInt(
693                 jalview.bin.Cache.getProperty("SCREENGEOMETRY_WIDTH"))));
694         double sh = ((1f * screenSize.height) / (1f * Integer.parseInt(
695                 jalview.bin.Cache.getProperty("SCREENGEOMETRY_HEIGHT"))));
696         // rescale the bounds depending upon the current screen geometry.
697         ix = (int) (ix * sw);
698         iw = (int) (iw * sw);
699         iy = (int) (iy * sh);
700         ih = (int) (ih * sh);
701         while (ix >= screenSize.width)
702         {
703           jalview.bin.Cache.log.debug(
704                   "Window geometry location recall error: shifting horizontal to within screenbounds.");
705           ix -= screenSize.width;
706         }
707         while (iy >= screenSize.height)
708         {
709           jalview.bin.Cache.log.debug(
710                   "Window geometry location recall error: shifting vertical to within screenbounds.");
711           iy -= screenSize.height;
712         }
713         jalview.bin.Cache.log.debug(
714                 "Got last known dimensions for " + windowName + ": x:" + ix
715                         + " y:" + iy + " width:" + iw + " height:" + ih);
716       }
717       // return dimensions for new instance
718       return new Rectangle(ix, iy, iw, ih);
719     }
720     return null;
721   }
722
723   private void doVamsasClientCheck()
724   {
725     if (jalview.bin.Cache.vamsasJarsPresent())
726     {
727       setupVamsasDisconnectedGui();
728       VamsasMenu.setVisible(true);
729       final Desktop us = this;
730       VamsasMenu.addMenuListener(new MenuListener()
731       {
732         // this listener remembers when the menu was first selected, and
733         // doesn't rebuild the session list until it has been cleared and
734         // reselected again.
735         boolean refresh = true;
736
737         @Override
738         public void menuCanceled(MenuEvent e)
739         {
740           refresh = true;
741         }
742
743         @Override
744         public void menuDeselected(MenuEvent e)
745         {
746           refresh = true;
747         }
748
749         @Override
750         public void menuSelected(MenuEvent e)
751         {
752           if (refresh)
753           {
754             us.buildVamsasStMenu();
755             refresh = false;
756           }
757         }
758       });
759       vamsasStart.setVisible(true);
760     }
761   }
762
763   void showPasteMenu(int x, int y)
764   {
765     JPopupMenu popup = new JPopupMenu();
766     JMenuItem item = new JMenuItem(
767             MessageManager.getString("label.paste_new_window"));
768     item.addActionListener(new ActionListener()
769     {
770       @Override
771       public void actionPerformed(ActionEvent evt)
772       {
773         paste();
774       }
775     });
776
777     popup.add(item);
778     popup.show(this, x, y);
779   }
780
781   public void paste()
782   {
783     try
784     {
785       Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
786       Transferable contents = c.getContents(this);
787
788       if (contents != null)
789       {
790         String file = (String) contents
791                 .getTransferData(DataFlavor.stringFlavor);
792
793         FileFormatI format = new IdentifyFile().identify(file,
794                 DataSourceType.PASTE);
795
796         new FileLoader().LoadFile(file, DataSourceType.PASTE, format);
797
798       }
799     } catch (Exception ex)
800     {
801       System.out.println(
802               "Unable to paste alignment from system clipboard:\n" + ex);
803     }
804   }
805
806   /**
807    * Adds and opens the given frame to the desktop
808    * 
809    * @param frame
810    *          Frame to show
811    * @param title
812    *          Visible Title
813    * @param w
814    *          width
815    * @param h
816    *          height
817    */
818   public static synchronized void addInternalFrame(
819           final JInternalFrame frame, String title, int w, int h)
820   {
821     addInternalFrame(frame, title, true, w, h, true, false);
822   }
823
824   /**
825    * Add an internal frame to the Jalview desktop
826    * 
827    * @param frame
828    *          Frame to show
829    * @param title
830    *          Visible Title
831    * @param makeVisible
832    *          When true, display frame immediately, otherwise, caller must call
833    *          setVisible themselves.
834    * @param w
835    *          width
836    * @param h
837    *          height
838    */
839   public static synchronized void addInternalFrame(
840           final JInternalFrame frame, String title, boolean makeVisible,
841           int w, int h)
842   {
843     addInternalFrame(frame, title, makeVisible, w, h, true, false);
844   }
845
846   /**
847    * Add an internal frame to the Jalview desktop and make it visible
848    * 
849    * @param frame
850    *          Frame to show
851    * @param title
852    *          Visible Title
853    * @param w
854    *          width
855    * @param h
856    *          height
857    * @param resizable
858    *          Allow resize
859    */
860   public static synchronized void addInternalFrame(
861           final JInternalFrame frame, String title, int w, int h,
862           boolean resizable)
863   {
864     addInternalFrame(frame, title, true, w, h, resizable, false);
865   }
866
867   /**
868    * Add an internal frame to the Jalview desktop
869    * 
870    * @param frame
871    *          Frame to show
872    * @param title
873    *          Visible Title
874    * @param makeVisible
875    *          When true, display frame immediately, otherwise, caller must call
876    *          setVisible themselves.
877    * @param w
878    *          width
879    * @param h
880    *          height
881    * @param resizable
882    *          Allow resize
883    * @param ignoreMinSize
884    *          Do not set the default minimum size for frame
885    */
886   public static synchronized void addInternalFrame(
887           final JInternalFrame frame, String title, boolean makeVisible,
888           int w, int h, boolean resizable, boolean ignoreMinSize)
889   {
890
891     // TODO: allow callers to determine X and Y position of frame (eg. via
892     // bounds object).
893     // TODO: consider fixing method to update entries in the window submenu with
894     // the current window title
895
896     frame.setTitle(title);
897     if (frame.getWidth() < 1 || frame.getHeight() < 1)
898     {
899       frame.setSize(w, h);
900     }
901     // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
902     // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
903     // IF JALVIEW IS RUNNING HEADLESS
904     // ///////////////////////////////////////////////
905     if (instance == null || (System.getProperty("java.awt.headless") != null
906             && System.getProperty("java.awt.headless").equals("true")))
907     {
908       return;
909     }
910
911     openFrameCount++;
912
913     if (!ignoreMinSize)
914     {
915       frame.setMinimumSize(
916               new Dimension(DEFAULT_MIN_WIDTH, DEFAULT_MIN_HEIGHT));
917
918       // Set default dimension for Alignment Frame window.
919       // The Alignment Frame window could be added from a number of places,
920       // hence,
921       // I did this here in order not to miss out on any Alignment frame.
922       if (frame instanceof AlignFrame)
923       {
924         frame.setMinimumSize(new Dimension(ALIGN_FRAME_DEFAULT_MIN_WIDTH,
925                 ALIGN_FRAME_DEFAULT_MIN_HEIGHT));
926       }
927     }
928
929     frame.setVisible(makeVisible);
930     frame.setClosable(true);
931     frame.setResizable(resizable);
932     frame.setMaximizable(resizable);
933     frame.setIconifiable(resizable);
934     frame.setOpaque(false);
935
936     if (frame.getX() < 1 && frame.getY() < 1)
937     {
938       frame.setLocation(xOffset * openFrameCount,
939               yOffset * ((openFrameCount - 1) % 10) + yOffset);
940     }
941
942     /*
943      * add an entry for the new frame in the Window menu 
944      * (and remove it when the frame is closed)
945      */
946     final JMenuItem menuItem = new JMenuItem(title);
947     frame.addInternalFrameListener(new InternalFrameAdapter()
948     {
949       @Override
950       public void internalFrameActivated(InternalFrameEvent evt)
951       {
952         JInternalFrame itf = desktop.getSelectedFrame();
953         if (itf != null)
954         {
955           if (itf instanceof AlignFrame)
956           {
957             Jalview.setCurrentAlignFrame((AlignFrame) itf);
958           }
959           itf.requestFocus();
960         }
961       }
962
963       @Override
964       public void internalFrameClosed(InternalFrameEvent evt)
965       {
966         PaintRefresher.RemoveComponent(frame);
967
968         /*
969          * defensive check to prevent frames being
970          * added half off the window
971          */
972         if (openFrameCount > 0)
973         {
974           openFrameCount--;
975         }
976
977         /*
978          * ensure no reference to alignFrame retained by menu item listener
979          */
980         if (menuItem.getActionListeners().length > 0)
981         {
982           menuItem.removeActionListener(menuItem.getActionListeners()[0]);
983         }
984         windowMenu.remove(menuItem);
985       };
986     });
987
988     menuItem.addActionListener(new ActionListener()
989     {
990       @Override
991       public void actionPerformed(ActionEvent e)
992       {
993         try
994         {
995           frame.setSelected(true);
996           frame.setIcon(false);
997         } catch (java.beans.PropertyVetoException ex)
998         {
999
1000         }
1001       }
1002     });
1003
1004     setKeyBindings(frame);
1005
1006     desktop.add(frame);
1007
1008     windowMenu.add(menuItem);
1009
1010     frame.toFront();
1011     try
1012     {
1013       frame.setSelected(true);
1014       frame.requestFocus();
1015     } catch (java.beans.PropertyVetoException ve)
1016     {
1017     } catch (java.lang.ClassCastException cex)
1018     {
1019       Cache.log.warn(
1020               "Squashed a possible GUI implementation error. If you can recreate this, please look at http://issues.jalview.org/browse/JAL-869",
1021               cex);
1022     }
1023   }
1024
1025   /**
1026    * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
1027    * the window
1028    * 
1029    * @param frame
1030    */
1031   private static void setKeyBindings(JInternalFrame frame)
1032   {
1033     @SuppressWarnings("serial")
1034     final Action closeAction = new AbstractAction()
1035     {
1036       @Override
1037       public void actionPerformed(ActionEvent e)
1038       {
1039         frame.dispose();
1040       }
1041     };
1042
1043     /*
1044      * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action
1045      */
1046     KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
1047             InputEvent.CTRL_DOWN_MASK);
1048     KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
1049             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
1050
1051     InputMap inputMap = frame
1052             .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
1053     String ctrlW = ctrlWKey.toString();
1054     inputMap.put(ctrlWKey, ctrlW);
1055     inputMap.put(cmdWKey, ctrlW);
1056
1057     ActionMap actionMap = frame.getActionMap();
1058     actionMap.put(ctrlW, closeAction);
1059   }
1060
1061   @Override
1062   public void lostOwnership(Clipboard clipboard, Transferable contents)
1063   {
1064     if (!internalCopy)
1065     {
1066       Desktop.jalviewClipboard = null;
1067     }
1068
1069     internalCopy = false;
1070   }
1071
1072   @Override
1073   public void dragEnter(DropTargetDragEvent evt)
1074   {
1075   }
1076
1077   @Override
1078   public void dragExit(DropTargetEvent evt)
1079   {
1080   }
1081
1082   @Override
1083   public void dragOver(DropTargetDragEvent evt)
1084   {
1085   }
1086
1087   @Override
1088   public void dropActionChanged(DropTargetDragEvent evt)
1089   {
1090   }
1091
1092   /**
1093    * DOCUMENT ME!
1094    * 
1095    * @param evt
1096    *          DOCUMENT ME!
1097    */
1098   @Override
1099   public void drop(DropTargetDropEvent evt)
1100   {
1101     boolean success = true;
1102     // JAL-1552 - acceptDrop required before getTransferable call for
1103     // Java's Transferable for native dnd
1104     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
1105     Transferable t = evt.getTransferable();
1106     List<String> files = new ArrayList<>();
1107     List<DataSourceType> protocols = new ArrayList<>();
1108
1109     try
1110     {
1111       Desktop.transferFromDropTarget(files, protocols, evt, t);
1112     } catch (Exception e)
1113     {
1114       e.printStackTrace();
1115       success = false;
1116     }
1117
1118     if (files != null)
1119     {
1120       try
1121       {
1122         for (int i = 0; i < files.size(); i++)
1123         {
1124           String file = files.get(i).toString();
1125           DataSourceType protocol = (protocols == null)
1126                   ? DataSourceType.FILE
1127                   : protocols.get(i);
1128           FileFormatI format = null;
1129
1130           if (file.endsWith(".jar"))
1131           {
1132             format = FileFormat.Jalview;
1133
1134           }
1135           else
1136           {
1137             format = new IdentifyFile().identify(file, protocol);
1138           }
1139
1140           new FileLoader().LoadFile(file, protocol, format);
1141
1142         }
1143       } catch (Exception ex)
1144       {
1145         success = false;
1146       }
1147     }
1148     evt.dropComplete(success); // need this to ensure input focus is properly
1149                                // transfered to any new windows created
1150   }
1151
1152   /**
1153    * DOCUMENT ME!
1154    * 
1155    * @param e
1156    *          DOCUMENT ME!
1157    */
1158   @Override
1159   public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
1160   {
1161     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
1162     JalviewFileChooser chooser = JalviewFileChooser
1163             .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat);
1164
1165     chooser.setFileView(new JalviewFileView());
1166     chooser.setDialogTitle(
1167             MessageManager.getString("label.open_local_file"));
1168     chooser.setToolTipText(MessageManager.getString("action.open"));
1169
1170     int value = chooser.showOpenDialog(this);
1171
1172     if (value == JalviewFileChooser.APPROVE_OPTION)
1173     {
1174       String choice = chooser.getSelectedFile().getPath();
1175       Cache.setProperty("LAST_DIRECTORY",
1176               chooser.getSelectedFile().getParent());
1177
1178       FileFormatI format = chooser.getSelectedFormat();
1179
1180       /*
1181        * Call IdentifyFile to verify the file contains what its extension implies.
1182        * Skip this step for dynamically added file formats, because
1183        * IdentifyFile does not know how to recognise them.
1184        */
1185       if (FileFormats.getInstance().isIdentifiable(format))
1186       {
1187         try
1188         {
1189           format = new IdentifyFile().identify(choice, DataSourceType.FILE);
1190         } catch (FileFormatException e)
1191         {
1192           // format = null; //??
1193         }
1194       }
1195
1196       if (viewport != null)
1197       {
1198         new FileLoader().LoadFile(viewport, choice, DataSourceType.FILE,
1199                 format);
1200       }
1201       else
1202       {
1203         new FileLoader().LoadFile(choice, DataSourceType.FILE, format);
1204       }
1205     }
1206   }
1207
1208   /**
1209    * DOCUMENT ME!
1210    * 
1211    * @param e
1212    *          DOCUMENT ME!
1213    */
1214   @Override
1215   public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
1216   {
1217     // This construct allows us to have a wider textfield
1218     // for viewing
1219     JLabel label = new JLabel(
1220             MessageManager.getString("label.input_file_url"));
1221     final JComboBox history = new JComboBox();
1222
1223     JPanel panel = new JPanel(new GridLayout(2, 1));
1224     panel.add(label);
1225     panel.add(history);
1226     history.setPreferredSize(new Dimension(400, 20));
1227     history.setEditable(true);
1228     history.addItem("http://www.");
1229
1230     String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
1231
1232     StringTokenizer st;
1233
1234     if (historyItems != null)
1235     {
1236       st = new StringTokenizer(historyItems, "\t");
1237
1238       while (st.hasMoreTokens())
1239       {
1240         history.addItem(st.nextElement());
1241       }
1242     }
1243
1244     int reply = JvOptionPane.showInternalConfirmDialog(desktop, panel,
1245             MessageManager.getString("label.input_alignment_from_url"),
1246             JvOptionPane.OK_CANCEL_OPTION);
1247
1248     if (reply != JvOptionPane.OK_OPTION)
1249     {
1250       return;
1251     }
1252
1253     String url = history.getSelectedItem().toString();
1254
1255     if (url.toLowerCase().endsWith(".jar"))
1256     {
1257       if (viewport != null)
1258       {
1259         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1260                 FileFormat.Jalview);
1261       }
1262       else
1263       {
1264         new FileLoader().LoadFile(url, DataSourceType.URL,
1265                 FileFormat.Jalview);
1266       }
1267     }
1268     else
1269     {
1270       FileFormatI format = null;
1271       try
1272       {
1273         format = new IdentifyFile().identify(url, DataSourceType.URL);
1274       } catch (FileFormatException e)
1275       {
1276         // TODO revise error handling, distinguish between
1277         // URL not found and response not valid
1278       }
1279
1280       if (format == null)
1281       {
1282         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1283                 MessageManager.formatMessage("label.couldnt_locate",
1284                         new Object[]
1285                         { url }),
1286                 MessageManager.getString("label.url_not_found"),
1287                 JvOptionPane.WARNING_MESSAGE);
1288
1289         return;
1290       }
1291
1292       if (viewport != null)
1293       {
1294         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1295                 format);
1296       }
1297       else
1298       {
1299         new FileLoader().LoadFile(url, DataSourceType.URL, format);
1300       }
1301     }
1302   }
1303
1304   /**
1305    * Opens the CutAndPaste window for the user to paste an alignment in to
1306    * 
1307    * @param viewPanel
1308    *          - if not null, the pasted alignment is added to the current
1309    *          alignment; if null, to a new alignment window
1310    */
1311   @Override
1312   public void inputTextboxMenuItem_actionPerformed(
1313           AlignmentViewPanel viewPanel)
1314   {
1315     CutAndPasteTransfer cap = new CutAndPasteTransfer();
1316     cap.setForInput(viewPanel);
1317     Desktop.addInternalFrame(cap,
1318             MessageManager.getString("label.cut_paste_alignmen_file"), true,
1319             600, 500);
1320   }
1321
1322   /*
1323    * Exit the program
1324    */
1325   @Override
1326   public void quit()
1327   {
1328     System.out.println("********** Desktop.quit()");
1329     System.out.println(savingFiles.toString());
1330     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
1331     jalview.bin.Cache.setProperty("SCREENGEOMETRY_WIDTH",
1332             screen.width + "");
1333     jalview.bin.Cache.setProperty("SCREENGEOMETRY_HEIGHT",
1334             screen.height + "");
1335     storeLastKnownDimensions("", new Rectangle(getBounds().x, getBounds().y,
1336             getWidth(), getHeight()));
1337
1338     if (jconsole != null)
1339     {
1340       storeLastKnownDimensions("JAVA_CONSOLE_", jconsole.getBounds());
1341       jconsole.stopConsole();
1342     }
1343     if (jvnews != null)
1344     {
1345       storeLastKnownDimensions("JALVIEW_RSS_WINDOW_", jvnews.getBounds());
1346
1347     }
1348     if (dialogExecutor != null)
1349     {
1350       dialogExecutor.shutdownNow();
1351     }
1352     closeAll_actionPerformed(null);
1353
1354     if (groovyConsole != null)
1355     {
1356       // suppress a possible repeat prompt to save script
1357       groovyConsole.setDirty(false);
1358       groovyConsole.exit();
1359     }
1360     System.exit(0);
1361   }
1362
1363   private void storeLastKnownDimensions(String string, Rectangle jc)
1364   {
1365     jalview.bin.Cache.log.debug("Storing last known dimensions for "
1366             + string + ": x:" + jc.x + " y:" + jc.y + " width:" + jc.width
1367             + " height:" + jc.height);
1368
1369     jalview.bin.Cache.setProperty(string + "SCREEN_X", jc.x + "");
1370     jalview.bin.Cache.setProperty(string + "SCREEN_Y", jc.y + "");
1371     jalview.bin.Cache.setProperty(string + "SCREEN_WIDTH", jc.width + "");
1372     jalview.bin.Cache.setProperty(string + "SCREEN_HEIGHT", jc.height + "");
1373   }
1374
1375   /**
1376    * DOCUMENT ME!
1377    * 
1378    * @param e
1379    *          DOCUMENT ME!
1380    */
1381   @Override
1382   public void aboutMenuItem_actionPerformed(ActionEvent e)
1383   {
1384     // StringBuffer message = getAboutMessage(false);
1385     // JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1386     //
1387     // message.toString(), "About Jalview", JvOptionPane.INFORMATION_MESSAGE);
1388     new Thread(new Runnable()
1389     {
1390       @Override
1391       public void run()
1392       {
1393         new SplashScreen(true);
1394       }
1395     }).start();
1396   }
1397
1398   public StringBuffer getAboutMessage(boolean shortv)
1399   {
1400     StringBuffer message = new StringBuffer();
1401     message.append("<html>");
1402     if (shortv)
1403     {
1404       message.append("<h1><strong>Version: "
1405               + jalview.bin.Cache.getProperty("VERSION")
1406               + "</strong></h1>");
1407       message.append("<strong>Last Updated: <em>"
1408               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")
1409               + "</em></strong>");
1410
1411     }
1412     else
1413     {
1414
1415       message.append("<strong>Version "
1416               + jalview.bin.Cache.getProperty("VERSION")
1417               + "; last updated: "
1418               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
1419     }
1420
1421     if (jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1422             .equals("Checking"))
1423     {
1424       message.append("<br>...Checking latest version...</br>");
1425     }
1426     else if (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1427             .equals(jalview.bin.Cache.getProperty("VERSION")))
1428     {
1429       boolean red = false;
1430       if (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
1431               .indexOf("automated build") == -1)
1432       {
1433         red = true;
1434         // Displayed when code version and jnlp version do not match and code
1435         // version is not a development build
1436         message.append("<div style=\"color: #FF0000;font-style: bold;\">");
1437       }
1438
1439       message.append("<br>!! Version "
1440               + jalview.bin.Cache.getDefault("LATEST_VERSION",
1441                       "..Checking..")
1442               + " is available for download from "
1443               + jalview.bin.Cache.getDefault("www.jalview.org",
1444                       "http://www.jalview.org")
1445               + " !!");
1446       if (red)
1447       {
1448         message.append("</div>");
1449       }
1450     }
1451     message.append("<br>Authors:  " + jalview.bin.Cache.getDefault(
1452             "AUTHORFNAMES",
1453             "The Jalview Authors (See AUTHORS file for current list)")
1454             + "<br><br>Development managed by The Barton Group, University of Dundee, Scotland, UK.<br>"
1455             + "<br><br>For help, see the FAQ at <a href=\"http://www.jalview.org/faq\">www.jalview.org/faq</a> and/or join the jalview-discuss@jalview.org mailing list"
1456             + "<br><br>If  you use Jalview, please cite:"
1457             + "<br>Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
1458             + "<br>Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
1459             + "<br>Bioinformatics doi: 10.1093/bioinformatics/btp033"
1460             + "</html>");
1461     return message;
1462   }
1463
1464   /**
1465    * DOCUMENT ME!
1466    * 
1467    * @param e
1468    *          DOCUMENT ME!
1469    */
1470   @Override
1471   public void documentationMenuItem_actionPerformed(ActionEvent e)
1472   {
1473     try
1474     {
1475       Help.showHelpWindow();
1476     } catch (Exception ex)
1477     {
1478     }
1479   }
1480
1481   @Override
1482   public void closeAll_actionPerformed(ActionEvent e)
1483   {
1484     // TODO show a progress bar while closing?
1485     JInternalFrame[] frames = desktop.getAllFrames();
1486     for (int i = 0; i < frames.length; i++)
1487     {
1488       try
1489       {
1490         frames[i].setClosed(true);
1491       } catch (java.beans.PropertyVetoException ex)
1492       {
1493       }
1494     }
1495     Jalview.setCurrentAlignFrame(null);
1496     System.out.println("ALL CLOSED");
1497     if (v_client != null)
1498     {
1499       // TODO clear binding to vamsas document objects on close_all
1500     }
1501
1502     /*
1503      * reset state of singleton objects as appropriate (clear down session state
1504      * when all windows are closed)
1505      */
1506     StructureSelectionManager ssm = StructureSelectionManager
1507             .getStructureSelectionManager(this);
1508     if (ssm != null)
1509     {
1510       ssm.resetAll();
1511     }
1512   }
1513
1514   @Override
1515   public void raiseRelated_actionPerformed(ActionEvent e)
1516   {
1517     reorderAssociatedWindows(false, false);
1518   }
1519
1520   @Override
1521   public void minimizeAssociated_actionPerformed(ActionEvent e)
1522   {
1523     reorderAssociatedWindows(true, false);
1524   }
1525
1526   void closeAssociatedWindows()
1527   {
1528     reorderAssociatedWindows(false, true);
1529   }
1530
1531   /*
1532    * (non-Javadoc)
1533    * 
1534    * @seejalview.jbgui.GDesktop#garbageCollect_actionPerformed(java.awt.event.
1535    * ActionEvent)
1536    */
1537   @Override
1538   protected void garbageCollect_actionPerformed(ActionEvent e)
1539   {
1540     // We simply collect the garbage
1541     jalview.bin.Cache.log.debug("Collecting garbage...");
1542     System.gc();
1543     jalview.bin.Cache.log.debug("Finished garbage collection.");
1544   }
1545
1546   /*
1547    * (non-Javadoc)
1548    * 
1549    * @see
1550    * jalview.jbgui.GDesktop#showMemusage_actionPerformed(java.awt.event.ActionEvent
1551    * )
1552    */
1553   @Override
1554   protected void showMemusage_actionPerformed(ActionEvent e)
1555   {
1556     desktop.showMemoryUsage(showMemusage.isSelected());
1557   }
1558
1559   /*
1560    * (non-Javadoc)
1561    * 
1562    * @see
1563    * jalview.jbgui.GDesktop#showConsole_actionPerformed(java.awt.event.ActionEvent
1564    * )
1565    */
1566   @Override
1567   protected void showConsole_actionPerformed(ActionEvent e)
1568   {
1569     showConsole(showConsole.isSelected());
1570   }
1571
1572   Console jconsole = null;
1573
1574   /**
1575    * control whether the java console is visible or not
1576    * 
1577    * @param selected
1578    */
1579   void showConsole(boolean selected)
1580   {
1581     showConsole.setSelected(selected);
1582     // TODO: decide if we should update properties file
1583     Cache.setProperty("SHOW_JAVA_CONSOLE",
1584             Boolean.valueOf(selected).toString());
1585     jconsole.setVisible(selected);
1586   }
1587
1588   void reorderAssociatedWindows(boolean minimize, boolean close)
1589   {
1590     JInternalFrame[] frames = desktop.getAllFrames();
1591     if (frames == null || frames.length < 1)
1592     {
1593       return;
1594     }
1595
1596     AlignmentViewport source = null, target = null;
1597     if (frames[0] instanceof AlignFrame)
1598     {
1599       source = ((AlignFrame) frames[0]).getCurrentView();
1600     }
1601     else if (frames[0] instanceof TreePanel)
1602     {
1603       source = ((TreePanel) frames[0]).getViewPort();
1604     }
1605     else if (frames[0] instanceof PCAPanel)
1606     {
1607       source = ((PCAPanel) frames[0]).av;
1608     }
1609     else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
1610     {
1611       source = ((PairwiseAlignPanel) frames[0].getContentPane()).av;
1612     }
1613
1614     if (source != null)
1615     {
1616       for (int i = 0; i < frames.length; i++)
1617       {
1618         target = null;
1619         if (frames[i] == null)
1620         {
1621           continue;
1622         }
1623         if (frames[i] instanceof AlignFrame)
1624         {
1625           target = ((AlignFrame) frames[i]).getCurrentView();
1626         }
1627         else if (frames[i] instanceof TreePanel)
1628         {
1629           target = ((TreePanel) frames[i]).getViewPort();
1630         }
1631         else if (frames[i] instanceof PCAPanel)
1632         {
1633           target = ((PCAPanel) frames[i]).av;
1634         }
1635         else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
1636         {
1637           target = ((PairwiseAlignPanel) frames[i].getContentPane()).av;
1638         }
1639
1640         if (source == target)
1641         {
1642           try
1643           {
1644             if (close)
1645             {
1646               frames[i].setClosed(true);
1647             }
1648             else
1649             {
1650               frames[i].setIcon(minimize);
1651               if (!minimize)
1652               {
1653                 frames[i].toFront();
1654               }
1655             }
1656
1657           } catch (java.beans.PropertyVetoException ex)
1658           {
1659           }
1660         }
1661       }
1662     }
1663   }
1664
1665   /**
1666    * DOCUMENT ME!
1667    * 
1668    * @param e
1669    *          DOCUMENT ME!
1670    */
1671   @Override
1672   protected void preferences_actionPerformed(ActionEvent e)
1673   {
1674     new Preferences();
1675   }
1676
1677   /**
1678    * DOCUMENT ME!
1679    * 
1680    * @param e
1681    *          DOCUMENT ME!
1682    */
1683   @Override
1684   public void saveState_actionPerformed(ActionEvent e)
1685   {
1686     JalviewFileChooser chooser = new JalviewFileChooser("jvp",
1687             "Jalview Project");
1688
1689     chooser.setFileView(new JalviewFileView());
1690     chooser.setDialogTitle(MessageManager.getString("label.save_state"));
1691
1692     int value = chooser.showSaveDialog(this);
1693
1694     if (value == JalviewFileChooser.APPROVE_OPTION)
1695     {
1696       final Desktop me = this;
1697       final java.io.File choice = chooser.getSelectedFile();
1698       setProjectFile(choice);
1699
1700       new Thread(new Runnable()
1701       {
1702         @Override
1703         public void run()
1704         {
1705           // TODO: refactor to Jalview desktop session controller action.
1706           setProgressBar(MessageManager.formatMessage(
1707                   "label.saving_jalview_project", new Object[]
1708                   { choice.getName() }), choice.hashCode());
1709           jalview.bin.Cache.setProperty("LAST_DIRECTORY",
1710                   choice.getParent());
1711           // TODO catch and handle errors for savestate
1712           // TODO prevent user from messing with the Desktop whilst we're saving
1713           try
1714           {
1715             new Jalview2XML().saveState(choice);
1716           } catch (OutOfMemoryError oom)
1717           {
1718             new OOMWarning(
1719                     "Whilst saving current state to " + choice.getName(),
1720                     oom);
1721           } catch (Exception ex)
1722           {
1723             Cache.log.error(
1724                     "Problems whilst trying to save to " + choice.getName(),
1725                     ex);
1726             JvOptionPane.showMessageDialog(me,
1727                     MessageManager.formatMessage(
1728                             "label.error_whilst_saving_current_state_to",
1729                             new Object[]
1730                             { choice.getName() }),
1731                     MessageManager.getString("label.couldnt_save_project"),
1732                     JvOptionPane.WARNING_MESSAGE);
1733           }
1734           setProgressBar(null, choice.hashCode());
1735         }
1736       }).start();
1737     }
1738   }
1739
1740   private void setProjectFile(File choice)
1741   {
1742     this.projectFile = choice;
1743   }
1744
1745   public File getProjectFile()
1746   {
1747     return this.projectFile;
1748   }
1749
1750   /**
1751    * DOCUMENT ME!
1752    * 
1753    * @param e
1754    *          DOCUMENT ME!
1755    */
1756   @Override
1757   public void loadState_actionPerformed(ActionEvent e)
1758   {
1759     JalviewFileChooser chooser = new JalviewFileChooser(
1760             Cache.getProperty("LAST_DIRECTORY"), new String[]
1761             { "jvp", "jar" },
1762             new String[]
1763             { "Jalview Project", "Jalview Project (old)" },
1764             "Jalview Project");
1765     chooser.setFileView(new JalviewFileView());
1766     chooser.setDialogTitle(MessageManager.getString("label.restore_state"));
1767
1768     int value = chooser.showOpenDialog(this);
1769
1770     if (value == JalviewFileChooser.APPROVE_OPTION)
1771     {
1772       final File selectedFile = chooser.getSelectedFile();
1773       setProjectFile(selectedFile);
1774       final String choice = selectedFile.getAbsolutePath();
1775       Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
1776       new Thread(new Runnable()
1777       {
1778         @Override
1779         public void run()
1780         {
1781           setProgressBar(MessageManager.formatMessage(
1782                   "label.loading_jalview_project", new Object[]
1783                   { choice }), choice.hashCode());
1784           try
1785           {
1786             new Jalview2XML().loadJalviewAlign(choice);
1787           } catch (OutOfMemoryError oom)
1788           {
1789             new OOMWarning("Whilst loading project from " + choice, oom);
1790           } catch (Exception ex)
1791           {
1792             Cache.log.error(
1793                     "Problems whilst loading project from " + choice, ex);
1794             JvOptionPane.showMessageDialog(Desktop.desktop,
1795                     MessageManager.formatMessage(
1796                             "label.error_whilst_loading_project_from",
1797                             new Object[]
1798                             { choice }),
1799                     MessageManager.getString("label.couldnt_load_project"),
1800                     JvOptionPane.WARNING_MESSAGE);
1801           }
1802           setProgressBar(null, choice.hashCode());
1803         }
1804       }).start();
1805     }
1806   }
1807
1808   @Override
1809   public void inputSequence_actionPerformed(ActionEvent e)
1810   {
1811     new SequenceFetcher(this);
1812   }
1813
1814   JPanel progressPanel;
1815
1816   ArrayList<JPanel> fileLoadingPanels = new ArrayList<>();
1817
1818   public void startLoading(final String fileName)
1819   {
1820     if (fileLoadingCount == 0)
1821     {
1822       fileLoadingPanels.add(addProgressPanel(MessageManager
1823               .formatMessage("label.loading_file", new Object[]
1824               { fileName })));
1825     }
1826     fileLoadingCount++;
1827   }
1828
1829   private JPanel addProgressPanel(String string)
1830   {
1831     if (progressPanel == null)
1832     {
1833       progressPanel = new JPanel(new GridLayout(1, 1));
1834       totalProgressCount = 0;
1835       instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
1836     }
1837     JPanel thisprogress = new JPanel(new BorderLayout(10, 5));
1838     JProgressBar progressBar = new JProgressBar();
1839     progressBar.setIndeterminate(true);
1840
1841     thisprogress.add(new JLabel(string), BorderLayout.WEST);
1842
1843     thisprogress.add(progressBar, BorderLayout.CENTER);
1844     progressPanel.add(thisprogress);
1845     ((GridLayout) progressPanel.getLayout()).setRows(
1846             ((GridLayout) progressPanel.getLayout()).getRows() + 1);
1847     ++totalProgressCount;
1848     instance.validate();
1849     return thisprogress;
1850   }
1851
1852   int totalProgressCount = 0;
1853
1854   private void removeProgressPanel(JPanel progbar)
1855   {
1856     if (progressPanel != null)
1857     {
1858       synchronized (progressPanel)
1859       {
1860         progressPanel.remove(progbar);
1861         GridLayout gl = (GridLayout) progressPanel.getLayout();
1862         gl.setRows(gl.getRows() - 1);
1863         if (--totalProgressCount < 1)
1864         {
1865           this.getContentPane().remove(progressPanel);
1866           progressPanel = null;
1867         }
1868       }
1869     }
1870     validate();
1871   }
1872
1873   public void stopLoading()
1874   {
1875     fileLoadingCount--;
1876     if (fileLoadingCount < 1)
1877     {
1878       while (fileLoadingPanels.size() > 0)
1879       {
1880         removeProgressPanel(fileLoadingPanels.remove(0));
1881       }
1882       fileLoadingPanels.clear();
1883       fileLoadingCount = 0;
1884     }
1885     validate();
1886   }
1887
1888   public static int getViewCount(String alignmentId)
1889   {
1890     AlignmentViewport[] aps = getViewports(alignmentId);
1891     return (aps == null) ? 0 : aps.length;
1892   }
1893
1894   /**
1895    * 
1896    * @param alignmentId
1897    *          - if null, all sets are returned
1898    * @return all AlignmentPanels concerning the alignmentId sequence set
1899    */
1900   public static AlignmentPanel[] getAlignmentPanels(String alignmentId)
1901   {
1902     if (Desktop.desktop == null)
1903     {
1904       // no frames created and in headless mode
1905       // TODO: verify that frames are recoverable when in headless mode
1906       return null;
1907     }
1908     List<AlignmentPanel> aps = new ArrayList<>();
1909     AlignFrame[] frames = getAlignFrames();
1910     if (frames == null)
1911     {
1912       return null;
1913     }
1914     for (AlignFrame af : frames)
1915     {
1916       for (AlignmentPanel ap : af.alignPanels)
1917       {
1918         if (alignmentId == null
1919                 || alignmentId.equals(ap.av.getSequenceSetId()))
1920         {
1921           aps.add(ap);
1922         }
1923       }
1924     }
1925     if (aps.size() == 0)
1926     {
1927       return null;
1928     }
1929     AlignmentPanel[] vap = aps.toArray(new AlignmentPanel[aps.size()]);
1930     return vap;
1931   }
1932
1933   /**
1934    * get all the viewports on an alignment.
1935    * 
1936    * @param sequenceSetId
1937    *          unique alignment id (may be null - all viewports returned in that
1938    *          case)
1939    * @return all viewports on the alignment bound to sequenceSetId
1940    */
1941   public static AlignmentViewport[] getViewports(String sequenceSetId)
1942   {
1943     List<AlignmentViewport> viewp = new ArrayList<>();
1944     if (desktop != null)
1945     {
1946       AlignFrame[] frames = Desktop.getAlignFrames();
1947
1948       for (AlignFrame afr : frames)
1949       {
1950         if (sequenceSetId == null || afr.getViewport().getSequenceSetId()
1951                 .equals(sequenceSetId))
1952         {
1953           if (afr.alignPanels != null)
1954           {
1955             for (AlignmentPanel ap : afr.alignPanels)
1956             {
1957               if (sequenceSetId == null
1958                       || sequenceSetId.equals(ap.av.getSequenceSetId()))
1959               {
1960                 viewp.add(ap.av);
1961               }
1962             }
1963           }
1964           else
1965           {
1966             viewp.add(afr.getViewport());
1967           }
1968         }
1969       }
1970       if (viewp.size() > 0)
1971       {
1972         return viewp.toArray(new AlignmentViewport[viewp.size()]);
1973       }
1974     }
1975     return null;
1976   }
1977
1978   /**
1979    * Explode the views in the given frame into separate AlignFrame
1980    * 
1981    * @param af
1982    */
1983   public static void explodeViews(AlignFrame af)
1984   {
1985     int size = af.alignPanels.size();
1986     if (size < 2)
1987     {
1988       return;
1989     }
1990
1991     for (int i = 0; i < size; i++)
1992     {
1993       AlignmentPanel ap = af.alignPanels.get(i);
1994       AlignFrame newaf = new AlignFrame(ap);
1995
1996       /*
1997        * Restore the view's last exploded frame geometry if known. Multiple
1998        * views from one exploded frame share and restore the same (frame)
1999        * position and size.
2000        */
2001       Rectangle geometry = ap.av.getExplodedGeometry();
2002       if (geometry != null)
2003       {
2004         newaf.setBounds(geometry);
2005       }
2006
2007       ap.av.setGatherViewsHere(false);
2008
2009       addInternalFrame(newaf, af.getTitle(), AlignFrame.DEFAULT_WIDTH,
2010               AlignFrame.DEFAULT_HEIGHT);
2011     }
2012
2013     af.alignPanels.clear();
2014     af.closeMenuItem_actionPerformed(true);
2015
2016   }
2017
2018   /**
2019    * Gather expanded views (separate AlignFrame's) with the same sequence set
2020    * identifier back in to this frame as additional views, and close the
2021    * expanded views. Note the expanded frames may themselves have multiple
2022    * views. We take the lot.
2023    * 
2024    * @param source
2025    */
2026   public void gatherViews(AlignFrame source)
2027   {
2028     source.viewport.setGatherViewsHere(true);
2029     source.viewport.setExplodedGeometry(source.getBounds());
2030     JInternalFrame[] frames = desktop.getAllFrames();
2031     String viewId = source.viewport.getSequenceSetId();
2032
2033     for (int t = 0; t < frames.length; t++)
2034     {
2035       if (frames[t] instanceof AlignFrame && frames[t] != source)
2036       {
2037         AlignFrame af = (AlignFrame) frames[t];
2038         boolean gatherThis = false;
2039         for (int a = 0; a < af.alignPanels.size(); a++)
2040         {
2041           AlignmentPanel ap = af.alignPanels.get(a);
2042           if (viewId.equals(ap.av.getSequenceSetId()))
2043           {
2044             gatherThis = true;
2045             ap.av.setGatherViewsHere(false);
2046             ap.av.setExplodedGeometry(af.getBounds());
2047             source.addAlignmentPanel(ap, false);
2048           }
2049         }
2050
2051         if (gatherThis)
2052         {
2053           af.alignPanels.clear();
2054           af.closeMenuItem_actionPerformed(true);
2055         }
2056       }
2057     }
2058
2059   }
2060
2061   jalview.gui.VamsasApplication v_client = null;
2062
2063   @Override
2064   public void vamsasImport_actionPerformed(ActionEvent e)
2065   {
2066     if (v_client == null)
2067     {
2068       // Load and try to start a session.
2069       JalviewFileChooser chooser = new JalviewFileChooser(
2070               jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
2071
2072       chooser.setFileView(new JalviewFileView());
2073       chooser.setDialogTitle(
2074               MessageManager.getString("label.open_saved_vamsas_session"));
2075       chooser.setToolTipText(MessageManager.getString(
2076               "label.select_vamsas_session_opened_as_new_vamsas_session"));
2077
2078       int value = chooser.showOpenDialog(this);
2079
2080       if (value == JalviewFileChooser.APPROVE_OPTION)
2081       {
2082         String fle = chooser.getSelectedFile().toString();
2083         if (!vamsasImport(chooser.getSelectedFile()))
2084         {
2085           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2086                   MessageManager.formatMessage(
2087                           "label.couldnt_import_as_vamsas_session",
2088                           new Object[]
2089                           { fle }),
2090                   MessageManager
2091                           .getString("label.vamsas_document_import_failed"),
2092                   JvOptionPane.ERROR_MESSAGE);
2093         }
2094       }
2095     }
2096     else
2097     {
2098       jalview.bin.Cache.log.error(
2099               "Implementation error - load session from a running session is not supported.");
2100     }
2101   }
2102
2103   /**
2104    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2105    * 
2106    * @param file
2107    * @return true if import was a success and a session was started.
2108    */
2109   public boolean vamsasImport(URL url)
2110   {
2111     // TODO: create progress bar
2112     if (v_client != null)
2113     {
2114
2115       jalview.bin.Cache.log.error(
2116               "Implementation error - load session from a running session is not supported.");
2117       return false;
2118     }
2119
2120     try
2121     {
2122       // copy the URL content to a temporary local file
2123       // TODO: be a bit cleverer here with nio (?!)
2124       File file = File.createTempFile("vdocfromurl", ".vdj");
2125       FileOutputStream fos = new FileOutputStream(file);
2126       BufferedInputStream bis = new BufferedInputStream(url.openStream());
2127       byte[] buffer = new byte[2048];
2128       int ln;
2129       while ((ln = bis.read(buffer)) > -1)
2130       {
2131         fos.write(buffer, 0, ln);
2132       }
2133       bis.close();
2134       fos.close();
2135       v_client = new jalview.gui.VamsasApplication(this, file,
2136               url.toExternalForm());
2137     } catch (Exception ex)
2138     {
2139       jalview.bin.Cache.log.error(
2140               "Failed to create new vamsas session from contents of URL "
2141                       + url,
2142               ex);
2143       return false;
2144     }
2145     setupVamsasConnectedGui();
2146     v_client.initial_update(); // TODO: thread ?
2147     return v_client.inSession();
2148   }
2149
2150   /**
2151    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2152    * 
2153    * @param file
2154    * @return true if import was a success and a session was started.
2155    */
2156   public boolean vamsasImport(File file)
2157   {
2158     if (v_client != null)
2159     {
2160
2161       jalview.bin.Cache.log.error(
2162               "Implementation error - load session from a running session is not supported.");
2163       return false;
2164     }
2165
2166     setProgressBar(MessageManager.formatMessage(
2167             "status.importing_vamsas_session_from", new Object[]
2168             { file.getName() }), file.hashCode());
2169     try
2170     {
2171       v_client = new jalview.gui.VamsasApplication(this, file, null);
2172     } catch (Exception ex)
2173     {
2174       setProgressBar(MessageManager.formatMessage(
2175               "status.importing_vamsas_session_from", new Object[]
2176               { file.getName() }), file.hashCode());
2177       jalview.bin.Cache.log.error(
2178               "New vamsas session from existing session file failed:", ex);
2179       return false;
2180     }
2181     setupVamsasConnectedGui();
2182     v_client.initial_update(); // TODO: thread ?
2183     setProgressBar(MessageManager.formatMessage(
2184             "status.importing_vamsas_session_from", new Object[]
2185             { file.getName() }), file.hashCode());
2186     return v_client.inSession();
2187   }
2188
2189   public boolean joinVamsasSession(String mysesid)
2190   {
2191     if (v_client != null)
2192     {
2193       throw new Error(MessageManager
2194               .getString("error.try_join_vamsas_session_another"));
2195     }
2196     if (mysesid == null)
2197     {
2198       throw new Error(
2199               MessageManager.getString("error.invalid_vamsas_session_id"));
2200     }
2201     v_client = new VamsasApplication(this, mysesid);
2202     setupVamsasConnectedGui();
2203     v_client.initial_update();
2204     return (v_client.inSession());
2205   }
2206
2207   @Override
2208   public void vamsasStart_actionPerformed(ActionEvent e)
2209   {
2210     if (v_client == null)
2211     {
2212       // Start a session.
2213       // we just start a default session for moment.
2214       /*
2215        * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
2216        * getProperty("LAST_DIRECTORY"));
2217        * 
2218        * chooser.setFileView(new JalviewFileView());
2219        * chooser.setDialogTitle("Load Vamsas file");
2220        * chooser.setToolTipText("Import");
2221        * 
2222        * int value = chooser.showOpenDialog(this);
2223        * 
2224        * if (value == JalviewFileChooser.APPROVE_OPTION) { v_client = new
2225        * jalview.gui.VamsasApplication(this, chooser.getSelectedFile());
2226        */
2227       v_client = new VamsasApplication(this);
2228       setupVamsasConnectedGui();
2229       v_client.initial_update(); // TODO: thread ?
2230     }
2231     else
2232     {
2233       // store current data in session.
2234       v_client.push_update(); // TODO: thread
2235     }
2236   }
2237
2238   protected void setupVamsasConnectedGui()
2239   {
2240     vamsasStart.setText(MessageManager.getString("label.session_update"));
2241     vamsasSave.setVisible(true);
2242     vamsasStop.setVisible(true);
2243     vamsasImport.setVisible(false); // Document import to existing session is
2244     // not possible for vamsas-client-1.0.
2245   }
2246
2247   protected void setupVamsasDisconnectedGui()
2248   {
2249     vamsasSave.setVisible(false);
2250     vamsasStop.setVisible(false);
2251     vamsasImport.setVisible(true);
2252     vamsasStart
2253             .setText(MessageManager.getString("label.new_vamsas_session"));
2254   }
2255
2256   @Override
2257   public void vamsasStop_actionPerformed(ActionEvent e)
2258   {
2259     if (v_client != null)
2260     {
2261       v_client.end_session();
2262       v_client = null;
2263       setupVamsasDisconnectedGui();
2264     }
2265   }
2266
2267   protected void buildVamsasStMenu()
2268   {
2269     if (v_client == null)
2270     {
2271       String[] sess = null;
2272       try
2273       {
2274         sess = VamsasApplication.getSessionList();
2275       } catch (Exception e)
2276       {
2277         jalview.bin.Cache.log.warn("Problem getting current sessions list.",
2278                 e);
2279         sess = null;
2280       }
2281       if (sess != null)
2282       {
2283         jalview.bin.Cache.log.debug(
2284                 "Got current sessions list: " + sess.length + " entries.");
2285         VamsasStMenu.removeAll();
2286         for (int i = 0; i < sess.length; i++)
2287         {
2288           JMenuItem sessit = new JMenuItem();
2289           sessit.setText(sess[i]);
2290           sessit.setToolTipText(MessageManager
2291                   .formatMessage("label.connect_to_session", new Object[]
2292                   { sess[i] }));
2293           final Desktop dsktp = this;
2294           final String mysesid = sess[i];
2295           sessit.addActionListener(new ActionListener()
2296           {
2297
2298             @Override
2299             public void actionPerformed(ActionEvent e)
2300             {
2301               if (dsktp.v_client == null)
2302               {
2303                 Thread rthr = new Thread(new Runnable()
2304                 {
2305
2306                   @Override
2307                   public void run()
2308                   {
2309                     dsktp.v_client = new VamsasApplication(dsktp, mysesid);
2310                     dsktp.setupVamsasConnectedGui();
2311                     dsktp.v_client.initial_update();
2312                   }
2313
2314                 });
2315                 rthr.start();
2316               }
2317             };
2318           });
2319           VamsasStMenu.add(sessit);
2320         }
2321         // don't show an empty menu.
2322         VamsasStMenu.setVisible(sess.length > 0);
2323
2324       }
2325       else
2326       {
2327         jalview.bin.Cache.log.debug("No current vamsas sessions.");
2328         VamsasStMenu.removeAll();
2329         VamsasStMenu.setVisible(false);
2330       }
2331     }
2332     else
2333     {
2334       // Not interested in the content. Just hide ourselves.
2335       VamsasStMenu.setVisible(false);
2336     }
2337   }
2338
2339   @Override
2340   public void vamsasSave_actionPerformed(ActionEvent e)
2341   {
2342     if (v_client != null)
2343     {
2344       // TODO: VAMSAS DOCUMENT EXTENSION is VDJ
2345       JalviewFileChooser chooser = new JalviewFileChooser("vdj",
2346               "Vamsas Document");
2347
2348       chooser.setFileView(new JalviewFileView());
2349       chooser.setDialogTitle(MessageManager
2350               .getString("label.save_vamsas_document_archive"));
2351
2352       int value = chooser.showSaveDialog(this);
2353
2354       if (value == JalviewFileChooser.APPROVE_OPTION)
2355       {
2356         java.io.File choice = chooser.getSelectedFile();
2357         JPanel progpanel = addProgressPanel(MessageManager
2358                 .formatMessage("label.saving_vamsas_doc", new Object[]
2359                 { choice.getName() }));
2360         Cache.setProperty("LAST_DIRECTORY", choice.getParent());
2361         String warnmsg = null;
2362         String warnttl = null;
2363         try
2364         {
2365           v_client.vclient.storeDocument(choice);
2366         } catch (Error ex)
2367         {
2368           warnttl = "Serious Problem saving Vamsas Document";
2369           warnmsg = ex.toString();
2370           jalview.bin.Cache.log
2371                   .error("Error Whilst saving document to " + choice, ex);
2372
2373         } catch (Exception ex)
2374         {
2375           warnttl = "Problem saving Vamsas Document.";
2376           warnmsg = ex.toString();
2377           jalview.bin.Cache.log.warn(
2378                   "Exception Whilst saving document to " + choice, ex);
2379
2380         }
2381         removeProgressPanel(progpanel);
2382         if (warnmsg != null)
2383         {
2384           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2385
2386                   warnmsg, warnttl, JvOptionPane.ERROR_MESSAGE);
2387         }
2388       }
2389     }
2390   }
2391
2392   JPanel vamUpdate = null;
2393
2394   /**
2395    * hide vamsas user gui bits when a vamsas document event is being handled.
2396    * 
2397    * @param b
2398    *          true to hide gui, false to reveal gui
2399    */
2400   public void setVamsasUpdate(boolean b)
2401   {
2402     Cache.log.debug("Setting gui for Vamsas update "
2403             + (b ? "in progress" : "finished"));
2404
2405     if (vamUpdate != null)
2406     {
2407       this.removeProgressPanel(vamUpdate);
2408     }
2409     if (b)
2410     {
2411       vamUpdate = this.addProgressPanel(
2412               MessageManager.getString("label.updating_vamsas_session"));
2413     }
2414     vamsasStart.setVisible(!b);
2415     vamsasStop.setVisible(!b);
2416     vamsasSave.setVisible(!b);
2417   }
2418
2419   public JInternalFrame[] getAllFrames()
2420   {
2421     return desktop.getAllFrames();
2422   }
2423
2424   /**
2425    * Checks the given url to see if it gives a response indicating that the user
2426    * should be informed of a new questionnaire.
2427    * 
2428    * @param url
2429    */
2430   public void checkForQuestionnaire(String url)
2431   {
2432     UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
2433     // javax.swing.SwingUtilities.invokeLater(jvq);
2434     new Thread(jvq).start();
2435   }
2436
2437   public void checkURLLinks()
2438   {
2439     // Thread off the URL link checker
2440     addDialogThread(new Runnable()
2441     {
2442       @Override
2443       public void run()
2444       {
2445         if (Cache.getDefault("CHECKURLLINKS", true))
2446         {
2447           // check what the actual links are - if it's just the default don't
2448           // bother with the warning
2449           List<String> links = Preferences.sequenceUrlLinks
2450                   .getLinksForMenu();
2451
2452           // only need to check links if there is one with a
2453           // SEQUENCE_ID which is not the default EMBL_EBI link
2454           ListIterator<String> li = links.listIterator();
2455           boolean check = false;
2456           List<JLabel> urls = new ArrayList<>();
2457           while (li.hasNext())
2458           {
2459             String link = li.next();
2460             if (link.contains(jalview.util.UrlConstants.SEQUENCE_ID)
2461                     && !UrlConstants.isDefaultString(link))
2462             {
2463               check = true;
2464               int barPos = link.indexOf("|");
2465               String urlMsg = barPos == -1 ? link
2466                       : link.substring(0, barPos) + ": "
2467                               + link.substring(barPos + 1);
2468               urls.add(new JLabel(urlMsg));
2469             }
2470           }
2471           if (!check)
2472           {
2473             return;
2474           }
2475
2476           // ask user to check in case URL links use old style tokens
2477           // ($SEQUENCE_ID$ for sequence id _or_ accession id)
2478           JPanel msgPanel = new JPanel();
2479           msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.PAGE_AXIS));
2480           msgPanel.add(Box.createVerticalGlue());
2481           JLabel msg = new JLabel(MessageManager
2482                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION1"));
2483           JLabel msg2 = new JLabel(MessageManager
2484                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION2"));
2485           msgPanel.add(msg);
2486           for (JLabel url : urls)
2487           {
2488             msgPanel.add(url);
2489           }
2490           msgPanel.add(msg2);
2491
2492           final JCheckBox jcb = new JCheckBox(
2493                   MessageManager.getString("label.do_not_display_again"));
2494           jcb.addActionListener(new ActionListener()
2495           {
2496             @Override
2497             public void actionPerformed(ActionEvent e)
2498             {
2499               // update Cache settings for "don't show this again"
2500               boolean showWarningAgain = !jcb.isSelected();
2501               Cache.setProperty("CHECKURLLINKS",
2502                       Boolean.valueOf(showWarningAgain).toString());
2503             }
2504           });
2505           msgPanel.add(jcb);
2506
2507           JvOptionPane.showMessageDialog(Desktop.desktop, msgPanel,
2508                   MessageManager
2509                           .getString("label.SEQUENCE_ID_no_longer_used"),
2510                   JvOptionPane.WARNING_MESSAGE);
2511         }
2512       }
2513     });
2514   }
2515
2516   /**
2517    * Proxy class for JDesktopPane which optionally displays the current memory
2518    * usage and highlights the desktop area with a red bar if free memory runs
2519    * low.
2520    * 
2521    * @author AMW
2522    */
2523   public class MyDesktopPane extends JDesktopPane implements Runnable
2524   {
2525
2526     private static final float ONE_MB = 1048576f;
2527
2528     boolean showMemoryUsage = false;
2529
2530     Runtime runtime;
2531
2532     java.text.NumberFormat df;
2533
2534     float maxMemory, allocatedMemory, freeMemory, totalFreeMemory,
2535             percentUsage;
2536
2537     public MyDesktopPane(boolean showMemoryUsage)
2538     {
2539       showMemoryUsage(showMemoryUsage);
2540     }
2541
2542     public void showMemoryUsage(boolean showMemory)
2543     {
2544       this.showMemoryUsage = showMemory;
2545       if (showMemory)
2546       {
2547         Thread worker = new Thread(this);
2548         worker.start();
2549       }
2550       repaint();
2551     }
2552
2553     public boolean isShowMemoryUsage()
2554     {
2555       return showMemoryUsage;
2556     }
2557
2558     @Override
2559     public void run()
2560     {
2561       df = java.text.NumberFormat.getNumberInstance();
2562       df.setMaximumFractionDigits(2);
2563       runtime = Runtime.getRuntime();
2564
2565       while (showMemoryUsage)
2566       {
2567         try
2568         {
2569           maxMemory = runtime.maxMemory() / ONE_MB;
2570           allocatedMemory = runtime.totalMemory() / ONE_MB;
2571           freeMemory = runtime.freeMemory() / ONE_MB;
2572           totalFreeMemory = freeMemory + (maxMemory - allocatedMemory);
2573
2574           percentUsage = (totalFreeMemory / maxMemory) * 100;
2575
2576           // if (percentUsage < 20)
2577           {
2578             // border1 = BorderFactory.createMatteBorder(12, 12, 12, 12,
2579             // Color.red);
2580             // instance.set.setBorder(border1);
2581           }
2582           repaint();
2583           // sleep after showing usage
2584           Thread.sleep(3000);
2585         } catch (Exception ex)
2586         {
2587           ex.printStackTrace();
2588         }
2589       }
2590     }
2591
2592     @Override
2593     public void paintComponent(Graphics g)
2594     {
2595       if (showMemoryUsage && g != null && df != null)
2596       {
2597         if (percentUsage < 20)
2598         {
2599           g.setColor(Color.red);
2600         }
2601         FontMetrics fm = g.getFontMetrics();
2602         if (fm != null)
2603         {
2604           g.drawString(MessageManager.formatMessage("label.memory_stats",
2605                   new Object[]
2606                   { df.format(totalFreeMemory), df.format(maxMemory),
2607                       df.format(percentUsage) }),
2608                   10, getHeight() - fm.getHeight());
2609         }
2610       }
2611     }
2612   }
2613
2614   /**
2615    * Accessor method to quickly get all the AlignmentFrames loaded.
2616    * 
2617    * @return an array of AlignFrame, or null if none found
2618    */
2619   public static AlignFrame[] getAlignFrames()
2620   {
2621     if (Jalview.isHeadlessMode())
2622     {
2623       // Desktop.desktop is null in headless mode
2624       return new AlignFrame[] { Jalview.currentAlignFrame };
2625     }
2626
2627     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2628
2629     if (frames == null)
2630     {
2631       return null;
2632     }
2633     List<AlignFrame> avp = new ArrayList<>();
2634     // REVERSE ORDER
2635     for (int i = frames.length - 1; i > -1; i--)
2636     {
2637       if (frames[i] instanceof AlignFrame)
2638       {
2639         avp.add((AlignFrame) frames[i]);
2640       }
2641       else if (frames[i] instanceof SplitFrame)
2642       {
2643         /*
2644          * Also check for a split frame containing an AlignFrame
2645          */
2646         GSplitFrame sf = (GSplitFrame) frames[i];
2647         if (sf.getTopFrame() instanceof AlignFrame)
2648         {
2649           avp.add((AlignFrame) sf.getTopFrame());
2650         }
2651         if (sf.getBottomFrame() instanceof AlignFrame)
2652         {
2653           avp.add((AlignFrame) sf.getBottomFrame());
2654         }
2655       }
2656     }
2657     if (avp.size() == 0)
2658     {
2659       return null;
2660     }
2661     AlignFrame afs[] = avp.toArray(new AlignFrame[avp.size()]);
2662     return afs;
2663   }
2664
2665   /**
2666    * Returns an array of any AppJmol frames in the Desktop (or null if none).
2667    * 
2668    * @return
2669    */
2670   public GStructureViewer[] getJmols()
2671   {
2672     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2673
2674     if (frames == null)
2675     {
2676       return null;
2677     }
2678     List<GStructureViewer> avp = new ArrayList<>();
2679     // REVERSE ORDER
2680     for (int i = frames.length - 1; i > -1; i--)
2681     {
2682       if (frames[i] instanceof AppJmol)
2683       {
2684         GStructureViewer af = (GStructureViewer) frames[i];
2685         avp.add(af);
2686       }
2687     }
2688     if (avp.size() == 0)
2689     {
2690       return null;
2691     }
2692     GStructureViewer afs[] = avp.toArray(new GStructureViewer[avp.size()]);
2693     return afs;
2694   }
2695
2696   /**
2697    * Add Groovy Support to Jalview
2698    */
2699   @Override
2700   public void groovyShell_actionPerformed()
2701   {
2702     try
2703     {
2704       openGroovyConsole();
2705     } catch (Exception ex)
2706     {
2707       jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex);
2708       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2709
2710               MessageManager.getString("label.couldnt_create_groovy_shell"),
2711               MessageManager.getString("label.groovy_support_failed"),
2712               JvOptionPane.ERROR_MESSAGE);
2713     }
2714   }
2715
2716   /**
2717    * Open the Groovy console
2718    */
2719   void openGroovyConsole()
2720   {
2721     if (groovyConsole == null)
2722     {
2723       groovyConsole = new groovy.ui.Console();
2724       groovyConsole.setVariable("Jalview", this);
2725       groovyConsole.run();
2726
2727       /*
2728        * We allow only one console at a time, so that AlignFrame menu option
2729        * 'Calculate | Run Groovy script' is unambiguous.
2730        * Disable 'Groovy Console', and enable 'Run script', when the console is 
2731        * opened, and the reverse when it is closed
2732        */
2733       Window window = (Window) groovyConsole.getFrame();
2734       window.addWindowListener(new WindowAdapter()
2735       {
2736         @Override
2737         public void windowClosed(WindowEvent e)
2738         {
2739           /*
2740            * rebind CMD-Q from Groovy Console to Jalview Quit
2741            */
2742           addQuitHandler();
2743           enableExecuteGroovy(false);
2744         }
2745       });
2746     }
2747
2748     /*
2749      * show Groovy console window (after close and reopen)
2750      */
2751     ((Window) groovyConsole.getFrame()).setVisible(true);
2752
2753     /*
2754      * if we got this far, enable 'Run Groovy' in AlignFrame menus
2755      * and disable opening a second console
2756      */
2757     enableExecuteGroovy(true);
2758   }
2759
2760   /**
2761    * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this
2762    * binding when opened
2763    */
2764   protected void addQuitHandler()
2765   {
2766     getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
2767             .put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
2768                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
2769                     "Quit");
2770     getRootPane().getActionMap().put("Quit", new AbstractAction()
2771     {
2772       @Override
2773       public void actionPerformed(ActionEvent e)
2774       {
2775         quit();
2776       }
2777     });
2778   }
2779
2780   /**
2781    * Enable or disable 'Run Groovy script' in AlignFrame calculate menus
2782    * 
2783    * @param enabled
2784    *          true if Groovy console is open
2785    */
2786   public void enableExecuteGroovy(boolean enabled)
2787   {
2788     /*
2789      * disable opening a second Groovy console
2790      * (or re-enable when the console is closed)
2791      */
2792     groovyShell.setEnabled(!enabled);
2793
2794     AlignFrame[] alignFrames = getAlignFrames();
2795     if (alignFrames != null)
2796     {
2797       for (AlignFrame af : alignFrames)
2798       {
2799         af.setGroovyEnabled(enabled);
2800       }
2801     }
2802   }
2803
2804   /**
2805    * Progress bars managed by the IProgressIndicator method.
2806    */
2807   private Hashtable<Long, JPanel> progressBars;
2808
2809   private Hashtable<Long, IProgressIndicatorHandler> progressBarHandlers;
2810
2811   /*
2812    * (non-Javadoc)
2813    * 
2814    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
2815    */
2816   @Override
2817   public void setProgressBar(String message, long id)
2818   {
2819     if (progressBars == null)
2820     {
2821       progressBars = new Hashtable<>();
2822       progressBarHandlers = new Hashtable<>();
2823     }
2824
2825     if (progressBars.get(new Long(id)) != null)
2826     {
2827       JPanel panel = progressBars.remove(new Long(id));
2828       if (progressBarHandlers.contains(new Long(id)))
2829       {
2830         progressBarHandlers.remove(new Long(id));
2831       }
2832       removeProgressPanel(panel);
2833     }
2834     else
2835     {
2836       progressBars.put(new Long(id), addProgressPanel(message));
2837     }
2838   }
2839
2840   /*
2841    * (non-Javadoc)
2842    * 
2843    * @see jalview.gui.IProgressIndicator#registerHandler(long,
2844    * jalview.gui.IProgressIndicatorHandler)
2845    */
2846   @Override
2847   public void registerHandler(final long id,
2848           final IProgressIndicatorHandler handler)
2849   {
2850     if (progressBarHandlers == null
2851             || !progressBars.containsKey(new Long(id)))
2852     {
2853       throw new Error(MessageManager.getString(
2854               "error.call_setprogressbar_before_registering_handler"));
2855     }
2856     progressBarHandlers.put(new Long(id), handler);
2857     final JPanel progressPanel = progressBars.get(new Long(id));
2858     if (handler.canCancel())
2859     {
2860       JButton cancel = new JButton(
2861               MessageManager.getString("action.cancel"));
2862       final IProgressIndicator us = this;
2863       cancel.addActionListener(new ActionListener()
2864       {
2865
2866         @Override
2867         public void actionPerformed(ActionEvent e)
2868         {
2869           handler.cancelActivity(id);
2870           us.setProgressBar(MessageManager
2871                   .formatMessage("label.cancelled_params", new Object[]
2872                   { ((JLabel) progressPanel.getComponent(0)).getText() }),
2873                   id);
2874         }
2875       });
2876       progressPanel.add(cancel, BorderLayout.EAST);
2877     }
2878   }
2879
2880   /**
2881    * 
2882    * @return true if any progress bars are still active
2883    */
2884   @Override
2885   public boolean operationInProgress()
2886   {
2887     if (progressBars != null && progressBars.size() > 0)
2888     {
2889       return true;
2890     }
2891     return false;
2892   }
2893
2894   /**
2895    * This will return the first AlignFrame holding the given viewport instance.
2896    * It will break if there are more than one AlignFrames viewing a particular
2897    * av.
2898    * 
2899    * @param viewport
2900    * @return alignFrame for viewport
2901    */
2902   public static AlignFrame getAlignFrameFor(AlignViewportI viewport)
2903   {
2904     if (desktop != null)
2905     {
2906       AlignmentPanel[] aps = getAlignmentPanels(
2907               viewport.getSequenceSetId());
2908       for (int panel = 0; aps != null && panel < aps.length; panel++)
2909       {
2910         if (aps[panel] != null && aps[panel].av == viewport)
2911         {
2912           return aps[panel].alignFrame;
2913         }
2914       }
2915     }
2916     return null;
2917   }
2918
2919   public VamsasApplication getVamsasApplication()
2920   {
2921     return v_client;
2922
2923   }
2924
2925   /**
2926    * flag set if jalview GUI is being operated programmatically
2927    */
2928   private boolean inBatchMode = false;
2929
2930   /**
2931    * check if jalview GUI is being operated programmatically
2932    * 
2933    * @return inBatchMode
2934    */
2935   public boolean isInBatchMode()
2936   {
2937     return inBatchMode;
2938   }
2939
2940   /**
2941    * set flag if jalview GUI is being operated programmatically
2942    * 
2943    * @param inBatchMode
2944    */
2945   public void setInBatchMode(boolean inBatchMode)
2946   {
2947     this.inBatchMode = inBatchMode;
2948   }
2949
2950   public void startServiceDiscovery()
2951   {
2952     startServiceDiscovery(false);
2953   }
2954
2955   public void startServiceDiscovery(boolean blocking)
2956   {
2957     boolean alive = true;
2958     Thread t0 = null, t1 = null, t2 = null;
2959     // JAL-940 - JALVIEW 1 services are now being EOLed as of JABA 2.1 release
2960     if (true)
2961     {
2962       // todo: changesupport handlers need to be transferred
2963       if (discoverer == null)
2964       {
2965         discoverer = new jalview.ws.jws1.Discoverer();
2966         // register PCS handler for desktop.
2967         discoverer.addPropertyChangeListener(changeSupport);
2968       }
2969       // JAL-940 - disabled JWS1 service configuration - always start discoverer
2970       // until we phase out completely
2971       (t0 = new Thread(discoverer)).start();
2972     }
2973
2974     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
2975     {
2976       t2 = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2977               .startDiscoverer(changeSupport);
2978     }
2979     Thread t3 = null;
2980     {
2981       // TODO: do rest service discovery
2982     }
2983     if (blocking)
2984     {
2985       while (alive)
2986       {
2987         try
2988         {
2989           Thread.sleep(15);
2990         } catch (Exception e)
2991         {
2992         }
2993         alive = (t1 != null && t1.isAlive()) || (t2 != null && t2.isAlive())
2994                 || (t3 != null && t3.isAlive())
2995                 || (t0 != null && t0.isAlive());
2996       }
2997     }
2998   }
2999
3000   /**
3001    * called to check if the service discovery process completed successfully.
3002    * 
3003    * @param evt
3004    */
3005   protected void JalviewServicesChanged(PropertyChangeEvent evt)
3006   {
3007     if (evt.getNewValue() == null || evt.getNewValue() instanceof Vector)
3008     {
3009       final String ermsg = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
3010               .getErrorMessages();
3011       if (ermsg != null)
3012       {
3013         if (Cache.getDefault("SHOW_WSDISCOVERY_ERRORS", true))
3014         {
3015           if (serviceChangedDialog == null)
3016           {
3017             // only run if we aren't already displaying one of these.
3018             addDialogThread(serviceChangedDialog = new Runnable()
3019             {
3020               @Override
3021               public void run()
3022               {
3023
3024                 /*
3025                  * JalviewDialog jd =new JalviewDialog() {
3026                  * 
3027                  * @Override protected void cancelPressed() { // TODO
3028                  * Auto-generated method stub
3029                  * 
3030                  * }@Override protected void okPressed() { // TODO
3031                  * Auto-generated method stub
3032                  * 
3033                  * }@Override protected void raiseClosed() { // TODO
3034                  * Auto-generated method stub
3035                  * 
3036                  * } }; jd.initDialogFrame(new
3037                  * JLabel("<html><table width=\"450\"><tr><td>" + ermsg +
3038                  * "<br/>It may be that you have invalid JABA URLs in your web service preferences,"
3039                  * + " or mis-configured HTTP proxy settings.<br/>" +
3040                  * "Check the <em>Connections</em> and <em>Web services</em> tab of the"
3041                  * +
3042                  * " Tools->Preferences dialog box to change them.</td></tr></table></html>"
3043                  * ), true, true, "Web Service Configuration Problem", 450,
3044                  * 400);
3045                  * 
3046                  * jd.waitForInput();
3047                  */
3048                 JvOptionPane.showConfirmDialog(Desktop.desktop,
3049                         new JLabel("<html><table width=\"450\"><tr><td>"
3050                                 + ermsg + "</td></tr></table>"
3051                                 + "<p>It may be that you have invalid JABA URLs<br/>in your web service preferences,"
3052                                 + "<br>or as a command-line argument, or mis-configured HTTP proxy settings.</p>"
3053                                 + "<p>Check the <em>Connections</em> and <em>Web services</em> tab<br/>of the"
3054                                 + " Tools->Preferences dialog box to change them.</p></html>"),
3055                         "Web Service Configuration Problem",
3056                         JvOptionPane.DEFAULT_OPTION,
3057                         JvOptionPane.ERROR_MESSAGE);
3058                 serviceChangedDialog = null;
3059
3060               }
3061             });
3062           }
3063         }
3064         else
3065         {
3066           Cache.log.error(
3067                   "Errors reported by JABA discovery service. Check web services preferences.\n"
3068                           + ermsg);
3069         }
3070       }
3071     }
3072   }
3073
3074   private Runnable serviceChangedDialog = null;
3075
3076   /**
3077    * start a thread to open a URL in the configured browser. Pops up a warning
3078    * dialog to the user if there is an exception when calling out to the browser
3079    * to open the URL.
3080    * 
3081    * @param url
3082    */
3083   public static void showUrl(final String url)
3084   {
3085     showUrl(url, Desktop.instance);
3086   }
3087
3088   /**
3089    * Like showUrl but allows progress handler to be specified
3090    * 
3091    * @param url
3092    * @param progress
3093    *          (null) or object implementing IProgressIndicator
3094    */
3095   public static void showUrl(final String url,
3096           final IProgressIndicator progress)
3097   {
3098     new Thread(new Runnable()
3099     {
3100       @Override
3101       public void run()
3102       {
3103         try
3104         {
3105           if (progress != null)
3106           {
3107             progress.setProgressBar(MessageManager
3108                     .formatMessage("status.opening_params", new Object[]
3109                     { url }), this.hashCode());
3110           }
3111           jalview.util.BrowserLauncher.openURL(url);
3112         } catch (Exception ex)
3113         {
3114           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3115                   MessageManager
3116                           .getString("label.web_browser_not_found_unix"),
3117                   MessageManager.getString("label.web_browser_not_found"),
3118                   JvOptionPane.WARNING_MESSAGE);
3119
3120           ex.printStackTrace();
3121         }
3122         if (progress != null)
3123         {
3124           progress.setProgressBar(null, this.hashCode());
3125         }
3126       }
3127     }).start();
3128   }
3129
3130   public static WsParamSetManager wsparamManager = null;
3131
3132   public static ParamManager getUserParameterStore()
3133   {
3134     if (wsparamManager == null)
3135     {
3136       wsparamManager = new WsParamSetManager();
3137     }
3138     return wsparamManager;
3139   }
3140
3141   /**
3142    * static hyperlink handler proxy method for use by Jalview's internal windows
3143    * 
3144    * @param e
3145    */
3146   public static void hyperlinkUpdate(HyperlinkEvent e)
3147   {
3148     if (e.getEventType() == EventType.ACTIVATED)
3149     {
3150       String url = null;
3151       try
3152       {
3153         url = e.getURL().toString();
3154         Desktop.showUrl(url);
3155       } catch (Exception x)
3156       {
3157         if (url != null)
3158         {
3159           if (Cache.log != null)
3160           {
3161             Cache.log.error("Couldn't handle string " + url + " as a URL.");
3162           }
3163           else
3164           {
3165             System.err.println(
3166                     "Couldn't handle string " + url + " as a URL.");
3167           }
3168         }
3169         // ignore any exceptions due to dud links.
3170       }
3171
3172     }
3173   }
3174
3175   /**
3176    * single thread that handles display of dialogs to user.
3177    */
3178   ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
3179
3180   /**
3181    * flag indicating if dialogExecutor should try to acquire a permit
3182    */
3183   private volatile boolean dialogPause = true;
3184
3185   /**
3186    * pause the queue
3187    */
3188   private java.util.concurrent.Semaphore block = new Semaphore(0);
3189
3190   private static groovy.ui.Console groovyConsole;
3191
3192   /**
3193    * add another dialog thread to the queue
3194    * 
3195    * @param prompter
3196    */
3197   public void addDialogThread(final Runnable prompter)
3198   {
3199     dialogExecutor.submit(new Runnable()
3200     {
3201       @Override
3202       public void run()
3203       {
3204         if (dialogPause)
3205         {
3206           try
3207           {
3208             block.acquire();
3209           } catch (InterruptedException x)
3210           {
3211           }
3212           ;
3213         }
3214         if (instance == null)
3215         {
3216           return;
3217         }
3218         try
3219         {
3220           SwingUtilities.invokeAndWait(prompter);
3221         } catch (Exception q)
3222         {
3223           Cache.log.warn("Unexpected Exception in dialog thread.", q);
3224         }
3225       }
3226     });
3227   }
3228
3229   public void startDialogQueue()
3230   {
3231     // set the flag so we don't pause waiting for another permit and semaphore
3232     // the current task to begin
3233     dialogPause = false;
3234     block.release();
3235   }
3236
3237   @Override
3238   protected void snapShotWindow_actionPerformed(ActionEvent e)
3239   {
3240     invalidate();
3241     File of;
3242     ImageMaker im = new jalview.util.ImageMaker(
3243             this, ImageMaker.TYPE.EPS, "View of Desktop", getWidth(),
3244             getHeight(), of = new File("Jalview_snapshot"
3245                     + System.currentTimeMillis() + ".eps"),
3246             "View of desktop", null, 0, false);
3247     try
3248     {
3249       paintAll(im.getGraphics());
3250       im.writeImage();
3251     } catch (Exception q)
3252     {
3253       Cache.log.error("Couldn't write snapshot to " + of.getAbsolutePath(),
3254               q);
3255       return;
3256     }
3257     Cache.log.info("Successfully written snapshot to file "
3258             + of.getAbsolutePath());
3259   }
3260
3261   /**
3262    * Explode the views in the given SplitFrame into separate SplitFrame windows.
3263    * This respects (remembers) any previous 'exploded geometry' i.e. the size
3264    * and location last time the view was expanded (if any). However it does not
3265    * remember the split pane divider location - this is set to match the
3266    * 'exploding' frame.
3267    * 
3268    * @param sf
3269    */
3270   public void explodeViews(SplitFrame sf)
3271   {
3272     AlignFrame oldTopFrame = (AlignFrame) sf.getTopFrame();
3273     AlignFrame oldBottomFrame = (AlignFrame) sf.getBottomFrame();
3274     List<? extends AlignmentViewPanel> topPanels = oldTopFrame
3275             .getAlignPanels();
3276     List<? extends AlignmentViewPanel> bottomPanels = oldBottomFrame
3277             .getAlignPanels();
3278     int viewCount = topPanels.size();
3279     if (viewCount < 2)
3280     {
3281       return;
3282     }
3283
3284     /*
3285      * Processing in reverse order works, forwards order leaves the first panels
3286      * not visible. I don't know why!
3287      */
3288     for (int i = viewCount - 1; i >= 0; i--)
3289     {
3290       /*
3291        * Make new top and bottom frames. These take over the respective
3292        * AlignmentPanel objects, including their AlignmentViewports, so the
3293        * cdna/protein relationships between the viewports is carried over to the
3294        * new split frames.
3295        * 
3296        * explodedGeometry holds the (x, y) position of the previously exploded
3297        * SplitFrame, and the (width, height) of the AlignFrame component
3298        */
3299       AlignmentPanel topPanel = (AlignmentPanel) topPanels.get(i);
3300       AlignFrame newTopFrame = new AlignFrame(topPanel);
3301       newTopFrame.setSize(oldTopFrame.getSize());
3302       newTopFrame.setVisible(true);
3303       Rectangle geometry = ((AlignViewport) topPanel.getAlignViewport())
3304               .getExplodedGeometry();
3305       if (geometry != null)
3306       {
3307         newTopFrame.setSize(geometry.getSize());
3308       }
3309
3310       AlignmentPanel bottomPanel = (AlignmentPanel) bottomPanels.get(i);
3311       AlignFrame newBottomFrame = new AlignFrame(bottomPanel);
3312       newBottomFrame.setSize(oldBottomFrame.getSize());
3313       newBottomFrame.setVisible(true);
3314       geometry = ((AlignViewport) bottomPanel.getAlignViewport())
3315               .getExplodedGeometry();
3316       if (geometry != null)
3317       {
3318         newBottomFrame.setSize(geometry.getSize());
3319       }
3320
3321       topPanel.av.setGatherViewsHere(false);
3322       bottomPanel.av.setGatherViewsHere(false);
3323       JInternalFrame splitFrame = new SplitFrame(newTopFrame,
3324               newBottomFrame);
3325       if (geometry != null)
3326       {
3327         splitFrame.setLocation(geometry.getLocation());
3328       }
3329       Desktop.addInternalFrame(splitFrame, sf.getTitle(), -1, -1);
3330     }
3331
3332     /*
3333      * Clear references to the panels (now relocated in the new SplitFrames)
3334      * before closing the old SplitFrame.
3335      */
3336     topPanels.clear();
3337     bottomPanels.clear();
3338     sf.close();
3339   }
3340
3341   /**
3342    * Gather expanded split frames, sharing the same pairs of sequence set ids,
3343    * back into the given SplitFrame as additional views. Note that the gathered
3344    * frames may themselves have multiple views.
3345    * 
3346    * @param source
3347    */
3348   public void gatherViews(GSplitFrame source)
3349   {
3350     /*
3351      * special handling of explodedGeometry for a view within a SplitFrame: - it
3352      * holds the (x, y) position of the enclosing SplitFrame, and the (width,
3353      * height) of the AlignFrame component
3354      */
3355     AlignFrame myTopFrame = (AlignFrame) source.getTopFrame();
3356     AlignFrame myBottomFrame = (AlignFrame) source.getBottomFrame();
3357     myTopFrame.viewport.setExplodedGeometry(new Rectangle(source.getX(),
3358             source.getY(), myTopFrame.getWidth(), myTopFrame.getHeight()));
3359     myBottomFrame.viewport
3360             .setExplodedGeometry(new Rectangle(source.getX(), source.getY(),
3361                     myBottomFrame.getWidth(), myBottomFrame.getHeight()));
3362     myTopFrame.viewport.setGatherViewsHere(true);
3363     myBottomFrame.viewport.setGatherViewsHere(true);
3364     String topViewId = myTopFrame.viewport.getSequenceSetId();
3365     String bottomViewId = myBottomFrame.viewport.getSequenceSetId();
3366
3367     JInternalFrame[] frames = desktop.getAllFrames();
3368     for (JInternalFrame frame : frames)
3369     {
3370       if (frame instanceof SplitFrame && frame != source)
3371       {
3372         SplitFrame sf = (SplitFrame) frame;
3373         AlignFrame topFrame = (AlignFrame) sf.getTopFrame();
3374         AlignFrame bottomFrame = (AlignFrame) sf.getBottomFrame();
3375         boolean gatherThis = false;
3376         for (int a = 0; a < topFrame.alignPanels.size(); a++)
3377         {
3378           AlignmentPanel topPanel = topFrame.alignPanels.get(a);
3379           AlignmentPanel bottomPanel = bottomFrame.alignPanels.get(a);
3380           if (topViewId.equals(topPanel.av.getSequenceSetId())
3381                   && bottomViewId.equals(bottomPanel.av.getSequenceSetId()))
3382           {
3383             gatherThis = true;
3384             topPanel.av.setGatherViewsHere(false);
3385             bottomPanel.av.setGatherViewsHere(false);
3386             topPanel.av.setExplodedGeometry(
3387                     new Rectangle(sf.getLocation(), topFrame.getSize()));
3388             bottomPanel.av.setExplodedGeometry(
3389                     new Rectangle(sf.getLocation(), bottomFrame.getSize()));
3390             myTopFrame.addAlignmentPanel(topPanel, false);
3391             myBottomFrame.addAlignmentPanel(bottomPanel, false);
3392           }
3393         }
3394
3395         if (gatherThis)
3396         {
3397           topFrame.getAlignPanels().clear();
3398           bottomFrame.getAlignPanels().clear();
3399           sf.close();
3400         }
3401       }
3402     }
3403
3404     /*
3405      * The dust settles...give focus to the tab we did this from.
3406      */
3407     myTopFrame.setDisplayedView(myTopFrame.alignPanel);
3408   }
3409
3410   public static groovy.ui.Console getGroovyConsole()
3411   {
3412     return groovyConsole;
3413   }
3414
3415   /**
3416    * handles the payload of a drag and drop event.
3417    * 
3418    * TODO refactor to desktop utilities class
3419    * 
3420    * @param files
3421    *          - Data source strings extracted from the drop event
3422    * @param protocols
3423    *          - protocol for each data source extracted from the drop event
3424    * @param evt
3425    *          - the drop event
3426    * @param t
3427    *          - the payload from the drop event
3428    * @throws Exception
3429    */
3430   public static void transferFromDropTarget(List<String> files,
3431           List<DataSourceType> protocols, DropTargetDropEvent evt,
3432           Transferable t) throws Exception
3433   {
3434
3435     DataFlavor uriListFlavor = new DataFlavor(
3436             "text/uri-list;class=java.lang.String"), urlFlavour = null;
3437     try
3438     {
3439       urlFlavour = new DataFlavor(
3440               "application/x-java-url; class=java.net.URL");
3441     } catch (ClassNotFoundException cfe)
3442     {
3443       Cache.log.debug("Couldn't instantiate the URL dataflavor.", cfe);
3444     }
3445
3446     if (urlFlavour != null && t.isDataFlavorSupported(urlFlavour))
3447     {
3448
3449       try
3450       {
3451         java.net.URL url = (URL) t.getTransferData(urlFlavour);
3452         // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099
3453         // means url may be null.
3454         if (url != null)
3455         {
3456           protocols.add(DataSourceType.URL);
3457           files.add(url.toString());
3458           Cache.log.debug("Drop handled as URL dataflavor "
3459                   + files.get(files.size() - 1));
3460           return;
3461         }
3462         else
3463         {
3464           if (Platform.isAMac())
3465           {
3466             System.err.println(
3467                     "Please ignore plist error - occurs due to problem with java 8 on OSX");
3468           }
3469           ;
3470         }
3471       } catch (Throwable ex)
3472       {
3473         Cache.log.debug("URL drop handler failed.", ex);
3474       }
3475     }
3476     if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
3477     {
3478       // Works on Windows and MacOSX
3479       Cache.log.debug("Drop handled as javaFileListFlavor");
3480       for (Object file : (List) t
3481               .getTransferData(DataFlavor.javaFileListFlavor))
3482       {
3483         files.add(((File) file).toString());
3484         protocols.add(DataSourceType.FILE);
3485       }
3486     }
3487     else
3488     {
3489       // Unix like behaviour
3490       boolean added = false;
3491       String data = null;
3492       if (t.isDataFlavorSupported(uriListFlavor))
3493       {
3494         Cache.log.debug("Drop handled as uriListFlavor");
3495         // This is used by Unix drag system
3496         data = (String) t.getTransferData(uriListFlavor);
3497       }
3498       if (data == null)
3499       {
3500         // fallback to text: workaround - on OSX where there's a JVM bug
3501         Cache.log.debug("standard URIListFlavor failed. Trying text");
3502         // try text fallback
3503         DataFlavor textDf = new DataFlavor(
3504                 "text/plain;class=java.lang.String");
3505         if (t.isDataFlavorSupported(textDf))
3506         {
3507           data = (String) t.getTransferData(textDf);
3508         }
3509
3510         Cache.log.debug("Plain text drop content returned "
3511                 + (data == null ? "Null - failed" : data));
3512
3513       }
3514       if (data != null)
3515       {
3516         while (protocols.size() < files.size())
3517         {
3518           Cache.log.debug("Adding missing FILE protocol for "
3519                   + files.get(protocols.size()));
3520           protocols.add(DataSourceType.FILE);
3521         }
3522         for (java.util.StringTokenizer st = new java.util.StringTokenizer(
3523                 data, "\r\n"); st.hasMoreTokens();)
3524         {
3525           added = true;
3526           String s = st.nextToken();
3527           if (s.startsWith("#"))
3528           {
3529             // the line is a comment (as per the RFC 2483)
3530             continue;
3531           }
3532           java.net.URI uri = new java.net.URI(s);
3533           if (uri.getScheme().toLowerCase().startsWith("http"))
3534           {
3535             protocols.add(DataSourceType.URL);
3536             files.add(uri.toString());
3537           }
3538           else
3539           {
3540             // otherwise preserve old behaviour: catch all for file objects
3541             java.io.File file = new java.io.File(uri);
3542             protocols.add(DataSourceType.FILE);
3543             files.add(file.toString());
3544           }
3545         }
3546       }
3547
3548       if (Cache.log.isDebugEnabled())
3549       {
3550         if (data == null || !added)
3551         {
3552
3553           if (t.getTransferDataFlavors() != null
3554                   && t.getTransferDataFlavors().length > 0)
3555           {
3556             Cache.log.debug(
3557                     "Couldn't resolve drop data. Here are the supported flavors:");
3558             for (DataFlavor fl : t.getTransferDataFlavors())
3559             {
3560               Cache.log.debug(
3561                       "Supported transfer dataflavor: " + fl.toString());
3562               Object df = t.getTransferData(fl);
3563               if (df != null)
3564               {
3565                 Cache.log.debug("Retrieves: " + df);
3566               }
3567               else
3568               {
3569                 Cache.log.debug("Retrieved nothing");
3570               }
3571             }
3572           }
3573           else
3574           {
3575             Cache.log.debug("Couldn't resolve dataflavor for drop: "
3576                     + t.toString());
3577           }
3578         }
3579       }
3580     }
3581     if (Platform.isWindows())
3582
3583     {
3584       Cache.log.debug("Scanning dropped content for Windows Link Files");
3585
3586       // resolve any .lnk files in the file drop
3587       for (int f = 0; f < files.size(); f++)
3588       {
3589         String source = files.get(f).toLowerCase();
3590         if (protocols.get(f).equals(DataSourceType.FILE)
3591                 && (source.endsWith(".lnk") || source.endsWith(".url")
3592                         || source.endsWith(".site")))
3593         {
3594           try
3595           {
3596             File lf = new File(files.get(f));
3597             // process link file to get a URL
3598             Cache.log.debug("Found potential link file: " + lf);
3599             WindowsShortcut wscfile = new WindowsShortcut(lf);
3600             String fullname = wscfile.getRealFilename();
3601             protocols.set(f, FormatAdapter.checkProtocol(fullname));
3602             files.set(f, fullname);
3603             Cache.log.debug("Parsed real filename " + fullname
3604                     + " to extract protocol: " + protocols.get(f));
3605           } catch (Exception ex)
3606           {
3607             Cache.log.error(
3608                     "Couldn't parse " + files.get(f) + " as a link file.",
3609                     ex);
3610           }
3611         }
3612       }
3613     }
3614   }
3615
3616   /**
3617    * Sets the Preferences property for experimental features to True or False
3618    * depending on the state of the controlling menu item
3619    */
3620   @Override
3621   protected void showExperimental_actionPerformed(boolean selected)
3622   {
3623     Cache.setProperty(EXPERIMENTAL_FEATURES, Boolean.toString(selected));
3624   }
3625
3626   /**
3627    * Answers a (possibly empty) list of any structure viewer frames (currently
3628    * for either Jmol or Chimera) which are currently open. This may optionally
3629    * be restricted to viewers of a specified class, or viewers linked to a
3630    * specified alignment panel.
3631    * 
3632    * @param apanel
3633    *          if not null, only return viewers linked to this panel
3634    * @param structureViewerClass
3635    *          if not null, only return viewers of this class
3636    * @return
3637    */
3638   public List<StructureViewerBase> getStructureViewers(
3639           AlignmentPanel apanel,
3640           Class<? extends StructureViewerBase> structureViewerClass)
3641   {
3642     List<StructureViewerBase> result = new ArrayList<>();
3643     JInternalFrame[] frames = Desktop.instance.getAllFrames();
3644
3645     for (JInternalFrame frame : frames)
3646     {
3647       if (frame instanceof StructureViewerBase)
3648       {
3649         if (structureViewerClass == null
3650                 || structureViewerClass.isInstance(frame))
3651         {
3652           if (apanel == null
3653                   || ((StructureViewerBase) frame).isLinkedWith(apanel))
3654           {
3655             result.add((StructureViewerBase) frame);
3656           }
3657         }
3658       }
3659     }
3660     return result;
3661   }
3662 }