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