JAL-3210 Barebones gradle/buildship/eclipse. See README
[jalview.git] / src / jalview / gui / Desktop.java
index 9fbfb83..9dd46d6 100644 (file)
@@ -22,9 +22,6 @@ package jalview.gui;
 
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
-import jalview.api.StructureSelectionManagerProvider;
-import jalview.bin.ApplicationSingletonProvider;
-import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.bin.Cache;
 import jalview.bin.Jalview;
 import jalview.gui.ImageExporter.ImageWriterI;
@@ -39,7 +36,6 @@ import jalview.io.FormatAdapter;
 import jalview.io.IdentifyFile;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
-import jalview.jbgui.GDesktop;
 import jalview.jbgui.GSplitFrame;
 import jalview.jbgui.GStructureViewer;
 import jalview.project.Jalview2XML;
@@ -51,7 +47,6 @@ import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.util.UrlConstants;
 import jalview.viewmodel.AlignmentViewport;
-import jalview.ws.jws1.Discoverer;
 import jalview.ws.params.ParamManager;
 import jalview.ws.utils.UrlDownloadClient;
 
@@ -87,9 +82,11 @@ import java.beans.PropertyChangeListener;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.ListIterator;
@@ -111,7 +108,6 @@ import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JDesktopPane;
-import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
 import javax.swing.JLabel;
 import javax.swing.JMenuItem;
@@ -137,28 +133,30 @@ import org.stackoverflowusers.file.WindowsShortcut;
  * @author $author$
  * @version $Revision: 1.155 $
  */
-@SuppressWarnings("serial")
-public class Desktop extends GDesktop
+public class Desktop extends jalview.jbgui.GDesktop
         implements DropTargetListener, ClipboardOwner, IProgressIndicator,
-        StructureSelectionManagerProvider, ApplicationSingletonI
+        jalview.api.StructureSelectionManagerProvider
 {
+  private static int DEFAULT_MIN_WIDTH = 300;
 
-  private final static int DEFAULT_MIN_WIDTH = 300;
+  private static int DEFAULT_MIN_HEIGHT = 250;
 
-  private final static int DEFAULT_MIN_HEIGHT = 250;
+  private static int ALIGN_FRAME_DEFAULT_MIN_WIDTH = 600;
 
-  private final static int ALIGN_FRAME_DEFAULT_MIN_WIDTH = 600;
+  private static int ALIGN_FRAME_DEFAULT_MIN_HEIGHT = 70;
 
-  private final static int ALIGN_FRAME_DEFAULT_MIN_HEIGHT = 70;
+  private static final String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES";
 
-  private final static String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES";
+  protected static final String CONFIRM_KEYBOARD_QUIT = "CONFIRM_KEYBOARD_QUIT";
+
+  public static HashMap<String, FileWriter> savingFiles = new HashMap<>();
 
   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
 
   /**
    * news reader - null if it was never started.
    */
-  BlogReader jvnews = null;
+  private BlogReader jvnews = null;
 
   private File projectFile;
 
@@ -197,16 +195,16 @@ public class Desktop extends GDesktop
             listener);
   }
 
-  public static MyDesktopPane getDesktopPane()
-  {
-    Desktop desktop = Desktop.getInstance();
-    return desktop == null ? null : desktop.desktopPane;
-  }
+  /** Singleton Desktop instance */
+  public static Desktop instance;
+
+  public static MyDesktopPane desktop;
 
-  public static StructureSelectionManager getStructureSelectionManager()
+  public static MyDesktopPane getDesktop()
   {
-    return StructureSelectionManager
-            .getStructureSelectionManager(getInstance());
+    // BH 2018 could use currentThread() here as a reference to a
+    // Hashtable<Thread, MyDesktopPane> in JavaScript
+    return desktop;
   }
 
   static int openFrameCount = 0;
@@ -215,22 +213,13 @@ public class Desktop extends GDesktop
 
   static final int yOffset = 30;
 
-  public Discoverer discoverer;
-
-  public Object[] jalviewClipboard;
-
-  public boolean internalCopy = false;
-
-  private static int fileLoadingCount = 0;
+  public static jalview.ws.jws1.Discoverer discoverer;
 
-  public JInternalFrame conservationSlider;
+  public static Object[] jalviewClipboard;
 
-  public JInternalFrame PIDSlider;
+  public static boolean internalCopy = false;
 
