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