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