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