Merge branch 'develop' into alpha/JAL-3362_Jalview_212_alpha
[jalview.git] / src / jalview / gui / Desktop.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.bin.Cache;
26 import jalview.bin.Jalview;
27 import jalview.io.BackupFiles;
28 import jalview.io.DataSourceType;
29 import jalview.io.FileFormat;
30 import jalview.io.FileFormatException;
31 import jalview.io.FileFormatI;
32 import jalview.io.FileFormats;
33 import jalview.io.FileLoader;
34 import jalview.io.FormatAdapter;
35 import jalview.io.IdentifyFile;
36 import jalview.io.JalviewFileChooser;
37 import jalview.io.JalviewFileView;
38 import jalview.jbgui.GSplitFrame;
39 import jalview.jbgui.GStructureViewer;
40 import jalview.project.Jalview2XML;
41 import jalview.structure.StructureSelectionManager;
42 import jalview.urls.IdOrgSettings;
43 import jalview.util.ImageMaker;
44 import jalview.util.MessageManager;
45 import jalview.util.Platform;
46 import jalview.util.UrlConstants;
47 import jalview.viewmodel.AlignmentViewport;
48 import jalview.ws.params.ParamManager;
49 import jalview.ws.utils.UrlDownloadClient;
50
51 import java.awt.BorderLayout;
52 import java.awt.Color;
53 import java.awt.Dimension;
54 import java.awt.FontMetrics;
55 import java.awt.Graphics;
56 import java.awt.GridLayout;
57 import java.awt.Point;
58 import java.awt.Rectangle;
59 import java.awt.Toolkit;
60 import java.awt.Window;
61 import java.awt.datatransfer.Clipboard;
62 import java.awt.datatransfer.ClipboardOwner;
63 import java.awt.datatransfer.DataFlavor;
64 import java.awt.datatransfer.Transferable;
65 import java.awt.dnd.DnDConstants;
66 import java.awt.dnd.DropTargetDragEvent;
67 import java.awt.dnd.DropTargetDropEvent;
68 import java.awt.dnd.DropTargetEvent;
69 import java.awt.dnd.DropTargetListener;
70 import java.awt.event.ActionEvent;
71 import java.awt.event.ActionListener;
72 import java.awt.event.InputEvent;
73 import java.awt.event.KeyEvent;
74 import java.awt.event.MouseAdapter;
75 import java.awt.event.MouseEvent;
76 import java.awt.event.WindowAdapter;
77 import java.awt.event.WindowEvent;
78 import java.beans.PropertyChangeEvent;
79 import java.beans.PropertyChangeListener;
80 import java.io.File;
81 import java.io.FileWriter;
82 import java.io.IOException;
83 import java.net.URL;
84 import java.util.ArrayList;
85 import java.util.HashMap;
86 import java.util.Hashtable;
87 import java.util.List;
88 import java.util.ListIterator;
89 import java.util.StringTokenizer;
90 import java.util.Vector;
91 import java.util.concurrent.ExecutorService;
92 import java.util.concurrent.Executors;
93 import java.util.concurrent.Semaphore;
94
95 import javax.swing.AbstractAction;
96 import javax.swing.Action;
97 import javax.swing.ActionMap;
98 import javax.swing.Box;
99 import javax.swing.BoxLayout;
100 import javax.swing.DefaultDesktopManager;
101 import javax.swing.DesktopManager;
102 import javax.swing.InputMap;
103 import javax.swing.JButton;
104 import javax.swing.JCheckBox;
105 import javax.swing.JComboBox;
106 import javax.swing.JComponent;
107 import javax.swing.JDesktopPane;
108 import javax.swing.JInternalFrame;
109 import javax.swing.JLabel;
110 import javax.swing.JMenuItem;
111 import javax.swing.JPanel;
112 import javax.swing.JPopupMenu;
113 import javax.swing.JProgressBar;
114 import javax.swing.KeyStroke;
115 import javax.swing.SwingUtilities;
116 import javax.swing.event.HyperlinkEvent;
117 import javax.swing.event.HyperlinkEvent.EventType;
118 import javax.swing.event.InternalFrameAdapter;
119 import javax.swing.event.InternalFrameEvent;
120
121 import org.stackoverflowusers.file.WindowsShortcut;
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
373     addWindowListener(new WindowAdapter()
374     {
375
376       @Override
377       public void windowClosing(WindowEvent ev)
378       {
379         quit();
380       }
381     });
382
383     boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",
384             false);
385
386     boolean showjconsole = jalview.bin.Cache.getDefault("SHOW_JAVA_CONSOLE",
387             false);
388     desktop = new MyDesktopPane(selmemusage);
389     showMemusage.setSelected(selmemusage);
390     desktop.setBackground(Color.white);
391
392     getContentPane().setLayout(new BorderLayout());
393     // alternate config - have scrollbars - see notes in JAL-153
394     // JScrollPane sp = new JScrollPane();
395     // sp.getViewport().setView(desktop);
396     // getContentPane().add(sp, BorderLayout.CENTER);
397     getContentPane().add(desktop, BorderLayout.CENTER);
398     desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
399
400     // This line prevents Windows Look&Feel resizing all new windows to maximum
401     // if previous window was maximised
402     desktop.setDesktopManager(new MyDesktopManager(
403             (Platform.isWindows() ? new DefaultDesktopManager()
404                     : Platform.isAMac()
405                             ? new AquaInternalFrameManager(
406                                     desktop.getDesktopManager())
407                             : desktop.getDesktopManager())));
408
409     Rectangle dims = getLastKnownDimensions("");
410     if (dims != null)
411     {
412       setBounds(dims);
413     }
414     else
415     {
416       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
417       setBounds((screenSize.width - 900) / 2, (screenSize.height - 650) / 2,
418               900, 650);
419     }
420     jconsole = new Console(this, showjconsole);
421     // add essential build information
422     jconsole.setHeader(jalview.bin.Cache.getVersionDetailsForConsole());
423
424     showConsole(showjconsole);
425
426     showNews.setVisible(false);
427
428     experimentalFeatures.setSelected(showExperimental());
429
430     getIdentifiersOrgData();
431
432     checkURLLinks();
433
434     this.addWindowListener(new WindowAdapter()
435     {
436       @Override
437       public void windowClosing(WindowEvent evt)
438       {
439         quit();
440       }
441     });
442
443     MouseAdapter ma;
444     this.addMouseListener(ma = new MouseAdapter()
445     {
446       @Override
447       public void mousePressed(MouseEvent evt)
448       {
449         if (evt.isPopupTrigger()) // Mac
450         {
451           showPasteMenu(evt.getX(), evt.getY());
452         }
453       }
454
455       @Override
456       public void mouseReleased(MouseEvent evt)
457       {
458         if (evt.isPopupTrigger()) // Windows
459         {
460           showPasteMenu(evt.getX(), evt.getY());
461         }
462       }
463     });
464     desktop.addMouseListener(ma);
465
466     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
467     // Spawn a thread that shows the splashscreen
468     SwingUtilities.invokeLater(new Runnable()
469     {
470       @Override
471       public void run()
472       {
473         new SplashScreen();
474       }
475     });
476
477     // Thread off a new instance of the file chooser - this reduces the time it
478     // takes to open it later on.
479     new Thread(new Runnable()
480     {
481       @Override
482       public void run()
483       {
484         Cache.log.debug("Filechooser init thread started.");
485         String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
486         JalviewFileChooser.forRead(Cache.getProperty("LAST_DIRECTORY"),
487                 fileFormat);
488         Cache.log.debug("Filechooser init thread finished.");
489       }
490     }).start();
491     // Add the service change listener
492     changeSupport.addJalviewPropertyChangeListener("services",
493             new PropertyChangeListener()
494             {
495
496               @Override
497               public void propertyChange(PropertyChangeEvent evt)
498               {
499                 Cache.log.debug("Firing service changed event for "
500                         + evt.getNewValue());
501                 JalviewServicesChanged(evt);
502               }
503
504             });
505   }
506
507   /**
508    * Answers true if user preferences to enable experimental features is True
509    * (on), else false
510    * 
511    * @return
512    */
513   public boolean showExperimental()
514   {
515     String experimental = Cache.getDefault(EXPERIMENTAL_FEATURES,
516             Boolean.FALSE.toString());
517     return Boolean.valueOf(experimental).booleanValue();
518   }
519
520   public void doConfigureStructurePrefs()
521   {
522     // configure services
523     StructureSelectionManager ssm = StructureSelectionManager
524             .getStructureSelectionManager(this);
525     if (jalview.bin.Cache.getDefault(Preferences.ADD_SS_ANN, true))
526     {
527       ssm.setAddTempFacAnnot(jalview.bin.Cache
528               .getDefault(Preferences.ADD_TEMPFACT_ANN, true));
529       ssm.setProcessSecondaryStructure(jalview.bin.Cache
530               .getDefault(Preferences.STRUCT_FROM_PDB, true));
531       ssm.setSecStructServices(
532               jalview.bin.Cache.getDefault(Preferences.USE_RNAVIEW, true));
533     }
534     else
535     {
536       ssm.setAddTempFacAnnot(false);
537       ssm.setProcessSecondaryStructure(false);
538       ssm.setSecStructServices(false);
539     }
540   }
541
542   public void checkForNews()
543   {
544     final Desktop me = this;
545     // Thread off the news reader, in case there are connection problems.
546     new Thread(new Runnable()
547     {
548       @Override
549       public void run()
550       {
551         Cache.log.debug("Starting news thread.");
552
553         jvnews = new BlogReader(me);
554         showNews.setVisible(true);
555         Cache.log.debug("Completed news thread.");
556       }
557     }).start();
558   }
559
560   public void getIdentifiersOrgData()
561   {
562     // Thread off the identifiers fetcher
563     new Thread(new Runnable()
564     {
565       @Override
566       public void run()
567       {
568         Cache.log.debug("Downloading data from identifiers.org");
569         UrlDownloadClient client = new UrlDownloadClient();
570         try
571         {
572           client.download(IdOrgSettings.getUrl(),
573                   IdOrgSettings.getDownloadLocation());
574         } catch (IOException e)
575         {
576           Cache.log.debug("Exception downloading identifiers.org data"
577                   + e.getMessage());
578         }
579       }
580     }).start();
581     ;
582   }
583
584   @Override
585   protected void showNews_actionPerformed(ActionEvent e)
586   {
587     showNews(showNews.isSelected());
588   }
589
590   void showNews(boolean visible)
591   {
592     {
593       Cache.log.debug((visible ? "Showing" : "Hiding") + " news.");
594       showNews.setSelected(visible);
595       if (visible && !jvnews.isVisible())
596       {
597         new Thread(new Runnable()
598         {
599           @Override
600           public void run()
601           {
602             long now = System.currentTimeMillis();
603             Desktop.instance.setProgressBar(
604                     MessageManager.getString("status.refreshing_news"),
605                     now);
606             jvnews.refreshNews();
607             Desktop.instance.setProgressBar(null, now);
608             jvnews.showNews();
609           }
610         }).start();
611       }
612     }
613   }
614
615   /**
616    * recover the last known dimensions for a jalview window
617    * 
618    * @param windowName
619    *          - empty string is desktop, all other windows have unique prefix
620    * @return null or last known dimensions scaled to current geometry (if last
621    *         window geom was known)
622    */
623   Rectangle getLastKnownDimensions(String windowName)
624   {
625     // TODO: lock aspect ratio for scaling desktop Bug #0058199
626     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
627     String x = jalview.bin.Cache.getProperty(windowName + "SCREEN_X");
628     String y = jalview.bin.Cache.getProperty(windowName + "SCREEN_Y");
629     String width = jalview.bin.Cache
630             .getProperty(windowName + "SCREEN_WIDTH");
631     String height = jalview.bin.Cache
632             .getProperty(windowName + "SCREEN_HEIGHT");
633     if ((x != null) && (y != null) && (width != null) && (height != null))
634     {
635       int ix = Integer.parseInt(x), iy = Integer.parseInt(y),
636               iw = Integer.parseInt(width), ih = Integer.parseInt(height);
637       if (jalview.bin.Cache.getProperty("SCREENGEOMETRY_WIDTH") != null)
638       {
639         // attempt #1 - try to cope with change in screen geometry - this
640         // version doesn't preserve original jv aspect ratio.
641         // take ratio of current screen size vs original screen size.
642         double sw = ((1f * screenSize.width) / (1f * Integer.parseInt(
643                 jalview.bin.Cache.getProperty("SCREENGEOMETRY_WIDTH"))));
644         double sh = ((1f * screenSize.height) / (1f * Integer.parseInt(
645                 jalview.bin.Cache.getProperty("SCREENGEOMETRY_HEIGHT"))));
646         // rescale the bounds depending upon the current screen geometry.
647         ix = (int) (ix * sw);
648         iw = (int) (iw * sw);
649         iy = (int) (iy * sh);
650         ih = (int) (ih * sh);
651         while (ix >= screenSize.width)
652         {
653           jalview.bin.Cache.log.debug(
654                   "Window geometry location recall error: shifting horizontal to within screenbounds.");
655           ix -= screenSize.width;
656         }
657         while (iy >= screenSize.height)
658         {
659           jalview.bin.Cache.log.debug(
660                   "Window geometry location recall error: shifting vertical to within screenbounds.");
661           iy -= screenSize.height;
662         }
663         jalview.bin.Cache.log.debug(
664                 "Got last known dimensions for " + windowName + ": x:" + ix
665                         + " y:" + iy + " width:" + iw + " height:" + ih);
666       }
667       // return dimensions for new instance
668       return new Rectangle(ix, iy, iw, ih);
669     }
670     return null;
671   }
672
673   void showPasteMenu(int x, int y)
674   {
675     JPopupMenu popup = new JPopupMenu();
676     JMenuItem item = new JMenuItem(
677             MessageManager.getString("label.paste_new_window"));
678     item.addActionListener(new ActionListener()
679     {
680       @Override
681       public void actionPerformed(ActionEvent evt)
682       {
683         paste();
684       }
685     });
686
687     popup.add(item);
688     popup.show(this, x, y);
689   }
690
691   public void paste()
692   {
693     try
694     {
695       Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
696       Transferable contents = c.getContents(this);
697
698       if (contents != null)
699       {
700         String file = (String) contents
701                 .getTransferData(DataFlavor.stringFlavor);
702
703         FileFormatI format = new IdentifyFile().identify(file,
704                 DataSourceType.PASTE);
705
706         new FileLoader().LoadFile(file, DataSourceType.PASTE, format);
707
708       }
709     } catch (Exception ex)
710     {
711       System.out.println(
712               "Unable to paste alignment from system clipboard:\n" + ex);
713     }
714   }
715
716   /**
717    * Adds and opens the given frame to the desktop
718    * 
719    * @param frame
720    *          Frame to show
721    * @param title
722    *          Visible Title
723    * @param w
724    *          width
725    * @param h
726    *          height
727    */
728   public static synchronized void addInternalFrame(
729           final JInternalFrame frame, String title, int w, int h)
730   {
731     addInternalFrame(frame, title, true, w, h, true, false);
732   }
733
734   /**
735    * Add an internal frame to the Jalview desktop
736    * 
737    * @param frame
738    *          Frame to show
739    * @param title
740    *          Visible Title
741    * @param makeVisible
742    *          When true, display frame immediately, otherwise, caller must call
743    *          setVisible themselves.
744    * @param w
745    *          width
746    * @param h
747    *          height
748    */
749   public static synchronized void addInternalFrame(
750           final JInternalFrame frame, String title, boolean makeVisible,
751           int w, int h)
752   {
753     addInternalFrame(frame, title, makeVisible, w, h, true, false);
754   }
755
756   /**
757    * Add an internal frame to the Jalview desktop and make it visible
758    * 
759    * @param frame
760    *          Frame to show
761    * @param title
762    *          Visible Title
763    * @param w
764    *          width
765    * @param h
766    *          height
767    * @param resizable
768    *          Allow resize
769    */
770   public static synchronized void addInternalFrame(
771           final JInternalFrame frame, String title, int w, int h,
772           boolean resizable)
773   {
774     addInternalFrame(frame, title, true, w, h, resizable, false);
775   }
776
777   /**
778    * Add an internal frame to the Jalview desktop
779    * 
780    * @param frame
781    *          Frame to show
782    * @param title
783    *          Visible Title
784    * @param makeVisible
785    *          When true, display frame immediately, otherwise, caller must call
786    *          setVisible themselves.
787    * @param w
788    *          width
789    * @param h
790    *          height
791    * @param resizable
792    *          Allow resize
793    * @param ignoreMinSize
794    *          Do not set the default minimum size for frame
795    */
796   public static synchronized void addInternalFrame(
797           final JInternalFrame frame, String title, boolean makeVisible,
798           int w, int h, boolean resizable, boolean ignoreMinSize)
799   {
800
801     // TODO: allow callers to determine X and Y position of frame (eg. via
802     // bounds object).
803     // TODO: consider fixing method to update entries in the window submenu with
804     // the current window title
805
806     frame.setTitle(title);
807     if (frame.getWidth() < 1 || frame.getHeight() < 1)
808     {
809       frame.setSize(w, h);
810     }
811     // THIS IS A PUBLIC STATIC METHOD, SO IT MAY BE CALLED EVEN IN
812     // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
813     // IF JALVIEW IS RUNNING HEADLESS
814     // ///////////////////////////////////////////////
815     if (instance == null || (System.getProperty("java.awt.headless") != null
816             && System.getProperty("java.awt.headless").equals("true")))
817     {
818       return;
819     }
820
821     openFrameCount++;
822
823     if (!ignoreMinSize)
824     {
825       frame.setMinimumSize(
826               new Dimension(DEFAULT_MIN_WIDTH, DEFAULT_MIN_HEIGHT));
827
828       // Set default dimension for Alignment Frame window.
829       // The Alignment Frame window could be added from a number of places,
830       // hence,
831       // I did this here in order not to miss out on any Alignment frame.
832       if (frame instanceof AlignFrame)
833       {
834         frame.setMinimumSize(new Dimension(ALIGN_FRAME_DEFAULT_MIN_WIDTH,
835                 ALIGN_FRAME_DEFAULT_MIN_HEIGHT));
836       }
837     }
838
839     frame.setVisible(makeVisible);
840     frame.setClosable(true);
841     frame.setResizable(resizable);
842     frame.setMaximizable(resizable);
843     frame.setIconifiable(resizable);
844     frame.setOpaque(false);
845
846     if (frame.getX() < 1 && frame.getY() < 1)
847     {
848       frame.setLocation(xOffset * openFrameCount,
849               yOffset * ((openFrameCount - 1) % 10) + yOffset);
850     }
851
852     /*
853      * add an entry for the new frame in the Window menu 
854      * (and remove it when the frame is closed)
855      */
856     final JMenuItem menuItem = new JMenuItem(title);
857     frame.addInternalFrameListener(new InternalFrameAdapter()
858     {
859       @Override
860       public void internalFrameActivated(InternalFrameEvent evt)
861       {
862         JInternalFrame itf = desktop.getSelectedFrame();
863         if (itf != null)
864         {
865           if (itf instanceof AlignFrame)
866           {
867             Jalview.setCurrentAlignFrame((AlignFrame) itf);
868           }
869           itf.requestFocus();
870         }
871       }
872
873       @Override
874       public void internalFrameClosed(InternalFrameEvent evt)
875       {
876         PaintRefresher.RemoveComponent(frame);
877
878         /*
879          * defensive check to prevent frames being
880          * added half off the window
881          */
882         if (openFrameCount > 0)
883         {
884           openFrameCount--;
885         }
886
887         /*
888          * ensure no reference to alignFrame retained by menu item listener
889          */
890         if (menuItem.getActionListeners().length > 0)
891         {
892           menuItem.removeActionListener(menuItem.getActionListeners()[0]);
893         }
894         windowMenu.remove(menuItem);
895       };
896     });
897
898     menuItem.addActionListener(new ActionListener()
899     {
900       @Override
901       public void actionPerformed(ActionEvent e)
902       {
903         try
904         {
905           frame.setSelected(true);
906           frame.setIcon(false);
907         } catch (java.beans.PropertyVetoException ex)
908         {
909
910         }
911       }
912     });
913
914     setKeyBindings(frame);
915
916     desktop.add(frame);
917
918     windowMenu.add(menuItem);
919
920     frame.toFront();
921     try
922     {
923       frame.setSelected(true);
924       frame.requestFocus();
925     } catch (java.beans.PropertyVetoException ve)
926     {
927     } catch (java.lang.ClassCastException cex)
928     {
929       Cache.log.warn(
930               "Squashed a possible GUI implementation error. If you can recreate this, please look at http://issues.jalview.org/browse/JAL-869",
931               cex);
932     }
933   }
934
935   /**
936    * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
937    * the window
938    * 
939    * @param frame
940    */
941   private static void setKeyBindings(JInternalFrame frame)
942   {
943     @SuppressWarnings("serial")
944     final Action closeAction = new AbstractAction()
945     {
946       @Override
947       public void actionPerformed(ActionEvent e)
948       {
949         frame.dispose();
950       }
951     };
952
953     /*
954      * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action
955      */
956     KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
957             InputEvent.CTRL_DOWN_MASK);
958     KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
959             jalview.util.ShortcutKeyMaskExWrapper.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
1073             .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat, BackupFiles.getEnabled());
1074
1075     chooser.setFileView(new JalviewFileView());
1076     chooser.setDialogTitle(
1077             MessageManager.getString("label.open_local_file"));
1078     chooser.setToolTipText(MessageManager.getString("action.open"));
1079
1080     int value = chooser.showOpenDialog(this);
1081
1082     if (value == JalviewFileChooser.APPROVE_OPTION)
1083     {
1084       String choice = chooser.getSelectedFile().getPath();
1085       Cache.setProperty("LAST_DIRECTORY",
1086               chooser.getSelectedFile().getParent());
1087
1088       FileFormatI format = chooser.getSelectedFormat();
1089
1090       /*
1091        * Call IdentifyFile to verify the file contains what its extension implies.
1092        * Skip this step for dynamically added file formats, because
1093        * IdentifyFile does not know how to recognise them.
1094        */
1095       if (FileFormats.getInstance().isIdentifiable(format))
1096       {
1097         try
1098         {
1099           format = new IdentifyFile().identify(choice, DataSourceType.FILE);
1100         } catch (FileFormatException e)
1101         {
1102           // format = null; //??
1103         }
1104       }
1105
1106       if (viewport != null)
1107       {
1108         new FileLoader().LoadFile(viewport, choice, DataSourceType.FILE,
1109                 format);
1110       }
1111       else
1112       {
1113         new FileLoader().LoadFile(choice, DataSourceType.FILE, format);
1114       }
1115     }
1116   }
1117
1118   /**
1119    * DOCUMENT ME!
1120    * 
1121    * @param e
1122    *          DOCUMENT ME!
1123    */
1124   @Override
1125   public void inputURLMenuItem_actionPerformed(AlignViewport viewport)
1126   {
1127     // This construct allows us to have a wider textfield
1128     // for viewing
1129     JLabel label = new JLabel(
1130             MessageManager.getString("label.input_file_url"));
1131     final JComboBox history = new JComboBox();
1132
1133     JPanel panel = new JPanel(new GridLayout(2, 1));
1134     panel.add(label);
1135     panel.add(history);
1136     history.setPreferredSize(new Dimension(400, 20));
1137     history.setEditable(true);
1138     history.addItem("http://www.");
1139
1140     String historyItems = jalview.bin.Cache.getProperty("RECENT_URL");
1141
1142     StringTokenizer st;
1143
1144     if (historyItems != null)
1145     {
1146       st = new StringTokenizer(historyItems, "\t");
1147
1148       while (st.hasMoreTokens())
1149       {
1150         history.addItem(st.nextElement());
1151       }
1152     }
1153
1154     int reply = JvOptionPane.showInternalConfirmDialog(desktop, panel,
1155             MessageManager.getString("label.input_alignment_from_url"),
1156             JvOptionPane.OK_CANCEL_OPTION);
1157
1158     if (reply != JvOptionPane.OK_OPTION)
1159     {
1160       return;
1161     }
1162
1163     String url = history.getSelectedItem().toString();
1164
1165     if (url.toLowerCase().endsWith(".jar"))
1166     {
1167       if (viewport != null)
1168       {
1169         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1170                 FileFormat.Jalview);
1171       }
1172       else
1173       {
1174         new FileLoader().LoadFile(url, DataSourceType.URL,
1175                 FileFormat.Jalview);
1176       }
1177     }
1178     else
1179     {
1180       FileFormatI format = null;
1181       try
1182       {
1183         format = new IdentifyFile().identify(url, DataSourceType.URL);
1184       } catch (FileFormatException e)
1185       {
1186         // TODO revise error handling, distinguish between
1187         // URL not found and response not valid
1188       }
1189
1190       if (format == null)
1191       {
1192         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1193                 MessageManager.formatMessage("label.couldnt_locate",
1194                         new Object[]
1195                         { url }),
1196                 MessageManager.getString("label.url_not_found"),
1197                 JvOptionPane.WARNING_MESSAGE);
1198
1199         return;
1200       }
1201
1202       if (viewport != null)
1203       {
1204         new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
1205                 format);
1206       }
1207       else
1208       {
1209         new FileLoader().LoadFile(url, DataSourceType.URL, format);
1210       }
1211     }
1212   }
1213
1214   /**
1215    * Opens the CutAndPaste window for the user to paste an alignment in to
1216    * 
1217    * @param viewPanel
1218    *          - if not null, the pasted alignment is added to the current
1219    *          alignment; if null, to a new alignment window
1220    */
1221   @Override
1222   public void inputTextboxMenuItem_actionPerformed(
1223           AlignmentViewPanel viewPanel)
1224   {
1225     CutAndPasteTransfer cap = new CutAndPasteTransfer();
1226     cap.setForInput(viewPanel);
1227     Desktop.addInternalFrame(cap,
1228             MessageManager.getString("label.cut_paste_alignmen_file"), true,
1229             600, 500);
1230   }
1231
1232   /*
1233    * Exit the program
1234    */
1235   @Override
1236   public void quit()
1237   {
1238     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
1239     jalview.bin.Cache.setProperty("SCREENGEOMETRY_WIDTH",
1240             screen.width + "");
1241     jalview.bin.Cache.setProperty("SCREENGEOMETRY_HEIGHT",
1242             screen.height + "");
1243     storeLastKnownDimensions("", new Rectangle(getBounds().x, getBounds().y,
1244             getWidth(), getHeight()));
1245
1246     if (jconsole != null)
1247     {
1248       storeLastKnownDimensions("JAVA_CONSOLE_", jconsole.getBounds());
1249       jconsole.stopConsole();
1250     }
1251     if (jvnews != null)
1252     {
1253       storeLastKnownDimensions("JALVIEW_RSS_WINDOW_", jvnews.getBounds());
1254
1255     }
1256     if (dialogExecutor != null)
1257     {
1258       dialogExecutor.shutdownNow();
1259     }
1260     closeAll_actionPerformed(null);
1261
1262     if (groovyConsole != null)
1263     {
1264       // suppress a possible repeat prompt to save script
1265       groovyConsole.setDirty(false);
1266       groovyConsole.exit();
1267     }
1268     System.exit(0);
1269   }
1270
1271   private void storeLastKnownDimensions(String string, Rectangle jc)
1272   {
1273     jalview.bin.Cache.log.debug("Storing last known dimensions for "
1274             + string + ": x:" + jc.x + " y:" + jc.y + " width:" + jc.width
1275             + " height:" + jc.height);
1276
1277     jalview.bin.Cache.setProperty(string + "SCREEN_X", jc.x + "");
1278     jalview.bin.Cache.setProperty(string + "SCREEN_Y", jc.y + "");
1279     jalview.bin.Cache.setProperty(string + "SCREEN_WIDTH", jc.width + "");
1280     jalview.bin.Cache.setProperty(string + "SCREEN_HEIGHT", jc.height + "");
1281   }
1282
1283   /**
1284    * DOCUMENT ME!
1285    * 
1286    * @param e
1287    *          DOCUMENT ME!
1288    */
1289   @Override
1290   public void aboutMenuItem_actionPerformed(ActionEvent e)
1291   {
1292     // StringBuffer message = getAboutMessage(false);
1293     // JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1294     //
1295     // message.toString(), "About Jalview", JvOptionPane.INFORMATION_MESSAGE);
1296     new Thread(new Runnable()
1297     {
1298       @Override
1299       public void run()
1300       {
1301         new SplashScreen(true);
1302       }
1303     }).start();
1304   }
1305
1306   public StringBuffer getAboutMessage(boolean shortv)
1307   {
1308     StringBuffer message = new StringBuffer();
1309     message.append("<html>");
1310     if (shortv)
1311     {
1312       message.append("<h1><strong>Version: "
1313               + jalview.bin.Cache.getProperty("VERSION")
1314               + "</strong></h1>");
1315       message.append("<strong>Built: <em>"
1316               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")
1317               + "</em> from " + jalview.bin.Cache.getBuildDetailsForSplash()
1318               + "</strong>");
1319
1320     }
1321     else
1322     {
1323
1324       message.append("<strong>Version "
1325               + jalview.bin.Cache.getProperty("VERSION")
1326               + "; last updated: "
1327               + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"));
1328     }
1329
1330     if (jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1331             .equals("Checking"))
1332     {
1333       // JBP removed this message for 2.11: May be reinstated in future version
1334       // message.append("<br>...Checking latest version...</br>");
1335     }
1336     else if (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
1337             .equals(jalview.bin.Cache.getProperty("VERSION")))
1338     {
1339       boolean red = false;
1340       if (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
1341               .indexOf("automated build") == -1)
1342       {
1343         red = true;
1344         // Displayed when code version and jnlp version do not match and code
1345         // version is not a development build
1346         message.append("<div style=\"color: #FF0000;font-style: bold;\">");
1347       }
1348
1349       message.append("<br>!! Version "
1350               + jalview.bin.Cache.getDefault("LATEST_VERSION",
1351                       "..Checking..")
1352               + " is available for download from "
1353               + jalview.bin.Cache.getDefault("www.jalview.org",
1354                       "http://www.jalview.org")
1355               + " !!");
1356       if (red)
1357       {
1358         message.append("</div>");
1359       }
1360     }
1361     message.append("<br>Authors:  " + jalview.bin.Cache.getDefault(
1362             "AUTHORFNAMES",
1363             "The Jalview Authors (See AUTHORS file for current list)")
1364             + "<br><br>Development managed by The Barton Group, University of Dundee, Scotland, UK.<br>"
1365             + "<br><br>For help, see the FAQ at <a href=\"http://www.jalview.org/faq\">www.jalview.org/faq</a> and/or join the jalview-discuss@jalview.org mailing list"
1366             + "<br><br>If  you use Jalview, please cite:"
1367             + "<br>Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
1368             + "<br>Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
1369             + "<br>Bioinformatics doi: 10.1093/bioinformatics/btp033"
1370             + "</html>");
1371     return message;
1372   }
1373
1374   /**
1375    * DOCUMENT ME!
1376    * 
1377    * @param e
1378    *          DOCUMENT ME!
1379    */
1380   @Override
1381   public void documentationMenuItem_actionPerformed(ActionEvent e)
1382   {
1383     try
1384     {
1385       Help.showHelpWindow();
1386     } catch (Exception ex)
1387     {
1388     }
1389   }
1390
1391   @Override
1392   public void closeAll_actionPerformed(ActionEvent e)
1393   {
1394     // TODO show a progress bar while closing?
1395     JInternalFrame[] frames = desktop.getAllFrames();
1396     for (int i = 0; i < frames.length; i++)
1397     {
1398       try
1399       {
1400         frames[i].setClosed(true);
1401       } catch (java.beans.PropertyVetoException ex)
1402       {
1403       }
1404     }
1405     Jalview.setCurrentAlignFrame(null);
1406     System.out.println("ALL CLOSED");
1407
1408     /*
1409      * reset state of singleton objects as appropriate (clear down session state
1410      * when all windows are closed)
1411      */
1412     StructureSelectionManager ssm = StructureSelectionManager
1413             .getStructureSelectionManager(this);
1414     if (ssm != null)
1415     {
1416       ssm.resetAll();
1417     }
1418   }
1419
1420   @Override
1421   public void raiseRelated_actionPerformed(ActionEvent e)
1422   {
1423     reorderAssociatedWindows(false, false);
1424   }
1425
1426   @Override
1427   public void minimizeAssociated_actionPerformed(ActionEvent e)
1428   {
1429     reorderAssociatedWindows(true, false);
1430   }
1431
1432   void closeAssociatedWindows()
1433   {
1434     reorderAssociatedWindows(false, true);
1435   }
1436
1437   /*
1438    * (non-Javadoc)
1439    * 
1440    * @seejalview.jbgui.GDesktop#garbageCollect_actionPerformed(java.awt.event.
1441    * ActionEvent)
1442    */
1443   @Override
1444   protected void garbageCollect_actionPerformed(ActionEvent e)
1445   {
1446     // We simply collect the garbage
1447     jalview.bin.Cache.log.debug("Collecting garbage...");
1448     System.gc();
1449     jalview.bin.Cache.log.debug("Finished garbage collection.");
1450   }
1451
1452   /*
1453    * (non-Javadoc)
1454    * 
1455    * @see
1456    * jalview.jbgui.GDesktop#showMemusage_actionPerformed(java.awt.event.ActionEvent
1457    * )
1458    */
1459   @Override
1460   protected void showMemusage_actionPerformed(ActionEvent e)
1461   {
1462     desktop.showMemoryUsage(showMemusage.isSelected());
1463   }
1464
1465   /*
1466    * (non-Javadoc)
1467    * 
1468    * @see
1469    * jalview.jbgui.GDesktop#showConsole_actionPerformed(java.awt.event.ActionEvent
1470    * )
1471    */
1472   @Override
1473   protected void showConsole_actionPerformed(ActionEvent e)
1474   {
1475     showConsole(showConsole.isSelected());
1476   }
1477
1478   Console jconsole = null;
1479
1480   /**
1481    * control whether the java console is visible or not
1482    * 
1483    * @param selected
1484    */
1485   void showConsole(boolean selected)
1486   {
1487     showConsole.setSelected(selected);
1488     // TODO: decide if we should update properties file
1489     Cache.setProperty("SHOW_JAVA_CONSOLE",
1490             Boolean.valueOf(selected).toString());
1491     jconsole.setVisible(selected);
1492   }
1493
1494   void reorderAssociatedWindows(boolean minimize, boolean close)
1495   {
1496     JInternalFrame[] frames = desktop.getAllFrames();
1497     if (frames == null || frames.length < 1)
1498     {
1499       return;
1500     }
1501
1502     AlignViewportI source = null;
1503     AlignViewportI 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     new Preferences();
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 booleans: allFiles,
1700                                             // allowBackupFiles
1701     chooser.setFileView(new JalviewFileView());
1702     chooser.setDialogTitle(MessageManager.getString("label.restore_state"));
1703
1704     int value = chooser.showOpenDialog(this);
1705
1706     if (value == JalviewFileChooser.APPROVE_OPTION)
1707     {
1708       final File selectedFile = chooser.getSelectedFile();
1709       setProjectFile(selectedFile);
1710       final String choice = selectedFile.getAbsolutePath();
1711       Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
1712       new Thread(new Runnable()
1713       {
1714         @Override
1715         public void run()
1716         {
1717           setProgressBar(MessageManager.formatMessage(
1718                   "label.loading_jalview_project", new Object[]
1719                   { choice }), choice.hashCode());
1720           try
1721           {
1722             new Jalview2XML().loadJalviewAlign(choice);
1723           } catch (OutOfMemoryError oom)
1724           {
1725             new OOMWarning("Whilst loading project from " + choice, oom);
1726           } catch (Exception ex)
1727           {
1728             Cache.log.error(
1729                     "Problems whilst loading project from " + choice, ex);
1730             JvOptionPane.showMessageDialog(Desktop.desktop,
1731                     MessageManager.formatMessage(
1732                             "label.error_whilst_loading_project_from",
1733                             new Object[]
1734                             { choice }),
1735                     MessageManager.getString("label.couldnt_load_project"),
1736                     JvOptionPane.WARNING_MESSAGE);
1737           }
1738           setProgressBar(null, choice.hashCode());
1739         }
1740       }).start();
1741     }
1742   }
1743
1744   @Override
1745   public void inputSequence_actionPerformed(ActionEvent e)
1746   {
1747     new SequenceFetcher(this);
1748   }
1749
1750   JPanel progressPanel;
1751
1752   ArrayList<JPanel> fileLoadingPanels = new ArrayList<>();
1753
1754   public void startLoading(final String fileName)
1755   {
1756     if (fileLoadingCount == 0)
1757     {
1758       fileLoadingPanels.add(addProgressPanel(MessageManager
1759               .formatMessage("label.loading_file", new Object[]
1760               { fileName })));
1761     }
1762     fileLoadingCount++;
1763   }
1764
1765   private JPanel addProgressPanel(String string)
1766   {
1767     if (progressPanel == null)
1768     {
1769       progressPanel = new JPanel(new GridLayout(1, 1));
1770       totalProgressCount = 0;
1771       instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
1772     }
1773     JPanel thisprogress = new JPanel(new BorderLayout(10, 5));
1774     JProgressBar progressBar = new JProgressBar();
1775     progressBar.setIndeterminate(true);
1776
1777     thisprogress.add(new JLabel(string), BorderLayout.WEST);
1778
1779     thisprogress.add(progressBar, BorderLayout.CENTER);
1780     progressPanel.add(thisprogress);
1781     ((GridLayout) progressPanel.getLayout()).setRows(
1782             ((GridLayout) progressPanel.getLayout()).getRows() + 1);
1783     ++totalProgressCount;
1784     instance.validate();
1785     return thisprogress;
1786   }
1787
1788   int totalProgressCount = 0;
1789
1790   private void removeProgressPanel(JPanel progbar)
1791   {
1792     if (progressPanel != null)
1793     {
1794       synchronized (progressPanel)
1795       {
1796         progressPanel.remove(progbar);
1797         GridLayout gl = (GridLayout) progressPanel.getLayout();
1798         gl.setRows(gl.getRows() - 1);
1799         if (--totalProgressCount < 1)
1800         {
1801           this.getContentPane().remove(progressPanel);
1802           progressPanel = null;
1803         }
1804       }
1805     }
1806     validate();
1807   }
1808
1809   public void stopLoading()
1810   {
1811     fileLoadingCount--;
1812     if (fileLoadingCount < 1)
1813     {
1814       while (fileLoadingPanels.size() > 0)
1815       {
1816         removeProgressPanel(fileLoadingPanels.remove(0));
1817       }
1818       fileLoadingPanels.clear();
1819       fileLoadingCount = 0;
1820     }
1821     validate();
1822   }
1823
1824   public static int getViewCount(String alignmentId)
1825   {
1826     AlignmentViewport[] aps = getViewports(alignmentId);
1827     return (aps == null) ? 0 : aps.length;
1828   }
1829
1830   /**
1831    * 
1832    * @param alignmentId
1833    *          - if null, all sets are returned
1834    * @return all AlignmentPanels concerning the alignmentId sequence set
1835    */
1836   public static AlignmentPanel[] getAlignmentPanels(String alignmentId)
1837   {
1838     if (Desktop.desktop == null)
1839     {
1840       // no frames created and in headless mode
1841       // TODO: verify that frames are recoverable when in headless mode
1842       return null;
1843     }
1844     List<AlignmentPanel> aps = new ArrayList<>();
1845     AlignFrame[] frames = getAlignFrames();
1846     if (frames == null)
1847     {
1848       return null;
1849     }
1850     for (AlignFrame af : frames)
1851     {
1852       for (AlignmentPanel ap : af.alignPanels)
1853       {
1854         if (alignmentId == null
1855                 || alignmentId.equals(ap.av.getSequenceSetId()))
1856         {
1857           aps.add(ap);
1858         }
1859       }
1860     }
1861     if (aps.size() == 0)
1862     {
1863       return null;
1864     }
1865     AlignmentPanel[] vap = aps.toArray(new AlignmentPanel[aps.size()]);
1866     return vap;
1867   }
1868
1869   /**
1870    * get all the viewports on an alignment.
1871    * 
1872    * @param sequenceSetId
1873    *          unique alignment id (may be null - all viewports returned in that
1874    *          case)
1875    * @return all viewports on the alignment bound to sequenceSetId
1876    */
1877   public static AlignmentViewport[] getViewports(String sequenceSetId)
1878   {
1879     List<AlignmentViewport> viewp = new ArrayList<>();
1880     if (desktop != null)
1881     {
1882       AlignFrame[] frames = Desktop.getAlignFrames();
1883
1884       for (AlignFrame afr : frames)
1885       {
1886         if (sequenceSetId == null || afr.getViewport().getSequenceSetId()
1887                 .equals(sequenceSetId))
1888         {
1889           if (afr.alignPanels != null)
1890           {
1891             for (AlignmentPanel ap : afr.alignPanels)
1892             {
1893               if (sequenceSetId == null
1894                       || sequenceSetId.equals(ap.av.getSequenceSetId()))
1895               {
1896                 viewp.add(ap.av);
1897               }
1898             }
1899           }
1900           else
1901           {
1902             viewp.add(afr.getViewport());
1903           }
1904         }
1905       }
1906       if (viewp.size() > 0)
1907       {
1908         return viewp.toArray(new AlignmentViewport[viewp.size()]);
1909       }
1910     }
1911     return null;
1912   }
1913
1914   /**
1915    * Explode the views in the given frame into separate AlignFrame
1916    * 
1917    * @param af
1918    */
1919   public static void explodeViews(AlignFrame af)
1920   {
1921     int size = af.alignPanels.size();
1922     if (size < 2)
1923     {
1924       return;
1925     }
1926
1927     // FIXME: ideally should use UI interface API
1928     FeatureSettings viewFeatureSettings = (af.featureSettings != null
1929             && af.featureSettings.isOpen())
1930             ? af.featureSettings
1931             : 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 alignframe
1965       // (closes the old as if 'OK' was pressed)
1966       if (ap == af.alignPanel && newaf.featureSettings != null
1967               && newaf.featureSettings.isOpen()
1968               && af.alignPanel.getAlignViewport().isShowSequenceFeatures())
1969       {
1970         newaf.showFeatureSettingsUI();
1971       }
1972     }
1973
1974     af.featureSettings = null;
1975     af.alignPanels.clear();
1976     af.closeMenuItem_actionPerformed(true);
1977
1978   }
1979
1980   /**
1981    * Gather expanded views (separate AlignFrame's) with the same sequence set
1982    * identifier back in to this frame as additional views, and close the
1983    * expanded views. Note the expanded frames may themselves have multiple
1984    * views. We take the lot.
1985    * 
1986    * @param source
1987    */
1988   public void gatherViews(AlignFrame source)
1989   {
1990     source.viewport.setGatherViewsHere(true);
1991     source.viewport.setExplodedGeometry(source.getBounds());
1992     JInternalFrame[] frames = desktop.getAllFrames();
1993     String viewId = source.viewport.getSequenceSetId();
1994     for (int t = 0; t < frames.length; t++)
1995     {
1996       if (frames[t] instanceof AlignFrame && frames[t] != source)
1997       {
1998         AlignFrame af = (AlignFrame) frames[t];
1999         boolean gatherThis = false;
2000         for (int a = 0; a < af.alignPanels.size(); a++)
2001         {
2002           AlignmentPanel ap = af.alignPanels.get(a);
2003           if (viewId.equals(ap.av.getSequenceSetId()))
2004           {
2005             gatherThis = true;
2006             ap.av.setGatherViewsHere(false);
2007             ap.av.setExplodedGeometry(af.getBounds());
2008             source.addAlignmentPanel(ap, false);
2009           }
2010         }
2011
2012         if (gatherThis)
2013         {
2014           if (af.featureSettings != null && af.featureSettings.isOpen())
2015           {
2016             if (source.featureSettings == null)
2017             {
2018               // preserve the feature settings geometry for this frame
2019               source.featureSettings = af.featureSettings;
2020               source.setFeatureSettingsGeometry(
2021                       af.getFeatureSettingsGeometry());
2022             }
2023             else
2024             {
2025               // close it and forget
2026               af.featureSettings.close();
2027             }
2028           }
2029           af.alignPanels.clear();
2030           af.closeMenuItem_actionPerformed(true);
2031         }
2032       }
2033     }
2034     // refresh the feature setting UI for the source frame if it exists
2035     if (source.featureSettings != null
2036             && 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().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
2391             .put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
2392                     jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx()),
2393                     "Quit");
2394     getRootPane().getActionMap().put("Quit", new AbstractAction()
2395     {
2396       @Override
2397       public void actionPerformed(ActionEvent e)
2398       {
2399         quit();
2400       }
2401     });
2402   }
2403
2404   /**
2405    * Enable or disable 'Run Groovy script' in AlignFrame calculate menus
2406    * 
2407    * @param enabled
2408    *          true if Groovy console is open
2409    */
2410   public void enableExecuteGroovy(boolean enabled)
2411   {
2412     /*
2413      * disable opening a second Groovy console
2414      * (or re-enable when the console is closed)
2415      */
2416     groovyShell.setEnabled(!enabled);
2417
2418     AlignFrame[] alignFrames = getAlignFrames();
2419     if (alignFrames != null)
2420     {
2421       for (AlignFrame af : alignFrames)
2422       {
2423         af.setGroovyEnabled(enabled);
2424       }
2425     }
2426   }
2427
2428   /**
2429    * Progress bars managed by the IProgressIndicator method.
2430    */
2431   private Hashtable<Long, JPanel> progressBars;
2432
2433   private Hashtable<Long, IProgressIndicatorHandler> progressBarHandlers;
2434
2435   /*
2436    * (non-Javadoc)
2437    * 
2438    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
2439    */
2440   @Override
2441   public void setProgressBar(String message, long id)
2442   {
2443     if (progressBars == null)
2444     {
2445       progressBars = new Hashtable<>();
2446       progressBarHandlers = new Hashtable<>();
2447     }
2448
2449     if (progressBars.get(Long.valueOf(id)) != null)
2450     {
2451       JPanel panel = progressBars.remove(Long.valueOf(id));
2452       if (progressBarHandlers.contains(Long.valueOf(id)))
2453       {
2454         progressBarHandlers.remove(Long.valueOf(id));
2455       }
2456       removeProgressPanel(panel);
2457     }
2458     else
2459     {
2460       progressBars.put(Long.valueOf(id), addProgressPanel(message));
2461     }
2462   }
2463
2464   /*
2465    * (non-Javadoc)
2466    * 
2467    * @see jalview.gui.IProgressIndicator#registerHandler(long,
2468    * jalview.gui.IProgressIndicatorHandler)
2469    */
2470   @Override
2471   public void registerHandler(final long id,
2472           final IProgressIndicatorHandler handler)
2473   {
2474     if (progressBarHandlers == null
2475             || !progressBars.containsKey(Long.valueOf(id)))
2476     {
2477       throw new Error(MessageManager.getString(
2478               "error.call_setprogressbar_before_registering_handler"));
2479     }
2480     progressBarHandlers.put(Long.valueOf(id), handler);
2481     final JPanel progressPanel = progressBars.get(Long.valueOf(id));
2482     if (handler.canCancel())
2483     {
2484       JButton cancel = new JButton(
2485               MessageManager.getString("action.cancel"));
2486       final IProgressIndicator us = this;
2487       cancel.addActionListener(new ActionListener()
2488       {
2489
2490         @Override
2491         public void actionPerformed(ActionEvent e)
2492         {
2493           handler.cancelActivity(id);
2494           us.setProgressBar(MessageManager
2495                   .formatMessage("label.cancelled_params", new Object[]
2496                   { ((JLabel) progressPanel.getComponent(0)).getText() }),
2497                   id);
2498         }
2499       });
2500       progressPanel.add(cancel, BorderLayout.EAST);
2501     }
2502   }
2503
2504   /**
2505    * 
2506    * @return true if any progress bars are still active
2507    */
2508   @Override
2509   public boolean operationInProgress()
2510   {
2511     if (progressBars != null && progressBars.size() > 0)
2512     {
2513       return true;
2514     }
2515     return false;
2516   }
2517
2518   /**
2519    * This will return the first AlignFrame holding the given viewport instance.
2520    * It will break if there are more than one AlignFrames viewing a particular
2521    * av.
2522    * 
2523    * @param viewport
2524    * @return alignFrame for viewport
2525    */
2526   public static AlignFrame getAlignFrameFor(AlignViewportI viewport)
2527   {
2528     if (desktop != null)
2529     {
2530       AlignmentPanel[] aps = getAlignmentPanels(
2531               viewport.getSequenceSetId());
2532       for (int panel = 0; aps != null && panel < aps.length; panel++)
2533       {
2534         if (aps[panel] != null && aps[panel].av == viewport)
2535         {
2536           return aps[panel].alignFrame;
2537         }
2538       }
2539     }
2540     return null;
2541   }
2542
2543   public VamsasApplication getVamsasApplication()
2544   {
2545     // TODO: JAL-3311 remove remaining code from Jalview relating to VAMSAS
2546     return null;
2547
2548   }
2549
2550   /**
2551    * flag set if jalview GUI is being operated programmatically
2552    */
2553   private boolean inBatchMode = false;
2554
2555   /**
2556    * check if jalview GUI is being operated programmatically
2557    * 
2558    * @return inBatchMode
2559    */
2560   public boolean isInBatchMode()
2561   {
2562     return inBatchMode;
2563   }
2564
2565   /**
2566    * set flag if jalview GUI is being operated programmatically
2567    * 
2568    * @param inBatchMode
2569    */
2570   public void setInBatchMode(boolean inBatchMode)
2571   {
2572     this.inBatchMode = inBatchMode;
2573   }
2574
2575   public void startServiceDiscovery()
2576   {
2577     startServiceDiscovery(false);
2578   }
2579
2580   public void startServiceDiscovery(boolean blocking)
2581   {
2582     boolean alive = true;
2583     Thread t0 = null, t1 = null, t2 = null;
2584     // JAL-940 - JALVIEW 1 services are now being EOLed as of JABA 2.1 release
2585     if (true)
2586     {
2587       // todo: changesupport handlers need to be transferred
2588       if (discoverer == null)
2589       {
2590         discoverer = new jalview.ws.jws1.Discoverer();
2591         // register PCS handler for desktop.
2592         discoverer.addPropertyChangeListener(changeSupport);
2593       }
2594       // JAL-940 - disabled JWS1 service configuration - always start discoverer
2595       // until we phase out completely
2596       (t0 = new Thread(discoverer)).start();
2597     }
2598
2599     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
2600     {
2601       t2 = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2602               .startDiscoverer(changeSupport);
2603     }
2604     Thread t3 = null;
2605     {
2606       // start slivka discovery
2607       t3 = new Thread(jalview.ws.slivkaws.SlivkaWSDiscoverer.getInstance());
2608       t3.start();
2609     }
2610     if (blocking)
2611     {
2612       while (alive)
2613       {
2614         try
2615         {
2616           Thread.sleep(15);
2617         } catch (Exception e)
2618         {
2619         }
2620         alive = (t1 != null && t1.isAlive()) || (t2 != null && t2.isAlive())
2621                 || (t3 != null && t3.isAlive())
2622                 || (t0 != null && t0.isAlive());
2623       }
2624     }
2625   }
2626
2627   /**
2628    * called to check if the service discovery process completed successfully.
2629    * 
2630    * @param evt
2631    */
2632   protected void JalviewServicesChanged(PropertyChangeEvent evt)
2633   {
2634     if (evt.getNewValue() == null || evt.getNewValue() instanceof Vector)
2635     {
2636       final String ermsg = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
2637               .getErrorMessages();
2638       if (ermsg != null)
2639       {
2640         if (Cache.getDefault("SHOW_WSDISCOVERY_ERRORS", true))
2641         {
2642           if (serviceChangedDialog == null)
2643           {
2644             // only run if we aren't already displaying one of these.
2645             addDialogThread(serviceChangedDialog = new Runnable()
2646             {
2647               @Override
2648               public void run()
2649               {
2650
2651                 /*
2652                  * JalviewDialog jd =new JalviewDialog() {
2653                  * 
2654                  * @Override protected void cancelPressed() { // TODO
2655                  * Auto-generated method stub
2656                  * 
2657                  * }@Override protected void okPressed() { // TODO
2658                  * Auto-generated method stub
2659                  * 
2660                  * }@Override protected void raiseClosed() { // TODO
2661                  * Auto-generated method stub
2662                  * 
2663                  * } }; jd.initDialogFrame(new
2664                  * JLabel("<html><table width=\"450\"><tr><td>" + ermsg +
2665                  * "<br/>It may be that you have invalid JABA URLs in your web service preferences,"
2666                  * + " or mis-configured HTTP proxy settings.<br/>" +
2667                  * "Check the <em>Connections</em> and <em>Web services</em> tab of the"
2668                  * +
2669                  * " Tools->Preferences dialog box to change them.</td></tr></table></html>"
2670                  * ), true, true, "Web Service Configuration Problem", 450,
2671                  * 400);
2672                  * 
2673                  * jd.waitForInput();
2674                  */
2675                 JvOptionPane.showConfirmDialog(Desktop.desktop,
2676                         new JLabel("<html><table width=\"450\"><tr><td>"
2677                                 + ermsg + "</td></tr></table>"
2678                                 + "<p>It may be that you have invalid JABA URLs<br/>in your web service preferences,"
2679                                 + "<br>or as a command-line argument, or mis-configured HTTP proxy settings.</p>"
2680                                 + "<p>Check the <em>Connections</em> and <em>Web services</em> tab<br/>of the"
2681                                 + " Tools->Preferences dialog box to change them.</p></html>"),
2682                         "Web Service Configuration Problem",
2683                         JvOptionPane.DEFAULT_OPTION,
2684                         JvOptionPane.ERROR_MESSAGE);
2685                 serviceChangedDialog = null;
2686
2687               }
2688             });
2689           }
2690         }
2691         else
2692         {
2693           Cache.log.error(
2694                   "Errors reported by JABA discovery service. Check web services preferences.\n"
2695                           + ermsg);
2696         }
2697       }
2698     }
2699   }
2700
2701   private Runnable serviceChangedDialog = null;
2702
2703   /**
2704    * start a thread to open a URL in the configured browser. Pops up a warning
2705    * dialog to the user if there is an exception when calling out to the browser
2706    * to open the URL.
2707    * 
2708    * @param url
2709    */
2710   public static void showUrl(final String url)
2711   {
2712     showUrl(url, Desktop.instance);
2713   }
2714
2715   /**
2716    * Like showUrl but allows progress handler to be specified
2717    * 
2718    * @param url
2719    * @param progress
2720    *          (null) or object implementing IProgressIndicator
2721    */
2722   public static void showUrl(final String url,
2723           final IProgressIndicator progress)
2724   {
2725     new Thread(new Runnable()
2726     {
2727       @Override
2728       public void run()
2729       {
2730         try
2731         {
2732           if (progress != null)
2733           {
2734             progress.setProgressBar(MessageManager
2735                     .formatMessage("status.opening_params", new Object[]
2736                     { url }), this.hashCode());
2737           }
2738           jalview.util.BrowserLauncher.openURL(url);
2739         } catch (Exception ex)
2740         {
2741           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2742                   MessageManager
2743                           .getString("label.web_browser_not_found_unix"),
2744                   MessageManager.getString("label.web_browser_not_found"),
2745                   JvOptionPane.WARNING_MESSAGE);
2746
2747           ex.printStackTrace();
2748         }
2749         if (progress != null)
2750         {
2751           progress.setProgressBar(null, this.hashCode());
2752         }
2753       }
2754     }).start();
2755   }
2756
2757   public static WsParamSetManager wsparamManager = null;
2758
2759   public static ParamManager getUserParameterStore()
2760   {
2761     if (wsparamManager == null)
2762     {
2763       wsparamManager = new WsParamSetManager();
2764     }
2765     return wsparamManager;
2766   }
2767
2768   /**
2769    * static hyperlink handler proxy method for use by Jalview's internal windows
2770    * 
2771    * @param e
2772    */
2773   public static void hyperlinkUpdate(HyperlinkEvent e)
2774   {
2775     if (e.getEventType() == EventType.ACTIVATED)
2776     {
2777       String url = null;
2778       try
2779       {
2780         url = e.getURL().toString();
2781         Desktop.showUrl(url);
2782       } catch (Exception x)
2783       {
2784         if (url != null)
2785         {
2786           if (Cache.log != null)
2787           {
2788             Cache.log.error("Couldn't handle string " + url + " as a URL.");
2789           }
2790           else
2791           {
2792             System.err.println(
2793                     "Couldn't handle string " + url + " as a URL.");
2794           }
2795         }
2796         // ignore any exceptions due to dud links.
2797       }
2798
2799     }
2800   }
2801
2802   /**
2803    * single thread that handles display of dialogs to user.
2804    */
2805   ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
2806
2807   /**
2808    * flag indicating if dialogExecutor should try to acquire a permit
2809    */
2810   private volatile boolean dialogPause = true;
2811
2812   /**
2813    * pause the queue
2814    */
2815   private java.util.concurrent.Semaphore block = new Semaphore(0);
2816
2817   private static groovy.ui.Console groovyConsole;
2818
2819   /**
2820    * add another dialog thread to the queue
2821    * 
2822    * @param prompter
2823    */
2824   public void addDialogThread(final Runnable prompter)
2825   {
2826     dialogExecutor.submit(new Runnable()
2827     {
2828       @Override
2829       public void run()
2830       {
2831         if (dialogPause)
2832         {
2833           try
2834           {
2835             block.acquire();
2836           } catch (InterruptedException x)
2837           {
2838           }
2839           ;
2840         }
2841         if (instance == null)
2842         {
2843           return;
2844         }
2845         try
2846         {
2847           SwingUtilities.invokeAndWait(prompter);
2848         } catch (Exception q)
2849         {
2850           Cache.log.warn("Unexpected Exception in dialog thread.", q);
2851         }
2852       }
2853     });
2854   }
2855
2856   public void startDialogQueue()
2857   {
2858     // set the flag so we don't pause waiting for another permit and semaphore
2859     // the current task to begin
2860     dialogPause = false;
2861     block.release();
2862   }
2863
2864   @Override
2865   protected void snapShotWindow_actionPerformed(ActionEvent e)
2866   {
2867     invalidate();
2868     File of;
2869     ImageMaker im = new jalview.util.ImageMaker(
2870             this, ImageMaker.TYPE.EPS, "View of Desktop", getWidth(),
2871             getHeight(), of = new File("Jalview_snapshot"
2872                     + System.currentTimeMillis() + ".eps"),
2873             "View of desktop", null, 0, false);
2874     try
2875     {
2876       paintAll(im.getGraphics());
2877       im.writeImage();
2878     } catch (Exception q)
2879     {
2880       Cache.log.error("Couldn't write snapshot to " + of.getAbsolutePath(),
2881               q);
2882       return;
2883     }
2884     Cache.log.info("Successfully written snapshot to file "
2885             + of.getAbsolutePath());
2886   }
2887
2888   /**
2889    * Explode the views in the given SplitFrame into separate SplitFrame windows.
2890    * This respects (remembers) any previous 'exploded geometry' i.e. the size
2891    * and location last time the view was expanded (if any). However it does not
2892    * remember the split pane divider location - this is set to match the
2893    * 'exploding' frame.
2894    * 
2895    * @param sf
2896    */
2897   public void explodeViews(SplitFrame sf)
2898   {
2899     AlignFrame oldTopFrame = (AlignFrame) sf.getTopFrame();
2900     AlignFrame oldBottomFrame = (AlignFrame) sf.getBottomFrame();
2901     List<? extends AlignmentViewPanel> topPanels = oldTopFrame
2902             .getAlignPanels();
2903     List<? extends AlignmentViewPanel> bottomPanels = oldBottomFrame
2904             .getAlignPanels();
2905     int viewCount = topPanels.size();
2906     if (viewCount < 2)
2907     {
2908       return;
2909     }
2910
2911     /*
2912      * Processing in reverse order works, forwards order leaves the first panels
2913      * not visible. I don't know why!
2914      */
2915     for (int i = viewCount - 1; i >= 0; i--)
2916     {
2917       /*
2918        * Make new top and bottom frames. These take over the respective
2919        * AlignmentPanel objects, including their AlignmentViewports, so the
2920        * cdna/protein relationships between the viewports is carried over to the
2921        * new split frames.
2922        * 
2923        * explodedGeometry holds the (x, y) position of the previously exploded
2924        * SplitFrame, and the (width, height) of the AlignFrame component
2925        */
2926       AlignmentPanel topPanel = (AlignmentPanel) topPanels.get(i);
2927       AlignFrame newTopFrame = new AlignFrame(topPanel);
2928       newTopFrame.setSize(oldTopFrame.getSize());
2929       newTopFrame.setVisible(true);
2930       Rectangle geometry = ((AlignViewport) topPanel.getAlignViewport())
2931               .getExplodedGeometry();
2932       if (geometry != null)
2933       {
2934         newTopFrame.setSize(geometry.getSize());
2935       }
2936
2937       AlignmentPanel bottomPanel = (AlignmentPanel) bottomPanels.get(i);
2938       AlignFrame newBottomFrame = new AlignFrame(bottomPanel);
2939       newBottomFrame.setSize(oldBottomFrame.getSize());
2940       newBottomFrame.setVisible(true);
2941       geometry = ((AlignViewport) bottomPanel.getAlignViewport())
2942               .getExplodedGeometry();
2943       if (geometry != null)
2944       {
2945         newBottomFrame.setSize(geometry.getSize());
2946       }
2947
2948       topPanel.av.setGatherViewsHere(false);
2949       bottomPanel.av.setGatherViewsHere(false);
2950       JInternalFrame splitFrame = new SplitFrame(newTopFrame,
2951               newBottomFrame);
2952       if (geometry != null)
2953       {
2954         splitFrame.setLocation(geometry.getLocation());
2955       }
2956       Desktop.addInternalFrame(splitFrame, sf.getTitle(), -1, -1);
2957     }
2958
2959     /*
2960      * Clear references to the panels (now relocated in the new SplitFrames)
2961      * before closing the old SplitFrame.
2962      */
2963     topPanels.clear();
2964     bottomPanels.clear();
2965     sf.close();
2966   }
2967
2968   /**
2969    * Gather expanded split frames, sharing the same pairs of sequence set ids,
2970    * back into the given SplitFrame as additional views. Note that the gathered
2971    * frames may themselves have multiple views.
2972    * 
2973    * @param source
2974    */
2975   public void gatherViews(GSplitFrame source)
2976   {
2977     /*
2978      * special handling of explodedGeometry for a view within a SplitFrame: - it
2979      * holds the (x, y) position of the enclosing SplitFrame, and the (width,
2980      * height) of the AlignFrame component
2981      */
2982     AlignFrame myTopFrame = (AlignFrame) source.getTopFrame();
2983     AlignFrame myBottomFrame = (AlignFrame) source.getBottomFrame();
2984     myTopFrame.viewport.setExplodedGeometry(new Rectangle(source.getX(),
2985             source.getY(), myTopFrame.getWidth(), myTopFrame.getHeight()));
2986     myBottomFrame.viewport
2987             .setExplodedGeometry(new Rectangle(source.getX(), source.getY(),
2988                     myBottomFrame.getWidth(), myBottomFrame.getHeight()));
2989     myTopFrame.viewport.setGatherViewsHere(true);
2990     myBottomFrame.viewport.setGatherViewsHere(true);
2991     String topViewId = myTopFrame.viewport.getSequenceSetId();
2992     String bottomViewId = myBottomFrame.viewport.getSequenceSetId();
2993
2994     JInternalFrame[] frames = desktop.getAllFrames();
2995     for (JInternalFrame frame : frames)
2996     {
2997       if (frame instanceof SplitFrame && frame != source)
2998       {
2999         SplitFrame sf = (SplitFrame) frame;
3000         AlignFrame topFrame = (AlignFrame) sf.getTopFrame();
3001         AlignFrame bottomFrame = (AlignFrame) sf.getBottomFrame();
3002         boolean gatherThis = false;
3003         for (int a = 0; a < topFrame.alignPanels.size(); a++)
3004         {
3005           AlignmentPanel topPanel = topFrame.alignPanels.get(a);
3006           AlignmentPanel bottomPanel = bottomFrame.alignPanels.get(a);
3007           if (topViewId.equals(topPanel.av.getSequenceSetId())
3008                   && bottomViewId.equals(bottomPanel.av.getSequenceSetId()))
3009           {
3010             gatherThis = true;
3011             topPanel.av.setGatherViewsHere(false);
3012             bottomPanel.av.setGatherViewsHere(false);
3013             topPanel.av.setExplodedGeometry(
3014                     new Rectangle(sf.getLocation(), topFrame.getSize()));
3015             bottomPanel.av.setExplodedGeometry(
3016                     new Rectangle(sf.getLocation(), bottomFrame.getSize()));
3017             myTopFrame.addAlignmentPanel(topPanel, false);
3018             myBottomFrame.addAlignmentPanel(bottomPanel, false);
3019           }
3020         }
3021
3022         if (gatherThis)
3023         {
3024           topFrame.getAlignPanels().clear();
3025           bottomFrame.getAlignPanels().clear();
3026           sf.close();
3027         }
3028       }
3029     }
3030
3031     /*
3032      * The dust settles...give focus to the tab we did this from.
3033      */
3034     myTopFrame.setDisplayedView(myTopFrame.alignPanel);
3035   }
3036
3037   public static groovy.ui.Console getGroovyConsole()
3038   {
3039     return groovyConsole;
3040   }
3041
3042   /**
3043    * handles the payload of a drag and drop event.
3044    * 
3045    * TODO refactor to desktop utilities class
3046    * 
3047    * @param files
3048    *          - Data source strings extracted from the drop event
3049    * @param protocols
3050    *          - protocol for each data source extracted from the drop event
3051    * @param evt
3052    *          - the drop event
3053    * @param t
3054    *          - the payload from the drop event
3055    * @throws Exception
3056    */
3057   public static void transferFromDropTarget(List<String> files,
3058           List<DataSourceType> protocols, DropTargetDropEvent evt,
3059           Transferable t) throws Exception
3060   {
3061
3062     DataFlavor uriListFlavor = new DataFlavor(
3063             "text/uri-list;class=java.lang.String"), urlFlavour = null;
3064     try
3065     {
3066       urlFlavour = new DataFlavor(
3067               "application/x-java-url; class=java.net.URL");
3068     } catch (ClassNotFoundException cfe)
3069     {
3070       Cache.log.debug("Couldn't instantiate the URL dataflavor.", cfe);
3071     }
3072
3073     if (urlFlavour != null && t.isDataFlavorSupported(urlFlavour))
3074     {
3075
3076       try
3077       {
3078         java.net.URL url = (URL) t.getTransferData(urlFlavour);
3079         // nb: java 8 osx bug https://bugs.openjdk.java.net/browse/JDK-8156099
3080         // means url may be null.
3081         if (url != null)
3082         {
3083           protocols.add(DataSourceType.URL);
3084           files.add(url.toString());
3085           Cache.log.debug("Drop handled as URL dataflavor "
3086                   + files.get(files.size() - 1));
3087           return;
3088         }
3089         else
3090         {
3091           if (Platform.isAMac())
3092           {
3093             System.err.println(
3094                     "Please ignore plist error - occurs due to problem with java 8 on OSX");
3095           }
3096           ;
3097         }
3098       } catch (Throwable ex)
3099       {
3100         Cache.log.debug("URL drop handler failed.", ex);
3101       }
3102     }
3103     if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
3104     {
3105       // Works on Windows and MacOSX
3106       Cache.log.debug("Drop handled as javaFileListFlavor");
3107       for (Object file : (List) t
3108               .getTransferData(DataFlavor.javaFileListFlavor))
3109       {
3110         files.add(((File) file).toString());
3111         protocols.add(DataSourceType.FILE);
3112       }
3113     }
3114     else
3115     {
3116       // Unix like behaviour
3117       boolean added = false;
3118       String data = null;
3119       if (t.isDataFlavorSupported(uriListFlavor))
3120       {
3121         Cache.log.debug("Drop handled as uriListFlavor");
3122         // This is used by Unix drag system
3123         data = (String) t.getTransferData(uriListFlavor);
3124       }
3125       if (data == null)
3126       {
3127         // fallback to text: workaround - on OSX where there's a JVM bug
3128         Cache.log.debug("standard URIListFlavor failed. Trying text");
3129         // try text fallback
3130         DataFlavor textDf = new DataFlavor(
3131                 "text/plain;class=java.lang.String");
3132         if (t.isDataFlavorSupported(textDf))
3133         {
3134           data = (String) t.getTransferData(textDf);
3135         }
3136
3137         Cache.log.debug("Plain text drop content returned "
3138                 + (data == null ? "Null - failed" : data));
3139
3140       }
3141       if (data != null)
3142       {
3143         while (protocols.size() < files.size())
3144         {
3145           Cache.log.debug("Adding missing FILE protocol for "
3146                   + files.get(protocols.size()));
3147           protocols.add(DataSourceType.FILE);
3148         }
3149         for (java.util.StringTokenizer st = new java.util.StringTokenizer(
3150                 data, "\r\n"); st.hasMoreTokens();)
3151         {
3152           added = true;
3153           String s = st.nextToken();
3154           if (s.startsWith("#"))
3155           {
3156             // the line is a comment (as per the RFC 2483)
3157             continue;
3158           }
3159           java.net.URI uri = new java.net.URI(s);
3160           if (uri.getScheme().toLowerCase().startsWith("http"))
3161           {
3162             protocols.add(DataSourceType.URL);
3163             files.add(uri.toString());
3164           }
3165           else
3166           {
3167             // otherwise preserve old behaviour: catch all for file objects
3168             java.io.File file = new java.io.File(uri);
3169             protocols.add(DataSourceType.FILE);
3170             files.add(file.toString());
3171           }
3172         }
3173       }
3174
3175       if (Cache.log.isDebugEnabled())
3176       {
3177         if (data == null || !added)
3178         {
3179
3180           if (t.getTransferDataFlavors() != null
3181                   && t.getTransferDataFlavors().length > 0)
3182           {
3183             Cache.log.debug(
3184                     "Couldn't resolve drop data. Here are the supported flavors:");
3185             for (DataFlavor fl : t.getTransferDataFlavors())
3186             {
3187               Cache.log.debug(
3188                       "Supported transfer dataflavor: " + fl.toString());
3189               Object df = t.getTransferData(fl);
3190               if (df != null)
3191               {
3192                 Cache.log.debug("Retrieves: " + df);
3193               }
3194               else
3195               {
3196                 Cache.log.debug("Retrieved nothing");
3197               }
3198             }
3199           }
3200           else
3201           {
3202             Cache.log.debug("Couldn't resolve dataflavor for drop: "
3203                     + t.toString());
3204           }
3205         }
3206       }
3207     }
3208     if (Platform.isWindows())
3209
3210     {
3211       Cache.log.debug("Scanning dropped content for Windows Link Files");
3212
3213       // resolve any .lnk files in the file drop
3214       for (int f = 0; f < files.size(); f++)
3215       {
3216         String source = files.get(f).toLowerCase();
3217         if (protocols.get(f).equals(DataSourceType.FILE)
3218                 && (source.endsWith(".lnk") || source.endsWith(".url")
3219                         || source.endsWith(".site")))
3220         {
3221           try
3222           {
3223             File lf = new File(files.get(f));
3224             // process link file to get a URL
3225             Cache.log.debug("Found potential link file: " + lf);
3226             WindowsShortcut wscfile = new WindowsShortcut(lf);
3227             String fullname = wscfile.getRealFilename();
3228             protocols.set(f, FormatAdapter.checkProtocol(fullname));
3229             files.set(f, fullname);
3230             Cache.log.debug("Parsed real filename " + fullname
3231                     + " to extract protocol: " + protocols.get(f));
3232           } catch (Exception ex)
3233           {
3234             Cache.log.error(
3235                     "Couldn't parse " + files.get(f) + " as a link file.",
3236                     ex);
3237           }
3238         }
3239       }
3240     }
3241   }
3242
3243   /**
3244    * Sets the Preferences property for experimental features to True or False
3245    * depending on the state of the controlling menu item
3246    */
3247   @Override
3248   protected void showExperimental_actionPerformed(boolean selected)
3249   {
3250     Cache.setProperty(EXPERIMENTAL_FEATURES, Boolean.toString(selected));
3251   }
3252
3253   /**
3254    * Answers a (possibly empty) list of any structure viewer frames (currently
3255    * for either Jmol or Chimera) which are currently open. This may optionally
3256    * be restricted to viewers of a specified class, or viewers linked to a
3257    * specified alignment panel.
3258    * 
3259    * @param apanel
3260    *          if not null, only return viewers linked to this panel
3261    * @param structureViewerClass
3262    *          if not null, only return viewers of this class
3263    * @return
3264    */
3265   public List<StructureViewerBase> getStructureViewers(
3266           AlignmentPanel apanel,
3267           Class<? extends StructureViewerBase> structureViewerClass)
3268   {
3269     List<StructureViewerBase> result = new ArrayList<>();
3270     JInternalFrame[] frames = Desktop.instance.getAllFrames();
3271
3272     for (JInternalFrame frame : frames)
3273     {
3274       if (frame instanceof StructureViewerBase)
3275       {
3276         if (structureViewerClass == null
3277                 || structureViewerClass.isInstance(frame))
3278         {
3279           if (apanel == null
3280                   || ((StructureViewerBase) frame).isLinkedWith(apanel))
3281           {
3282             result.add((StructureViewerBase) frame);
3283           }
3284         }
3285       }
3286     }
3287     return result;
3288   }
3289 }