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