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