Close associated windows
[jalview.git] / src / jalview / gui / Desktop.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import jalview.io.*;
22
23 import java.awt.*;
24 import java.awt.datatransfer.*;
25 import java.awt.dnd.*;
26 import java.awt.event.*;
27 import java.util.*;
28
29 import javax.swing.*;
30
31
32 /**
33  * DOCUMENT ME!
34  *
35  * @author $author$
36  * @version $Revision$
37  */
38 public class Desktop extends jalview.jbgui.GDesktop
39     implements DropTargetListener, ClipboardOwner
40 {
41     /** DOCUMENT ME!! */
42     public static Desktop instance;
43     public static JDesktopPane desktop;
44     static int openFrameCount = 0;
45     static final int xOffset = 30;
46     static final int yOffset = 30;
47     public static jalview.ws.Discoverer discoverer;
48
49     public static Object [] jalviewClipboard;
50
51     static int fileLoadingCount= 0;
52
53     /**
54      * Creates a new Desktop object.
55      */
56     public Desktop()
57     {
58         instance = this;
59         doVamsasClientCheck();
60         Image image = null;
61
62         try
63         {
64             java.net.URL url = getClass().getResource("/images/logo.gif");
65
66             if (url != null)
67             {
68                 image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
69
70                 MediaTracker mt = new MediaTracker(this);
71                 mt.addImage(image, 0);
72                 mt.waitForID(0);
73                 setIconImage(image);
74             }
75         }
76         catch (Exception ex)
77         {
78         }
79
80         setTitle("Jalview "+jalview.bin.Cache.getProperty("VERSION"));
81         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82         desktop = new JDesktopPane();
83         desktop.setBackground(Color.white);
84         getContentPane().setLayout(new BorderLayout());
85         getContentPane().add(desktop, BorderLayout.CENTER);
86         desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
87
88         // This line prevents Windows Look&Feel resizing all new windows to maximum
89         // if previous window was maximised
90         desktop.setDesktopManager(new DefaultDesktopManager());
91
92         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
93         String x = jalview.bin.Cache.getProperty("SCREEN_X");
94         String y = jalview.bin.Cache.getProperty("SCREEN_Y");
95         String width = jalview.bin.Cache.getProperty("SCREEN_WIDTH");
96         String height = jalview.bin.Cache.getProperty("SCREEN_HEIGHT");
97
98         if ((x != null) && (y != null) && (width != null) && (height != null))
99         {
100             setBounds(Integer.parseInt(x), Integer.parseInt(y),
101                 Integer.parseInt(width), Integer.parseInt(height));
102         }
103         else
104         {
105             setBounds((int) (screenSize.width - 900) / 2,
106                 (int) (screenSize.height - 650) / 2, 900, 650);
107         }
108
109         this.addWindowListener(new WindowAdapter()
110             {
111                 public void windowClosing(WindowEvent evt)
112                 {
113                     quit();
114                 }
115             });
116
117         this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
118
119         /////////Add a splashscreen on startup
120         /////////Add a splashscreen on startup
121         JInternalFrame frame = new JInternalFrame();
122
123         SplashScreen splash = new SplashScreen(frame, image);
124         frame.setContentPane(splash);
125         frame.setLayer(JLayeredPane.PALETTE_LAYER);
126         frame.setLocation((int) ((getWidth() - 750) / 2),
127             (int) ((getHeight() - 160) / 2));
128
129         addInternalFrame(frame, "", 750, 160, false);
130
131         discoverer=new jalview.ws.Discoverer(); // Only gets started if gui is displayed.
132     }
133
134     private void doVamsasClientCheck() {
135       if (jalview.bin.Cache.vamsasJarsPresent()) {
136         VamsasMenu.setVisible(true);
137         vamsasLoad.setVisible(true);
138       }
139
140     }
141
142     /**
143      * DOCUMENT ME!
144      *
145      * @param frame DOCUMENT ME!
146      * @param title DOCUMENT ME!
147      * @param w DOCUMENT ME!
148      * @param h DOCUMENT ME!
149      */
150     public static synchronized void addInternalFrame(final JInternalFrame frame,
151         String title, int w, int h)
152     {
153         addInternalFrame(frame, title, w, h, true);
154     }
155
156     /**
157      * DOCUMENT ME!
158      *
159      * @param frame DOCUMENT ME!
160      * @param title DOCUMENT ME!
161      * @param w DOCUMENT ME!
162      * @param h DOCUMENT ME!
163      * @param resizable DOCUMENT ME!
164      */
165     public static synchronized void addInternalFrame(final JInternalFrame frame,
166         String title, int w, int h, boolean resizable)
167     {
168
169       frame.setTitle(title);
170       if(frame.getWidth()<1 || frame.getHeight()<1)
171       {
172         frame.setSize(w, h);
173       }
174       // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
175       // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
176       // IF JALVIEW IS RUNNING HEADLESS
177       /////////////////////////////////////////////////
178       if (System.getProperty("java.awt.headless") != null
179           && System.getProperty("java.awt.headless").equals("true"))
180       {
181         return;
182       }
183
184
185         openFrameCount++;
186
187         frame.setVisible(true);
188         frame.setClosable(true);
189         frame.setResizable(resizable);
190         frame.setMaximizable(resizable);
191         frame.setIconifiable(resizable);
192         frame.setFrameIcon(null);
193
194         if (frame.getX()<1 && frame.getY()<1)
195        {
196          frame.setLocation(xOffset * openFrameCount, yOffset * ((openFrameCount-1)%10)+yOffset);
197        }
198
199         final JMenuItem menuItem = new JMenuItem(title);
200         frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
201             {
202               public void internalFrameActivated(javax.swing.event.
203                                                  InternalFrameEvent evt)
204               {
205                 JInternalFrame itf = desktop.getSelectedFrame();
206                 if (itf != null)
207                   itf.requestFocus();
208
209               }
210
211                 public void internalFrameClosed(
212                     javax.swing.event.InternalFrameEvent evt)
213                 {
214                     openFrameCount--;
215                     windowMenu.remove(menuItem);
216                     JInternalFrame itf = desktop.getSelectedFrame();
217                        if (itf != null)
218                         itf.requestFocus();
219                 }
220                 ;
221             });
222
223         menuItem.addActionListener(new ActionListener()
224             {
225                 public void actionPerformed(ActionEvent e)
226                 {
227                     try
228                     {
229                         frame.setSelected(true);
230                         frame.setIcon(false);
231                     }
232                     catch (java.beans.PropertyVetoException ex)
233                     {
234
235                     }
236                 }
237             });
238
239         windowMenu.add(menuItem);
240
241         desktop.add(frame);
242         frame.toFront();
243         try{
244           frame.setSelected(true);
245           frame.requestFocus();
246         }catch(java.beans.PropertyVetoException ve)
247         {}
248     }
249
250     public void lostOwnership(Clipboard clipboard, Transferable contents)
251     {
252       Desktop.jalviewClipboard = null;
253     }
254
255     public void dragEnter(DropTargetDragEvent evt)
256     {}
257
258     public void dragExit(DropTargetEvent evt)
259     {}
260
261     public void dragOver(DropTargetDragEvent evt)
262     {}
263
264     public void dropActionChanged(DropTargetDragEvent evt)
265     {}
266
267     /**
268      * DOCUMENT ME!
269      *
270      * @param evt DOCUMENT ME!
271      */
272     public void drop(DropTargetDropEvent evt)
273     {
274         Transferable t = evt.getTransferable();
275         java.util.List files = null;
276
277         try
278         {
279           DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
280           if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
281           {
282             //Works on Windows and MacOSX
283             evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
284             files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
285           }
286           else if (t.isDataFlavorSupported(uriListFlavor))
287           {
288             // This is used by Unix drag system
289             evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
290             String data = (String) t.getTransferData(uriListFlavor);
291             files = new java.util.ArrayList(1);
292             for (java.util.StringTokenizer st = new java.util.StringTokenizer(
293                 data,
294                 "\r\n");
295                  st.hasMoreTokens(); )
296             {
297               String s = st.nextToken();
298               if (s.startsWith("#"))
299               {
300                 // the line is a comment (as per the RFC 2483)
301                 continue;
302               }
303
304               java.net.URI uri = new java.net.URI(s);
305               java.io.File file = new java.io.File(uri);
306               files.add(file);
307             }
308           }
309         }
310         catch (Exception e)
311         {
312           e.printStackTrace();
313         }
314
315         if (files != null)
316         {
317           try
318           {
319             for (int i = 0; i < files.size(); i++)
320             {
321               String file = files.get(i).toString();
322               String protocol = FormatAdapter.FILE;
323               String format = null;
324
325               if (file.endsWith(".jar"))
326               {
327                 format = "Jalview";
328
329               }
330               else
331               {
332                 format = new IdentifyFile().Identify(file,
333                                                           protocol);
334               }
335
336
337               new FileLoader().LoadFile(file, protocol, format);
338
339             }
340           }
341           catch (Exception ex)
342           {
343             ex.printStackTrace();
344           }
345         }
346     }
347
348     /**
349      * DOCUMENT ME!
350      *
351      * @param e DOCUMENT ME!
352      */
353     public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
354     {
355         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
356                     "LAST_DIRECTORY"),
357                 new String[]
358                 {
359                     "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
360                     "jar"
361                 },
362                 new String[]
363                 {
364                     "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview"
365                 }, jalview.bin.Cache.getProperty("DEFAULT_FILE_FORMAT"));
366
367         chooser.setFileView(new JalviewFileView());
368         chooser.setDialogTitle("Open local file");
369         chooser.setToolTipText("Open");
370
371         int value = chooser.showOpenDialog(this);
372
373         if (value == JalviewFileChooser.APPROVE_OPTION)
374         {
375             String choice = chooser.getSelectedFile().getPath();
376             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
377                 chooser.getSelectedFile().getParent());
378
379             String format = null;
380             if (chooser.getSelectedFormat().equals("Jalview"))
381             {
382                 format = "Jalview";
383             }
384             else
385             {
386                 format = new IdentifyFile().Identify(choice, FormatAdapter.FILE);
387             }
388
389             if (viewport != null)
390               new FileLoader().LoadFile(viewport, choice, FormatAdapter.FILE, format);
391             else
392               new FileLoader().LoadFile(choice, FormatAdapter.FILE, format);
393         }
394     }
395
396
397
398     /**
399      * DOCUMENT ME!
400      *
401      * @param e DOCUMENT ME!
402      */
403     public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
404     {
405       // This construct allows us to have a wider textfield
406       // for viewing
407       JLabel label = new JLabel("Enter URL of Input File");
408       final JComboBox history = new JComboBox();
409
410       JPanel panel = new JPanel(new GridLayout(2,1));
411       panel.add(label);
412       panel.add(history);
413       history.setPreferredSize(new Dimension(400,20));
414       history.setEditable(true);
415       history.addItem("http://www.");
416
417       String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
418
419       StringTokenizer st;
420
421       if (historyItems != null)
422       {
423         st = new StringTokenizer(historyItems, "\t");
424
425         while (st.hasMoreTokens())
426         {
427           history.addItem(st.nextElement());
428         }
429       }
430
431        int reply = JOptionPane.showInternalConfirmDialog(desktop,
432           panel, "Input Alignment From URL",
433           JOptionPane.OK_CANCEL_OPTION );
434
435
436         if (reply != JOptionPane.OK_OPTION )
437         {
438             return;
439         }
440
441         String url = history.getSelectedItem().toString();
442
443         if (url.toLowerCase().endsWith(".jar"))
444         {
445           if (viewport != null)
446             new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, "Jalview");
447           else
448             new FileLoader().LoadFile(url, FormatAdapter.URL, "Jalview");
449         }
450         else
451         {
452           String format = new IdentifyFile().Identify(url, FormatAdapter.URL);
453
454           if (format.equals("URL NOT FOUND"))
455           {
456             JOptionPane.showInternalMessageDialog(Desktop.desktop,
457                                                   "Couldn't locate " + url,
458                                                   "URL not found",
459                                                   JOptionPane.WARNING_MESSAGE);
460
461             return;
462           }
463
464           if (viewport != null)
465             new FileLoader().LoadFile(viewport, url, FormatAdapter.URL, format);
466           else
467             new FileLoader().LoadFile(url, FormatAdapter.URL, format);
468         }
469     }
470
471     /**
472      * DOCUMENT ME!
473      *
474      * @param e DOCUMENT ME!
475      */
476     public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport)
477     {
478         CutAndPasteTransfer cap = new CutAndPasteTransfer();
479         cap.setForInput(viewport);
480         Desktop.addInternalFrame(cap, "Cut & Paste Alignment File", 600, 500);
481     }
482
483     /*
484      * Exit the program
485      */
486     public void quit()
487     {
488         jalview.bin.Cache.setProperty("SCREEN_X", getBounds().x + "");
489         jalview.bin.Cache.setProperty("SCREEN_Y", getBounds().y + "");
490         jalview.bin.Cache.setProperty("SCREEN_WIDTH", getWidth() + "");
491         jalview.bin.Cache.setProperty("SCREEN_HEIGHT", getHeight() + "");
492         System.exit(0);
493     }
494
495     /**
496      * DOCUMENT ME!
497      *
498      * @param e DOCUMENT ME!
499      */
500     public void aboutMenuItem_actionPerformed(ActionEvent e)
501     {
502       StringBuffer message = new StringBuffer("JalView version " +
503                                               jalview.bin.Cache.getProperty(
504           "VERSION") +
505                                               "; last updated: " +
506                                               jalview.bin.
507                                               Cache.getDefault("BUILD_DATE", "unknown"));
508
509       if (!jalview.bin.Cache.getProperty("LATEST_VERSION").equals(
510           jalview.bin.Cache.getProperty("VERSION")))
511       {
512         message.append("\n\n!! Jalview version "
513                        + jalview.bin.Cache.getProperty("LATEST_VERSION")
514                        + " is available for download from http://www.jalview.org !!\n");
515
516       }
517
518       message.append( "\nAuthors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton." +
519             "\nCurrent development managed by Andrew Waterhouse; Barton Group, University of Dundee." +
520             "\nFor all issues relating to Jalview, email help@jalview.org" +
521             "\n\nIf  you use JalView, please cite:" +
522             "\n\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"" +
523             "\nBioinformatics,  2004 20;426-7.");
524
525         JOptionPane.showInternalMessageDialog(Desktop.desktop,
526
527            message.toString(), "About Jalview",
528             JOptionPane.INFORMATION_MESSAGE);
529     }
530
531     /**
532      * DOCUMENT ME!
533      *
534      * @param e DOCUMENT ME!
535      */
536     public void documentationMenuItem_actionPerformed(ActionEvent e)
537     {
538         try
539         {
540             ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
541             java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
542             javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
543
544             javax.help.HelpBroker hb = hs.createHelpBroker();
545             hb.setCurrentID("home");
546             hb.setDisplayed(true);
547         }
548         catch (Exception ex)
549         {
550             ex.printStackTrace();
551         }
552     }
553
554     public void closeAll_actionPerformed(ActionEvent e)
555     {
556       JInternalFrame [] frames = desktop.getAllFrames();
557       for(int i=0; i<frames.length; i++)
558       {
559         try{
560         frames[i].setClosed(true);
561       }catch(java.beans.PropertyVetoException ex){}
562       }
563     }
564
565     public void raiseRelated_actionPerformed(ActionEvent e)
566     {
567       reorderAssociatedWindows(false, false);
568     }
569
570     public void minimizeAssociated_actionPerformed(ActionEvent e)
571     {
572       reorderAssociatedWindows(true, false);
573     }
574
575     void closeAssociatedWindows()
576     {
577       reorderAssociatedWindows(false, true);
578     }
579
580     void reorderAssociatedWindows(boolean minimize, boolean close)
581     {
582       JInternalFrame[] frames = desktop.getAllFrames();
583       if (frames == null || frames.length<1)
584         return;
585
586       AlignViewport source = null, target = null;
587       if (frames[0] instanceof AlignFrame)
588         source = ( (AlignFrame) frames[0]).getCurrentView();
589       else if (frames[0] instanceof TreePanel)
590         source = ( (TreePanel) frames[0]).getViewPort();
591       else if (frames[0] instanceof PCAPanel)
592         source = ( (PCAPanel) frames[0]).av;
593       else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
594         source = ((PairwiseAlignPanel)frames[0].getContentPane()).av;
595
596
597       if (source != null)
598         for (int i = 0; i < frames.length; i++)
599         {
600           target = null;
601           if(frames[i]==null)
602           {
603             System.out.println("its gone");
604             continue;
605           }
606           if (frames[i] instanceof AlignFrame)
607             target = ( (AlignFrame) frames[i]).getCurrentView();
608           else if (frames[i] instanceof TreePanel)
609             target = ( (TreePanel) frames[i]).getViewPort();
610           else if (frames[i] instanceof PCAPanel)
611             target = ( (PCAPanel) frames[i]).av;
612           else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
613             target = ( (PairwiseAlignPanel) frames[i].getContentPane()).av;
614
615           if (source == target)
616           {
617             try
618             {
619               if (close)
620                 frames[i].setClosed(true);
621
622               else if (!minimize)
623                 frames[i].toFront();
624               else
625                 frames[i].setIcon(minimize);
626             }
627             catch (java.beans.PropertyVetoException ex)
628             {}
629           }
630         }
631     }
632
633
634
635     /**
636      * DOCUMENT ME!
637      *
638      * @param e DOCUMENT ME!
639      */
640     protected void preferences_actionPerformed(ActionEvent e)
641     {
642         new Preferences();
643     }
644
645     /**
646      * DOCUMENT ME!
647      *
648      * @param e DOCUMENT ME!
649      */
650     public void saveState_actionPerformed(ActionEvent e)
651     {
652         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
653                     "LAST_DIRECTORY"), new String[] { "jar" },
654                 new String[] { "Jalview Project" }, "Jalview Project");
655
656         chooser.setFileView(new JalviewFileView());
657         chooser.setDialogTitle("Save State");
658
659         int value = chooser.showSaveDialog(this);
660
661         if (value == JalviewFileChooser.APPROVE_OPTION)
662         {
663             java.io.File choice = chooser.getSelectedFile();
664             jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
665             new Jalview2XML().SaveState(choice);
666         }
667     }
668
669     /**
670      * DOCUMENT ME!
671      *
672      * @param e DOCUMENT ME!
673      */
674     public void loadState_actionPerformed(ActionEvent e)
675     {
676         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
677                     "LAST_DIRECTORY"), new String[] { "jar" },
678                 new String[] { "Jalview Project" }, "Jalview Project");
679         chooser.setFileView(new JalviewFileView());
680         chooser.setDialogTitle("Restore state");
681
682         int value = chooser.showOpenDialog(this);
683
684         if (value == JalviewFileChooser.APPROVE_OPTION)
685         {
686             String choice = chooser.getSelectedFile().getAbsolutePath();
687             jalview.bin.Cache.setProperty("LAST_DIRECTORY",
688                 chooser.getSelectedFile().getParent());
689             new Jalview2XML().LoadJalviewAlign(choice);
690         }
691     }
692
693   /*  public void vamsasLoad_actionPerformed(ActionEvent e)
694     {
695       JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
696           getProperty("LAST_DIRECTORY"));
697
698       chooser.setFileView(new JalviewFileView());
699       chooser.setDialogTitle("Load Vamsas file");
700       chooser.setToolTipText("Import");
701
702       int value = chooser.showOpenDialog(this);
703
704       if (value == JalviewFileChooser.APPROVE_OPTION)
705       {
706         jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(null);
707         vs.load(
708             chooser.getSelectedFile().getAbsolutePath()
709             );
710       }
711
712     }*/
713
714
715     public void inputSequence_actionPerformed(ActionEvent e)
716     {
717       new SequenceFetcher(null);
718     }
719
720     JPanel progressPanel;
721
722     public void startLoading(final String fileName)
723     {
724       if (fileLoadingCount == 0)
725       {
726         progressPanel = new JPanel(new BorderLayout());
727         JProgressBar progressBar = new JProgressBar();
728         progressBar.setIndeterminate(true);
729
730         progressPanel.add(new JLabel("Loading File: " + fileName + "   "),
731                           BorderLayout.WEST);
732
733         progressPanel.add(progressBar, BorderLayout.CENTER);
734
735         instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
736       }
737       fileLoadingCount++;
738       validate();
739     }
740
741     public void stopLoading()
742     {
743       fileLoadingCount--;
744       if (fileLoadingCount < 1)
745       {
746         if(progressPanel!=null)
747         {
748           this.getContentPane().remove(progressPanel);
749           progressPanel = null;
750         }
751         fileLoadingCount = 0;
752       }
753       validate();
754     }
755
756     public static int getViewCount(String viewId)
757     {
758       int count = 0;
759       JInternalFrame[] frames = Desktop.desktop.getAllFrames();
760       for (int t = 0; t < frames.length; t++)
761       {
762         if (frames[t] instanceof AlignFrame)
763         {
764           AlignFrame af = (AlignFrame) frames[t];
765           for(int a=0; a<af.alignPanels.size(); a++)
766           {
767             if(viewId.equals(
768                 ((AlignmentPanel)af.alignPanels.elementAt(a)).av.getSequenceSetId() )
769                 )
770             count ++;
771           }
772         }
773       }
774
775       return count;
776     }
777
778     public void explodeViews(AlignFrame af)
779     {
780       int size = af.alignPanels.size();
781       if(size<2)
782         return;
783
784       for(int i=0; i<size; i++)
785       {
786         AlignmentPanel ap = (AlignmentPanel)af.alignPanels.elementAt(i);
787         AlignFrame newaf = new AlignFrame(ap);
788         if(ap.av.explodedPosition!=null)
789           newaf.setBounds(ap.av.explodedPosition);
790
791         ap.av.gatherViewsHere = false;
792
793         addInternalFrame(newaf, af.getTitle(),
794                          AlignFrame.DEFAULT_WIDTH,
795                          AlignFrame.DEFAULT_HEIGHT);
796       }
797
798       af.alignPanels.clear();
799       af.closeMenuItem_actionPerformed(true);
800
801     }
802
803     public void gatherViews(AlignFrame source)
804     {
805       source.viewport.gatherViewsHere = true;
806       source.viewport.explodedPosition = source.getBounds();
807       JInternalFrame[] frames = desktop.getAllFrames();
808       String viewId = source.viewport.sequenceSetID;
809
810       for (int t = 0; t < frames.length; t++)
811       {
812         if (frames[t] instanceof AlignFrame && frames[t] != source)
813         {
814           AlignFrame af = (AlignFrame) frames[t];
815           boolean gatherThis = false;
816           for (int a = 0; a < af.alignPanels.size(); a++)
817           {
818             AlignmentPanel ap = (AlignmentPanel) af.alignPanels.elementAt(a);
819             if (viewId.equals(ap.av.getSequenceSetId()))
820             {
821               gatherThis = true;
822               ap.av.gatherViewsHere = false;
823               ap.av.explodedPosition = af.getBounds();
824               source.addAlignmentPanel(ap, false);
825             }
826           }
827
828           if (gatherThis)
829           {
830             af.alignPanels.clear();
831             af.closeMenuItem_actionPerformed(true);
832           }
833         }
834       }
835
836     }
837
838     jalview.gui.VamsasClient v_client=null;
839     public void vamsasLoad_actionPerformed(ActionEvent e)
840     {
841       if (v_client==null) {
842         // Start a session.
843         JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
844             getProperty("LAST_DIRECTORY"));
845
846         chooser.setFileView(new JalviewFileView());
847         chooser.setDialogTitle("Load Vamsas file");
848         chooser.setToolTipText("Import");
849
850         int value = chooser.showOpenDialog(this);
851
852         if (value == JalviewFileChooser.APPROVE_OPTION)
853         {
854           v_client = new jalview.gui.VamsasClient(this,
855               chooser.getSelectedFile());
856           this.vamsasLoad.setText("Session Update");
857           this.vamsasStop.setVisible(true);
858           v_client.initial_update();
859           v_client.startWatcher();
860         }
861       } else {
862         // store current data in session.
863         v_client.push_update();
864       }
865     }
866     public void vamsasStop_actionPerformed(ActionEvent e) {
867       if (v_client!=null) {
868         v_client.end_session();
869         v_client=null;
870         this.vamsasStop.setVisible(false);
871         this.vamsasLoad.setText("Start Vamsas Session...");
872       }
873     }
874     /**
875      * hide vamsas user gui bits when a vamsas document event is being handled.
876      * @param b true to hide gui, false to reveal gui
877      */
878     public void setVamsasUpdate(boolean b) {
879       jalview.bin.Cache.log.debug("Setting gui for Vamsas update "+(b ? "in progress" : "finished"));
880       vamsasLoad.setVisible(!b);
881       vamsasStop.setVisible(!b);
882
883     }
884 }
885