JAL-3026 JDesktop/JInternalFrames support + relative URL file loading
[jalview.git] / src / jalview / gui / Desktop.java
index 04487e0..fc95f83 100644 (file)
@@ -69,6 +69,7 @@ import java.awt.dnd.DropTargetEvent;
 import java.awt.dnd.DropTargetListener;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
@@ -92,10 +93,13 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Semaphore;
 
 import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.ActionMap;
 import javax.swing.Box;
 import javax.swing.BoxLayout;
 import javax.swing.DefaultDesktopManager;
 import javax.swing.DesktopManager;
+import javax.swing.InputMap;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
@@ -378,65 +382,39 @@ public class Desktop extends jalview.jbgui.GDesktop
       setBounds((screenSize.width - 900) / 2, (screenSize.height - 650) / 2,
               900, 650);
     }
-    jconsole = new Console(this, showjconsole);
-    // add essential build information
-    jconsole.setHeader(
-            "Jalview Version: " + jalview.bin.Cache.getProperty("VERSION")
-                    + "\n" + "Jalview Installation: "
-                    + jalview.bin.Cache.getDefault("INSTALLATION",
-                            "unknown")
-                    + "\n" + "Build Date: "
-                    + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")
-                    + "\n" + "Java version: "
-                    + System.getProperty("java.version") + "\n"
-                    + System.getProperty("os.arch") + " "
-                    + System.getProperty("os.name") + " "
-                    + System.getProperty("os.version"));
-
-    showConsole(showjconsole);
-
-    showNews.setVisible(false);
+    /**
+     * BH 2018
+     * 
+     * @j2sNative
+     */
+    {
 
-    experimentalFeatures.setSelected(showExperimental());
+      jconsole = new Console(this, showjconsole);
+      // add essential build information
+      jconsole.setHeader("Jalview Version: "
+              + jalview.bin.Cache.getProperty("VERSION") + "\n"
+              + "Jalview Installation: "
+              + jalview.bin.Cache.getDefault("INSTALLATION", "unknown")
+              + "\n" + "Build Date: "
+              + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown") + "\n"
+              + "Java version: " + System.getProperty("java.version") + "\n"
+              + System.getProperty("os.arch") + " "
+              + System.getProperty("os.name") + " "
+              + System.getProperty("os.version"));
 
-    getIdentifiersOrgData();
+      showConsole(showjconsole);
 
-    checkURLLinks();
+      showNews.setVisible(false);
 
-    this.addWindowListener(new WindowAdapter()
-    {
-      @Override
-      public void windowClosing(WindowEvent evt)
-      {
-        quit();
-      }
-    });
+      experimentalFeatures.setSelected(showExperimental());
 
-    MouseAdapter ma;
-    this.addMouseListener(ma = new MouseAdapter()
-    {
-      @Override
-      public void mousePressed(MouseEvent evt)
-      {
-        if (evt.isPopupTrigger()) // Mac
-        {
-          showPasteMenu(evt.getX(), evt.getY());
-        }
-      }
+      getIdentifiersOrgData();
 
-      @Override
-      public void mouseReleased(MouseEvent evt)
-      {
-        if (evt.isPopupTrigger()) // Windows
-        {
-          showPasteMenu(evt.getX(), evt.getY());
-        }
-      }
-    });
-    desktop.addMouseListener(ma);
+      checkURLLinks();
 
     this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this));
     // Spawn a thread that shows the splashscreen
+
     SwingUtilities.invokeLater(new Runnable()
     {
       @Override
@@ -474,6 +452,41 @@ public class Desktop extends jalview.jbgui.GDesktop
               }
 
             });
+
+    } // end BH 2018 ignore
+
+    this.addWindowListener(new WindowAdapter()
+    {
+      @Override
+      public void windowClosing(WindowEvent evt)
+      {
+        quit();
+      }
+    });
+
+    MouseAdapter ma;
+    this.addMouseListener(ma = new MouseAdapter()
+    {
+      @Override
+      public void mousePressed(MouseEvent evt)
+      {
+        if (evt.isPopupTrigger()) // Mac
+        {
+          showPasteMenu(evt.getX(), evt.getY());
+        }
+      }
+
+      @Override
+      public void mouseReleased(MouseEvent evt)
+      {
+        if (evt.isPopupTrigger()) // Windows
+        {
+          showPasteMenu(evt.getX(), evt.getY());
+        }
+      }
+    });
+    desktop.addMouseListener(ma);
+
   }
 
   /**
@@ -513,20 +526,28 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   public void checkForNews()
   {
+
+    /**
+     * BH 2018
+     * 
+     * @j2sNative
+     */
+    {
     final Desktop me = this;
     // Thread off the news reader, in case there are connection problems.
     addDialogThread(new Runnable()
     {
-      @Override
-      public void run()
-      {
-        Cache.log.debug("Starting news thread.");
+        @Override
+        public void run()
+        {
+          Cache.log.debug("Starting news thread.");
 
-        jvnews = new BlogReader(me);
-        showNews.setVisible(true);
-        Cache.log.debug("Completed news thread.");
-      }
+          jvnews = new BlogReader(me);
+          showNews.setVisible(true);
+          Cache.log.debug("Completed news thread.");
+        }
     });
+    }
   }
 
   public void getIdentifiersOrgData()
