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