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