@@ -560,6 +581,12 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   void showNews(boolean visible)
   {
+    /**
+     * BH 2018
+     * 
+     * @j2sNative
+     * 
+     */
     {
       Cache.log.debug((visible ? "Showing" : "Hiding") + " news.");
       showNews.setSelected(visible);
@@ -643,7 +670,8 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   private void doVamsasClientCheck()
   {
-    if (jalview.bin.Cache.vamsasJarsPresent())
+    if (/** @j2sNative false && */ // BH 2018
+    jalview.bin.Cache.vamsasJarsPresent())
     {
       setupVamsasDisconnectedGui();
       VamsasMenu.setVisible(true);
@@ -903,8 +931,6 @@ public class Desktop extends jalview.jbgui.GDesktop
           menuItem.removeActionListener(menuItem.getActionListeners()[0]);
         }
         windowMenu.remove(menuItem);
-
-        System.gc();
       };
     });
 
@@ -924,6 +950,8 @@ public class Desktop extends jalview.jbgui.GDesktop
       }
     });
 
+    setKeyBindings(frame);
+
     desktop.add(frame);
 
     windowMenu.add(menuItem);
@@ -943,6 +971,42 @@ public class Desktop extends jalview.jbgui.GDesktop
     }
   }
 
+  /**
+   * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
+   * the window
+   * 
+   * @param frame
+   */
+  private static void setKeyBindings(JInternalFrame frame)
+  {
+    @SuppressWarnings("serial")
+    final Action closeAction = new AbstractAction()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        frame.dispose();
+      }
+    };
+
+    /*
+     * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action
+     */
+    KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
+            InputEvent.CTRL_DOWN_MASK);
+    KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
+            Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
+
+    InputMap inputMap = frame
+            .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
+    String ctrlW = ctrlWKey.toString();
+    inputMap.put(ctrlWKey, ctrlW);
+    inputMap.put(cmdWKey, ctrlW);
+
+    ActionMap actionMap = frame.getActionMap();
+    actionMap.put(ctrlW, closeAction);
+  }
+
   @Override
   public void lostOwnership(Clipboard clipboard, Transferable contents)
   {
@@ -1392,7 +1456,6 @@ public class Desktop extends jalview.jbgui.GDesktop
     {
       ssm.resetAll();
     }
-    System.gc();
   }
 
   @Override
@@ -1462,11 +1525,14 @@ public class Desktop extends jalview.jbgui.GDesktop
    */
   void showConsole(boolean selected)
   {
-    showConsole.setSelected(selected);
     // TODO: decide if we should update properties file
-    Cache.setProperty("SHOW_JAVA_CONSOLE",
-            Boolean.valueOf(selected).toString());
-    jconsole.setVisible(selected);
+    if (jconsole != null) // BH 2018
+    {
+      showConsole.setSelected(selected);
+      Cache.setProperty("SHOW_JAVA_CONSOLE",
+              Boolean.valueOf(selected).toString());
+      jconsole.setVisible(selected);
+    }
   }
 
   void reorderAssociatedWindows(boolean minimize, boolean close)
@@ -2326,7 +2392,8 @@ public class Desktop extends jalview.jbgui.GDesktop
       @Override
       public void run()
       {
-        if (Cache.getDefault("CHECKURLLINKS", true))
+        if (/** @j2sNative false && */ // BH 2018
+        Cache.getDefault("CHECKURLLINKS", true))
         {
           // check what the actual links are - if it's just the default don't
           // bother with the warning
@@ -3297,6 +3364,21 @@ public class Desktop extends jalview.jbgui.GDesktop
     return groovyConsole;
   }
 
+  /**
+   * handles the payload of a drag and drop event.
+   * 
+   * TODO refactor to desktop utilities class
+   * 
+   * @param files
+   *          - Data source strings extracted from the drop event
+   * @param protocols
+   *          - protocol for each data source extracted from the drop event
+   * @param evt
+   *          - the drop event
+   * @param t
+   *          - the payload from the drop event
+   * @throws Exception
+   */
   public static void transferFromDropTarget(List<String> files,
           List<DataSourceType> protocols, DropTargetDropEvent evt,
           Transferable t) throws Exception
@@ -3456,7 +3538,10 @@ public class Desktop extends jalview.jbgui.GDesktop
       // resolve any .lnk files in the file drop
       for (int f = 0; f < files.size(); f++)
       {
-        if (protocols.get(f).equals(DataSourceType.FILE) && files.get(f).toLowerCase().endsWith(".lnk") || files.get(f).toLowerCase().endsWith(".url")|| files.get(f).toLowerCase().endsWith(".site"))
+        String source = files.get(f).toLowerCase();
+        if (protocols.get(f).equals(DataSourceType.FILE)
+                && (source.endsWith(".lnk") || source.endsWith(".url")
+                        || source.endsWith(".site")))
         {
           try {
             File lf = new File(files.get(f));