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