-  /**
-   * just an instance (for testng, probably); no actual frames
-   */
-  private boolean instanceOnly;
+  static int fileLoadingCount = 0;
 
   class MyDesktopManager implements DesktopManager
   {
@@ -251,7 +240,7 @@ public class Desktop extends GDesktop
       } catch (NullPointerException npe)
       {
         Point p = getMousePosition();
-        showPasteMenu(p.x, p.y);
+        instance.showPasteMenu(p.x, p.y);
       }
     }
 
@@ -299,14 +288,14 @@ public class Desktop extends GDesktop
     public void endDraggingFrame(JComponent f)
     {
       delegate.endDraggingFrame(f);
-      desktopPane.repaint();
+      desktop.repaint();
     }
 
     @Override
     public void endResizingFrame(JComponent f)
     {
       delegate.endResizingFrame(f);
-      desktopPane.repaint();
+      desktop.repaint();
     }
 
     @Override
@@ -355,86 +344,100 @@ public class Desktop extends GDesktop
 
   }
 
-  public MyDesktopPane desktopPane;
-
-  /**
-   * Answers an 'application scope' singleton instance of this class. Separate
-   * SwingJS 'applets' running in the same browser page will each have a
-   * distinct instance of Desktop.
-   * 
-   * @return
-   */
-  public static Desktop getInstance()
-  {
-    return Jalview.isHeadlessMode() ? null
-            : (Desktop) ApplicationSingletonProvider
-                    .getInstance(Desktop.class);
-  }
-
-  /**
-   * For testing.
-   * 
-   * @param forInstance
-   */
-  public Desktop(boolean forInstance)
-  {
-    instanceOnly = true;
-  }
-  
   /**
-   * Private constructor enforces singleton pattern. It is called by reflection
-   * from ApplicationSingletonProvider.getInstance().
+   * Creates a new Desktop object.
    */
-  @SuppressWarnings("unused")
-  private Desktop()
+  public Desktop()
   {
-    try
-    {
+    super();
     /**
      * A note to implementors. It is ESSENTIAL that any activities that might
      * block are spawned off as threads rather than waited for during this
      * constructor.
      */
+    instance = this;
     if (!Platform.isJS())
     {
       doVamsasClientCheck();
     }
-    
+
     doConfigureStructurePrefs();
     setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
-    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+    /*
+    if (!Platform.isAMac())
+    {
+      // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+    }
+    else
+    {
+     this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
+    }
+    */
+
+    try
+    {
+           if (!Platform.isJS())
+                   /*
+                    * @j2sIgnore
+                    */
+           {
+             APQHandlers.setAPQHandlers(this);
+           }
+    } catch (Exception e)
+    {
+      System.out.println("Cannot set APQHandlers");
+      // e.printStackTrace();
+    } catch (Throwable t)
+    {
+      System.out.println("Cannot set APQHandlers");
+      // t.printStackTrace();
+    }
+
+
+    addWindowListener(new WindowAdapter()
+    {
+
+      @Override
+      public void windowClosing(WindowEvent ev)
+      {
+        quit();
+      }
+    });
+
     boolean selmemusage = jalview.bin.Cache.getDefault("SHOW_MEMUSAGE",
             false);
+
     boolean showjconsole = jalview.bin.Cache.getDefault("SHOW_JAVA_CONSOLE",
             false);
-    desktopPane = new MyDesktopPane(selmemusage);
-    
+    desktop = new MyDesktopPane(selmemusage);
+
     showMemusage.setSelected(selmemusage);
-    desktopPane.setBackground(Color.white);
+    desktop.setBackground(Color.white);
+
     getContentPane().setLayout(new BorderLayout());
     // alternate config - have scrollbars - see notes in JAL-153
     // JScrollPane sp = new JScrollPane();
     // sp.getViewport().setView(desktop);
     // getContentPane().add(sp, BorderLayout.CENTER);
-    
+
     // BH 2018 - just an experiment to try unclipped JInternalFrames.
     if (Platform.isJS())
     {
       getRootPane().putClientProperty("swingjs.overflow.hidden", "false");
     }
-    
-    getContentPane().add(desktopPane, BorderLayout.CENTER);
-    desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
-    
+
+    getContentPane().add(desktop, BorderLayout.CENTER);
+    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
+
     // This line prevents Windows Look&Feel resizing all new windows to maximum
     // if previous window was maximised
-    desktopPane.setDesktopManager(new MyDesktopManager(
+    desktop.setDesktopManager(new MyDesktopManager(
             (Platform.isWindowsAndNotJS() ? new DefaultDesktopManager()
                     : Platform.isAMacAndNotJS()
                             ? new AquaInternalFrameManager(
-                                    desktopPane.getDesktopManager())
-                            : desktopPane.getDesktopManager())));
-    
+                                    desktop.getDesktopManager())
+                            : desktop.getDesktopManager())));
+
     Rectangle dims = getLastKnownDimensions("");
     if (dims != null)
     {
@@ -447,7 +450,7 @@ public class Desktop extends GDesktop
       int yPos = Math.max(5, (screenSize.height - 650) / 2);
       setBounds(xPos, yPos, 900, 650);
     }
