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