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