-    
+
     if (!Platform.isJS())
     /**
      * Java only
@@ -455,7 +458,7 @@ public class Desktop extends GDesktop
      * @j2sIgnore
      */
     {
-    
+
       jconsole = new Console(this, showjconsole);
       // add essential build information
       jconsole.setHeader("Jalview Version: "
@@ -467,20 +470,27 @@ public class Desktop extends GDesktop
               + "Java version: " + System.getProperty("java.version") + "\n"
               + System.getProperty("os.arch") + " "
               + System.getProperty("os.name") + " "
-              + System.getProperty("os.version"));
-    
+              + System.getProperty("os.version")
+              + (jalview.bin.Cache.getProperty("VERSION").equals("DEVELOPMENT")
+                              ? "\nJava path:"
+                                      + System.getProperty(
+                                              "java.home")
+                                      + File.separator + "bin"
+                                      + File.separator + "java"
+                              : ""));
+
       showConsole(showjconsole);
-    
+
       showNews.setVisible(false);
-    
+
       experimentalFeatures.setSelected(showExperimental());
-    
+
       getIdentifiersOrgData();
-    
+
       checkURLLinks();
-    
+
       // Spawn a thread that shows the splashscreen
-    
+
       SwingUtilities.invokeLater(new Runnable()
       {
         @Override
@@ -489,10 +499,9 @@ public class Desktop extends GDesktop
           new SplashScreen();
         }
       });
-    
+
       // Thread off a new instance of the file chooser - this reduces the time
-      // it
-      // takes to open it later on.
+      // it takes to open it later on.
       new Thread(new Runnable()
       {
         @Override
@@ -509,7 +518,7 @@ public class Desktop extends GDesktop
       changeSupport.addJalviewPropertyChangeListener("services",
               new PropertyChangeListener()
               {
-    
+
                 @Override
                 public void propertyChange(PropertyChangeEvent evt)
                 {
@@ -517,13 +526,13 @@ public class Desktop extends GDesktop
                           + evt.getNewValue());
                   JalviewServicesChanged(evt);
                 }
-    
+
               });
-    
+
     }
-    
-    this.setDropTarget(new java.awt.dnd.DropTarget(desktopPane, this));
-    
+
+    this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
+
     this.addWindowListener(new WindowAdapter()
     {
       @Override
@@ -532,7 +541,7 @@ public class Desktop extends GDesktop
         quit();
       }
     });
-    
+
     MouseAdapter ma;
     this.addMouseListener(ma = new MouseAdapter()
     {
@@ -544,7 +553,7 @@ public class Desktop extends GDesktop
           showPasteMenu(evt.getX(), evt.getY());
         }
       }
-    
+
       @Override
       public void mouseReleased(MouseEvent evt)
       {
@@ -554,11 +563,8 @@ public class Desktop extends GDesktop
         }
       }
     });
