JAL-3048 DialogRunnerI reduced to minimal implementation
[jalview.git] / src / jalview / gui / Desktop.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import static jalview.util.UrlConstants.SEQUENCE_ID;
24
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.bin.Cache;
28 import jalview.bin.Jalview;
29 import jalview.gui.ImageExporter.ImageWriterI;
30 import jalview.io.DataSourceType;
31 import jalview.io.FileFormat;
32 import jalview.io.FileFormatException;
33 import jalview.io.FileFormatI;
34 import jalview.io.FileFormats;
35 import jalview.io.FileLoader;
36 import jalview.io.FormatAdapter;
37 import jalview.io.IdentifyFile;
38 import jalview.io.JalviewFileChooser;
39 import jalview.io.JalviewFileView;
40 import jalview.jbgui.GSplitFrame;
41 import jalview.jbgui.GStructureViewer;
42 import jalview.structure.StructureSelectionManager;
43 import jalview.urls.IdOrgSettings;
44 import jalview.util.BrowserLauncher;
45 import jalview.util.ImageMaker.TYPE;
46 import jalview.util.MessageManager;
47 import jalview.util.Platform;
48 import jalview.util.UrlConstants;
49 import jalview.viewmodel.AlignmentViewport;
50 import jalview.ws.params.ParamManager;
51 import jalview.ws.utils.UrlDownloadClient;
52
53 import java.awt.BorderLayout;
54 import java.awt.Color;
55 import java.awt.Dimension;
56 import java.awt.FontMetrics;
57 import java.awt.Graphics;
58 import java.awt.GridLayout;
59 import java.awt.Point;
60 import java.awt.Rectangle;
61 import java.awt.Toolkit;
62 import java.awt.Window;
63 import java.awt.datatransfer.Clipboard;
64 import java.awt.datatransfer.ClipboardOwner;
65 import java.awt.datatransfer.DataFlavor;
66 import java.awt.datatransfer.Transferable;
67 import java.awt.dnd.DnDConstants;
68 import java.awt.dnd.DropTargetDragEvent;
69 import java.awt.dnd.DropTargetDropEvent;
70 import java.awt.dnd.DropTargetEvent;
71 import java.awt.dnd.DropTargetListener;
72 import java.awt.event.ActionEvent;
73 import java.awt.event.ActionListener;
74 import java.awt.event.InputEvent;
75 import java.awt.event.KeyEvent;
76 import java.awt.event.MouseAdapter;
77 import java.awt.event.MouseEvent;
78 import java.awt.event.WindowAdapter;
79 import java.awt.event.WindowEvent;
80 import java.beans.PropertyChangeEvent;
81 import java.beans.PropertyChangeListener;
82 import java.io.BufferedInputStream;
83 import java.io.File;
84 import java.io.FileOutputStream;
85 import java.io.IOException;
86 import java.net.URL;
87 import java.util.ArrayList;
88 import java.util.Hashtable;
89 import java.util.List;
90 import java.util.ListIterator;
91 import java.util.Vector;
92 import java.util.concurrent.ExecutorService;
93 import java.util.concurrent.Executors;
94 import java.util.concurrent.Semaphore;
95
96 import javax.swing.AbstractAction;
97 import javax.swing.Action;
98 import javax.swing.ActionMap;
99 import javax.swing.Box;
100 import javax.swing.BoxLayout;
101 import javax.swing.DefaultDesktopManager;
102 import javax.swing.DesktopManager;
103 import javax.swing.InputMap;
104 import javax.swing.JButton;
105 import javax.swing.JCheckBox;
106 import javax.swing.JComboBox;
107 import javax.swing.JComponent;
108 import javax.swing.JDesktopPane;
109 import javax.swing.JFrame;
110 import javax.swing.JInternalFrame;
111 import javax.swing.JLabel;
112 import javax.swing.JMenuItem;
113 import javax.swing.JPanel;
114 import javax.swing.JPopupMenu;
115 import javax.swing.JProgressBar;
116 import javax.swing.JTextField;
117 import javax.swing.KeyStroke;
118 import javax.swing.SwingUtilities;
119 import javax.swing.event.HyperlinkEvent;
120 import javax.swing.event.HyperlinkEvent.EventType;
121 import javax.swing.event.InternalFrameAdapter;
122 import javax.swing.event.InternalFrameEvent;
123 import javax.swing.event.MenuEvent;
124 import javax.swing.event.MenuListener;
125
126 import org.stackoverflowusers.file.WindowsShortcut;
127
128 /**
129  * Jalview Desktop
130  * 
131  * 
132  * @author $author$
133  * @version $Revision: 1.155 $
134  */
135 public class Desktop extends jalview.jbgui.GDesktop
136         implements DropTargetListener, ClipboardOwner, IProgressIndicator,
137         jalview.api.StructureSelectionManagerProvider
138 {
139   private static int DEFAULT_MIN_WIDTH = 300;
140
141   private static int DEFAULT_MIN_HEIGHT = 250;
142
143   private static int ALIGN_FRAME_DEFAULT_MIN_WIDTH = 600;
144
145   private static int ALIGN_FRAME_DEFAULT_MIN_HEIGHT = 70;
146
147   private static final String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES";
148
149   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
150
151   /**
152    * news reader - null if it was never started.
153    */
154   private BlogReader jvnews = null;
155
156   private File projectFile;
157
158   /**
159    * @param listener
160    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
161    */
162   public void addJalviewPropertyChangeListener(
163           PropertyChangeListener listener)
164   {
165     changeSupport.addJalviewPropertyChangeListener(listener);
166   }
167
168   /**
169    * @param propertyName
170    * @param listener
171    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.lang.String,
172    *      java.beans.PropertyChangeListener)
173    */
174   public void addJalviewPropertyChangeListener(String propertyName,
175           PropertyChangeListener listener)
176   {
177     changeSupport.addJalviewPropertyChangeListener(propertyName, listener);
178   }
179
180   /**
181    * @param propertyName
182    * @param listener
183    * @see jalview.gui.JalviewChangeSupport#removeJalviewPropertyChangeListener(java.lang.String,
184    *      java.beans.PropertyChangeListener)
185    */
186   public void removeJalviewPropertyChangeListener(String propertyName,
187           PropertyChangeListener listener)
188   {
189     changeSupport.removeJalviewPropertyChangeListener(propertyName,
190             listener);
191   }
192
193   /** Singleton Desktop instance */
194   public static Desktop instance;
195
196   public static MyDesktopPane desktop;
197
198   public static MyDesktopPane getDesktop()
199   {
200     // BH 2018 could use currentThread() here as a reference to a
201     // Hashtable<Thread, MyDesktopPane> in JavaScript
202     return desktop;
203   }
204
205   static int openFrameCount = 0;
206
207   static final int xOffset = 30;
208
209   static final int yOffset = 30;
210
211   public static jalview.ws.jws1.Discoverer discoverer;
212
213   public static Object[] jalviewClipboard;
214
215   public static boolean internalCopy = false;
216
217   static int fileLoadingCount = 0;
218
219   class MyDesktopManager implements DesktopManager
220   {
221
222     private DesktopManager delegate;
223
224     public MyDesktopManager(DesktopManager delegate)
225     {
226       this.delegate = delegate;
227     }
228
229     @Override
230     public void activateFrame(JInternalFrame f)
231     {
232       try
233       {
234         delegate.activateFrame(f);
235       } catch (NullPointerException npe)
236       {
237         Point p = getMousePosition();
238         instance.showPasteMenu(p.x, p.y);
239       }
240     }
241
242     @Override
243     public void beginDraggingFrame(JComponent f)
244     {
245       delegate.beginDraggingFrame(f);
246     }
247
248     @Override
249     public void beginResizingFrame(JComponent f, int direction)
250     {
251       delegate.beginResizingFrame(f, direction);
252     }
253
254     @Override
255     public void closeFrame(JInternalFrame f)
256     {
257       delegate.closeFrame(f);
258     }
259
260     @Override
261     public void deactivateFrame(JInternalFrame f)
262     {
263       delegate.deactivateFrame(f);
264     }
265
266     @Override
267     public void deiconifyFrame(JInternalFrame f)
268     {
269       delegate.deiconifyFrame(f);
270     }
271
272     @Override
273     public void dragFrame(JComponent f, int newX, int newY)
274     {
275       if (newY < 0)
276       {
277         newY = 0;
278       }
279       delegate.dragFrame(f, newX, newY);
280     }
281
282     @Override
283     public void endDraggingFrame(JComponent f)
284     {
285       delegate.endDraggingFrame(f);
286       desktop.repaint();
287     }
288
289     @Override
290     public void endResizingFrame(JComponent f)
291     {
292       delegate.endResizingFrame(f);
293       desktop.repaint();
294     }
295
296     @Override
297     public void iconifyFrame(JInternalFrame f)
298     {
299       delegate.iconifyFrame(f);
300     }
301
302     @Override
303     public void maximizeFrame(JInternalFrame f)
304     {
305       delegate.maximizeFrame(f);
306     }
307
308     @Override
309     public void minimizeFrame(JInternalFrame f)
310     {
311       delegate.minimizeFrame(f);
312     }
313
314     @Override
315     public void openFrame(JInternalFrame f)
316     {
317       delegate.openFrame(f);
318     }
319
320     @Override
321     public void resizeFrame(JComponent f, int newX, int newY, int newWidth,
322             int newHeight)
323     {
324       if (newY < 0)
325       {
326         newY = 0;
327       }
328       delegate.resizeFrame(f, newX, newY, newWidth, newHeight);
329     }
330
331     @Override
332     public void setBoundsForFrame(JComponent f, int newX, int newY,
333             int newWidth, int newHeight)
334     {
335       delegate.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
336     }
337
338     // All other methods, simply delegate
339
340   }
341
342   /**
343    * Creates a new Desktop object.
344    */
345   public Desktop()
346   {
347     /**
348      * A note to implementors. It is ESSENTIAL that any activities that might block
349      * are spawned off as threads rather than waited for during this constructor.
350      */
351     instance = this;
352     if (!Jalview.isJS())
353     {
354       doVamsasClientCheck();
355     }
356
357     doConfigureStructurePrefs();
358     setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
359     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
360     boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",
361             false);
362     boolean showjconsole = jalview.bin.Cache.getDefault("SHOW_JAVA_CONSOLE",
363             false);
364     desktop = new MyDesktopPane(selmemusage);
365     
366     
367     showMemusage.setSelected(selmemusage);
368     desktop.setBackground(Color.white);
369     getContentPane().setLayout(new BorderLayout());
370     // alternate config - have scrollbars - see notes in JAL-153
371     // JScrollPane sp = new JScrollPane();
372     // sp.getViewport().setView(desktop);
373     // getContentPane().add(sp, BorderLayout.CENTER);
374     
375     // BH 2018 - just an experiment to try unclipped JInternalFrames. 
376     // Must set for all three to be active:
377         if (Jalview.isJS()) 
378         {
379           getRootPane().putClientProperty("swingjs.overflow.hidden", "false");
380           ((JComponent) getContentPane()).putClientProperty("swingjs.overflow.hidden", "false");
381           desktop.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.isWindows() ? new DefaultDesktopManager()
391                     : Platform.isAMac()
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);
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 (Jalview.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 = Jalview.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 (Jalview.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(boolean asCastor)
1655   {
1656     JalviewFileChooser chooser = new JalviewFileChooser(
1657             asCastor ? "jvp" : "jvx",
1658             "Jalview Project");
1659
1660     chooser.setFileView(new JalviewFileView());
1661     chooser.setDialogTitle(MessageManager.getString("label.save_state"));
1662     int option = chooser.showSaveDialog(this);
1663     if (option == JalviewFileChooser.APPROVE_OPTION)
1664     {
1665       File choice = chooser.getSelectedFile();
1666       setProjectFile(choice);
1667
1668       new Thread(new Runnable()
1669       {
1670         @Override
1671         public void run()
1672         {
1673           // TODO: refactor to Jalview desktop session controller action.
1674           setProgressBar(MessageManager.formatMessage(
1675                   "label.saving_jalview_project", new Object[]
1676                   { choice.getName() }), choice.hashCode());
1677           jalview.bin.Cache.setProperty("LAST_DIRECTORY",
1678                   choice.getParent());
1679           // TODO catch and handle errors for savestate
1680           // TODO prevent user from messing with the Desktop whilst we're saving
1681           try
1682           {
1683             if (asCastor)
1684             {
1685               new Jalview2XML().saveState(choice);
1686             }
1687             else
1688             {
1689               new jalview.project.Jalview2XML().saveState(choice);
1690             }
1691           } catch (OutOfMemoryError oom)
1692           {
1693             new OOMWarning(
1694                     "Whilst saving current state to " + choice.getName(),
1695                     oom);
1696           } catch (Exception ex)
1697           {
1698             Cache.log.error(
1699                     "Problems whilst trying to save to " + choice.getName(),
1700                     ex);
1701             JvOptionPane.showMessageDialog(Desktop.this,
1702                     MessageManager.formatMessage(
1703                             "label.error_whilst_saving_current_state_to",
1704                             new Object[]
1705                             { choice.getName() }),
1706                     MessageManager.getString("label.couldnt_save_project"),
1707                     JvOptionPane.WARNING_MESSAGE);
1708           }
1709           setProgressBar(null, choice.hashCode());
1710         }
1711       }).start();
1712       }
1713   }
1714
1715   void setProjectFile(File choice)
1716   {
1717     this.projectFile = choice;
1718   }
1719
1720   public File getProjectFile()
1721   {
1722     return this.projectFile;
1723   }
1724
1725   /**
1726    * Prompts the user to choose a file and loads in as a Jalview project file
1727    */
1728   @Override
1729   public void loadState_actionPerformed(boolean asCastor)
1730   {
1731     // TODO: GET RID OF .JVX BEFORE RELEASE JIM!
1732     final String[] suffix = asCastor ? new String[] { "jvp", "jar" }
1733             : new String[]
1734             { "jvx" };
1735     final String[] desc = asCastor
1736             ? new String[]
1737             { "Jalview Project", "Jalview Project (old)" }
1738             : new String[]
1739             { "Jalview Project" };
1740     JalviewFileChooser chooser = new JalviewFileChooser(
1741             Cache.getProperty("LAST_DIRECTORY"), suffix,
1742             desc,
1743             "Jalview Project");
1744     chooser.setFileView(new JalviewFileView());
1745     chooser.setDialogTitle(MessageManager.getString("label.restore_state"));
1746     chooser.setResponseHandler(0, new Runnable()
1747     {
1748       @Override
1749       public void run()
1750       {
1751         File selectedFile = chooser.getSelectedFile();
1752         setProjectFile(selectedFile);
1753         final String choice = selectedFile.getAbsolutePath();
1754         Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
1755         new Thread(new Runnable()
1756         {
1757           @Override
1758           public void run()
1759           {
1760                 try {
1761               if (asCastor)
1762               {
1763                 new Jalview2XML().loadJalviewAlign(choice);
1764               }
1765               else
1766               {
1767                 new jalview.project.Jalview2XML().loadJalviewAlign(choice);
1768               }
1769             } catch (OutOfMemoryError oom)
1770             {
1771             new OOMWarning("Whilst loading project from " + choice, oom);
1772             } catch (Exception ex)
1773             {
1774             Cache.log.error(
1775                     "Problems whilst loading project from " + choice, ex);
1776             JvOptionPane.showMessageDialog(Desktop.desktop,
1777                     MessageManager.formatMessage(
1778                             "label.error_whilst_loading_project_from",
1779                             new Object[]
1780                             { choice }),
1781                     MessageManager.getString("label.couldnt_load_project"),
1782                     JvOptionPane.WARNING_MESSAGE);
1783             }
1784           }
1785         }).start();
1786       }
1787     });
1788     
1789     chooser.showOpenDialog(this);
1790   }
1791
1792   @Override
1793   public void inputSequence_actionPerformed(ActionEvent e)
1794   {
1795     new SequenceFetcher(this);
1796   }
1797
1798   JPanel progressPanel;
1799
1800   ArrayList<JPanel> fileLoadingPanels = new ArrayList<>();
1801
1802   public void startLoading(final Object fileName)
1803   {
1804     if (fileLoadingCount == 0)
1805     {
1806       fileLoadingPanels.add(addProgressPanel(MessageManager
1807               .formatMessage("label.loading_file", new Object[]
1808               { fileName })));
1809     }
1810     fileLoadingCount++;
1811   }
1812
1813   private JPanel addProgressPanel(String string)
1814   {
1815     if (progressPanel == null)
1816     {
1817       progressPanel = new JPanel(new GridLayout(1, 1));
1818       totalProgressCount = 0;
1819       instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
1820     }
1821     JPanel thisprogress = new JPanel(new BorderLayout(10, 5));
1822     JProgressBar progressBar = new JProgressBar();
1823     progressBar.setIndeterminate(true);
1824
1825     thisprogress.add(new JLabel(string), BorderLayout.WEST);
1826
1827     thisprogress.add(progressBar, BorderLayout.CENTER);
1828     progressPanel.add(thisprogress);
1829     ((GridLayout) progressPanel.getLayout()).setRows(
1830             ((GridLayout) progressPanel.getLayout()).getRows() + 1);
1831     ++totalProgressCount;
1832     instance.validate();
1833     return thisprogress;
1834   }
1835
1836   int totalProgressCount = 0;
1837
1838   private void removeProgressPanel(JPanel progbar)
1839   {
1840     if (progressPanel != null)
1841     {
1842       synchronized (progressPanel)
1843       {
1844         progressPanel.remove(progbar);
1845         GridLayout gl = (GridLayout) progressPanel.getLayout();
1846         gl.setRows(gl.getRows() - 1);
1847         if (--totalProgressCount < 1)
1848         {
1849           this.getContentPane().remove(progressPanel);
1850           progressPanel = null;
1851         }
1852       }
1853     }
1854     validate();
1855   }
1856
1857   public void stopLoading()
1858   {
1859     fileLoadingCount--;
1860     if (fileLoadingCount < 1)
1861     {
1862       while (fileLoadingPanels.size() > 0)
1863       {
1864         removeProgressPanel(fileLoadingPanels.remove(0));
1865       }
1866       fileLoadingPanels.clear();
1867       fileLoadingCount = 0;
1868     }
1869     validate();
1870   }
1871
1872   public static int getViewCount(String alignmentId)
1873   {
1874     AlignmentViewport[] aps = getViewports(alignmentId);
1875     return (aps == null) ? 0 : aps.length;
1876   }
1877
1878   /**
1879    * 
1880    * @param alignmentId
1881    *          - if null, all sets are returned
1882    * @return all AlignmentPanels concerning the alignmentId sequence set
1883    */
1884   public static AlignmentPanel[] getAlignmentPanels(String alignmentId)
1885   {
1886     if (Desktop.desktop == null)
1887     {
1888       // no frames created and in headless mode
1889       // TODO: verify that frames are recoverable when in headless mode
1890       return null;
1891     }
1892     List<AlignmentPanel> aps = new ArrayList<>();
1893     AlignFrame[] frames = getAlignFrames();
1894     if (frames == null)
1895     {
1896       return null;
1897     }
1898     for (AlignFrame af : frames)
1899     {
1900       for (AlignmentPanel ap : af.alignPanels)
1901       {
1902         if (alignmentId == null
1903                 || alignmentId.equals(ap.av.getSequenceSetId()))
1904         {
1905           aps.add(ap);
1906         }
1907       }
1908     }
1909     if (aps.size() == 0)
1910     {
1911       return null;
1912     }
1913     AlignmentPanel[] vap = aps.toArray(new AlignmentPanel[aps.size()]);
1914     return vap;
1915   }
1916
1917   /**
1918    * get all the viewports on an alignment.
1919    * 
1920    * @param sequenceSetId
1921    *          unique alignment id (may be null - all viewports returned in that
1922    *          case)
1923    * @return all viewports on the alignment bound to sequenceSetId
1924    */
1925   public static AlignmentViewport[] getViewports(String sequenceSetId)
1926   {
1927     List<AlignmentViewport> viewp = new ArrayList<>();
1928     if (desktop != null)
1929     {
1930       AlignFrame[] frames = Desktop.getAlignFrames();
1931
1932       for (AlignFrame afr : frames)
1933       {
1934         if (sequenceSetId == null || afr.getViewport().getSequenceSetId()
1935                 .equals(sequenceSetId))
1936         {
1937           if (afr.alignPanels != null)
1938           {
1939             for (AlignmentPanel ap : afr.alignPanels)
1940             {
1941               if (sequenceSetId == null
1942                       || sequenceSetId.equals(ap.av.getSequenceSetId()))
1943               {
1944                 viewp.add(ap.av);
1945               }
1946             }
1947           }
1948           else
1949           {
1950             viewp.add(afr.getViewport());
1951           }
1952         }
1953       }
1954       if (viewp.size() > 0)
1955       {
1956         return viewp.toArray(new AlignmentViewport[viewp.size()]);
1957       }
1958     }
1959     return null;
1960   }
1961
1962   /**
1963    * Explode the views in the given frame into separate AlignFrame
1964    * 
1965    * @param af
1966    */
1967   public static void explodeViews(AlignFrame af)
1968   {
1969     int size = af.alignPanels.size();
1970     if (size < 2)
1971     {
1972       return;
1973     }
1974
1975     for (int i = 0; i < size; i++)
1976     {
1977       AlignmentPanel ap = af.alignPanels.get(i);
1978       AlignFrame newaf = new AlignFrame(ap);
1979
1980       /*
1981        * Restore the view's last exploded frame geometry if known. Multiple
1982        * views from one exploded frame share and restore the same (frame)
1983        * position and size.
1984        */
1985       Rectangle geometry = ap.av.getExplodedGeometry();
1986       if (geometry != null)
1987       {
1988         newaf.setBounds(geometry);
1989       }
1990
1991       ap.av.setGatherViewsHere(false);
1992
1993       addInternalFrame(newaf, af.getTitle(), AlignFrame.DEFAULT_WIDTH,
1994               AlignFrame.DEFAULT_HEIGHT);
1995     }
1996
1997     af.alignPanels.clear();
1998     af.closeMenuItem_actionPerformed(true);
1999
2000   }
2001
2002   /**
2003    * Gather expanded views (separate AlignFrame's) with the same sequence set
2004    * identifier back in to this frame as additional views, and close the expanded
2005    * views. Note the expanded frames may themselves have multiple views. We take
2006    * the lot.
2007    * 
2008    * @param source
2009    */
2010   public void gatherViews(AlignFrame source)
2011   {
2012     source.viewport.setGatherViewsHere(true);
2013     source.viewport.setExplodedGeometry(source.getBounds());
2014     JInternalFrame[] frames = desktop.getAllFrames();
2015     String viewId = source.viewport.getSequenceSetId();
2016
2017     for (int t = 0; t < frames.length; t++)
2018     {
2019       if (frames[t] instanceof AlignFrame && frames[t] != source)
2020       {
2021         AlignFrame af = (AlignFrame) frames[t];
2022         boolean gatherThis = false;
2023         for (int a = 0; a < af.alignPanels.size(); a++)
2024         {
2025           AlignmentPanel ap = af.alignPanels.get(a);
2026           if (viewId.equals(ap.av.getSequenceSetId()))
2027           {
2028             gatherThis = true;
2029             ap.av.setGatherViewsHere(false);
2030             ap.av.setExplodedGeometry(af.getBounds());
2031             source.addAlignmentPanel(ap, false);
2032           }
2033         }
2034
2035         if (gatherThis)
2036         {
2037           af.alignPanels.clear();
2038           af.closeMenuItem_actionPerformed(true);
2039         }
2040       }
2041     }
2042
2043   }
2044
2045   jalview.gui.VamsasApplication v_client = null;
2046
2047   @Override
2048   public void vamsasImport_actionPerformed(ActionEvent e)
2049   {
2050     // TODO: JAL-3048 not needed for Jalview-JS
2051
2052     if (v_client == null)
2053     {
2054       // Load and try to start a session.
2055       JalviewFileChooser chooser = new JalviewFileChooser(
2056               jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
2057
2058       chooser.setFileView(new JalviewFileView());
2059       chooser.setDialogTitle(
2060               MessageManager.getString("label.open_saved_vamsas_session"));
2061       chooser.setToolTipText(MessageManager.getString(
2062               "label.select_vamsas_session_opened_as_new_vamsas_session"));
2063
2064       int value = chooser.showOpenDialog(this);
2065
2066       if (value == JalviewFileChooser.APPROVE_OPTION)
2067       {
2068         String fle = chooser.getSelectedFile().toString();
2069         if (!vamsasImport(chooser.getSelectedFile()))
2070         {
2071           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2072                   MessageManager.formatMessage(
2073                           "label.couldnt_import_as_vamsas_session",
2074                           new Object[]
2075                           { fle }),
2076                   MessageManager
2077                           .getString("label.vamsas_document_import_failed"),
2078                   JvOptionPane.ERROR_MESSAGE);
2079         }
2080       }
2081     }
2082     else
2083     {
2084       jalview.bin.Cache.log.error(
2085               "Implementation error - load session from a running session is not supported.");
2086     }
2087   }
2088
2089   /**
2090    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2091    * 
2092    * @param file
2093    * @return true if import was a success and a session was started.
2094    */
2095   public boolean vamsasImport(URL url)
2096   {
2097     // TODO: create progress bar
2098     if (v_client != null)
2099     {
2100
2101       jalview.bin.Cache.log.error(
2102               "Implementation error - load session from a running session is not supported.");
2103       return false;
2104     }
2105
2106     try
2107     {
2108       // copy the URL content to a temporary local file
2109       // TODO: be a bit cleverer here with nio (?!)
2110       File file = File.createTempFile("vdocfromurl", ".vdj");
2111       FileOutputStream fos = new FileOutputStream(file);
2112       BufferedInputStream bis = new BufferedInputStream(url.openStream());
2113       byte[] buffer = new byte[2048];
2114       int ln;
2115       while ((ln = bis.read(buffer)) > -1)
2116       {
2117         fos.write(buffer, 0, ln);
2118       }
2119       bis.close();
2120       fos.close();
2121       v_client = new jalview.gui.VamsasApplication(this, file,
2122               url.toExternalForm());
2123     } catch (Exception ex)
2124     {
2125       jalview.bin.Cache.log.error(
2126               "Failed to create new vamsas session from contents of URL "
2127                       + url,
2128               ex);
2129       return false;
2130     }
2131     setupVamsasConnectedGui();
2132     v_client.initial_update(); // TODO: thread ?
2133     return v_client.inSession();
2134   }
2135
2136   /**
2137    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2138    * 
2139    * @param file
2140    * @return true if import was a success and a session was started.
2141    */
2142   public boolean vamsasImport(File file)
2143   {
2144     if (v_client != null)
2145     {
2146
2147       jalview.bin.Cache.log.error(
2148               "Implementation error - load session from a running session is not supported.");
2149       return false;
2150     }
2151
2152     setProgressBar(MessageManager.formatMessage(
2153             "status.importing_vamsas_session_from", new Object[]
2154             { file.getName() }), file.hashCode());
2155     try
2156     {
2157       v_client = new jalview.gui.VamsasApplication(this, file, null);
2158     } catch (Exception ex)
2159     {
2160       setProgressBar(MessageManager.formatMessage(
2161               "status.importing_vamsas_session_from", new Object[]
2162               { file.getName() }), file.hashCode());
2163       jalview.bin.Cache.log.error(
2164               "New vamsas session from existing session file failed:", ex);
2165       return false;
2166     }
2167     setupVamsasConnectedGui();
2168     v_client.initial_update(); // TODO: thread ?
2169     setProgressBar(MessageManager.formatMessage(
2170             "status.importing_vamsas_session_from", new Object[]
2171             { file.getName() }), file.hashCode());
2172     return v_client.inSession();
2173   }
2174
2175   public boolean joinVamsasSession(String mysesid)
2176   {
2177     if (v_client != null)
2178     {
2179       throw new Error(MessageManager
2180               .getString("error.try_join_vamsas_session_another"));
2181     }
2182     if (mysesid == null)
2183     {
2184       throw new Error(
2185               MessageManager.getString("error.invalid_vamsas_session_id"));
2186     }
2187     v_client = new VamsasApplication(this, mysesid);
2188     setupVamsasConnectedGui();
2189     v_client.initial_update();
2190     return (v_client.inSession());
2191   }
2192
2193   @Override
2194   public void vamsasStart_actionPerformed(ActionEvent e)
2195   {
2196     if (v_client == null)
2197     {
2198       // Start a session.
2199       // we just start a default session for moment.
2200       /*
2201        * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
2202        * getProperty("LAST_DIRECTORY"));
2203        * 
2204        * chooser.setFileView(new JalviewFileView());
2205        * chooser.setDialogTitle("Load Vamsas file");
2206        * chooser.setToolTipText("Import");
2207        * 
2208        * int value = chooser.showOpenDialog(this);
2209        * 
2210        * if (value == JalviewFileChooser.APPROVE_OPTION) { v_client = new
2211        * jalview.gui.VamsasApplication(this, chooser.getSelectedFile());
2212        */
2213       v_client = new VamsasApplication(this);
2214       setupVamsasConnectedGui();
2215       v_client.initial_update(); // TODO: thread ?
2216     }
2217     else
2218     {
2219       // store current data in session.
2220       v_client.push_update(); // TODO: thread
2221     }
2222   }
2223
2224   protected void setupVamsasConnectedGui()
2225   {
2226     vamsasStart.setText(MessageManager.getString("label.session_update"));
2227     vamsasSave.setVisible(true);
2228     vamsasStop.setVisible(true);
2229     vamsasImport.setVisible(false); // Document import to existing session is
2230     // not possible for vamsas-client-1.0.
2231   }
2232
2233   protected void setupVamsasDisconnectedGui()
2234   {
2235     vamsasSave.setVisible(false);
2236     vamsasStop.setVisible(false);
2237     vamsasImport.setVisible(true);
2238     vamsasStart
2239             .setText(MessageManager.getString("label.new_vamsas_session"));
2240   }
2241
2242   @Override
2243   public void vamsasStop_actionPerformed(ActionEvent e)
2244   {
2245     if (v_client != null)
2246     {
2247       v_client.end_session();
2248       v_client = null;
2249       setupVamsasDisconnectedGui();
2250     }
2251   }
2252
2253   protected void buildVamsasStMenu()
2254   {
2255     if (v_client == null)
2256     {
2257       String[] sess = null;
2258       try
2259       {
2260         sess = VamsasApplication.getSessionList();
2261       } catch (Exception e)
2262       {
2263         jalview.bin.Cache.log.warn("Problem getting current sessions list.",
2264                 e);
2265         sess = null;
2266       }
2267       if (sess != null)
2268       {
2269         jalview.bin.Cache.log.debug(
2270                 "Got current sessions list: " + sess.length + " entries.");
2271         VamsasStMenu.removeAll();
2272         for (int i = 0; i < sess.length; i++)
2273         {
2274           JMenuItem sessit = new JMenuItem();
2275           sessit.setText(sess[i]);
2276           sessit.setToolTipText(MessageManager
2277                   .formatMessage("label.connect_to_session", new Object[]
2278                   { sess[i] }));
2279           final Desktop dsktp = this;
2280           final String mysesid = sess[i];
2281           sessit.addActionListener(new ActionListener()
2282           {
2283
2284             @Override
2285             public void actionPerformed(ActionEvent e)
2286             {
2287               if (dsktp.v_client == null)
2288               {
2289                 Thread rthr = new Thread(new Runnable()
2290                 {
2291
2292                   @Override
2293                   public void run()
2294                   {
2295                     dsktp.v_client = new VamsasApplication(dsktp, mysesid);
2296                     dsktp.setupVamsasConnectedGui();
2297                     dsktp.v_client.initial_update();
2298                   }
2299
2300                 });
2301                 rthr.start();
2302               }
2303             };
2304           });
2305           VamsasStMenu.add(sessit);
2306         }
2307         // don't show an empty menu.
2308         VamsasStMenu.setVisible(sess.length > 0);
2309
2310       }
2311       else
2312       {
2313         jalview.bin.Cache.log.debug("No current vamsas sessions.");
2314         VamsasStMenu.removeAll();
2315         VamsasStMenu.setVisible(false);
2316       }
2317     }
2318     else
2319     {
2320       // Not interested in the content. Just hide ourselves.
2321       VamsasStMenu.setVisible(false);
2322     }
2323   }
2324
2325   @Override
2326   public void vamsasSave_actionPerformed(ActionEvent e)
2327   {
2328     // TODO: JAL-3048 not needed for Jalview-JS
2329
2330     if (v_client != null)
2331     {
2332       // TODO: VAMSAS DOCUMENT EXTENSION is VDJ
2333       JalviewFileChooser chooser = new JalviewFileChooser("vdj",
2334               "Vamsas Document");
2335
2336       chooser.setFileView(new JalviewFileView());
2337       chooser.setDialogTitle(MessageManager
2338               .getString("label.save_vamsas_document_archive"));
2339
2340       int value = chooser.showSaveDialog(this);
2341
2342       if (value == JalviewFileChooser.APPROVE_OPTION)
2343       {
2344         java.io.File choice = chooser.getSelectedFile();
2345         JPanel progpanel = addProgressPanel(MessageManager
2346                 .formatMessage("label.saving_vamsas_doc", new Object[]
2347                 { choice.getName() }));
2348         Cache.setProperty("LAST_DIRECTORY", choice.getParent());
2349         String warnmsg = null;
2350         String warnttl = null;
2351         try
2352         {
2353           v_client.vclient.storeDocument(choice);
2354         } catch (Error ex)
2355         {
2356           warnttl = "Serious Problem saving Vamsas Document";
2357           warnmsg = ex.toString();
2358           jalview.bin.Cache.log
2359                   .error("Error Whilst saving document to " + choice, ex);
2360
2361         } catch (Exception ex)
2362         {
2363           warnttl = "Problem saving Vamsas Document.";
2364           warnmsg = ex.toString();
2365           jalview.bin.Cache.log.warn(
2366                   "Exception Whilst saving document to " + choice, ex);
2367
2368         }
2369         removeProgressPanel(progpanel);
2370         if (warnmsg != null)
2371         {
2372           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2373
2374                   warnmsg, warnttl, JvOptionPane.ERROR_MESSAGE);
2375         }
2376       }
2377     }
2378   }
2379
2380   JPanel vamUpdate = null;
2381
2382   /**
2383    * hide vamsas user gui bits when a vamsas document event is being handled.
2384    * 
2385    * @param b
2386    *          true to hide gui, false to reveal gui
2387    */
2388   public void setVamsasUpdate(boolean b)
2389   {
2390     Cache.log.debug("Setting gui for Vamsas update "
2391             + (b ? "in progress" : "finished"));
2392
2393     if (vamUpdate != null)
2394     {
2395       this.removeProgressPanel(vamUpdate);
2396     }
2397     if (b)
2398     {
2399       vamUpdate = this.addProgressPanel(
2400               MessageManager.getString("label.updating_vamsas_session"));
2401     }
2402     vamsasStart.setVisible(!b);
2403     vamsasStop.setVisible(!b);
2404     vamsasSave.setVisible(!b);
2405   }
2406
2407   public JInternalFrame[] getAllFrames()
2408   {
2409     return desktop.getAllFrames();
2410   }
2411
2412   /**
2413    * Checks the given url to see if it gives a response indicating that the user
2414    * should be informed of a new questionnaire.
2415    * 
2416    * @param url
2417    */
2418   public void checkForQuestionnaire(String url)
2419   {
2420     UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
2421     // javax.swing.SwingUtilities.invokeLater(jvq);
2422     new Thread(jvq).start();
2423   }
2424
2425   public void checkURLLinks()
2426   {
2427     // Thread off the URL link checker
2428     addDialogThread(new Runnable()
2429     {
2430       @Override
2431       public void run()
2432       {
2433         if (Cache.getDefault("CHECKURLLINKS", true))
2434         {
2435           // check what the actual links are - if it's just the default don't
2436           // bother with the warning
2437           List<String> links = Preferences.sequenceUrlLinks
2438                   .getLinksForMenu();
2439
2440           // only need to check links if there is one with a
2441           // SEQUENCE_ID which is not the default EMBL_EBI link
2442           ListIterator<String> li = links.listIterator();
2443           boolean check = false;
2444           List<JLabel> urls = new ArrayList<>();
2445           while (li.hasNext())
2446           {
2447             String link = li.next();
2448             if (link.contains(SEQUENCE_ID)
2449                     && !UrlConstants.isDefaultString(link))
2450             {
2451               check = true;
2452               int barPos = link.indexOf("|");
2453               String urlMsg = barPos == -1 ? link
2454                       : link.substring(0, barPos) + ": "
2455                               + link.substring(barPos + 1);
2456               urls.add(new JLabel(urlMsg));
2457             }
2458           }
2459           if (!check)
2460           {
2461             return;
2462           }
2463
2464           // ask user to check in case URL links use old style tokens
2465           // ($SEQUENCE_ID$ for sequence id _or_ accession id)
2466           JPanel msgPanel = new JPanel();
2467           msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.PAGE_AXIS));
2468           msgPanel.add(Box.createVerticalGlue());
2469           JLabel msg = new JLabel(MessageManager
2470                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION1"));
2471           JLabel msg2 = new JLabel(MessageManager
2472                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION2"));
2473           msgPanel.add(msg);
2474           for (JLabel url : urls)
2475           {
2476             msgPanel.add(url);
2477           }
2478           msgPanel.add(msg2);
2479
2480           final JCheckBox jcb = new JCheckBox(
2481                   MessageManager.getString("label.do_not_display_again"));
2482           jcb.addActionListener(new ActionListener()
2483           {
2484             @Override
2485             public void actionPerformed(ActionEvent e)
2486             {
2487               // update Cache settings for "don't show this again"
2488               boolean showWarningAgain = !jcb.isSelected();
2489               Cache.setProperty("CHECKURLLINKS",
2490                       Boolean.valueOf(showWarningAgain).toString());
2491             }
2492           });
2493           msgPanel.add(jcb);
2494
2495           JvOptionPane.showMessageDialog(Desktop.desktop, msgPanel,
2496                   MessageManager
2497                           .getString("label.SEQUENCE_ID_no_longer_used"),
2498                   JvOptionPane.WARNING_MESSAGE);
2499         }
2500       }
2501     });
2502   }
2503
2504   /**
2505    * Proxy class for JDesktopPane which optionally displays the current memory
2506    * usage and highlights the desktop area with a red bar if free memory runs low.
2507    * 
2508    * @author AMW
2509    */
2510   public class MyDesktopPane extends JDesktopPane
2511           implements Runnable
2512   {
2513     private static final float ONE_MB = 1048576f;
2514
2515     boolean showMemoryUsage = false;
2516
2517     Runtime runtime;
2518
2519     java.text.NumberFormat df;
2520
2521     float maxMemory, allocatedMemory, freeMemory, totalFreeMemory,
2522             percentUsage;
2523
2524     public MyDesktopPane(boolean showMemoryUsage)
2525     {
2526       showMemoryUsage(showMemoryUsage);
2527     }
2528
2529     public void showMemoryUsage(boolean showMemory)
2530     {
2531       this.showMemoryUsage = showMemory;
2532       if (showMemory)
2533       {
2534         Thread worker = new Thread(this);
2535         worker.start();
2536       }
2537       repaint();
2538     }
2539
2540     public boolean isShowMemoryUsage()
2541     {
2542       return showMemoryUsage;
2543     }
2544
2545     @Override
2546     public void run()
2547     {
2548       df = java.text.NumberFormat.getNumberInstance();
2549       df.setMaximumFractionDigits(2);
2550       runtime = Runtime.getRuntime();
2551
2552       while (showMemoryUsage)
2553       {
2554         try
2555         {
2556           maxMemory = runtime.maxMemory() / ONE_MB;
2557           allocatedMemory = runtime.totalMemory() / ONE_MB;
2558           freeMemory = runtime.freeMemory() / ONE_MB;
2559           totalFreeMemory = freeMemory + (maxMemory - allocatedMemory);
2560
2561           percentUsage = (totalFreeMemory / maxMemory) * 100;
2562
2563           // if (percentUsage < 20)
2564           {
2565             // border1 = BorderFactory.createMatteBorder(12, 12, 12, 12,
2566             // Color.red);
2567             // instance.set.setBorder(border1);
2568           }
2569           repaint();
2570           // sleep after showing usage
2571           Thread.sleep(3000);
2572         } catch (Exception ex)
2573         {
2574           ex.printStackTrace();
2575         }
2576       }
2577     }
2578
2579     @Override
2580     public void paintComponent(Graphics g)
2581     {
2582       if (showMemoryUsage && g != null && df != null)
2583       {
2584         if (percentUsage < 20)
2585         {
2586           g.setColor(Color.red);
2587         }
2588         FontMetrics fm = g.getFontMetrics();
2589         if (fm != null)
2590         {
2591           g.drawString(MessageManager.formatMessage("label.memory_stats",
2592                   new Object[]
2593                   { df.format(totalFreeMemory), df.format(maxMemory),
2594                       df.format(percentUsage) }),
2595                   10, getHeight() - fm.getHeight());
2596         }
2597       }
2598     }
2599   }
2600
2601   /**
2602    * Accessor method to quickly get all the AlignmentFrames loaded.
2603    * 
2604    * @return an array of AlignFrame, or null if none found
2605    */
2606   public static AlignFrame[] getAlignFrames()
2607   {
2608     if (Jalview.isHeadlessMode())
2609     {
2610       // Desktop.desktop is null in headless mode
2611       return new AlignFrame[] { Jalview.currentAlignFrame };
2612     }
2613
2614     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2615
2616     if (frames == null)
2617     {
2618       return null;
2619     }
2620     List<AlignFrame> avp = new ArrayList<>();
2621     // REVERSE ORDER
2622     for (int i = frames.length - 1; i > -1; i--)
2623     {
2624       if (frames[i] instanceof AlignFrame)
2625       {
2626         avp.add((AlignFrame) frames[i]);
2627       }
2628       else if (frames[i] instanceof SplitFrame)
2629       {
2630         /*
2631          * Also check for a split frame containing an AlignFrame
2632          */
2633         GSplitFrame sf = (GSplitFrame) frames[i];
2634         if (sf.getTopFrame() instanceof AlignFrame)
2635         {
2636           avp.add((AlignFrame) sf.getTopFrame());
2637         }
2638         if (sf.getBottomFrame() instanceof AlignFrame)
2639         {
2640           avp.add((AlignFrame) sf.getBottomFrame());
2641         }
2642       }
2643     }
2644     if (avp.size() == 0)
2645     {
2646       return null;
2647     }
2648     AlignFrame afs[] = avp.toArray(new AlignFrame[avp.size()]);
2649     return afs;
2650   }
2651
2652   /**
2653    * Returns an array of any AppJmol frames in the Desktop (or null if none).
2654    * 
2655    * @return
2656    */
2657   public GStructureViewer[] getJmols()
2658   {
2659     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2660
2661     if (frames == null)
2662     {
2663       return null;
2664     }
2665     List<GStructureViewer> avp = new ArrayList<>();
2666     // REVERSE ORDER
2667     for (int i = frames.length - 1; i > -1; i--)
2668     {
2669       if (frames[i] instanceof AppJmol)
2670       {
2671         GStructureViewer af = (GStructureViewer) frames[i];
2672         avp.add(af);
2673       }
2674     }
2675     if (avp.size() == 0)
2676     {
2677       return null;
2678     }
2679     GStructureViewer afs[] = avp.toArray(new GStructureViewer[avp.size()]);
2680     return afs;
2681   }
2682
2683   /**
2684    * Add Groovy Support to Jalview
2685    */
2686   @Override
2687   public void groovyShell_actionPerformed()
2688   {
2689     try
2690     {
2691       openGroovyConsole();
2692     } catch (Exception ex)
2693     {
2694       jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex);
2695       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2696
2697               MessageManager.getString("label.couldnt_create_groovy_shell"),
2698               MessageManager.getString("label.groovy_support_failed"),
2699               JvOptionPane.ERROR_MESSAGE);
2700     }
2701   }
2702
2703   /**
2704    * Open the Groovy console
2705    */
2706   void openGroovyConsole()
2707   {
2708     if (groovyConsole == null)
2709     {
2710       groovyConsole = new groovy.ui.Console();
2711       groovyConsole.setVariable("Jalview", this);
2712       groovyConsole.run();
2713
2714       /*
2715        * We allow only one console at a time, so that AlignFrame menu option
2716        * 'Calculate | Run Groovy script' is unambiguous.
2717        * Disable 'Groovy Console', and enable 'Run script', when the console is 
2718        * opened, and the reverse when it is closed
2719        */
2720       Window window = (Window) groovyConsole.getFrame();
2721       window.addWindowListener(new WindowAdapter()
2722       {
2723         @Override
2724         public void windowClosed(WindowEvent e)
2725         {
2726           /*
2727            * rebind CMD-Q from Groovy Console to Jalview Quit
2728            */
2729           addQuitHandler();
2730           enableExecuteGroovy(false);
2731         }
2732       });
2733     }
2734
2735     /*
2736      * show Groovy console window (after close and reopen)
2737      */
2738     ((Window) groovyConsole.getFrame()).setVisible(true);
2739
2740     /*
2741      * if we got this far, enable 'Run Groovy' in AlignFrame menus
2742      * and disable opening a second console
2743      */
2744     enableExecuteGroovy(true);
2745   }
2746
2747   /**
2748    * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this binding
2749    * when opened
2750    */
2751   protected void addQuitHandler()
2752   {
2753     getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
2754             .put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
2755                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
2756                     "Quit");
2757     getRootPane().getActionMap().put("Quit", new AbstractAction()
2758     {
2759       @Override
2760       public void actionPerformed(ActionEvent e)
2761       {
2762         quit();
2763       }
2764     });
2765   }
2766
2767   /**
2768    * Enable or disable 'Run Groovy script' in AlignFrame calculate menus
2769    * 
2770    * @param enabled
2771    *          true if Groovy console is open
2772    */
2773   public void enableExecuteGroovy(boolean enabled)
2774   {
2775     /*
2776      * disable opening a second Groovy console
2777      * (or re-enable when the console is closed)
2778      */
2779     groovyShell.setEnabled(!enabled);
2780
2781     AlignFrame[] alignFrames = getAlignFrames();
2782     if (alignFrames != null)
2783     {
2784       for (AlignFrame af : alignFrames)
2785       {
2786         af.setGroovyEnabled(enabled);
2787       }
2788     }
2789   }
2790
2791   /**
2792    * Progress bars managed by the IProgressIndicator method.
2793    */
2794   private Hashtable<Long, JPanel> progressBars;
2795
2796   private Hashtable<Long, IProgressIndicatorHandler> progressBarHandlers;
2797
2798   /*
2799    * (non-Javadoc)
2800    * 
2801    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
2802    */
2803   @Override
2804   public void setProgressBar(String message, long id)
2805   {
2806     if (progressBars == null)
2807     {
2808       progressBars = new Hashtable<>();
2809       progressBarHandlers = new Hashtable<>();
2810     }
2811
2812     if (progressBars.get(new Long(id)) != null)
2813     {
2814       JPanel panel = progressBars.remove(new Long(id));
2815       if (progressBarHandlers.contains(new Long(id)))
2816       {
2817         progressBarHandlers.remove(new Long(id));
2818       }
2819       removeProgressPanel(panel);
2820     }
2821     else
2822     {
2823       progressBars.put(new Long(id), addProgressPanel(message));
2824     }
2825   }
2826
2827   /*
2828    * (non-Javadoc)
2829    * 
2830    * @see jalview.gui.IProgressIndicator#registerHandler(long,
2831    * jalview.gui.IProgressIndicatorHandler)
2832    */
2833   @Override
2834   public void registerHandler(final long id,
2835           final IProgressIndicatorHandler handler)
2836   {
2837     if (progressBarHandlers == null
2838             || !progressBars.containsKey(new Long(id)))
2839     {
2840       throw new Error(MessageManager.getString(
2841               "error.call_setprogressbar_before_registering_handler"));
2842     }
2843     progressBarHandlers.put(new Long(id), handler);
2844     final JPanel progressPanel = progressBars.get(new Long(id));
2845     if (handler.canCancel())
2846     {
2847       JButton cancel = new JButton(
2848               MessageManager.getString("action.cancel"));
2849       final IProgressIndicator us = this;
2850       cancel.addActionListener(new ActionListener()
2851       {
2852
2853         @Override
2854         public void actionPerformed(ActionEvent e)
2855         {
2856           handler.cancelActivity(id);
2857           us.setProgressBar(MessageManager
2858                   .formatMessage("label.cancelled_params", new Object[]
2859                   { ((JLabel) progressPanel.getComponent(0)).getText() }),
2860                   id);
2861         }
2862       });
2863       progressPanel.add(cancel, BorderLayout.EAST);
2864     }
2865   }
2866
2867   /**
2868    * 
2869    * @return true if any progress bars are still active
2870    */
2871   @Override
2872   public boolean operationInProgress()
2873   {
2874     if (progressBars != null && progressBars.size() > 0)
2875     {
2876       return true;
2877     }
2878     return false;
2879   }
2880
2881   /**
2882    * This will return the first AlignFrame holding the given viewport instance. It
2883    * will break if there are more than one AlignFrames viewing a particular av.
2884    * 
2885    * @param viewport
2886    * @return alignFrame for viewport
2887    */
2888   public static AlignFrame getAlignFrameFor(AlignViewportI viewport)
2889   {
2890     if (desktop != null)
2891     {
2892       AlignmentPanel[] aps = getAlignmentPanels(
2893               viewport.getSequenceSetId());
2894       for (int panel = 0; aps != null && panel < aps.length; panel++)
2895       {
2896         if (aps[panel] != null && aps[panel].av == viewport)
2897         {
2898           return aps[panel].alignFrame;
2899         }
2900       }
2901     }
2902     return null;
2903   }
2904
2905   public VamsasApplication getVamsasApplication()
2906   {
2907     return v_client;
2908
2909   }
2910
2911   /**
2912    * flag set if jalview GUI is being operated programmatically
2913    */
2914   private boolean inBatchMode = false;
2915
2916   /**
2917    * check if jalview GUI is being operated programmatically
2918    * 
2919    * @return inBatchMode
2920    */
2921   public boolean isInBatchMode()
2922   {
2923     return inBatchMode;
2924   }
2925
2926   /**
2927    * set flag if jalview GUI is being operated programmatically
2928    * 
2929    * @param inBatchMode
2930    */
2931   public void setInBatchMode(boolean inBatchMode)
2932   {
2933     this.inBatchMode = inBatchMode;
2934   }
2935
2936   public void startServiceDiscovery()
2937   {
2938     startServiceDiscovery(false);
2939   }
2940
2941   public void startServiceDiscovery(boolean blocking)
2942   {
2943     boolean alive = true;
2944     Thread t0 = null, t1 = null, t2 = null;
2945     // JAL-940 - JALVIEW 1 services are now being EOLed as of JABA 2.1 release
2946     if (true)
2947     {
2948       // todo: changesupport handlers need to be transferred
2949       if (discoverer == null)
2950       {
2951         discoverer = new jalview.ws.jws1.Discoverer();
2952         // register PCS handler for desktop.
2953         discoverer.addPropertyChangeListener(changeSupport);
2954       }
2955       // JAL-940 - disabled JWS1 service configuration - always start discoverer
2956       // until we phase out completely
2957       (t0 = new Thread(discoverer)).start();
2958     }
2959
2960     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
2961     {
2962       t2 = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2963               .startDiscoverer(changeSupport);
2964     }
2965     Thread t3 = null;
2966     {
2967       // TODO: do rest service discovery
2968     }
2969     if (blocking)
2970     {
2971       while (alive)
2972       {
2973         try
2974         {
2975           Thread.sleep(15);
2976         } catch (Exception e)
2977         {
2978         }
2979         alive = (t1 != null && t1.isAlive()) || (t2 != null && t2.isAlive())
2980                 || (t3 != null && t3.isAlive())
2981                 || (t0 != null && t0.isAlive());
2982       }
2983     }
2984   }
2985
2986   /**
2987    * called to check if the service discovery process completed successfully.
2988    * 
2989    * @param evt
2990    */
2991   protected void JalviewServicesChanged(PropertyChangeEvent evt)
2992   {
2993     if (evt.getNewValue() == null || evt.getNewValue() instanceof Vector)
2994     {
2995       final String ermsg = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2996               .getErrorMessages();
2997       if (ermsg != null)
2998       {
2999         if (Cache.getDefault("SHOW_WSDISCOVERY_ERRORS", true))
3000         {
3001           if (serviceChangedDialog == null)
3002           {
3003             // only run if we aren't already displaying one of these.
3004             addDialogThread(serviceChangedDialog = new Runnable()
3005             {
3006               @Override
3007               public void run()
3008               {
3009
3010                 /*
3011                  * JalviewDialog jd =new JalviewDialog() {
3012                  * 
3013                  * @Override protected void cancelPressed() { // TODO
3014                  * Auto-generated method stub
3015                  * 
3016                  * }@Override protected void okPressed() { // TODO
3017                  * Auto-generated method stub
3018                  * 
3019                  * }@Override protected void raiseClosed() { // TODO
3020                  * Auto-generated method stub
3021                  * 
3022                  * } }; jd.initDialogFrame(new
3023                  * JLabel("<html><table width=\"450\"><tr><td>" + ermsg +
3024                  * "<br/>It may be that you have invalid JABA URLs in your web service preferences,"
3025                  * + " or mis-configured HTTP proxy settings.<br/>" +
3026                  * "Check the <em>Connections</em> and <em>Web services</em> tab of the"
3027                  * +
3028                  * " Tools->Preferences dialog box to change them.</td></tr></table></html>"
3029                  * ), true, true, "Web Service Configuration Problem", 450,
3030                  * 400);
3031                  * 
3032                  * jd.waitForInput();
3033                  */
3034                 JvOptionPane.showConfirmDialog(Desktop.desktop,
3035                         new JLabel("<html><table width=\"450\"><tr><td>"
3036                                 + ermsg + "</td></tr></table>"
3037                                 + "<p>It may be that you have invalid JABA URLs<br/>in your web service preferences,"
3038                                 + "<br>or as a command-line argument, or mis-configured HTTP proxy settings.</p>"
3039                                 + "<p>Check the <em>Connections</em> and <em>Web services</em> tab<br/>of the"
3040                                 + " Tools->Preferences dialog box to change them.</p></html>"),
3041                         "Web Service Configuration Problem",
3042                         JvOptionPane.DEFAULT_OPTION,
3043                         JvOptionPane.ERROR_MESSAGE);
3044                 serviceChangedDialog = null;
3045
3046               }
3047             });
3048           }
3049         }
3050         else
3051         {
3052           Cache.log.error(
3053                   "Errors reported by JABA discovery service. Check web services preferences.\n"
3054                           + ermsg);
3055         }
3056       }
3057     }
3058   }
3059
3060   private Runnable serviceChangedDialog = null;
3061
3062   /**
3063    * start a thread to open a URL in the configured browser. Pops up a warning
3064    * dialog to the user if there is an exception when calling out to the browser
3065    * to open the URL.
3066    * 
3067    * @param url
3068    */
3069   public static void showUrl(final String url)
3070   {
3071     showUrl(url, Desktop.instance);
3072   }
3073
3074   /**
3075    * Like showUrl but allows progress handler to be specified
3076    * 
3077    * @param url
3078    * @param progress
3079    *          (null) or object implementing IProgressIndicator
3080    */
3081   public static void showUrl(final String url,
3082           final IProgressIndicator progress)
3083   {
3084     new Thread(new Runnable()
3085     {
3086       @Override
3087       public void run()
3088       {
3089         try
3090         {
3091           if (progress != null)
3092           {
3093             progress.setProgressBar(MessageManager
3094                     .formatMessage("status.opening_params", new Object[]
3095                     { url }), this.hashCode());
3096           }
3097           jalview.util.BrowserLauncher.openURL(url);
3098         } catch (Exception ex)
3099         {
3100           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3101                   MessageManager
3102                           .getString("label.web_browser_not_found_unix"),
3103                   MessageManager.getString("label.web_browser_not_found"),
3104                   JvOptionPane.WARNING_MESSAGE);
3105
3106           ex.printStackTrace();
3107         }
3108         if (progress != null)
3109         {
3110           progress.setProgressBar(null, this.hashCode());
3111         }
3112       }
3113     }).start();
3114   }
3115
3116   public static WsParamSetManager wsparamManager = null;
3117
3118   public static ParamManager getUserParameterStore()
3119   {
3120     if (wsparamManager == null)
3121     {
3122       wsparamManager = new WsParamSetManager();
3123     }
3124     return wsparamManager;
3125   }
3126
3127   /**
3128    * static hyperlink handler proxy method for use by Jalview's internal windows
3129    * 
3130    * @param e
3131    */
3132   public static void hyperlinkUpdate(HyperlinkEvent e)
3133   {
3134     if (e.getEventType() == EventType.ACTIVATED)
3135     {
3136       String url = null;
3137       try
3138       {
3139         url = e.getURL().toString();
3140         Desktop.showUrl(url);
3141       } catch (Exception x)
3142       {
3143         if (url != null)
3144         {
3145           if (Cache.log != null)
3146           {
3147             Cache.log.error("Couldn't handle string " + url + " as a URL.");
3148           }
3149           else
3150           {
3151             System.err.println(
3152                     "Couldn't handle string " + url + " as a URL.");
3153           }
3154         }
3155         // ignore any exceptions due to dud links.
3156       }
3157
3158     }
3159   }
3160
3161   /**
3162    * single thread that handles display of dialogs to user.
3163    */
3164   ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
3165
3166   /**
3167    * flag indicating if dialogExecutor should try to acquire a permit
3168    */
3169   private volatile boolean dialogPause = true;
3170
3171   /**
3172    * pause the queue
3173    */
3174   private java.util.concurrent.Semaphore block = new Semaphore(0);
3175
3176   private static groovy.ui.Console groovyConsole;
3177
3178   /**
3179    * add another dialog thread to the queue
3180    * 
3181    * @param prompter
3182    */
3183   public void addDialogThread(final Runnable prompter)
3184   {
3185     dialogExecutor.submit(new Runnable()
3186     {
3187       @Override
3188       public void run()
3189       {
3190         if (dialogPause)
3191         {
3192           try
3193           {
3194             block.acquire();
3195           } catch (InterruptedException x)
3196           {
3197           }
3198           ;
3199         }
3200         if (instance == null)
3201         {
3202           return;
3203         }
3204         try
3205         {
3206           SwingUtilities.invokeAndWait(prompter);
3207         } catch (Exception q)
3208         {
3209           Cache.log.warn("Unexpected Exception in dialog thread.", q);
3210         }
3211       }
3212     });
3213   }
3214
3215   public void startDialogQueue()
3216   {
3217     // set the flag so we don't pause waiting for another permit and semaphore
3218     // the current task to begin
3219     dialogPause = false;
3220     block.release();
3221   }
3222
3223   /**
3224    * Outputs an image of the desktop to file in EPS format, after prompting the
3225    * user for choice of Text or Lineart character rendering (unless a preference
3226    * has been set). The file name is generated as
3227    * 
3228    * <pre>
3229    * Jalview_snapshot_nnnnn.eps where nnnnn is the current timestamp in milliseconds
3230    * </pre>
3231    */
3232   @Override
3233   protected void snapShotWindow_actionPerformed(ActionEvent e)
3234   {
3235     // currently the menu option to do this is not shown
3236     invalidate();
3237
3238     int width = getWidth();
3239     int height = getHeight();
3240     File of = new File(
3241             "Jalview_snapshot_" + System.currentTimeMillis() + ".eps");
3242     ImageWriterI writer = new ImageWriterI()
3243     {
3244       @Override
3245       public void exportImage(Graphics g) throws Exception
3246       {
3247         paintAll(g);
3248         Cache.log.info("Successfully written snapshot to file "
3249                 + of.getAbsolutePath());
3250       }
3251     };
3252     String title = "View of desktop";
3253     ImageExporter exporter = new ImageExporter(writer, null, TYPE.EPS,
3254             title);
3255     exporter.doExport(of, this, width, height, title);
3256   }
3257
3258   /**
3259    * Explode the views in the given SplitFrame into separate SplitFrame windows.
3260    * This respects (remembers) any previous 'exploded geometry' i.e. the size and
3261    * location last time the view was expanded (if any). However it does not
3262    * remember the split pane divider location - this is set to match the
3263    * 'exploding' frame.
3264    * 
3265    * @param sf
3266    */
3267   public void explodeViews(SplitFrame sf)
3268   {
3269     AlignFrame oldTopFrame = (AlignFrame) sf.getTopFrame();
3270     AlignFrame oldBottomFrame = (AlignFrame) sf.getBottomFrame();
3271     List<? extends AlignmentViewPanel> topPanels = oldTopFrame
3272             .getAlignPanels();
3273     List<? extends AlignmentViewPanel> bottomPanels = oldBottomFrame
3274             .getAlignPanels();
3275     int viewCount = topPanels.size();
3276     if (viewCount < 2)
3277     {
3278       return;
3279     }
3280
3281     /*
3282      * Processing in reverse order works, forwards order leaves the first panels
3283      * not visible. I don't know why!
3284      */
3285     for (int i = viewCount - 1; i >= 0; i--)
3286     {
3287       /*
3288        * Make new top and bottom frames. These take over the respective
3289        * AlignmentPanel objects, including their AlignmentViewports, so the
3290        * cdna/protein relationships between the viewports is carried over to the
3291        * new split frames.
3292        * 
3293        * explodedGeometry holds the (x, y) position of the previously exploded
3294        * SplitFrame, and the (width, height) of the AlignFrame component
3295        */
3296       AlignmentPanel topPanel = (AlignmentPanel) topPanels.get(i);
3297       AlignFrame newTopFrame = new AlignFrame(topPanel);
3298       newTopFrame.setSize(oldTopFrame.getSize());
3299       newTopFrame.setVisible(true);
3300       Rectangle geometry = ((AlignViewport) topPanel.getAlignViewport())
3301               .getExplodedGeometry();
3302       if (geometry != null)
3303       {
3304         newTopFrame.setSize(geometry.getSize());
3305       }
3306
3307       AlignmentPanel bottomPanel = (AlignmentPanel) bottomPanels.get(i);
3308       AlignFrame newBottomFrame = new AlignFrame(bottomPanel);
3309       newBottomFrame.setSize(oldBottomFrame.getSize());
3310       newBottomFrame.setVisible(true);
3311       geometry = ((AlignViewport) bottomPanel.getAlignViewport())
3312               .getExplodedGeometry();
3313       if (geometry != null)
3314       {
3315         newBottomFrame.setSize(geometry.getSize());
3316       }
3317
3318       topPanel.av.setGatherViewsHere(false);
3319       bottomPanel.av.setGatherViewsHere(false);
3320       JInternalFrame splitFrame = new SplitFrame(newTopFrame,
3321               newBottomFrame);
3322       if (geometry != null)
3323       {
3324         splitFrame.setLocation(geometry.getLocation());
3325       }
3326       Desktop.addInternalFrame(splitFrame, sf.getTitle(), -1, -1);
3327     }
3328
3329     /*
3330      * Clear references to the panels (now relocated in the new SplitFrames)
3331      * before closing the old SplitFrame.
3332      */
3333     topPanels.clear();
3334     bottomPanels.clear();
3335     sf.close();
3336   }
3337
3338   /**
3339    * Gather expanded split frames, sharing the same pairs of sequence set ids,
3340    * back into the given SplitFrame as additional views. Note that the gathered
3341    * frames may themselves have multiple views.
3342    * 
3343    * @param source
3344    */
3345   public void gatherViews(GSplitFrame source)
3346   {
3347     /*
3348      * special handling of explodedGeometry for a view within a SplitFrame: - it
3349      * holds the (x, y) position of the enclosing SplitFrame, and the (width,
3350      * height) of the AlignFrame component
3351      */
3352     AlignFrame myTopFrame = (AlignFrame) source.getTopFrame();
3353     AlignFrame myBottomFrame = (AlignFrame) source.getBottomFrame();
3354     myTopFrame.viewport.setExplodedGeometry(new Rectangle(source.getX(),
3355             source.getY(), myTopFrame.getWidth(), myTopFrame.getHeight()));
3356     myBottomFrame.viewport
3357             .setExplodedGeometry(new Rectangle(source.getX(), source.getY(),
3358                     myBottomFrame.getWidth(), myBottomFrame.getHeight()));
3359     myTopFrame.viewport.setGatherViewsHere(true);
3360     myBottomFrame.viewport.setGatherViewsHere(true);
3361     String topViewId = myTopFrame.viewport.getSequenceSetId();
3362     String bottomViewId = myBottomFrame.viewport.getSequenceSetId();
3363
3364     JInternalFrame[] frames = desktop.getAllFrames();
3365     for (JInternalFrame frame : frames)
3366     {
3367       if (frame instanceof SplitFrame && frame != source)
3368       {
3369         SplitFrame sf = (SplitFrame) frame;
3370         AlignFrame topFrame = (AlignFrame) sf.getTopFrame();
3371         AlignFrame bottomFrame = (AlignFrame) sf.getBottomFrame();
3372         boolean gatherThis = false;
3373         for (int a = 0; a < topFrame.alignPanels.size(); a++)
3374         {
3375           AlignmentPanel topPanel = topFrame.alignPanels.get(a);
3376           AlignmentPanel bottomPanel = bottomFrame.alignPanels.get(a);
3377           if (topViewId.equals(topPanel.av.getSequenceSetId())
3378                   && bottomViewId.equals(bottomPanel.av.getSequenceSetId()))
3379           {
3380             gatherThis = true;
3381             topPanel.av.setGatherViewsHere(false);
3382             bottomPanel.av.setGatherViewsHere(false);
3383             topPanel.av.setExplodedGeometry(
3384                     new Rectangle(sf.getLocation(), topFrame.getSize()));
3385             bottomPanel.av.setExplodedGeometry(
3386                     new Rectangle(sf.getLocation(), bottomFrame.getSize()));
3387             myTopFrame.addAlignmentPanel(topPanel, false);
3388             myBottomFrame.addAlignmentPanel(bottomPanel, false);
3389           }
3390         }
3391
3392         if (gatherThis)
3393         {
3394           topFrame.getAlignPanels().clear();
3395           bottomFrame.getAlignPanels().clear();
3396           sf.close();
3397         }
3398       }
3399     }
3400
3401     /*
3402      * The dust settles...give focus to the tab we did this from.
3403      */
3404     myTopFrame.setDisplayedView(myTopFrame.alignPanel);
3405   }
3406
3407   public static groovy.ui.Console getGroovyConsole()
3408   {
3409     return groovyConsole;
3410   }
3411
3412   /**
3413    * handles the payload of a drag and drop event.
3414    * 
3415    * TODO refactor to desktop utilities class
3416    * 
3417    * @param files
3418    *          - Data source strings extracted from the drop event
3419    * @param protocols
3420    *          - protocol for each data source extracted from the drop event
3421    * @param evt
3422    *          - the drop event
3423    * @param t
3424    *          - the payload from the drop event
3425    * @throws Exception
3426    */
3427   public static void transferFromDropTarget(List<Object> files,
3428           List<DataSourceType> protocols, DropTargetDropEvent evt,
3429           Transferable t) throws Exception
3430   {
3431
3432     // BH 2018 changed List<String> to List<Object> to allow for File from SwingJS
3433
3434     // DataFlavor[] flavors = t.getTransferDataFlavors();
3435     // for (int i = 0; i < flavors.length; i++) {
3436     // if (flavors[i].isFlavorJavaFileListType()) {
3437     // evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
3438     // List<File> list = (List<File>) t.getTransferData(flavors[i]);
3439     // for (int j = 0; j < list.size(); j++) {
3440     // File file = (File) list.get(j);
3441     // byte[] data = getDroppedFileBytes(file);
3442     // fileName.setText(file.getName() + " - " + data.length + " " +
3443     // evt.getLocation());
3444     // JTextArea target = (JTextArea) ((DropTarget) evt.getSource()).getComponent();
3445     // target.setText(new String(data));
3446     // }
3447     // dtde.dropComplete(true);
3448     // return;
3449     // }
3450     //
3451
3452     DataFlavor uriListFlavor = new DataFlavor(
3453             "text/uri-list;class=java.lang.String"), urlFlavour = null;
3454     try
3455     {
3456       urlFlavour = new DataFlavor(
3457               "application/x-java-url; class=java.net.URL");
3458     } catch (ClassNotFoundException cfe)
3459     {
3460       Cache.log.debug("Couldn't instantiate the URL dataflavor.", cfe);
3461     }
3462
3463     if (urlFlavour != null && t.isDataFlavorSupported(urlFlavour))
3464     {
3465
3466       try
3467       {
3468         java.net.URL url = (URL) t.getTransferData(urlFlavour);
3469         // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099
3470         // means url may be null.
3471         if (url != null)
3472         {
3473           protocols.add(DataSourceType.URL);
3474           files.add(url.toString());
3475           Cache.log.debug("Drop handled as URL dataflavor "
3476                   + files.get(files.size() - 1));
3477           return;
3478         }
3479         else
3480         {
3481           if (Platform.isAMac())
3482           {
3483             System.err.println(
3484                     "Please ignore plist error - occurs due to problem with java 8 on OSX");
3485           }
3486           ;
3487         }
3488       } catch (Throwable ex)
3489       {
3490         Cache.log.debug("URL drop handler failed.", ex);
3491       }
3492     }
3493     if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
3494     {
3495       // Works on Windows and MacOSX
3496       Cache.log.debug("Drop handled as javaFileListFlavor");
3497       for (Object file : (List) t
3498               .getTransferData(DataFlavor.javaFileListFlavor))
3499       {
3500         files.add(file);
3501         protocols.add(DataSourceType.FILE);
3502       }
3503     }
3504     else
3505     {
3506       // Unix like behaviour
3507       boolean added = false;
3508       String data = null;
3509       if (t.isDataFlavorSupported(uriListFlavor))
3510       {
3511         Cache.log.debug("Drop handled as uriListFlavor");
3512         // This is used by Unix drag system
3513         data = (String) t.getTransferData(uriListFlavor);
3514       }
3515       if (data == null)
3516       {
3517         // fallback to text: workaround - on OSX where there's a JVM bug
3518         Cache.log.debug("standard URIListFlavor failed. Trying text");
3519         // try text fallback
3520         DataFlavor textDf = new DataFlavor(
3521                 "text/plain;class=java.lang.String");
3522         if (t.isDataFlavorSupported(textDf))
3523         {
3524           data = (String) t.getTransferData(textDf);
3525         }
3526
3527         Cache.log.debug("Plain text drop content returned "
3528                 + (data == null ? "Null - failed" : data));
3529
3530       }
3531       if (data != null)
3532       {
3533         while (protocols.size() < files.size())
3534         {
3535           Cache.log.debug("Adding missing FILE protocol for "
3536                   + files.get(protocols.size()));
3537           protocols.add(DataSourceType.FILE);
3538         }
3539         for (java.util.StringTokenizer st = new java.util.StringTokenizer(
3540                 data, "\r\n"); st.hasMoreTokens();)
3541         {
3542           added = true;
3543           String s = st.nextToken();
3544           if (s.startsWith("#"))
3545           {
3546             // the line is a comment (as per the RFC 2483)
3547             continue;
3548           }
3549           java.net.URI uri = new java.net.URI(s);
3550           if (uri.getScheme().toLowerCase().startsWith("http"))
3551           {
3552             protocols.add(DataSourceType.URL);
3553             files.add(uri.toString());
3554           }
3555           else
3556           {
3557             // otherwise preserve old behaviour: catch all for file objects
3558             java.io.File file = new java.io.File(uri);
3559             protocols.add(DataSourceType.FILE);
3560             files.add(file.toString());
3561           }
3562         }
3563       }
3564
3565       if (Cache.log.isDebugEnabled())
3566       {
3567         if (data == null || !added)
3568         {
3569
3570           if (t.getTransferDataFlavors() != null
3571                   && t.getTransferDataFlavors().length > 0)
3572           {
3573             Cache.log.debug(
3574                     "Couldn't resolve drop data. Here are the supported flavors:");
3575             for (DataFlavor fl : t.getTransferDataFlavors())
3576             {
3577               Cache.log.debug(
3578                       "Supported transfer dataflavor: " + fl.toString());
3579               Object df = t.getTransferData(fl);
3580               if (df != null)
3581               {
3582                 Cache.log.debug("Retrieves: " + df);
3583               }
3584               else
3585               {
3586                 Cache.log.debug("Retrieved nothing");
3587               }
3588             }
3589           }
3590           else
3591           {
3592             Cache.log.debug("Couldn't resolve dataflavor for drop: "
3593                     + t.toString());
3594           }
3595         }
3596       }
3597     }
3598     if (Platform.isWindows())
3599
3600     {
3601       Cache.log.debug("Scanning dropped content for Windows Link Files");
3602
3603       // resolve any .lnk files in the file drop
3604       for (int f = 0; f < files.size(); f++)
3605       {
3606         String source = files.get(f).toString().toLowerCase();
3607         if (protocols.get(f).equals(DataSourceType.FILE)
3608                 && (source.endsWith(".lnk") || source.endsWith(".url")
3609                         || source.endsWith(".site")))
3610         {
3611           try
3612           {
3613             Object obj = files.get(f);
3614             File lf = (obj instanceof File ? (File) obj
3615                     : new File((String) obj));
3616             // process link file to get a URL
3617             Cache.log.debug("Found potential link file: " + lf);
3618             WindowsShortcut wscfile = new WindowsShortcut(lf);
3619             String fullname = wscfile.getRealFilename();
3620             protocols.set(f, FormatAdapter.checkProtocol(fullname));
3621             files.set(f, fullname);
3622             Cache.log.debug("Parsed real filename " + fullname
3623                     + " to extract protocol: " + protocols.get(f));
3624           } catch (Exception ex)
3625           {
3626             Cache.log.error(
3627                     "Couldn't parse " + files.get(f) + " as a link file.",
3628                     ex);
3629           }
3630         }
3631       }
3632     }
3633   }
3634
3635   /**
3636    * Sets the Preferences property for experimental features to True or False
3637    * depending on the state of the controlling menu item
3638    */
3639   @Override
3640   protected void showExperimental_actionPerformed(boolean selected)
3641   {
3642     Cache.setProperty(EXPERIMENTAL_FEATURES, Boolean.toString(selected));
3643   }
3644
3645   /**
3646    * Answers a (possibly empty) list of any structure viewer frames (currently for
3647    * either Jmol or Chimera) which are currently open. This may optionally be
3648    * restricted to viewers of a specified class, or viewers linked to a specified
3649    * alignment panel.
3650    * 
3651    * @param apanel
3652    *          if not null, only return viewers linked to this panel
3653    * @param structureViewerClass
3654    *          if not null, only return viewers of this class
3655    * @return
3656    */
3657   public List<StructureViewerBase> getStructureViewers(
3658           AlignmentPanel apanel,
3659           Class<? extends StructureViewerBase> structureViewerClass)
3660   {
3661     List<StructureViewerBase> result = new ArrayList<>();
3662     JInternalFrame[] frames = Desktop.instance.getAllFrames();
3663
3664     for (JInternalFrame frame : frames)
3665     {
3666       if (frame instanceof StructureViewerBase)
3667       {
3668         if (structureViewerClass == null
3669                 || structureViewerClass.isInstance(frame))
3670         {
3671           if (apanel == null
3672                   || ((StructureViewerBase) frame).isLinkedWith(apanel))
3673           {
3674             result.add((StructureViewerBase) frame);
3675           }
3676         }
3677       }
3678     }
3679     return result;
3680   }
3681 }