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