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