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