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