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