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