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