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