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