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