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