fc95f830bf3a68193d972d46c9a055d03cfb991e
[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(false);
884
885     if (frame.getX() < 1 && frame.getY() < 1)
886     {
887       frame.setLocation(xOffset * openFrameCount,
888               yOffset * ((openFrameCount - 1) % 10) + yOffset);
889     }
890
891     /*
892      * add an entry for the new frame in the Window menu 
893      * (and remove it when the frame is closed)
894      */
895     final JMenuItem menuItem = new JMenuItem(title);
896     frame.addInternalFrameListener(new InternalFrameAdapter()
897     {
898       @Override
899       public void internalFrameActivated(InternalFrameEvent evt)
900       {
901         JInternalFrame itf = desktop.getSelectedFrame();
902         if (itf != null)
903         {
904           if (itf instanceof AlignFrame)
905           {
906             Jalview.setCurrentAlignFrame((AlignFrame) itf);
907           }
908           itf.requestFocus();
909         }
910       }
911
912       @Override
913       public void internalFrameClosed(InternalFrameEvent evt)
914       {
915         PaintRefresher.RemoveComponent(frame);
916
917         /*
918          * defensive check to prevent frames being
919          * added half off the window
920          */
921         if (openFrameCount > 0)
922         {
923           openFrameCount--;
924         }
925
926         /*
927          * ensure no reference to alignFrame retained by menu item listener
928          */
929         if (menuItem.getActionListeners().length > 0)
930         {
931           menuItem.removeActionListener(menuItem.getActionListeners()[0]);
932         }
933         windowMenu.remove(menuItem);
934       };
935     });
936
937     menuItem.addActionListener(new ActionListener()
938     {
939       @Override
940       public void actionPerformed(ActionEvent e)
941       {
942         try
943         {
944           frame.setSelected(true);
945           frame.setIcon(false);
946         } catch (java.beans.PropertyVetoException ex)
947         {
948
949         }
950       }
951     });
952
953     setKeyBindings(frame);
954
955     desktop.add(frame);
956
957     windowMenu.add(menuItem);
958
959     frame.toFront();
960     try
961     {
962       frame.setSelected(true);
963       frame.requestFocus();
964     } catch (java.beans.PropertyVetoException ve)
965     {
966     } catch (java.lang.ClassCastException cex)
967     {
968       Cache.log.warn(
969               "Squashed a possible GUI implementation error. If you can recreate this, please look at http://issues.jalview.org/browse/JAL-869",
970               cex);
971     }
972   }
973
974   /**
975    * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
976    * the window
977    * 
978    * @param frame
979    */
980   private static void setKeyBindings(JInternalFrame frame)
981   {
982     @SuppressWarnings("serial")
983     final Action closeAction = new AbstractAction()
984     {
985       @Override
986       public void actionPerformed(ActionEvent e)
987       {
988         frame.dispose();
989       }
990     };
991
992     /*
993      * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action
994      */
995     KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
996             InputEvent.CTRL_DOWN_MASK);
997     KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
998             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
999
1000     InputMap inputMap = frame
1001             .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
1002     String ctrlW = ctrlWKey.toString();
1003     inputMap.put(ctrlWKey, ctrlW);
1004     inputMap.put(cmdWKey, ctrlW);
1005
1006     ActionMap actionMap = frame.getActionMap();
1007     actionMap.put(ctrlW, closeAction);
1008   }
1009
1010   @Override
1011   public void lostOwnership(Clipboard clipboard, Transferable contents)
1012   {
1013     if (!internalCopy)
1014     {
1015       Desktop.jalviewClipboard = null;
1016     }
1017
1018     internalCopy = false;
1019   }
1020
1021   @Override
1022   public void dragEnter(DropTargetDragEvent evt)
1023   {
1024   }
1025
1026   @Override
1027   public void dragExit(DropTargetEvent evt)
1028   {
1029   }
1030
1031   @Override
1032   public void dragOver(DropTargetDragEvent evt)
1033   {
1034   }
1035
1036   @Override
1037   public void dropActionChanged(DropTargetDragEvent evt)
1038   {
1039   }
1040
1041   /**
1042    * DOCUMENT ME!
1043    * 
1044    * @param evt
1045    *          DOCUMENT ME!
1046    */
1047   @Override
1048   public void drop(DropTargetDropEvent evt)
1049   {
1050     boolean success = true;
1051     // JAL-1552 - acceptDrop required before getTransferable call for
1052     // Java's Transferable for native dnd
1053     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
1054     Transferable t = evt.getTransferable();
1055     List<String> files = new ArrayList<>();
1056     List<DataSourceType> protocols = new ArrayList<>();
1057
1058     try
1059     {
1060       Desktop.transferFromDropTarget(files, protocols, evt, t);
1061     } catch (Exception e)
1062     {
1063       e.printStackTrace();
1064       success = false;
1065     }
1066
1067     if (files != null)
1068     {
1069       try
1070       {
1071         for (int i = 0; i < files.size(); i++)
1072         {
1073           String file = files.get(i).toString();
1074           DataSourceType protocol = (protocols == null)
1075                   ? DataSourceType.FILE
1076                   : protocols.get(i);
1077           FileFormatI format = null;
1078
1079           if (file.endsWith(".jar"))
1080           {
1081             format = FileFormat.Jalview;
1082
1083           }
1084           else
1085           {
1086             format = new IdentifyFile().identify(file, protocol);
1087           }
1088
1089           new FileLoader().LoadFile(file, protocol, format);
1090
1091         }
1092       } catch (Exception ex)
1093       {
1094         success = false;
1095       }
1096     }
1097     evt.dropComplete(success); // need this to ensure input focus is properly
1098                                // transfered to any new windows created
1099   }
1100
1101   /**
1102    * DOCUMENT ME!
1103    * 
1104    * @param e
1105    *          DOCUMENT ME!
1106    */
1107   @Override
1108   public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
1109   {
1110     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
1111     JalviewFileChooser chooser = JalviewFileChooser
1112             .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat);
1113
1114     chooser.setFileView(new JalviewFileView());
1115     chooser.setDialogTitle(
1116             MessageManager.getString("label.open_local_file"));
1117     chooser.setToolTipText(MessageManager.getString("action.open"));
1118
1119     int value = chooser.showOpenDialog(this);
1120
1121     if (value == JalviewFileChooser.APPROVE_OPTION)
1122     {
1123       String choice = chooser.getSelectedFile().getPath();
1124       Cache.setProperty("LAST_DIRECTORY",
1125               chooser.getSelectedFile().getParent());
1126
1127       FileFormatI format = chooser.getSelectedFormat();
1128
1129       /*
1130        * Call IdentifyFile to verify the file contains what its extension implies.
1131        * Skip this step for dynamically added file formats, because
1132        * IdentifyFile does not know how to recognise them.
1133        */
1134       if (FileFormats.getInstance().isIdentifiable(format))
1135       {
1136         try
1137         {
1138           format = new IdentifyFile().identify(choice, DataSourceType.FILE);
1139         } catch (FileFormatException e)
1140         {
1141           // format = null; //??
1142         }
1143       }
1144
1145       if (viewport != null)
1146       {
1147         new FileLoader().LoadFile(viewport, choice, DataSourceType.FILE,
1148                 format);
1149       }
1150       else
1151       {
1152         new FileLoader().LoadFile(choice, DataSourceType.FILE, format);
1153       }
1154     }
1155   }
1156
1157   /**
1158    * DOCUMENT ME!
1159    * 
1160    * @param e
1161    *          DOCUMENT ME!
1162    */
1163   @Override
1164   public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
1165   {
1166     // This construct allows us to have a wider textfield
1167     // for viewing
1168     JLabel label = new JLabel(
1169             MessageManager.getString("label.input_file_url"));
1170     final JComboBox history = new JComboBox();
1171
1172     JPanel panel = new JPanel(new GridLayout(2, 1));
1173     panel.add(label);
1174     panel.add(history);
1175     history.setPreferredSize(new Dimension(400, 20));
1176     history.setEditable(true);
1177     history.addItem("http://www.");
1178
1179     String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
1180
1181     StringTokenizer st;
1182
1183     if (historyItems != null)
1184     {
1185       st = new StringTokenizer(historyItems, "\t");
1186
1187       while (st.hasMoreTokens())
1188       {
1189         history.addItem(st.nextElement());
1190       }
1191     }
1192
1193     int reply = JvOptionPane.showInternalConfirmDialog(desktop, panel,
1194             MessageManager.getString("label.input_alignment_from_url"),
1195             JvOptionPane.OK_CANCEL_OPTION);
1196
1197     if (reply != JvOptionPane.OK_OPTION)
1198     {
1199       return;
1200     }
1201
1202     String url = history.getSelectedItem().toString();
1203
1204     if (url.toLowerCase().endsWith(".jar"))
1205     {
1206       if (viewport != null)
1207       {
1208         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1209                 FileFormat.Jalview);
1210       }
1211       else
1212       {
1213         new FileLoader().LoadFile(url, DataSourceType.URL,
1214                 FileFormat.Jalview);
1215       }
1216     }
1217     else
1218     {
1219       FileFormatI format = null;
1220       try
1221       {
1222         format = new IdentifyFile().identify(url, DataSourceType.URL);
1223       } catch (FileFormatException e)
1224       {
1225         // TODO revise error handling, distinguish between
1226         // URL not found and response not valid
1227       }
1228
1229       if (format == null)
1230       {
1231         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1232                 MessageManager.formatMessage("label.couldnt_locate",
1233                         new Object[]
1234                         { url }),
1235                 MessageManager.getString("label.url_not_found"),
1236                 JvOptionPane.WARNING_MESSAGE);
1237
1238         return;
1239       }
1240
1241       if (viewport != null)
1242       {
1243         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1244                 format);
1245       }
1246       else
1247       {
1248         new FileLoader().LoadFile(url, DataSourceType.URL, format);
1249       }
1250     }
1251   }
1252
1253   /**
1254    * Opens the CutAndPaste window for the user to paste an alignment in to
1255    * 
1256    * @param viewPanel
1257    *          - if not null, the pasted alignment is added to the current
1258    *          alignment; if null, to a new alignment window
1259    */
1260   @Override
1261   public void inputTextboxMenuItem_actionPerformed(
1262           AlignmentViewPanel viewPanel)
1263   {
1264     CutAndPasteTransfer cap = new CutAndPasteTransfer();
1265     cap.setForInput(viewPanel);
1266     Desktop.addInternalFrame(cap,
1267             MessageManager.getString("label.cut_paste_alignmen_file"), true,
1268             600, 500);
1269   }
1270
1271   /*
1272    * Exit the program
1273    */
1274   @Override
1275   public void quit()
1276   {
1277     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
1278     jalview.bin.Cache.setProperty("SCREENGEOMETRY_WIDTH",
1279             screen.width + "");
1280     jalview.bin.Cache.setProperty("SCREENGEOMETRY_HEIGHT",
1281             screen.height + "");
1282     storeLastKnownDimensions("", new Rectangle(getBounds().x, getBounds().y,
1283             getWidth(), getHeight()));
1284
1285     if (jconsole != null)
1286     {
1287       storeLastKnownDimensions("JAVA_CONSOLE_", jconsole.getBounds());
1288       jconsole.stopConsole();
1289     }
1290     if (jvnews != null)
1291     {
1292       storeLastKnownDimensions("JALVIEW_RSS_WINDOW_", jvnews.getBounds());
1293
1294     }
1295     if (dialogExecutor != null)
1296     {
1297       dialogExecutor.shutdownNow();
1298     }
1299     closeAll_actionPerformed(null);
1300
1301     if (groovyConsole != null)
1302     {
1303       // suppress a possible repeat prompt to save script
1304       groovyConsole.setDirty(false);
1305       groovyConsole.exit();
1306     }
1307     System.exit(0);
1308   }
1309
1310   private void storeLastKnownDimensions(String string, Rectangle jc)
1311   {
1312     jalview.bin.Cache.log.debug("Storing last known dimensions for "
1313             + string + ": x:" + jc.x + " y:" + jc.y + " width:" + jc.width
1314             + " height:" + jc.height);
1315
1316     jalview.bin.Cache.setProperty(string + "SCREEN_X", jc.x + "");
1317     jalview.bin.Cache.setProperty(string + "SCREEN_Y", jc.y + "");
1318     jalview.bin.Cache.setProperty(string + "SCREEN_WIDTH", jc.width + "");
1319     jalview.bin.Cache.setProperty(string + "SCREEN_HEIGHT", jc.height + "");
1320   }
1321
1322   /**
1323    * DOCUMENT ME!
1324    * 
1325    * @param e
1326    *          DOCUMENT ME!
1327    */
1328   @Override
1329   public void aboutMenuItem_actionPerformed(ActionEvent e)
1330   {
1331     // StringBuffer message = getAboutMessage(false);
1332     // JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1333     //
1334     // message.toString(), "About Jalview", JvOptionPane.INFORMATION_MESSAGE);
1335     new Thread(new Runnable()
1336     {
1337       @Override
1338       public void run()
1339       {
1340         new SplashScreen(true);
1341       }
1342     }).start();
1343   }
1344
1345   public StringBuffer getAboutMessage(boolean shortv)
1346   {
1347     StringBuffer message = new StringBuffer();
1348     message.append("<html>");
1349     if (shortv)
1350     {
1351       message.append("<h1><strong>Version: "
1352               + jalview.bin.Cache.getProperty("VERSION")
1353               + "</strong></h1>");
1354       message.append("<strong>Last Updated: <em>"
1355               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")
1356               + "</em></strong>");
1357
1358     }
1359     else
1360     {
1361
1362       message.append("<strong>Version "
1363               + jalview.bin.Cache.getProperty("VERSION")
1364               + "; last updated: "
1365               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
1366     }
1367
1368     if (jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1369             .equals("Checking"))
1370     {
1371       message.append("<br>...Checking latest version...</br>");
1372     }
1373     else if (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1374             .equals(jalview.bin.Cache.getProperty("VERSION")))
1375     {
1376       boolean red = false;
1377       if (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
1378               .indexOf("automated build") == -1)
1379       {
1380         red = true;
1381         // Displayed when code version and jnlp version do not match and code
1382         // version is not a development build
1383         message.append("<div style=\"color: #FF0000;font-style: bold;\">");
1384       }
1385
1386       message.append("<br>!! Version "
1387               + jalview.bin.Cache.getDefault("LATEST_VERSION",
1388                       "..Checking..")
1389               + " is available for download from "
1390               + jalview.bin.Cache.getDefault("www.jalview.org",
1391                       "http://www.jalview.org")
1392               + " !!");
1393       if (red)
1394       {
1395         message.append("</div>");
1396       }
1397     }
1398     message.append("<br>Authors:  " + jalview.bin.Cache.getDefault(
1399             "AUTHORFNAMES",
1400             "The Jalview Authors (See AUTHORS file for current list)")
1401             + "<br><br>Development managed by The Barton Group, University of Dundee, Scotland, UK.<br>"
1402             + "<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"
1403             + "<br><br>If  you use Jalview, please cite:"
1404             + "<br>Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
1405             + "<br>Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
1406             + "<br>Bioinformatics doi: 10.1093/bioinformatics/btp033"
1407             + "</html>");
1408     return message;
1409   }
1410
1411   /**
1412    * DOCUMENT ME!
1413    * 
1414    * @param e
1415    *          DOCUMENT ME!
1416    */
1417   @Override
1418   public void documentationMenuItem_actionPerformed(ActionEvent e)
1419   {
1420     try
1421     {
1422       Help.showHelpWindow();
1423     } catch (Exception ex)
1424     {
1425     }
1426   }
1427
1428   @Override
1429   public void closeAll_actionPerformed(ActionEvent e)
1430   {
1431     // TODO show a progress bar while closing?
1432     JInternalFrame[] frames = desktop.getAllFrames();
1433     for (int i = 0; i < frames.length; i++)
1434     {
1435       try
1436       {
1437         frames[i].setClosed(true);
1438       } catch (java.beans.PropertyVetoException ex)
1439       {
1440       }
1441     }
1442     Jalview.setCurrentAlignFrame(null);
1443     System.out.println("ALL CLOSED");
1444     if (v_client != null)
1445     {
1446       // TODO clear binding to vamsas document objects on close_all
1447     }
1448
1449     /*
1450      * reset state of singleton objects as appropriate (clear down session state
1451      * when all windows are closed)
1452      */
1453     StructureSelectionManager ssm = StructureSelectionManager
1454             .getStructureSelectionManager(this);
1455     if (ssm != null)
1456     {
1457       ssm.resetAll();
1458     }
1459   }
1460
1461   @Override
1462   public void raiseRelated_actionPerformed(ActionEvent e)
1463   {
1464     reorderAssociatedWindows(false, false);
1465   }
1466
1467   @Override
1468   public void minimizeAssociated_actionPerformed(ActionEvent e)
1469   {
1470     reorderAssociatedWindows(true, false);
1471   }
1472
1473   void closeAssociatedWindows()
1474   {
1475     reorderAssociatedWindows(false, true);
1476   }
1477
1478   /*
1479    * (non-Javadoc)
1480    * 
1481    * @seejalview.jbgui.GDesktop#garbageCollect_actionPerformed(java.awt.event.
1482    * ActionEvent)
1483    */
1484   @Override
1485   protected void garbageCollect_actionPerformed(ActionEvent e)
1486   {
1487     // We simply collect the garbage
1488     jalview.bin.Cache.log.debug("Collecting garbage...");
1489     System.gc();
1490     jalview.bin.Cache.log.debug("Finished garbage collection.");
1491   }
1492
1493   /*
1494    * (non-Javadoc)
1495    * 
1496    * @see
1497    * jalview.jbgui.GDesktop#showMemusage_actionPerformed(java.awt.event.ActionEvent
1498    * )
1499    */
1500   @Override
1501   protected void showMemusage_actionPerformed(ActionEvent e)
1502   {
1503     desktop.showMemoryUsage(showMemusage.isSelected());
1504   }
1505
1506   /*
1507    * (non-Javadoc)
1508    * 
1509    * @see
1510    * jalview.jbgui.GDesktop#showConsole_actionPerformed(java.awt.event.ActionEvent
1511    * )
1512    */
1513   @Override
1514   protected void showConsole_actionPerformed(ActionEvent e)
1515   {
1516     showConsole(showConsole.isSelected());
1517   }
1518
1519   Console jconsole = null;
1520
1521   /**
1522    * control whether the java console is visible or not
1523    * 
1524    * @param selected
1525    */
1526   void showConsole(boolean selected)
1527   {
1528     // TODO: decide if we should update properties file
1529     if (jconsole != null) // BH 2018
1530     {
1531       showConsole.setSelected(selected);
1532       Cache.setProperty("SHOW_JAVA_CONSOLE",
1533               Boolean.valueOf(selected).toString());
1534       jconsole.setVisible(selected);
1535     }
1536   }
1537
1538   void reorderAssociatedWindows(boolean minimize, boolean close)
1539   {
1540     JInternalFrame[] frames = desktop.getAllFrames();
1541     if (frames == null || frames.length < 1)
1542     {
1543       return;
1544     }
1545
1546     AlignmentViewport source = null, target = null;
1547     if (frames[0] instanceof AlignFrame)
1548     {
1549       source = ((AlignFrame) frames[0]).getCurrentView();
1550     }
1551     else if (frames[0] instanceof TreePanel)
1552     {
1553       source = ((TreePanel) frames[0]).getViewPort();
1554     }
1555     else if (frames[0] instanceof PCAPanel)
1556     {
1557       source = ((PCAPanel) frames[0]).av;
1558     }
1559     else if (frames[0].getContentPane() instanceof PairwiseAlignPanel)
1560     {
1561       source = ((PairwiseAlignPanel) frames[0].getContentPane()).av;
1562     }
1563
1564     if (source != null)
1565     {
1566       for (int i = 0; i < frames.length; i++)
1567       {
1568         target = null;
1569         if (frames[i] == null)
1570         {
1571           continue;
1572         }
1573         if (frames[i] instanceof AlignFrame)
1574         {
1575           target = ((AlignFrame) frames[i]).getCurrentView();
1576         }
1577         else if (frames[i] instanceof TreePanel)
1578         {
1579           target = ((TreePanel) frames[i]).getViewPort();
1580         }
1581         else if (frames[i] instanceof PCAPanel)
1582         {
1583           target = ((PCAPanel) frames[i]).av;
1584         }
1585         else if (frames[i].getContentPane() instanceof PairwiseAlignPanel)
1586         {
1587           target = ((PairwiseAlignPanel) frames[i].getContentPane()).av;
1588         }
1589
1590         if (source == target)
1591         {
1592           try
1593           {
1594             if (close)
1595             {
1596               frames[i].setClosed(true);
1597             }
1598             else
1599             {
1600               frames[i].setIcon(minimize);
1601               if (!minimize)
1602               {
1603                 frames[i].toFront();
1604               }
1605             }
1606
1607           } catch (java.beans.PropertyVetoException ex)
1608           {
1609           }
1610         }
1611       }
1612     }
1613   }
1614
1615   /**
1616    * DOCUMENT ME!
1617    * 
1618    * @param e
1619    *          DOCUMENT ME!
1620    */
1621   @Override
1622   protected void preferences_actionPerformed(ActionEvent e)
1623   {
1624     new Preferences();
1625   }
1626
1627   /**
1628    * DOCUMENT ME!
1629    * 
1630    * @param e
1631    *          DOCUMENT ME!
1632    */
1633   @Override
1634   public void saveState_actionPerformed(ActionEvent e)
1635   {
1636     JalviewFileChooser chooser = new JalviewFileChooser("jvp",
1637             "Jalview Project");
1638
1639     chooser.setFileView(new JalviewFileView());
1640     chooser.setDialogTitle(MessageManager.getString("label.save_state"));
1641
1642     int value = chooser.showSaveDialog(this);
1643
1644     if (value == JalviewFileChooser.APPROVE_OPTION)
1645     {
1646       final Desktop me = this;
1647       final java.io.File choice = chooser.getSelectedFile();
1648       setProjectFile(choice);
1649
1650       new Thread(new Runnable()
1651       {
1652         @Override
1653         public void run()
1654         {
1655           // TODO: refactor to Jalview desktop session controller action.
1656           setProgressBar(MessageManager.formatMessage(
1657                   "label.saving_jalview_project", new Object[]
1658                   { choice.getName() }), choice.hashCode());
1659           jalview.bin.Cache.setProperty("LAST_DIRECTORY",
1660                   choice.getParent());
1661           // TODO catch and handle errors for savestate
1662           // TODO prevent user from messing with the Desktop whilst we're saving
1663           try
1664           {
1665             new Jalview2XML().saveState(choice);
1666           } catch (OutOfMemoryError oom)
1667           {
1668             new OOMWarning(
1669                     "Whilst saving current state to " + choice.getName(),
1670                     oom);
1671           } catch (Exception ex)
1672           {
1673             Cache.log.error(
1674                     "Problems whilst trying to save to " + choice.getName(),
1675                     ex);
1676             JvOptionPane.showMessageDialog(me,
1677                     MessageManager.formatMessage(
1678                             "label.error_whilst_saving_current_state_to",
1679                             new Object[]
1680                             { choice.getName() }),
1681                     MessageManager.getString("label.couldnt_save_project"),
1682                     JvOptionPane.WARNING_MESSAGE);
1683           }
1684           setProgressBar(null, choice.hashCode());
1685         }
1686       }).start();
1687     }
1688   }
1689
1690   private void setProjectFile(File choice)
1691   {
1692     this.projectFile = choice;
1693   }
1694
1695   public File getProjectFile()
1696   {
1697     return this.projectFile;
1698   }
1699
1700   /**
1701    * DOCUMENT ME!
1702    * 
1703    * @param e
1704    *          DOCUMENT ME!
1705    */
1706   @Override
1707   public void loadState_actionPerformed(ActionEvent e)
1708   {
1709     JalviewFileChooser chooser = new JalviewFileChooser(
1710             Cache.getProperty("LAST_DIRECTORY"), new String[]
1711             { "jvp", "jar" },
1712             new String[]
1713             { "Jalview Project", "Jalview Project (old)" },
1714             "Jalview Project");
1715     chooser.setFileView(new JalviewFileView());
1716     chooser.setDialogTitle(MessageManager.getString("label.restore_state"));
1717
1718     int value = chooser.showOpenDialog(this);
1719
1720     if (value == JalviewFileChooser.APPROVE_OPTION)
1721     {
1722       final File selectedFile = chooser.getSelectedFile();
1723       setProjectFile(selectedFile);
1724       final String choice = selectedFile.getAbsolutePath();
1725       Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
1726       new Thread(new Runnable()
1727       {
1728         @Override
1729         public void run()
1730         {
1731           setProgressBar(MessageManager.formatMessage(
1732                   "label.loading_jalview_project", new Object[]
1733                   { choice }), choice.hashCode());
1734           try
1735           {
1736             new Jalview2XML().loadJalviewAlign(choice);
1737           } catch (OutOfMemoryError oom)
1738           {
1739             new OOMWarning("Whilst loading project from " + choice, oom);
1740           } catch (Exception ex)
1741           {
1742             Cache.log.error(
1743                     "Problems whilst loading project from " + choice, ex);
1744             JvOptionPane.showMessageDialog(Desktop.desktop,
1745                     MessageManager.formatMessage(
1746                             "label.error_whilst_loading_project_from",
1747                             new Object[]
1748                             { choice }),
1749                     MessageManager.getString("label.couldnt_load_project"),
1750                     JvOptionPane.WARNING_MESSAGE);
1751           }
1752           setProgressBar(null, choice.hashCode());
1753         }
1754       }).start();
1755     }
1756   }
1757
1758   @Override
1759   public void inputSequence_actionPerformed(ActionEvent e)
1760   {
1761     new SequenceFetcher(this);
1762   }
1763
1764   JPanel progressPanel;
1765
1766   ArrayList<JPanel> fileLoadingPanels = new ArrayList<>();
1767
1768   public void startLoading(final String fileName)
1769   {
1770     if (fileLoadingCount == 0)
1771     {
1772       fileLoadingPanels.add(addProgressPanel(MessageManager
1773               .formatMessage("label.loading_file", new Object[]
1774               { fileName })));
1775     }
1776     fileLoadingCount++;
1777   }
1778
1779   private JPanel addProgressPanel(String string)
1780   {
1781     if (progressPanel == null)
1782     {
1783       progressPanel = new JPanel(new GridLayout(1, 1));
1784       totalProgressCount = 0;
1785       instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
1786     }
1787     JPanel thisprogress = new JPanel(new BorderLayout(10, 5));
1788     JProgressBar progressBar = new JProgressBar();
1789     progressBar.setIndeterminate(true);
1790
1791     thisprogress.add(new JLabel(string), BorderLayout.WEST);
1792
1793     thisprogress.add(progressBar, BorderLayout.CENTER);
1794     progressPanel.add(thisprogress);
1795     ((GridLayout) progressPanel.getLayout()).setRows(
1796             ((GridLayout) progressPanel.getLayout()).getRows() + 1);
1797     ++totalProgressCount;
1798     instance.validate();
1799     return thisprogress;
1800   }
1801
1802   int totalProgressCount = 0;
1803
1804   private void removeProgressPanel(JPanel progbar)
1805   {
1806     if (progressPanel != null)
1807     {
1808       synchronized (progressPanel)
1809       {
1810         progressPanel.remove(progbar);
1811         GridLayout gl = (GridLayout) progressPanel.getLayout();
1812         gl.setRows(gl.getRows() - 1);
1813         if (--totalProgressCount < 1)
1814         {
1815           this.getContentPane().remove(progressPanel);
1816           progressPanel = null;
1817         }
1818       }
1819     }
1820     validate();
1821   }
1822
1823   public void stopLoading()
1824   {
1825     fileLoadingCount--;
1826     if (fileLoadingCount < 1)
1827     {
1828       while (fileLoadingPanels.size() > 0)
1829       {
1830         removeProgressPanel(fileLoadingPanels.remove(0));
1831       }
1832       fileLoadingPanels.clear();
1833       fileLoadingCount = 0;
1834     }
1835     validate();
1836   }
1837
1838   public static int getViewCount(String alignmentId)
1839   {
1840     AlignmentViewport[] aps = getViewports(alignmentId);
1841     return (aps == null) ? 0 : aps.length;
1842   }
1843
1844   /**
1845    * 
1846    * @param alignmentId
1847    *          - if null, all sets are returned
1848    * @return all AlignmentPanels concerning the alignmentId sequence set
1849    */
1850   public static AlignmentPanel[] getAlignmentPanels(String alignmentId)
1851   {
1852     if (Desktop.desktop == null)
1853     {
1854       // no frames created and in headless mode
1855       // TODO: verify that frames are recoverable when in headless mode
1856       return null;
1857     }
1858     List<AlignmentPanel> aps = new ArrayList<>();
1859     AlignFrame[] frames = getAlignFrames();
1860     if (frames == null)
1861     {
1862       return null;
1863     }
1864     for (AlignFrame af : frames)
1865     {
1866       for (AlignmentPanel ap : af.alignPanels)
1867       {
1868         if (alignmentId == null
1869                 || alignmentId.equals(ap.av.getSequenceSetId()))
1870         {
1871           aps.add(ap);
1872         }
1873       }
1874     }
1875     if (aps.size() == 0)
1876     {
1877       return null;
1878     }
1879     AlignmentPanel[] vap = aps.toArray(new AlignmentPanel[aps.size()]);
1880     return vap;
1881   }
1882
1883   /**
1884    * get all the viewports on an alignment.
1885    * 
1886    * @param sequenceSetId
1887    *          unique alignment id (may be null - all viewports returned in that
1888    *          case)
1889    * @return all viewports on the alignment bound to sequenceSetId
1890    */
1891   public static AlignmentViewport[] getViewports(String sequenceSetId)
1892   {
1893     List<AlignmentViewport> viewp = new ArrayList<>();
1894     if (desktop != null)
1895     {
1896       AlignFrame[] frames = Desktop.getAlignFrames();
1897
1898       for (AlignFrame afr : frames)
1899       {
1900         if (sequenceSetId == null || afr.getViewport().getSequenceSetId()
1901                 .equals(sequenceSetId))
1902         {
1903           if (afr.alignPanels != null)
1904           {
1905             for (AlignmentPanel ap : afr.alignPanels)
1906             {
1907               if (sequenceSetId == null
1908                       || sequenceSetId.equals(ap.av.getSequenceSetId()))
1909               {
1910                 viewp.add(ap.av);
1911               }
1912             }
1913           }
1914           else
1915           {
1916             viewp.add(afr.getViewport());
1917           }
1918         }
1919       }
1920       if (viewp.size() > 0)
1921       {
1922         return viewp.toArray(new AlignmentViewport[viewp.size()]);
1923       }
1924     }
1925     return null;
1926   }
1927
1928   /**
1929    * Explode the views in the given frame into separate AlignFrame
1930    * 
1931    * @param af
1932    */
1933   public static void explodeViews(AlignFrame af)
1934   {
1935     int size = af.alignPanels.size();
1936     if (size < 2)
1937     {
1938       return;
1939     }
1940
1941     for (int i = 0; i < size; i++)
1942     {
1943       AlignmentPanel ap = af.alignPanels.get(i);
1944       AlignFrame newaf = new AlignFrame(ap);
1945
1946       /*
1947        * Restore the view's last exploded frame geometry if known. Multiple
1948        * views from one exploded frame share and restore the same (frame)
1949        * position and size.
1950        */
1951       Rectangle geometry = ap.av.getExplodedGeometry();
1952       if (geometry != null)
1953       {
1954         newaf.setBounds(geometry);
1955       }
1956
1957       ap.av.setGatherViewsHere(false);
1958
1959       addInternalFrame(newaf, af.getTitle(), AlignFrame.DEFAULT_WIDTH,
1960               AlignFrame.DEFAULT_HEIGHT);
1961     }
1962
1963     af.alignPanels.clear();
1964     af.closeMenuItem_actionPerformed(true);
1965
1966   }
1967
1968   /**
1969    * Gather expanded views (separate AlignFrame's) with the same sequence set
1970    * identifier back in to this frame as additional views, and close the
1971    * expanded views. Note the expanded frames may themselves have multiple
1972    * views. We take the lot.
1973    * 
1974    * @param source
1975    */
1976   public void gatherViews(AlignFrame source)
1977   {
1978     source.viewport.setGatherViewsHere(true);
1979     source.viewport.setExplodedGeometry(source.getBounds());
1980     JInternalFrame[] frames = desktop.getAllFrames();
1981     String viewId = source.viewport.getSequenceSetId();
1982
1983     for (int t = 0; t < frames.length; t++)
1984     {
1985       if (frames[t] instanceof AlignFrame && frames[t] != source)
1986       {
1987         AlignFrame af = (AlignFrame) frames[t];
1988         boolean gatherThis = false;
1989         for (int a = 0; a < af.alignPanels.size(); a++)
1990         {
1991           AlignmentPanel ap = af.alignPanels.get(a);
1992           if (viewId.equals(ap.av.getSequenceSetId()))
1993           {
1994             gatherThis = true;
1995             ap.av.setGatherViewsHere(false);
1996             ap.av.setExplodedGeometry(af.getBounds());
1997             source.addAlignmentPanel(ap, false);
1998           }
1999         }
2000
2001         if (gatherThis)
2002         {
2003           af.alignPanels.clear();
2004           af.closeMenuItem_actionPerformed(true);
2005         }
2006       }
2007     }
2008
2009   }
2010
2011   jalview.gui.VamsasApplication v_client = null;
2012
2013   @Override
2014   public void vamsasImport_actionPerformed(ActionEvent e)
2015   {
2016     if (v_client == null)
2017     {
2018       // Load and try to start a session.
2019       JalviewFileChooser chooser = new JalviewFileChooser(
2020               jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
2021
2022       chooser.setFileView(new JalviewFileView());
2023       chooser.setDialogTitle(
2024               MessageManager.getString("label.open_saved_vamsas_session"));
2025       chooser.setToolTipText(MessageManager.getString(
2026               "label.select_vamsas_session_opened_as_new_vamsas_session"));
2027
2028       int value = chooser.showOpenDialog(this);
2029
2030       if (value == JalviewFileChooser.APPROVE_OPTION)
2031       {
2032         String fle = chooser.getSelectedFile().toString();
2033         if (!vamsasImport(chooser.getSelectedFile()))
2034         {
2035           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2036                   MessageManager.formatMessage(
2037                           "label.couldnt_import_as_vamsas_session",
2038                           new Object[]
2039                           { fle }),
2040                   MessageManager
2041                           .getString("label.vamsas_document_import_failed"),
2042                   JvOptionPane.ERROR_MESSAGE);
2043         }
2044       }
2045     }
2046     else
2047     {
2048       jalview.bin.Cache.log.error(
2049               "Implementation error - load session from a running session is not supported.");
2050     }
2051   }
2052
2053   /**
2054    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2055    * 
2056    * @param file
2057    * @return true if import was a success and a session was started.
2058    */
2059   public boolean vamsasImport(URL url)
2060   {
2061     // TODO: create progress bar
2062     if (v_client != null)
2063     {
2064
2065       jalview.bin.Cache.log.error(
2066               "Implementation error - load session from a running session is not supported.");
2067       return false;
2068     }
2069
2070     try
2071     {
2072       // copy the URL content to a temporary local file
2073       // TODO: be a bit cleverer here with nio (?!)
2074       File file = File.createTempFile("vdocfromurl", ".vdj");
2075       FileOutputStream fos = new FileOutputStream(file);
2076       BufferedInputStream bis = new BufferedInputStream(url.openStream());
2077       byte[] buffer = new byte[2048];
2078       int ln;
2079       while ((ln = bis.read(buffer)) > -1)
2080       {
2081         fos.write(buffer, 0, ln);
2082       }
2083       bis.close();
2084       fos.close();
2085       v_client = new jalview.gui.VamsasApplication(this, file,
2086               url.toExternalForm());
2087     } catch (Exception ex)
2088     {
2089       jalview.bin.Cache.log.error(
2090               "Failed to create new vamsas session from contents of URL "
2091                       + url,
2092               ex);
2093       return false;
2094     }
2095     setupVamsasConnectedGui();
2096     v_client.initial_update(); // TODO: thread ?
2097     return v_client.inSession();
2098   }
2099
2100   /**
2101    * import file into a new vamsas session (uses jalview.gui.VamsasApplication)
2102    * 
2103    * @param file
2104    * @return true if import was a success and a session was started.
2105    */
2106   public boolean vamsasImport(File file)
2107   {
2108     if (v_client != null)
2109     {
2110
2111       jalview.bin.Cache.log.error(
2112               "Implementation error - load session from a running session is not supported.");
2113       return false;
2114     }
2115
2116     setProgressBar(MessageManager.formatMessage(
2117             "status.importing_vamsas_session_from", new Object[]
2118             { file.getName() }), file.hashCode());
2119     try
2120     {
2121       v_client = new jalview.gui.VamsasApplication(this, file, null);
2122     } catch (Exception ex)
2123     {
2124       setProgressBar(MessageManager.formatMessage(
2125               "status.importing_vamsas_session_from", new Object[]
2126               { file.getName() }), file.hashCode());
2127       jalview.bin.Cache.log.error(
2128               "New vamsas session from existing session file failed:", ex);
2129       return false;
2130     }
2131     setupVamsasConnectedGui();
2132     v_client.initial_update(); // TODO: thread ?
2133     setProgressBar(MessageManager.formatMessage(
2134             "status.importing_vamsas_session_from", new Object[]
2135             { file.getName() }), file.hashCode());
2136     return v_client.inSession();
2137   }
2138
2139   public boolean joinVamsasSession(String mysesid)
2140   {
2141     if (v_client != null)
2142     {
2143       throw new Error(MessageManager
2144               .getString("error.try_join_vamsas_session_another"));
2145     }
2146     if (mysesid == null)
2147     {
2148       throw new Error(
2149               MessageManager.getString("error.invalid_vamsas_session_id"));
2150     }
2151     v_client = new VamsasApplication(this, mysesid);
2152     setupVamsasConnectedGui();
2153     v_client.initial_update();
2154     return (v_client.inSession());
2155   }
2156
2157   @Override
2158   public void vamsasStart_actionPerformed(ActionEvent e)
2159   {
2160     if (v_client == null)
2161     {
2162       // Start a session.
2163       // we just start a default session for moment.
2164       /*
2165        * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
2166        * getProperty("LAST_DIRECTORY"));
2167        * 
2168        * chooser.setFileView(new JalviewFileView());
2169        * chooser.setDialogTitle("Load Vamsas file");
2170        * chooser.setToolTipText("Import");
2171        * 
2172        * int value = chooser.showOpenDialog(this);
2173        * 
2174        * if (value == JalviewFileChooser.APPROVE_OPTION) { v_client = new
2175        * jalview.gui.VamsasApplication(this, chooser.getSelectedFile());
2176        */
2177       v_client = new VamsasApplication(this);
2178       setupVamsasConnectedGui();
2179       v_client.initial_update(); // TODO: thread ?
2180     }
2181     else
2182     {
2183       // store current data in session.
2184       v_client.push_update(); // TODO: thread
2185     }
2186   }
2187
2188   protected void setupVamsasConnectedGui()
2189   {
2190     vamsasStart.setText(MessageManager.getString("label.session_update"));
2191     vamsasSave.setVisible(true);
2192     vamsasStop.setVisible(true);
2193     vamsasImport.setVisible(false); // Document import to existing session is
2194     // not possible for vamsas-client-1.0.
2195   }
2196
2197   protected void setupVamsasDisconnectedGui()
2198   {
2199     vamsasSave.setVisible(false);
2200     vamsasStop.setVisible(false);
2201     vamsasImport.setVisible(true);
2202     vamsasStart
2203             .setText(MessageManager.getString("label.new_vamsas_session"));
2204   }
2205
2206   @Override
2207   public void vamsasStop_actionPerformed(ActionEvent e)
2208   {
2209     if (v_client != null)
2210     {
2211       v_client.end_session();
2212       v_client = null;
2213       setupVamsasDisconnectedGui();
2214     }
2215   }
2216
2217   protected void buildVamsasStMenu()
2218   {
2219     if (v_client == null)
2220     {
2221       String[] sess = null;
2222       try
2223       {
2224         sess = VamsasApplication.getSessionList();
2225       } catch (Exception e)
2226       {
2227         jalview.bin.Cache.log.warn("Problem getting current sessions list.",
2228                 e);
2229         sess = null;
2230       }
2231       if (sess != null)
2232       {
2233         jalview.bin.Cache.log.debug(
2234                 "Got current sessions list: " + sess.length + " entries.");
2235         VamsasStMenu.removeAll();
2236         for (int i = 0; i < sess.length; i++)
2237         {
2238           JMenuItem sessit = new JMenuItem();
2239           sessit.setText(sess[i]);
2240           sessit.setToolTipText(MessageManager
2241                   .formatMessage("label.connect_to_session", new Object[]
2242                   { sess[i] }));
2243           final Desktop dsktp = this;
2244           final String mysesid = sess[i];
2245           sessit.addActionListener(new ActionListener()
2246           {
2247
2248             @Override
2249             public void actionPerformed(ActionEvent e)
2250             {
2251               if (dsktp.v_client == null)
2252               {
2253                 Thread rthr = new Thread(new Runnable()
2254                 {
2255
2256                   @Override
2257                   public void run()
2258                   {
2259                     dsktp.v_client = new VamsasApplication(dsktp, mysesid);
2260                     dsktp.setupVamsasConnectedGui();
2261                     dsktp.v_client.initial_update();
2262                   }
2263
2264                 });
2265                 rthr.start();
2266               }
2267             };
2268           });
2269           VamsasStMenu.add(sessit);
2270         }
2271         // don't show an empty menu.
2272         VamsasStMenu.setVisible(sess.length > 0);
2273
2274       }
2275       else
2276       {
2277         jalview.bin.Cache.log.debug("No current vamsas sessions.");
2278         VamsasStMenu.removeAll();
2279         VamsasStMenu.setVisible(false);
2280       }
2281     }
2282     else
2283     {
2284       // Not interested in the content. Just hide ourselves.
2285       VamsasStMenu.setVisible(false);
2286     }
2287   }
2288
2289   @Override
2290   public void vamsasSave_actionPerformed(ActionEvent e)
2291   {
2292     if (v_client != null)
2293     {
2294       // TODO: VAMSAS DOCUMENT EXTENSION is VDJ
2295       JalviewFileChooser chooser = new JalviewFileChooser("vdj",
2296               "Vamsas Document");
2297
2298       chooser.setFileView(new JalviewFileView());
2299       chooser.setDialogTitle(MessageManager
2300               .getString("label.save_vamsas_document_archive"));
2301
2302       int value = chooser.showSaveDialog(this);
2303
2304       if (value == JalviewFileChooser.APPROVE_OPTION)
2305       {
2306         java.io.File choice = chooser.getSelectedFile();
2307         JPanel progpanel = addProgressPanel(MessageManager
2308                 .formatMessage("label.saving_vamsas_doc", new Object[]
2309                 { choice.getName() }));
2310         Cache.setProperty("LAST_DIRECTORY", choice.getParent());
2311         String warnmsg = null;
2312         String warnttl = null;
2313         try
2314         {
2315           v_client.vclient.storeDocument(choice);
2316         } catch (Error ex)
2317         {
2318           warnttl = "Serious Problem saving Vamsas Document";
2319           warnmsg = ex.toString();
2320           jalview.bin.Cache.log
2321                   .error("Error Whilst saving document to " + choice, ex);
2322
2323         } catch (Exception ex)
2324         {
2325           warnttl = "Problem saving Vamsas Document.";
2326           warnmsg = ex.toString();
2327           jalview.bin.Cache.log.warn(
2328                   "Exception Whilst saving document to " + choice, ex);
2329
2330         }
2331         removeProgressPanel(progpanel);
2332         if (warnmsg != null)
2333         {
2334           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2335
2336                   warnmsg, warnttl, JvOptionPane.ERROR_MESSAGE);
2337         }
2338       }
2339     }
2340   }
2341
2342   JPanel vamUpdate = null;
2343
2344   /**
2345    * hide vamsas user gui bits when a vamsas document event is being handled.
2346    * 
2347    * @param b
2348    *          true to hide gui, false to reveal gui
2349    */
2350   public void setVamsasUpdate(boolean b)
2351   {
2352     Cache.log.debug("Setting gui for Vamsas update "
2353             + (b ? "in progress" : "finished"));
2354
2355     if (vamUpdate != null)
2356     {
2357       this.removeProgressPanel(vamUpdate);
2358     }
2359     if (b)
2360     {
2361       vamUpdate = this.addProgressPanel(
2362               MessageManager.getString("label.updating_vamsas_session"));
2363     }
2364     vamsasStart.setVisible(!b);
2365     vamsasStop.setVisible(!b);
2366     vamsasSave.setVisible(!b);
2367   }
2368
2369   public JInternalFrame[] getAllFrames()
2370   {
2371     return desktop.getAllFrames();
2372   }
2373
2374   /**
2375    * Checks the given url to see if it gives a response indicating that the user
2376    * should be informed of a new questionnaire.
2377    * 
2378    * @param url
2379    */
2380   public void checkForQuestionnaire(String url)
2381   {
2382     UserQuestionnaireCheck jvq = new UserQuestionnaireCheck(url);
2383     // javax.swing.SwingUtilities.invokeLater(jvq);
2384     new Thread(jvq).start();
2385   }
2386
2387   public void checkURLLinks()
2388   {
2389     // Thread off the URL link checker
2390     addDialogThread(new Runnable()
2391     {
2392       @Override
2393       public void run()
2394       {
2395         if (/** @j2sNative false && */ // BH 2018
2396         Cache.getDefault("CHECKURLLINKS", true))
2397         {
2398           // check what the actual links are - if it's just the default don't
2399           // bother with the warning
2400           List<String> links = Preferences.sequenceUrlLinks
2401                   .getLinksForMenu();
2402
2403           // only need to check links if there is one with a
2404           // SEQUENCE_ID which is not the default EMBL_EBI link
2405           ListIterator<String> li = links.listIterator();
2406           boolean check = false;
2407           List<JLabel> urls = new ArrayList<>();
2408           while (li.hasNext())
2409           {
2410             String link = li.next();
2411             if (link.contains(SEQUENCE_ID)
2412                     && !UrlConstants.isDefaultString(link))
2413             {
2414               check = true;
2415               int barPos = link.indexOf("|");
2416               String urlMsg = barPos == -1 ? link
2417                       : link.substring(0, barPos) + ": "
2418                               + link.substring(barPos + 1);
2419               urls.add(new JLabel(urlMsg));
2420             }
2421           }
2422           if (!check)
2423           {
2424             return;
2425           }
2426
2427           // ask user to check in case URL links use old style tokens
2428           // ($SEQUENCE_ID$ for sequence id _or_ accession id)
2429           JPanel msgPanel = new JPanel();
2430           msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.PAGE_AXIS));
2431           msgPanel.add(Box.createVerticalGlue());
2432           JLabel msg = new JLabel(MessageManager
2433                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION1"));
2434           JLabel msg2 = new JLabel(MessageManager
2435                   .getString("label.SEQUENCE_ID_for_DB_ACCESSION2"));
2436           msgPanel.add(msg);
2437           for (JLabel url : urls)
2438           {
2439             msgPanel.add(url);
2440           }
2441           msgPanel.add(msg2);
2442
2443           final JCheckBox jcb = new JCheckBox(
2444                   MessageManager.getString("label.do_not_display_again"));
2445           jcb.addActionListener(new ActionListener()
2446           {
2447             @Override
2448             public void actionPerformed(ActionEvent e)
2449             {
2450               // update Cache settings for "don't show this again"
2451               boolean showWarningAgain = !jcb.isSelected();
2452               Cache.setProperty("CHECKURLLINKS",
2453                       Boolean.valueOf(showWarningAgain).toString());
2454             }
2455           });
2456           msgPanel.add(jcb);
2457
2458           JvOptionPane.showMessageDialog(Desktop.desktop, msgPanel,
2459                   MessageManager
2460                           .getString("label.SEQUENCE_ID_no_longer_used"),
2461                   JvOptionPane.WARNING_MESSAGE);
2462         }
2463       }
2464     });
2465   }
2466
2467   /**
2468    * Proxy class for JDesktopPane which optionally displays the current memory
2469    * usage and highlights the desktop area with a red bar if free memory runs
2470    * low.
2471    * 
2472    * @author AMW
2473    */
2474   public class MyDesktopPane extends JDesktopPane implements Runnable
2475   {
2476
2477     private static final float ONE_MB = 1048576f;
2478
2479     boolean showMemoryUsage = false;
2480
2481     Runtime runtime;
2482
2483     java.text.NumberFormat df;
2484
2485     float maxMemory, allocatedMemory, freeMemory, totalFreeMemory,
2486             percentUsage;
2487
2488     public MyDesktopPane(boolean showMemoryUsage)
2489     {
2490       showMemoryUsage(showMemoryUsage);
2491     }
2492
2493     public void showMemoryUsage(boolean showMemory)
2494     {
2495       this.showMemoryUsage = showMemory;
2496       if (showMemory)
2497       {
2498         Thread worker = new Thread(this);
2499         worker.start();
2500       }
2501       repaint();
2502     }
2503
2504     public boolean isShowMemoryUsage()
2505     {
2506       return showMemoryUsage;
2507     }
2508
2509     @Override
2510     public void run()
2511     {
2512       df = java.text.NumberFormat.getNumberInstance();
2513       df.setMaximumFractionDigits(2);
2514       runtime = Runtime.getRuntime();
2515
2516       while (showMemoryUsage)
2517       {
2518         try
2519         {
2520           maxMemory = runtime.maxMemory() / ONE_MB;
2521           allocatedMemory = runtime.totalMemory() / ONE_MB;
2522           freeMemory = runtime.freeMemory() / ONE_MB;
2523           totalFreeMemory = freeMemory + (maxMemory - allocatedMemory);
2524
2525           percentUsage = (totalFreeMemory / maxMemory) * 100;
2526
2527           // if (percentUsage < 20)
2528           {
2529             // border1 = BorderFactory.createMatteBorder(12, 12, 12, 12,
2530             // Color.red);
2531             // instance.set.setBorder(border1);
2532           }
2533           repaint();
2534           // sleep after showing usage
2535           Thread.sleep(3000);
2536         } catch (Exception ex)
2537         {
2538           ex.printStackTrace();
2539         }
2540       }
2541     }
2542
2543     @Override
2544     public void paintComponent(Graphics g)
2545     {
2546       if (showMemoryUsage && g != null && df != null)
2547       {
2548         if (percentUsage < 20)
2549         {
2550           g.setColor(Color.red);
2551         }
2552         FontMetrics fm = g.getFontMetrics();
2553         if (fm != null)
2554         {
2555           g.drawString(MessageManager.formatMessage("label.memory_stats",
2556                   new Object[]
2557                   { df.format(totalFreeMemory), df.format(maxMemory),
2558                       df.format(percentUsage) }),
2559                   10, getHeight() - fm.getHeight());
2560         }
2561       }
2562     }
2563   }
2564
2565
2566   /**
2567    * Accessor method to quickly get all the AlignmentFrames loaded.
2568    * 
2569    * @return an array of AlignFrame, or null if none found
2570    */
2571   public static AlignFrame[] getAlignFrames()
2572   {
2573     if (Jalview.isHeadlessMode())
2574     {
2575       // Desktop.desktop is null in headless mode
2576       return new AlignFrame[] { Jalview.currentAlignFrame };
2577     }
2578
2579     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2580
2581     if (frames == null)
2582     {
2583       return null;
2584     }
2585     List<AlignFrame> avp = new ArrayList<>();
2586     // REVERSE ORDER
2587     for (int i = frames.length - 1; i > -1; i--)
2588     {
2589       if (frames[i] instanceof AlignFrame)
2590       {
2591         avp.add((AlignFrame) frames[i]);
2592       }
2593       else if (frames[i] instanceof SplitFrame)
2594       {
2595         /*
2596          * Also check for a split frame containing an AlignFrame
2597          */
2598         GSplitFrame sf = (GSplitFrame) frames[i];
2599         if (sf.getTopFrame() instanceof AlignFrame)
2600         {
2601           avp.add((AlignFrame) sf.getTopFrame());
2602         }
2603         if (sf.getBottomFrame() instanceof AlignFrame)
2604         {
2605           avp.add((AlignFrame) sf.getBottomFrame());
2606         }
2607       }
2608     }
2609     if (avp.size() == 0)
2610     {
2611       return null;
2612     }
2613     AlignFrame afs[] = avp.toArray(new AlignFrame[avp.size()]);
2614     return afs;
2615   }
2616
2617   /**
2618    * Returns an array of any AppJmol frames in the Desktop (or null if none).
2619    * 
2620    * @return
2621    */
2622   public GStructureViewer[] getJmols()
2623   {
2624     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2625
2626     if (frames == null)
2627     {
2628       return null;
2629     }
2630     List<GStructureViewer> avp = new ArrayList<>();
2631     // REVERSE ORDER
2632     for (int i = frames.length - 1; i > -1; i--)
2633     {
2634       if (frames[i] instanceof AppJmol)
2635       {
2636         GStructureViewer af = (GStructureViewer) frames[i];
2637         avp.add(af);
2638       }
2639     }
2640     if (avp.size() == 0)
2641     {
2642       return null;
2643     }
2644     GStructureViewer afs[] = avp.toArray(new GStructureViewer[avp.size()]);
2645     return afs;
2646   }
2647
2648   /**
2649    * Add Groovy Support to Jalview
2650    */
2651   @Override
2652   public void groovyShell_actionPerformed()
2653   {
2654     try
2655     {
2656       openGroovyConsole();
2657     } catch (Exception ex)
2658     {
2659       jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex);
2660       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2661
2662               MessageManager.getString("label.couldnt_create_groovy_shell"),
2663               MessageManager.getString("label.groovy_support_failed"),
2664               JvOptionPane.ERROR_MESSAGE);
2665     }
2666   }
2667
2668   /**
2669    * Open the Groovy console
2670    */
2671   void openGroovyConsole()
2672   {
2673     if (groovyConsole == null)
2674     {
2675       groovyConsole = new groovy.ui.Console();
2676       groovyConsole.setVariable("Jalview", this);
2677       groovyConsole.run();
2678
2679       /*
2680        * We allow only one console at a time, so that AlignFrame menu option
2681        * 'Calculate | Run Groovy script' is unambiguous.
2682        * Disable 'Groovy Console', and enable 'Run script', when the console is 
2683        * opened, and the reverse when it is closed
2684        */
2685       Window window = (Window) groovyConsole.getFrame();
2686       window.addWindowListener(new WindowAdapter()
2687       {
2688         @Override
2689         public void windowClosed(WindowEvent e)
2690         {
2691           /*
2692            * rebind CMD-Q from Groovy Console to Jalview Quit
2693            */
2694           addQuitHandler();
2695           enableExecuteGroovy(false);
2696         }
2697       });
2698     }
2699
2700     /*
2701      * show Groovy console window (after close and reopen)
2702      */
2703     ((Window) groovyConsole.getFrame()).setVisible(true);
2704
2705     /*
2706      * if we got this far, enable 'Run Groovy' in AlignFrame menus
2707      * and disable opening a second console
2708      */
2709     enableExecuteGroovy(true);
2710   }
2711
2712   /**
2713    * Bind Ctrl/Cmd-Q to Quit - for reset as Groovy Console takes over this
2714    * binding when opened
2715    */
2716   protected void addQuitHandler()
2717   {
2718     getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
2719             .put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
2720                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
2721                     "Quit");
2722     getRootPane().getActionMap().put("Quit", new AbstractAction()
2723     {
2724       @Override
2725       public void actionPerformed(ActionEvent e)
2726       {
2727         quit();
2728       }
2729     });
2730   }
2731
2732   /**
2733    * Enable or disable 'Run Groovy script' in AlignFrame calculate menus
2734    * 
2735    * @param enabled
2736    *          true if Groovy console is open
2737    */
2738   public void enableExecuteGroovy(boolean enabled)
2739   {
2740     /*
2741      * disable opening a second Groovy console
2742      * (or re-enable when the console is closed)
2743      */
2744     groovyShell.setEnabled(!enabled);
2745
2746     AlignFrame[] alignFrames = getAlignFrames();
2747     if (alignFrames != null)
2748     {
2749       for (AlignFrame af : alignFrames)
2750       {
2751         af.setGroovyEnabled(enabled);
2752       }
2753     }
2754   }
2755
2756   /**
2757    * Progress bars managed by the IProgressIndicator method.
2758    */
2759   private Hashtable<Long, JPanel> progressBars;
2760
2761   private Hashtable<Long, IProgressIndicatorHandler> progressBarHandlers;
2762
2763   /*
2764    * (non-Javadoc)
2765    * 
2766    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
2767    */
2768   @Override
2769   public void setProgressBar(String message, long id)
2770   {
2771     if (progressBars == null)
2772     {
2773       progressBars = new Hashtable<>();
2774       progressBarHandlers = new Hashtable<>();
2775     }
2776
2777     if (progressBars.get(new Long(id)) != null)
2778     {
2779       JPanel panel = progressBars.remove(new Long(id));
2780       if (progressBarHandlers.contains(new Long(id)))
2781       {
2782         progressBarHandlers.remove(new Long(id));
2783       }
2784       removeProgressPanel(panel);
2785     }
2786     else
2787     {
2788       progressBars.put(new Long(id), addProgressPanel(message));
2789     }
2790   }
2791
2792   /*
2793    * (non-Javadoc)
2794    * 
2795    * @see jalview.gui.IProgressIndicator#registerHandler(long,
2796    * jalview.gui.IProgressIndicatorHandler)
2797    */
2798   @Override
2799   public void registerHandler(final long id,
2800           final IProgressIndicatorHandler handler)
2801   {
2802     if (progressBarHandlers == null
2803             || !progressBars.containsKey(new Long(id)))
2804     {
2805       throw new Error(MessageManager.getString(
2806               "error.call_setprogressbar_before_registering_handler"));
2807     }
2808     progressBarHandlers.put(new Long(id), handler);
2809     final JPanel progressPanel = progressBars.get(new Long(id));
2810     if (handler.canCancel())
2811     {
2812       JButton cancel = new JButton(
2813               MessageManager.getString("action.cancel"));
2814       final IProgressIndicator us = this;
2815       cancel.addActionListener(new ActionListener()
2816       {
2817
2818         @Override
2819         public void actionPerformed(ActionEvent e)
2820         {
2821           handler.cancelActivity(id);
2822           us.setProgressBar(MessageManager
2823                   .formatMessage("label.cancelled_params", new Object[]
2824                   { ((JLabel) progressPanel.getComponent(0)).getText() }),
2825                   id);
2826         }
2827       });
2828       progressPanel.add(cancel, BorderLayout.EAST);
2829     }
2830   }
2831
2832   /**
2833    * 
2834    * @return true if any progress bars are still active
2835    */
2836   @Override
2837   public boolean operationInProgress()
2838   {
2839     if (progressBars != null && progressBars.size() > 0)
2840     {
2841       return true;
2842     }
2843     return false;
2844   }
2845
2846   /**
2847    * This will return the first AlignFrame holding the given viewport instance.
2848    * It will break if there are more than one AlignFrames viewing a particular
2849    * av.
2850    * 
2851    * @param viewport
2852    * @return alignFrame for viewport
2853    */
2854   public static AlignFrame getAlignFrameFor(AlignViewportI viewport)
2855   {
2856     if (desktop != null)
2857     {
2858       AlignmentPanel[] aps = getAlignmentPanels(
2859               viewport.getSequenceSetId());
2860       for (int panel = 0; aps != null && panel < aps.length; panel++)
2861       {
2862         if (aps[panel] != null && aps[panel].av == viewport)
2863         {
2864           return aps[panel].alignFrame;
2865         }
2866       }
2867     }
2868     return null;
2869   }
2870
2871   public VamsasApplication getVamsasApplication()
2872   {
2873     return v_client;
2874
2875   }
2876
2877   /**
2878    * flag set if jalview GUI is being operated programmatically
2879    */
2880   private boolean inBatchMode = false;
2881
2882   /**
2883    * check if jalview GUI is being operated programmatically
2884    * 
2885    * @return inBatchMode
2886    */
2887   public boolean isInBatchMode()
2888   {
2889     return inBatchMode;
2890   }
2891
2892   /**
2893    * set flag if jalview GUI is being operated programmatically
2894    * 
2895    * @param inBatchMode
2896    */
2897   public void setInBatchMode(boolean inBatchMode)
2898   {
2899     this.inBatchMode = inBatchMode;
2900   }
2901
2902   public void startServiceDiscovery()
2903   {
2904     startServiceDiscovery(false);
2905   }
2906
2907   public void startServiceDiscovery(boolean blocking)
2908   {
2909     boolean alive = true;
2910     Thread t0 = null, t1 = null, t2 = null;
2911     // JAL-940 - JALVIEW 1 services are now being EOLed as of JABA 2.1 release
2912     if (true)
2913     {
2914       // todo: changesupport handlers need to be transferred
2915       if (discoverer == null)
2916       {
2917         discoverer = new jalview.ws.jws1.Discoverer();
2918         // register PCS handler for desktop.
2919         discoverer.addPropertyChangeListener(changeSupport);
2920       }
2921       // JAL-940 - disabled JWS1 service configuration - always start discoverer
2922       // until we phase out completely
2923       (t0 = new Thread(discoverer)).start();
2924     }
2925
2926     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
2927     {
2928       t2 = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2929               .startDiscoverer(changeSupport);
2930     }
2931     Thread t3 = null;
2932     {
2933       // TODO: do rest service discovery
2934     }
2935     if (blocking)
2936     {
2937       while (alive)
2938       {
2939         try
2940         {
2941           Thread.sleep(15);
2942         } catch (Exception e)
2943         {
2944         }
2945         alive = (t1 != null && t1.isAlive()) || (t2 != null && t2.isAlive())
2946                 || (t3 != null && t3.isAlive())
2947                 || (t0 != null && t0.isAlive());
2948       }
2949     }
2950   }
2951
2952   /**
2953    * called to check if the service discovery process completed successfully.
2954    * 
2955    * @param evt
2956    */
2957   protected void JalviewServicesChanged(PropertyChangeEvent evt)
2958   {
2959     if (evt.getNewValue() == null || evt.getNewValue() instanceof Vector)
2960     {
2961       final String ermsg = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2962               .getErrorMessages();
2963       if (ermsg != null)
2964       {
2965         if (Cache.getDefault("SHOW_WSDISCOVERY_ERRORS", true))
2966         {
2967           if (serviceChangedDialog == null)
2968           {
2969             // only run if we aren't already displaying one of these.
2970             addDialogThread(serviceChangedDialog = new Runnable()
2971             {
2972               @Override
2973               public void run()
2974               {
2975
2976                 /*
2977                  * JalviewDialog jd =new JalviewDialog() {
2978                  * 
2979                  * @Override protected void cancelPressed() { // TODO
2980                  * Auto-generated method stub
2981                  * 
2982                  * }@Override protected void okPressed() { // TODO
2983                  * Auto-generated method stub
2984                  * 
2985                  * }@Override protected void raiseClosed() { // TODO
2986                  * Auto-generated method stub
2987                  * 
2988                  * } }; jd.initDialogFrame(new
2989                  * JLabel("<html><table width=\"450\"><tr><td>" + ermsg +
2990                  * "<br/>It may be that you have invalid JABA URLs in your web service preferences,"
2991                  * + " or mis-configured HTTP proxy settings.<br/>" +
2992                  * "Check the <em>Connections</em> and <em>Web services</em> tab of the"
2993                  * +
2994                  * " Tools->Preferences dialog box to change them.</td></tr></table></html>"
2995                  * ), true, true, "Web Service Configuration Problem", 450,
2996                  * 400);
2997                  * 
2998                  * jd.waitForInput();
2999                  */
3000                 JvOptionPane.showConfirmDialog(Desktop.desktop,
3001                         new JLabel("<html><table width=\"450\"><tr><td>"
3002                                 + ermsg + "</td></tr></table>"
3003                                 + "<p>It may be that you have invalid JABA URLs<br/>in your web service preferences,"
3004                                 + "<br>or as a command-line argument, or mis-configured HTTP proxy settings.</p>"
3005                                 + "<p>Check the <em>Connections</em> and <em>Web services</em> tab<br/>of the"
3006                                 + " Tools->Preferences dialog box to change them.</p></html>"),
3007                         "Web Service Configuration Problem",
3008                         JvOptionPane.DEFAULT_OPTION,
3009                         JvOptionPane.ERROR_MESSAGE);
3010                 serviceChangedDialog = null;
3011
3012               }
3013             });
3014           }
3015         }
3016         else
3017         {
3018           Cache.log.error(
3019                   "Errors reported by JABA discovery service. Check web services preferences.\n"
3020                           + ermsg);
3021         }
3022       }
3023     }
3024   }
3025
3026   private Runnable serviceChangedDialog = null;
3027
3028   /**
3029    * start a thread to open a URL in the configured browser. Pops up a warning
3030    * dialog to the user if there is an exception when calling out to the browser
3031    * to open the URL.
3032    * 
3033    * @param url
3034    */
3035   public static void showUrl(final String url)
3036   {
3037     showUrl(url, Desktop.instance);
3038   }
3039
3040   /**
3041    * Like showUrl but allows progress handler to be specified
3042    * 
3043    * @param url
3044    * @param progress
3045    *          (null) or object implementing IProgressIndicator
3046    */
3047   public static void showUrl(final String url,
3048           final IProgressIndicator progress)
3049   {
3050     new Thread(new Runnable()
3051     {
3052       @Override
3053       public void run()
3054       {
3055         try
3056         {
3057           if (progress != null)
3058           {
3059             progress.setProgressBar(MessageManager
3060                     .formatMessage("status.opening_params", new Object[]
3061                     { url }), this.hashCode());
3062           }
3063           jalview.util.BrowserLauncher.openURL(url);
3064         } catch (Exception ex)
3065         {
3066           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3067                   MessageManager
3068                           .getString("label.web_browser_not_found_unix"),
3069                   MessageManager.getString("label.web_browser_not_found"),
3070                   JvOptionPane.WARNING_MESSAGE);
3071
3072           ex.printStackTrace();
3073         }
3074         if (progress != null)
3075         {
3076           progress.setProgressBar(null, this.hashCode());
3077         }
3078       }
3079     }).start();
3080   }
3081
3082   public static WsParamSetManager wsparamManager = null;
3083
3084   public static ParamManager getUserParameterStore()
3085   {
3086     if (wsparamManager == null)
3087     {
3088       wsparamManager = new WsParamSetManager();
3089     }
3090     return wsparamManager;
3091   }
3092
3093   /**
3094    * static hyperlink handler proxy method for use by Jalview's internal windows
3095    * 
3096    * @param e
3097    */
3098   public static void hyperlinkUpdate(HyperlinkEvent e)
3099   {
3100     if (e.getEventType() == EventType.ACTIVATED)
3101     {
3102       String url = null;
3103       try
3104       {
3105         url = e.getURL().toString();
3106         Desktop.showUrl(url);
3107       } catch (Exception x)
3108       {
3109         if (url != null)
3110         {
3111           if (Cache.log != null)
3112           {
3113             Cache.log.error("Couldn't handle string " + url + " as a URL.");
3114           }
3115           else
3116           {
3117             System.err.println(
3118                     "Couldn't handle string " + url + " as a URL.");
3119           }
3120         }
3121         // ignore any exceptions due to dud links.
3122       }
3123
3124     }
3125   }
3126
3127   /**
3128    * single thread that handles display of dialogs to user.
3129    */
3130   ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
3131
3132   /**
3133    * flag indicating if dialogExecutor should try to acquire a permit
3134    */
3135   private volatile boolean dialogPause = true;
3136
3137   /**
3138    * pause the queue
3139    */
3140   private java.util.concurrent.Semaphore block = new Semaphore(0);
3141
3142   private static groovy.ui.Console groovyConsole;
3143
3144   /**
3145    * add another dialog thread to the queue
3146    * 
3147    * @param prompter
3148    */
3149   public void addDialogThread(final Runnable prompter)
3150   {
3151     dialogExecutor.submit(new Runnable()
3152     {
3153       @Override
3154       public void run()
3155       {
3156         if (dialogPause)
3157         {
3158           try
3159           {
3160             block.acquire();
3161           } catch (InterruptedException x)
3162           {
3163           }
3164           ;
3165         }
3166         if (instance == null)
3167         {
3168           return;
3169         }
3170         try
3171         {
3172           SwingUtilities.invokeAndWait(prompter);
3173         } catch (Exception q)
3174         {
3175           Cache.log.warn("Unexpected Exception in dialog thread.", q);
3176         }
3177       }
3178     });
3179   }
3180
3181   public void startDialogQueue()
3182   {
3183     // set the flag so we don't pause waiting for another permit and semaphore
3184     // the current task to begin
3185     dialogPause = false;
3186     block.release();
3187   }
3188
3189   @Override
3190   protected void snapShotWindow_actionPerformed(ActionEvent e)
3191   {
3192     invalidate();
3193     File of;
3194     ImageMaker im = new jalview.util.ImageMaker(
3195             this, ImageMaker.TYPE.EPS, "View of Desktop", getWidth(),
3196             getHeight(), of = new File("Jalview_snapshot"
3197                     + System.currentTimeMillis() + ".eps"),
3198             "View of desktop", null, 0, false);
3199     try
3200     {
3201       paintAll(im.getGraphics());
3202       im.writeImage();
3203     } catch (Exception q)
3204     {
3205       Cache.log.error("Couldn't write snapshot to " + of.getAbsolutePath(),
3206               q);
3207       return;
3208     }
3209     Cache.log.info("Successfully written snapshot to file "
3210             + of.getAbsolutePath());
3211   }
3212
3213   /**
3214    * Explode the views in the given SplitFrame into separate SplitFrame windows.
3215    * This respects (remembers) any previous 'exploded geometry' i.e. the size
3216    * and location last time the view was expanded (if any). However it does not
3217    * remember the split pane divider location - this is set to match the
3218    * 'exploding' frame.
3219    * 
3220    * @param sf
3221    */
3222   public void explodeViews(SplitFrame sf)
3223   {
3224     AlignFrame oldTopFrame = (AlignFrame) sf.getTopFrame();
3225     AlignFrame oldBottomFrame = (AlignFrame) sf.getBottomFrame();
3226     List<? extends AlignmentViewPanel> topPanels = oldTopFrame
3227             .getAlignPanels();
3228     List<? extends AlignmentViewPanel> bottomPanels = oldBottomFrame
3229             .getAlignPanels();
3230     int viewCount = topPanels.size();
3231     if (viewCount < 2)
3232     {
3233       return;
3234     }
3235
3236     /*
3237      * Processing in reverse order works, forwards order leaves the first panels
3238      * not visible. I don't know why!
3239      */
3240     for (int i = viewCount - 1; i >= 0; i--)
3241     {
3242       /*
3243        * Make new top and bottom frames. These take over the respective
3244        * AlignmentPanel objects, including their AlignmentViewports, so the
3245        * cdna/protein relationships between the viewports is carried over to the
3246        * new split frames.
3247        * 
3248        * explodedGeometry holds the (x, y) position of the previously exploded
3249        * SplitFrame, and the (width, height) of the AlignFrame component
3250        */
3251       AlignmentPanel topPanel = (AlignmentPanel) topPanels.get(i);
3252       AlignFrame newTopFrame = new AlignFrame(topPanel);
3253       newTopFrame.setSize(oldTopFrame.getSize());
3254       newTopFrame.setVisible(true);
3255       Rectangle geometry = ((AlignViewport) topPanel.getAlignViewport())
3256               .getExplodedGeometry();
3257       if (geometry != null)
3258       {
3259         newTopFrame.setSize(geometry.getSize());
3260       }
3261
3262       AlignmentPanel bottomPanel = (AlignmentPanel) bottomPanels.get(i);
3263       AlignFrame newBottomFrame = new AlignFrame(bottomPanel);
3264       newBottomFrame.setSize(oldBottomFrame.getSize());
3265       newBottomFrame.setVisible(true);
3266       geometry = ((AlignViewport) bottomPanel.getAlignViewport())
3267               .getExplodedGeometry();
3268       if (geometry != null)
3269       {
3270         newBottomFrame.setSize(geometry.getSize());
3271       }
3272
3273       topPanel.av.setGatherViewsHere(false);
3274       bottomPanel.av.setGatherViewsHere(false);
3275       JInternalFrame splitFrame = new SplitFrame(newTopFrame,
3276               newBottomFrame);
3277       if (geometry != null)
3278       {
3279         splitFrame.setLocation(geometry.getLocation());
3280       }
3281       Desktop.addInternalFrame(splitFrame, sf.getTitle(), -1, -1);
3282     }
3283
3284     /*
3285      * Clear references to the panels (now relocated in the new SplitFrames)
3286      * before closing the old SplitFrame.
3287      */
3288     topPanels.clear();
3289     bottomPanels.clear();
3290     sf.close();
3291   }
3292
3293   /**
3294    * Gather expanded split frames, sharing the same pairs of sequence set ids,
3295    * back into the given SplitFrame as additional views. Note that the gathered
3296    * frames may themselves have multiple views.
3297    * 
3298    * @param source
3299    */
3300   public void gatherViews(GSplitFrame source)
3301   {
3302     /*
3303      * special handling of explodedGeometry for a view within a SplitFrame: - it
3304      * holds the (x, y) position of the enclosing SplitFrame, and the (width,
3305      * height) of the AlignFrame component
3306      */
3307     AlignFrame myTopFrame = (AlignFrame) source.getTopFrame();
3308     AlignFrame myBottomFrame = (AlignFrame) source.getBottomFrame();
3309     myTopFrame.viewport.setExplodedGeometry(new Rectangle(source.getX(),
3310             source.getY(), myTopFrame.getWidth(), myTopFrame.getHeight()));
3311     myBottomFrame.viewport
3312             .setExplodedGeometry(new Rectangle(source.getX(), source.getY(),
3313                     myBottomFrame.getWidth(), myBottomFrame.getHeight()));
3314     myTopFrame.viewport.setGatherViewsHere(true);
3315     myBottomFrame.viewport.setGatherViewsHere(true);
3316     String topViewId = myTopFrame.viewport.getSequenceSetId();
3317     String bottomViewId = myBottomFrame.viewport.getSequenceSetId();
3318
3319     JInternalFrame[] frames = desktop.getAllFrames();
3320     for (JInternalFrame frame : frames)
3321     {
3322       if (frame instanceof SplitFrame && frame != source)
3323       {
3324         SplitFrame sf = (SplitFrame) frame;
3325         AlignFrame topFrame = (AlignFrame) sf.getTopFrame();
3326         AlignFrame bottomFrame = (AlignFrame) sf.getBottomFrame();
3327         boolean gatherThis = false;
3328         for (int a = 0; a < topFrame.alignPanels.size(); a++)
3329         {
3330           AlignmentPanel topPanel = topFrame.alignPanels.get(a);
3331           AlignmentPanel bottomPanel = bottomFrame.alignPanels.get(a);
3332           if (topViewId.equals(topPanel.av.getSequenceSetId())
3333                   && bottomViewId.equals(bottomPanel.av.getSequenceSetId()))
3334           {
3335             gatherThis = true;
3336             topPanel.av.setGatherViewsHere(false);
3337             bottomPanel.av.setGatherViewsHere(false);
3338             topPanel.av.setExplodedGeometry(
3339                     new Rectangle(sf.getLocation(), topFrame.getSize()));
3340             bottomPanel.av.setExplodedGeometry(
3341                     new Rectangle(sf.getLocation(), bottomFrame.getSize()));
3342             myTopFrame.addAlignmentPanel(topPanel, false);
3343             myBottomFrame.addAlignmentPanel(bottomPanel, false);
3344           }
3345         }
3346
3347         if (gatherThis)
3348         {
3349           topFrame.getAlignPanels().clear();
3350           bottomFrame.getAlignPanels().clear();
3351           sf.close();
3352         }
3353       }
3354     }
3355
3356     /*
3357      * The dust settles...give focus to the tab we did this from.
3358      */
3359     myTopFrame.setDisplayedView(myTopFrame.alignPanel);
3360   }
3361
3362   public static groovy.ui.Console getGroovyConsole()
3363   {
3364     return groovyConsole;
3365   }
3366
3367   /**
3368    * handles the payload of a drag and drop event.
3369    * 
3370    * TODO refactor to desktop utilities class
3371    * 
3372    * @param files
3373    *          - Data source strings extracted from the drop event
3374    * @param protocols
3375    *          - protocol for each data source extracted from the drop event
3376    * @param evt
3377    *          - the drop event
3378    * @param t
3379    *          - the payload from the drop event
3380    * @throws Exception
3381    */
3382   public static void transferFromDropTarget(List<String> files,
3383           List<DataSourceType> protocols, DropTargetDropEvent evt,
3384           Transferable t) throws Exception
3385   {
3386
3387     DataFlavor uriListFlavor = new DataFlavor(
3388             "text/uri-list;class=java.lang.String"), urlFlavour = null;
3389     try
3390     {
3391       urlFlavour = new DataFlavor(
3392               "application/x-java-url; class=java.net.URL");
3393     } catch (ClassNotFoundException cfe)
3394     {
3395       Cache.log.debug("Couldn't instantiate the URL dataflavor.", cfe);
3396     }
3397
3398     if (urlFlavour != null && t.isDataFlavorSupported(urlFlavour))
3399     {
3400
3401       try
3402       {
3403       java.net.URL url = (URL) t.getTransferData(urlFlavour);
3404         // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099
3405         // means url may be null.
3406       if (url != null)
3407       {
3408         protocols.add(DataSourceType.URL);
3409         files.add(url.toString());
3410         Cache.log.debug("Drop handled as URL dataflavor "
3411                 + files.get(files.size() - 1));
3412           return;
3413         }
3414         else
3415         {
3416           if (Platform.isAMac())
3417           {
3418             System.err.println(
3419                     "Please ignore plist error - occurs due to problem with java 8 on OSX");
3420           }
3421           ;
3422       }
3423       } catch (Throwable ex)
3424       {
3425         Cache.log.debug("URL drop handler failed.", ex);
3426       }
3427     }
3428     if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
3429     {
3430       // Works on Windows and MacOSX
3431       Cache.log.debug("Drop handled as javaFileListFlavor");
3432       for (Object file : (List) t
3433               .getTransferData(DataFlavor.javaFileListFlavor))
3434       {
3435         files.add(((File) file).toString());
3436         protocols.add(DataSourceType.FILE);
3437       }
3438     }
3439     else
3440     {
3441       // Unix like behaviour
3442       boolean added = false;
3443       String data = null;
3444       if (t.isDataFlavorSupported(uriListFlavor))
3445       {
3446         Cache.log.debug("Drop handled as uriListFlavor");
3447         // This is used by Unix drag system
3448         data = (String) t.getTransferData(uriListFlavor);
3449       }
3450       if (data == null)
3451       {
3452         // fallback to text: workaround - on OSX where there's a JVM bug
3453         Cache.log.debug("standard URIListFlavor failed. Trying text");
3454         // try text fallback
3455         DataFlavor textDf = new DataFlavor(
3456                 "text/plain;class=java.lang.String");
3457         if (t.isDataFlavorSupported(textDf))
3458         {
3459           data = (String) t.getTransferData(textDf);
3460         }
3461
3462         Cache.log.debug("Plain text drop content returned "
3463                 + (data == null ? "Null - failed" : data));
3464
3465       }
3466       if (data != null)
3467       {
3468         while (protocols.size() < files.size())
3469         {
3470           Cache.log.debug("Adding missing FILE protocol for "
3471                   + files.get(protocols.size()));
3472           protocols.add(DataSourceType.FILE);
3473         }
3474         for (java.util.StringTokenizer st = new java.util.StringTokenizer(
3475                 data, "\r\n"); st.hasMoreTokens();)
3476         {
3477           added = true;
3478           String s = st.nextToken();
3479           if (s.startsWith("#"))
3480           {
3481             // the line is a comment (as per the RFC 2483)
3482             continue;
3483           }
3484           java.net.URI uri = new java.net.URI(s);
3485           if (uri.getScheme().toLowerCase().startsWith("http"))
3486           {
3487             protocols.add(DataSourceType.URL);
3488             files.add(uri.toString());
3489           }
3490           else
3491           {
3492             // otherwise preserve old behaviour: catch all for file objects
3493             java.io.File file = new java.io.File(uri);
3494             protocols.add(DataSourceType.FILE);
3495             files.add(file.toString());
3496           }
3497         }
3498       }
3499
3500       if (Cache.log.isDebugEnabled())
3501       {
3502         if (data == null || !added)
3503         {
3504
3505           if (t.getTransferDataFlavors() != null
3506                   && t.getTransferDataFlavors().length > 0)
3507           {
3508             Cache.log.debug(
3509                     "Couldn't resolve drop data. Here are the supported flavors:");
3510             for (DataFlavor fl : t.getTransferDataFlavors())
3511             {
3512               Cache.log.debug(
3513                       "Supported transfer dataflavor: " + fl.toString());
3514               Object df = t.getTransferData(fl);
3515               if (df != null)
3516               {
3517                 Cache.log.debug("Retrieves: " + df);
3518               }
3519               else
3520               {
3521                 Cache.log.debug("Retrieved nothing");
3522               }
3523             }
3524           }
3525           else
3526           {
3527             Cache.log.debug("Couldn't resolve dataflavor for drop: "
3528                     + t.toString());
3529           }
3530         }
3531       }
3532     }
3533     if (Platform.isWindows())
3534
3535     {
3536       Cache.log.debug("Scanning dropped content for Windows Link Files");
3537
3538       // resolve any .lnk files in the file drop
3539       for (int f = 0; f < files.size(); f++)
3540       {
3541         String source = files.get(f).toLowerCase();
3542         if (protocols.get(f).equals(DataSourceType.FILE)
3543                 && (source.endsWith(".lnk") || source.endsWith(".url")
3544                         || source.endsWith(".site")))
3545         {
3546           try {
3547             File lf = new File(files.get(f));
3548             // process link file to get a URL
3549             Cache.log.debug("Found potential link file: " + lf);
3550             WindowsShortcut wscfile = new WindowsShortcut(lf);
3551             String fullname = wscfile.getRealFilename();
3552             protocols.set(f, FormatAdapter.checkProtocol(fullname));
3553             files.set(f, fullname);
3554             Cache.log.debug("Parsed real filename " + fullname
3555                     + " to extract protocol: " + protocols.get(f));
3556           }
3557           catch (Exception ex)
3558           {
3559             Cache.log.error("Couldn't parse "+files.get(f)+" as a link file.",ex);
3560           }
3561         }
3562       }
3563     }
3564   }
3565
3566   /**
3567    * Sets the Preferences property for experimental features to True or False
3568    * depending on the state of the controlling menu item
3569    */
3570   @Override
3571   protected void showExperimental_actionPerformed(boolean selected)
3572   {
3573     Cache.setProperty(EXPERIMENTAL_FEATURES, Boolean.toString(selected));
3574   }
3575
3576   /**
3577    * Answers a (possibly empty) list of any structure viewer frames (currently
3578    * for either Jmol or Chimera) which are currently open. This may optionally
3579    * be restricted to viewers of a specified class, or viewers linked to a
3580    * specified alignment panel.
3581    * 
3582    * @param apanel
3583    *          if not null, only return viewers linked to this panel
3584    * @param structureViewerClass
3585    *          if not null, only return viewers of this class
3586    * @return
3587    */
3588   public List<StructureViewerBase> getStructureViewers(
3589           AlignmentPanel apanel,
3590           Class<? extends StructureViewerBase> structureViewerClass)
3591   {
3592     List<StructureViewerBase> result = new ArrayList<>();
3593     JInternalFrame[] frames = Desktop.instance.getAllFrames();
3594
3595     for (JInternalFrame frame : frames)
3596     {
3597       if (frame instanceof StructureViewerBase)
3598       {
3599         if (structureViewerClass == null
3600                 || structureViewerClass.isInstance(frame))
3601         {
3602           if (apanel == null
3603                   || ((StructureViewerBase) frame).isLinkedWith(apanel))
3604           {
3605             result.add((StructureViewerBase) frame);
3606           }
3607         }
3608       }
3609     }
3610     return result;
3611   }
3612 }