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