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