-    desktopPane.addMouseListener(ma);
-    } catch (Throwable t)
-    {
-      t.printStackTrace();
-    }
+    desktop.addMouseListener(ma);
+
   }
 
   /**
@@ -622,10 +628,10 @@ public class Desktop extends GDesktop
       public void run()
       {
         Cache.log.debug("Downloading data from identifiers.org");
-        // UrlDownloadClient client = new UrlDownloadClient();
+        UrlDownloadClient client = new UrlDownloadClient();
         try
         {
-          UrlDownloadClient.download(IdOrgSettings.getUrl(),
+          client.download(IdOrgSettings.getUrl(),
                   IdOrgSettings.getDownloadLocation());
         } catch (IOException e)
         {
@@ -643,7 +649,7 @@ public class Desktop extends GDesktop
     showNews(showNews.isSelected());
   }
 
-  protected void showNews(boolean visible)
+  void showNews(boolean visible)
   {
     Cache.log.debug((visible ? "Showing" : "Hiding") + " news.");
     showNews.setSelected(visible);
@@ -655,10 +661,10 @@ public class Desktop extends GDesktop
         public void run()
         {
           long now = System.currentTimeMillis();
-          setProgressBar(
+          Desktop.instance.setProgressBar(
                   MessageManager.getString("status.refreshing_news"), now);
           jvnews.refreshNews();
-          setProgressBar(null, now);
+          Desktop.instance.setProgressBar(null, now);
           jvnews.showNews();
         }
       }).start();
@@ -763,7 +769,7 @@ public class Desktop extends GDesktop
     }
   }
 
-  protected void showPasteMenu(int x, int y)
+  void showPasteMenu(int x, int y)
   {
     JPopupMenu popup = new JPopupMenu();
     JMenuItem item = new JMenuItem(
@@ -796,7 +802,7 @@ public class Desktop extends GDesktop
         FileFormatI format = new IdentifyFile().identify(file,
                 DataSourceType.PASTE);
 
-        new FileLoader().loadFile(file, DataSourceType.PASTE, format);
+        new FileLoader().LoadFile(file, DataSourceType.PASTE, format);
 
       }
     } catch (Exception ex)
@@ -891,14 +897,13 @@ public class Desktop extends GDesktop
           int w, int h, boolean resizable, boolean ignoreMinSize)
   {
 
-
     // TODO: allow callers to determine X and Y position of frame (eg. via
     // bounds object).
     // TODO: consider fixing method to update entries in the window submenu with
     // the current window title
 
     frame.setTitle(title);
-    if (w > 0 && (frame.getWidth() < 1 || frame.getHeight() < 1))
+    if (frame.getWidth() < 1 || frame.getHeight() < 1)
     {
       frame.setSize(w, h);
     }
@@ -906,7 +911,8 @@ public class Desktop extends GDesktop
     // A HEADLESS STATE WHEN NO DESKTOP EXISTS. MUST RETURN
     // IF JALVIEW IS RUNNING HEADLESS
     // ///////////////////////////////////////////////
-    if (Desktop.getInstance().instanceOnly || Jalview.isHeadlessMode())
+    if (instance == null || (System.getProperty("java.awt.headless") != null
+            && System.getProperty("java.awt.headless").equals("true")))
     {
       return;
     }
@@ -946,13 +952,13 @@ public class Desktop extends GDesktop
      * add an entry for the new frame in the Window menu 
      * (and remove it when the frame is closed)
      */
-    JMenuItem menuItem = new JMenuItem(title);
+    final JMenuItem menuItem = new JMenuItem(title);
     frame.addInternalFrameListener(new InternalFrameAdapter()
     {
       @Override
       public void internalFrameActivated(InternalFrameEvent evt)
       {
-        JInternalFrame itf = getDesktopPane().getSelectedFrame();
+        JInternalFrame itf = desktop.getSelectedFrame();
         if (itf != null)
         {
           if (itf instanceof AlignFrame)
@@ -984,7 +990,7 @@ public class Desktop extends GDesktop
         {
           menuItem.removeActionListener(menuItem.getActionListeners()[0]);
         }
-        Desktop.getInstance().windowMenu.remove(menuItem);
+        windowMenu.remove(menuItem);
       }
     });
 
@@ -1006,9 +1012,9 @@ public class Desktop extends GDesktop
 
     setKeyBindings(frame);
 
-    getDesktopPane().add(frame);
+    desktop.add(frame);
 
-    Desktop.getInstance().windowMenu.add(menuItem);
+    windowMenu.add(menuItem);
 
     frame.toFront();
     try
@@ -1033,6 +1039,7 @@ public class Desktop extends GDesktop
    */
   private static void setKeyBindings(JInternalFrame frame)
   {
+    @SuppressWarnings("serial")
     final Action closeAction = new AbstractAction()
     {
       @Override
@@ -1048,7 +1055,7 @@ public class Desktop extends GDesktop
     KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
             InputEvent.CTRL_DOWN_MASK);
     KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
-            Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
+            jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx());
 
     InputMap inputMap = frame
             .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
@@ -1065,7 +1072,7 @@ public class Desktop extends GDesktop
   {
     if (!internalCopy)
     {
-      Desktop.getInstance().jalviewClipboard = null;
+      Desktop.jalviewClipboard = null;
     }
 
     internalCopy = false;
@@ -1144,7 +1151,7 @@ public class Desktop extends GDesktop
           {
             Platform.cacheFileData((File) file);
           }
-          new FileLoader().loadFile(null, file, protocol, format);
+          new FileLoader().LoadFile(null, file, protocol, format);
 
         }
       } catch (Exception ex)
@@ -1167,7 +1174,7 @@ public class Desktop extends GDesktop
   {
     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
     JalviewFileChooser chooser = JalviewFileChooser
-            .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat, true);
+            .forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat, BackupFiles.getEnabled());
 
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(
@@ -1201,7 +1208,7 @@ public class Desktop extends GDesktop
           }
         }
 
