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