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