-        new FileLoader().loadFile(viewport, selectedFile,
+        new FileLoader().LoadFile(viewport, selectedFile,
                 DataSourceType.FILE, format);
       }
     });
@@ -1275,12 +1282,12 @@ public class Desktop extends GDesktop
         {
           if (viewport != null)
           {
-            new FileLoader().loadFile(viewport, url, DataSourceType.URL,
+            new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
                     FileFormat.Jalview);
           }
           else
           {
-            new FileLoader().loadFile(url, DataSourceType.URL,
+            new FileLoader().LoadFile(url, DataSourceType.URL,
                     FileFormat.Jalview);
           }
         }
@@ -1300,8 +1307,7 @@ public class Desktop extends GDesktop
           {
             String msg = MessageManager
                     .formatMessage("label.couldnt_locate", url);
-            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
-                    msg,
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg,
                     MessageManager.getString("label.url_not_found"),
                     JvOptionPane.WARNING_MESSAGE);
 
@@ -1310,20 +1316,19 @@ public class Desktop extends GDesktop
 
           if (viewport != null)
           {
-            new FileLoader().loadFile(viewport, url, DataSourceType.URL,
+            new FileLoader().LoadFile(viewport, url, DataSourceType.URL,
                     format);
           }
           else
           {
-            new FileLoader().loadFile(url, DataSourceType.URL, format);
+            new FileLoader().LoadFile(url, DataSourceType.URL, format);
           }
         }
       }
     };
     String dialogOption = MessageManager
             .getString("label.input_alignment_from_url");
