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