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