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