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