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