-    JvOptionPane.newOptionDialog(getDesktopPane())
-            .setResponseHandler(0, action)
+    JvOptionPane.newOptionDialog(desktop).setResponseHandler(0, action)
             .showInternalDialog(panel, dialogOption,
                     JvOptionPane.YES_NO_CANCEL_OPTION,
                     JvOptionPane.PLAIN_MESSAGE, null, options,
@@ -1409,7 +1414,7 @@ public class Desktop extends GDesktop
   public void aboutMenuItem_actionPerformed(ActionEvent e)
   {
     // StringBuffer message = getAboutMessage(false);
-    // JvOptionPane.showInternalMessageDialog(Desktop.getDesktop(),
+    // JvOptionPane.showInternalMessageDialog(Desktop.desktop,
     //
     // message.toString(), "About Jalview", JvOptionPane.INFORMATION_MESSAGE);
     new Thread(new Runnable()
@@ -1518,12 +1523,8 @@ public class Desktop extends GDesktop
   @Override
   public void closeAll_actionPerformed(ActionEvent e)
   {
-    if (desktopPane == null)
-    {
-      return;
-    }
     // TODO show a progress bar while closing?
-    JInternalFrame[] frames = desktopPane.getAllFrames();
+    JInternalFrame[] frames = desktop.getAllFrames();
     for (int i = 0; i < frames.length; i++)
     {
       try
@@ -1544,7 +1545,12 @@ public class Desktop extends GDesktop
      * reset state of singleton objects as appropriate (clear down session state
      * when all windows are closed)
      */
-    getStructureSelectionManager().resetAll();
+    StructureSelectionManager ssm = StructureSelectionManager
+            .getStructureSelectionManager(this);
+    if (ssm != null)
+    {
+      ssm.resetAll();
+    }
   }
 
   @Override
@@ -1589,7 +1595,7 @@ public class Desktop extends GDesktop
   @Override
   protected void showMemusage_actionPerformed(ActionEvent e)
   {
-    getDesktopPane().showMemoryUsage(showMemusage.isSelected());
+    desktop.showMemoryUsage(showMemusage.isSelected());
   }
 
   /*
@@ -1626,7 +1632,7 @@ public class Desktop extends GDesktop
 
   void reorderAssociatedWindows(boolean minimize, boolean close)
   {
-    JInternalFrame[] frames = getDesktopPane().getAllFrames();
+    JInternalFrame[] frames = desktop.getAllFrames();
     if (frames == null || frames.length < 1)
     {
       return;
@@ -1810,7 +1816,7 @@ public class Desktop extends GDesktop
     saveState_actionPerformed(true);
   }
 
-  protected void setProjectFile(File choice)
+  private void setProjectFile(File choice)
   {
     this.projectFile = choice;
   }
@@ -1832,7 +1838,7 @@ public class Desktop extends GDesktop
         "Jalview Project (old)" };
     JalviewFileChooser chooser = new JalviewFileChooser(
             Cache.getProperty("LAST_DIRECTORY"), suffix, desc,
-            "Jalview Project", true, true); // last two booleans: allFiles,
+            "Jalview Project", true, BackupFiles.getEnabled()); // last two booleans: allFiles,
                                             // allowBackupFiles
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(MessageManager.getString("label.restore_state"));
@@ -1860,7 +1866,7 @@ public class Desktop extends GDesktop
                {
                  Cache.log.error(
                          "Problems whilst loading project from " + choice, ex);
-              JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
+                 JvOptionPane.showMessageDialog(Desktop.desktop,
                          MessageManager.formatMessage(
                                  "label.error_whilst_loading_project_from",
                                new Object[]
@@ -1903,7 +1909,7 @@ public class Desktop extends GDesktop
     {
       progressPanel = new JPanel(new GridLayout(1, 1));
       totalProgressCount = 0;
-      getContentPane().add(progressPanel, BorderLayout.SOUTH);
+      instance.getContentPane().add(progressPanel, BorderLayout.SOUTH);
     }
     JPanel thisprogress = new JPanel(new BorderLayout(10, 5));
     JProgressBar progressBar = new JProgressBar();
@@ -1916,7 +1922,7 @@ public class Desktop extends GDesktop
     ((GridLayout) progressPanel.getLayout()).setRows(
             ((GridLayout) progressPanel.getLayout()).getRows() + 1);
     ++totalProgressCount;
-    validate();
+    instance.validate();
     return thisprogress;
   }
 
@@ -1970,7 +1976,7 @@ public class Desktop extends GDesktop
    */
   public static AlignmentPanel[] getAlignmentPanels(String alignmentId)
   {
-    if (Desktop.getDesktopPane() == null)
+    if (Desktop.desktop == null)
     {
       // no frames created and in headless mode
       // TODO: verify that frames are recoverable when in headless mode
@@ -2012,7 +2018,7 @@ public class Desktop extends GDesktop
   public static AlignmentViewport[] getViewports(String sequenceSetId)
   {
     List<AlignmentViewport> viewp = new ArrayList<>();
-    if (getDesktopPane() != null)
+    if (desktop != null)
     {
       AlignFrame[] frames = Desktop.getAlignFrames();
 
@@ -2098,7 +2104,7 @@ public class Desktop extends GDesktop
   {
     source.viewport.setGatherViewsHere(true);
     source.viewport.setExplodedGeometry(source.getBounds());
-    JInternalFrame[] frames = getAllFrames();
+    JInternalFrame[] frames = desktop.getAllFrames();
     String viewId = source.viewport.getSequenceSetId();
 
     for (int t = 0; t < frames.length; t++)
@@ -2155,7 +2161,7 @@ public class Desktop extends GDesktop
         String fle = chooser.getSelectedFile().toString();
         if (!vamsasImport(chooser.getSelectedFile()))
         {
-          JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                   MessageManager.formatMessage(
                           "label.couldnt_import_as_vamsas_session",
                           new Object[]
@@ -2456,7 +2462,7 @@ public class Desktop extends GDesktop
         removeProgressPanel(progpanel);
         if (warnmsg != null)
         {
-          JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
 
                   warnmsg, warnttl, JvOptionPane.ERROR_MESSAGE);
         }
@@ -2493,7 +2499,7 @@ public class Desktop extends GDesktop
 
   public JInternalFrame[] getAllFrames()
   {
-    return desktopPane.getAllFrames();
+    return desktop.getAllFrames();
   }
 
   /**
@@ -2532,7 +2538,7 @@ public class Desktop extends GDesktop
           while (li.hasNext())
           {
             String link = li.next();
-            if (link.contains(UrlConstants.SEQUENCE_ID)
+            if (link.contains(jalview.util.UrlConstants.SEQUENCE_ID)
                     && !UrlConstants.isDefaultString(link))
             {
               check = true;
@@ -2579,7 +2585,7 @@ public class Desktop extends GDesktop
           });
           msgPanel.add(jcb);
 
-          JvOptionPane.showMessageDialog(desktopPane, msgPanel,
+          JvOptionPane.showMessageDialog(Desktop.desktop, msgPanel,
                   MessageManager
                           .getString("label.SEQUENCE_ID_no_longer_used"),
                   JvOptionPane.WARNING_MESSAGE);
@@ -2694,11 +2700,11 @@ public class Desktop extends GDesktop
   {
     if (Jalview.isHeadlessMode())
     {
-      // Desktop.getDesktop() is null in headless mode
-      return new AlignFrame[] { Jalview.getCurrentAlignFrame() };
+      // Desktop.desktop is null in headless mode
+      return new AlignFrame[] { Jalview.currentAlignFrame };
     }
 
-    JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
+    JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
     if (frames == null)
     {
@@ -2743,7 +2749,7 @@ public class Desktop extends GDesktop
    */
   public GStructureViewer[] getJmols()
   {
-    JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
+    JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
     if (frames == null)
     {
@@ -2779,7 +2785,7 @@ public class Desktop extends GDesktop
     } catch (Exception ex)
     {
       jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex);
-      JvOptionPane.showInternalMessageDialog(desktopPane,
+      JvOptionPane.showInternalMessageDialog(Desktop.desktop,
 
               MessageManager.getString("label.couldnt_create_groovy_shell"),
               MessageManager.getString("label.groovy_support_failed"),
@@ -2790,7 +2796,7 @@ public class Desktop extends GDesktop
   /**
    * Open the Groovy console
    */
-  private void openGroovyConsole()
+  void openGroovyConsole()
   {
     if (groovyConsole == null)
     {
@@ -2839,7 +2845,7 @@ public class Desktop extends GDesktop
   {
     getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
             .put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
-                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
+                    jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx()),
                     "Quit");
     getRootPane().getActionMap().put("Quit", new AbstractAction()
     {
@@ -2898,18 +2904,18 @@ public class Desktop extends GDesktop
       progressBarHandlers = new Hashtable<>();
     }
 
-    if (progressBars.get(new Long(id)) != null)
+    if (progressBars.get(Long.valueOf(id)) != null)
     {
-      JPanel panel = progressBars.remove(new Long(id));
-      if (progressBarHandlers.contains(new Long(id)))
+      JPanel panel = progressBars.remove(Long.valueOf(id));
+      if (progressBarHandlers.contains(Long.valueOf(id)))
       {
-        progressBarHandlers.remove(new Long(id));
+        progressBarHandlers.remove(Long.valueOf(id));
       }
       removeProgressPanel(panel);
     }
     else
     {
-      progressBars.put(new Long(id), addProgressPanel(message));
+      progressBars.put(Long.valueOf(id), addProgressPanel(message));
     }
   }
 
@@ -2924,13 +2930,13 @@ public class Desktop extends GDesktop
           final IProgressIndicatorHandler handler)
   {
     if (progressBarHandlers == null
-            || !progressBars.containsKey(new Long(id)))
+            || !progressBars.containsKey(Long.valueOf(id)))
     {
       throw new Error(MessageManager.getString(
               "error.call_setprogressbar_before_registering_handler"));
     }
-    progressBarHandlers.put(new Long(id), handler);
-    final JPanel progressPanel = progressBars.get(new Long(id));
+    progressBarHandlers.put(Long.valueOf(id), handler);
+    final JPanel progressPanel = progressBars.get(Long.valueOf(id));
     if (handler.canCancel())
     {
       JButton cancel = new JButton(
@@ -2976,7 +2982,7 @@ public class Desktop extends GDesktop
    */
   public static AlignFrame getAlignFrameFor(AlignViewportI viewport)
   {
-    if (getDesktopPane() != null)
+    if (desktop != null)
     {
       AlignmentPanel[] aps = getAlignmentPanels(
               viewport.getSequenceSetId());
@@ -3037,8 +3043,8 @@ public class Desktop extends GDesktop
       // todo: changesupport handlers need to be transferred
       if (discoverer == null)
       {
-        discoverer = Discoverer.getInstance();
-        // register PCS handler for getDesktop().
+        discoverer = new jalview.ws.jws1.Discoverer();
+        // register PCS handler for desktop.
         discoverer.addPropertyChangeListener(changeSupport);
       }
       // JAL-940 - disabled JWS1 service configuration - always start discoverer
@@ -3048,7 +3054,7 @@ public class Desktop extends GDesktop
 
     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
     {
-      t2 = jalview.ws.jws2.Jws2Discoverer.getInstance()
+      t2 = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
               .startDiscoverer(changeSupport);
     }
     Thread t3 = null;
@@ -3081,7 +3087,7 @@ public class Desktop extends GDesktop
   {
     if (evt.getNewValue() == null || evt.getNewValue() instanceof Vector)
     {
-      final String ermsg = jalview.ws.jws2.Jws2Discoverer.getInstance()
+      final String ermsg = jalview.ws.jws2.Jws2Discoverer.getDiscoverer()
               .getErrorMessages();
       if (ermsg != null)
       {
@@ -3120,7 +3126,7 @@ public class Desktop extends GDesktop
                  * 
                  * jd.waitForInput();
                  */
-                JvOptionPane.showConfirmDialog(Desktop.getDesktopPane(),
+                JvOptionPane.showConfirmDialog(Desktop.desktop,
                         new JLabel("<html><table width=\"450\"><tr><td>"
                                 + ermsg + "</td></tr></table>"
                                 + "<p>It may be that you have invalid JABA URLs<br/>in your web service preferences,"
@@ -3146,7 +3152,7 @@ public class Desktop extends GDesktop
     }
   }
 
-  Runnable serviceChangedDialog = null;
+  private Runnable serviceChangedDialog = null;
 
   /**
    * start a thread to open a URL in the configured browser. Pops up a warning
@@ -3157,7 +3163,7 @@ public class Desktop extends GDesktop
    */
   public static void showUrl(final String url)
   {
-    showUrl(url, Desktop.getInstance());
+    showUrl(url, Desktop.instance);
   }
 
   /**
@@ -3183,10 +3189,10 @@ public class Desktop extends GDesktop
                     .formatMessage("status.opening_params", new Object[]
                     { url }), this.hashCode());
           }
-          BrowserLauncher.openURL(url);
+          jalview.util.BrowserLauncher.openURL(url);
         } catch (Exception ex)
         {
-          JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                   MessageManager
                           .getString("label.web_browser_not_found_unix"),
                   MessageManager.getString("label.web_browser_not_found"),
@@ -3202,16 +3208,15 @@ public class Desktop extends GDesktop
     }).start();
   }
 
-  private WsParamSetManager wsparamManager = null;
+  public static WsParamSetManager wsparamManager = null;
 
   public static ParamManager getUserParameterStore()
   {
-    Desktop d = Desktop.getInstance();
-    if (d.wsparamManager == null)
+    if (wsparamManager == null)
     {
-      d.wsparamManager = new WsParamSetManager();
+      wsparamManager = new WsParamSetManager();
     }
-    return d.wsparamManager;
+    return wsparamManager;
   }
 
   /**
@@ -3256,16 +3261,14 @@ public class Desktop extends GDesktop
   /**
    * flag indicating if dialogExecutor should try to acquire a permit
    */
-  volatile boolean dialogPause = true;
+  private volatile boolean dialogPause = true;
 
   /**
    * pause the queue
    */
-  java.util.concurrent.Semaphore block = new Semaphore(0);
-
-  private groovy.ui.Console groovyConsole;
+  private java.util.concurrent.Semaphore block = new Semaphore(0);
 
-  public StructureViewer lastTargetedView;
+  private static groovy.ui.Console groovyConsole;
 
   /**
    * add another dialog thread to the queue
@@ -3288,7 +3291,7 @@ public class Desktop extends GDesktop
           {
           }
         }
-        if (instanceOnly)
+        if (instance == null)
         {
           return;
         }
@@ -3452,7 +3455,7 @@ public class Desktop extends GDesktop
     String topViewId = myTopFrame.viewport.getSequenceSetId();
     String bottomViewId = myBottomFrame.viewport.getSequenceSetId();
 
-    JInternalFrame[] frames = desktopPane.getAllFrames();
+    JInternalFrame[] frames = desktop.getAllFrames();
     for (JInternalFrame frame : frames)
     {
       if (frame instanceof SplitFrame && frame != source)
@@ -3497,8 +3500,7 @@ public class Desktop extends GDesktop
 
   public static groovy.ui.Console getGroovyConsole()
   {
-    Desktop desktop = Desktop.getInstance();
-    return desktop == null ? null : desktop.groovyConsole;
+    return groovyConsole;
   }
 
   /**
@@ -3516,7 +3518,6 @@ public class Desktop extends GDesktop
    *          - the payload from the drop event
    * @throws Exception
    */
-  @SuppressWarnings("unchecked")
   public static void transferFromDropTarget(List<Object> files,
           List<DataSourceType> protocols, DropTargetDropEvent evt,
           Transferable t) throws Exception
@@ -3586,7 +3587,7 @@ public class Desktop extends GDesktop
     {
       // Works on Windows and MacOSX
       Cache.log.debug("Drop handled as javaFileListFlavor");
-      for (Object file : (List<Object>) t
+      for (Object file : (List) t
               .getTransferData(DataFlavor.javaFileListFlavor))
       {
         files.add(file);
@@ -3750,7 +3751,7 @@ public class Desktop extends GDesktop
           Class<? extends StructureViewerBase> structureViewerClass)
   {
     List<StructureViewerBase> result = new ArrayList<>();
-    JInternalFrame[] frames = getAllFrames();
+    JInternalFrame[] frames = Desktop.instance.getAllFrames();
 
     for (JInternalFrame frame : frames)
     {
@@ -3769,5 +3770,4 @@ public class Desktop extends GDesktop
     }
     return result;
   }
-
 }