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