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