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