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