Merge branch 'develop' into features/JAL-2068groovyAnnotationWorker
[jalview.git] / src / jalview / gui / Desktop.java
index 392bf32..e56c42f 100644 (file)
@@ -72,9 +72,6 @@ import java.beans.PropertyVetoException;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Hashtable;
@@ -172,8 +169,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
   static final int yOffset = 30;
 
-  private static AlignFrame currentAlignFrame;
-
   public static jalview.ws.jws1.Discoverer discoverer;
 
   public static Object[] jalviewClipboard;
@@ -315,7 +310,20 @@ public class Desktop extends jalview.jbgui.GDesktop implements
      */
     instance = this;
     doVamsasClientCheck();
-    doGroovyCheck();
+
+    groovyShell = new JMenuItem();
+    groovyShell.setText(MessageManager.getString("label.groovy_console"));
+    groovyShell.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        groovyShell_actionPerformed();
+      }
+    });
+    toolsMenu.add(groovyShell);
+    groovyShell.setVisible(true);
+
     doConfigureStructurePrefs();
     setTitle("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -942,53 +950,15 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   {
     boolean success = true;
     Transferable t = evt.getTransferable();
-    java.util.List files = null;
-    java.util.List protocols = null;
+    java.util.List<String> files = new ArrayList<String>();
+    java.util.List<String> protocols = new ArrayList<String>();
 
     try
     {
-      DataFlavor uriListFlavor = new DataFlavor(
-              "text/uri-list;class=java.lang.String");
-      if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
-      {
-        // Works on Windows and MacOSX
-        evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
-        files = (java.util.List) t
-                .getTransferData(DataFlavor.javaFileListFlavor);
-      }
-      else if (t.isDataFlavorSupported(uriListFlavor))
-      {
-        // This is used by Unix drag system
-        evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
-        String data = (String) t.getTransferData(uriListFlavor);
-        files = new java.util.ArrayList(1);
-        protocols = new java.util.ArrayList(1);
-        for (java.util.StringTokenizer st = new java.util.StringTokenizer(
-                data, "\r\n"); st.hasMoreTokens();)
-        {
-          String s = st.nextToken();
-          if (s.startsWith("#"))
-          {
-            // the line is a comment (as per the RFC 2483)
-            continue;
-          }
-          java.net.URI uri = new java.net.URI(s);
-          if (uri.getScheme().toLowerCase().startsWith("http"))
-          {
-            protocols.add(FormatAdapter.URL);
-            files.add(uri.toString());
-          }
-          else
-          {
-            // otherwise preserve old behaviour: catch all for file objects
-            java.io.File file = new java.io.File(uri);
-            protocols.add(FormatAdapter.FILE);
-            files.add(file.toString());
-          }
-        }
-      }
+      Desktop.transferFromDropTarget(files, protocols, evt, t);
     } catch (Exception e)
     {
+      e.printStackTrace();
       success = false;
     }
 
@@ -2389,25 +2359,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
   protected JMenuItem groovyShell;
 
-  public void doGroovyCheck()
-  {
-    if (jalview.bin.Cache.groovyJarsPresent())
-    {
-      groovyShell = new JMenuItem();
-      groovyShell.setText(MessageManager.getString("label.groovy_console"));
-      groovyShell.addActionListener(new ActionListener()
-      {
-        @Override
-        public void actionPerformed(ActionEvent e)
-        {
-          groovyShell_actionPerformed();
-        }
-      });
-      toolsMenu.add(groovyShell);
-      groovyShell.setVisible(true);
-    }
-  }
-
   /**
    * Accessor method to quickly get all the AlignmentFrames loaded.
    * 
@@ -2418,7 +2369,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     if (Jalview.isHeadlessMode())
     {
       // Desktop.desktop is null in headless mode
-      return new AlignFrame[] { currentAlignFrame };
+      return new AlignFrame[] { Jalview.currentAlignFrame };
     }
 
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
@@ -2495,12 +2446,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    */
   public void groovyShell_actionPerformed()
   {
-    if (!jalview.bin.Cache.groovyJarsPresent())
-    {
-      throw new Error(
-              MessageManager
-                      .getString("error.implementation_error_cannot_create_groovyshell"));
-    }
     try
     {
       openGroovyConsole();
@@ -2516,36 +2461,21 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   }
 
   /**
-   * Open the Groovy console, using reflection to avoid creating compile-time
-   * dependency on Groovy libraries
-   * 
-   * @throws ClassNotFoundException
-   * @throws NoSuchMethodException
-   * @throws InstantiationException
-   * @throws IllegalAccessException
-   * @throws InvocationTargetException
+   * Open the Groovy console
    */
-  void openGroovyConsole() throws ClassNotFoundException,
-          NoSuchMethodException, InstantiationException,
-          IllegalAccessException, InvocationTargetException
+  void openGroovyConsole()
   {
-    Class<?> gcClass = Desktop.class.getClassLoader().loadClass(
-            "groovy.ui.Console");
-    Constructor<?> gccons = gcClass.getConstructor();
-    groovyConsole = gccons.newInstance();
+    groovyConsole = new groovy.ui.Console();
 
     /*
      * bind groovy variable 'Jalview' to the Desktop object
      */
-    Method setvar = gcClass.getMethod("setVariable", new Class[] {
-        String.class, Object.class });
-    setvar.invoke(groovyConsole, new Object[] { "Jalview", this });
+    groovyConsole.setVariable("Jalview", this);
 
     /*
      * start the console
      */
-    Method run = gcClass.getMethod("run");
-    run.invoke(groovyConsole);
+    groovyConsole.run();
 
     /*
      * Allow only one console at a time, so that the AlignFrame menu option
@@ -2553,14 +2483,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
      * Disable 'new console', and enable 'Run script', when the console is 
      * opened, and the reverse when it is closed
      */
-    Method getFrame = gcClass.getMethod("getFrame");
-    Window window = (Window) getFrame.invoke(groovyConsole);
+    Window window = (Window) groovyConsole.getFrame();
     window.addWindowListener(new WindowAdapter()
     {
       @Override
       public void windowClosed(WindowEvent e)
       {
-        groovyShell.setEnabled(true);
         enableExecuteGroovy(false);
       }
     });
@@ -2569,7 +2497,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements
      * if we got this far, enable 'Run Groovy' in AlignFrame menus
      * and disable opening a second console
      */
-    groovyShell.setEnabled(false);
     enableExecuteGroovy(true);
   }
 
@@ -2577,9 +2504,16 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    * Enable or disable 'Run Groovy script' in AlignFrame calculate menus
    * 
    * @param enabled
+   *          true if Groovy console is open
    */
   public void enableExecuteGroovy(boolean enabled)
   {
+    /*
+     * disable opening a second Groovy console
+     * (or re-enable when the console is closed)
+     */
+    groovyShell.setEnabled(!enabled);
+
     AlignFrame[] alignFrames = getAlignFrames();
     if (alignFrames != null)
     {
@@ -2986,12 +2920,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    */
   private java.util.concurrent.Semaphore block = new Semaphore(0);
 
-  /*
-   * groovy.ui.Console object - if Groovy jars are present and the 
-   * user has activated the Groovy console. Use via reflection to
-   * avoid compile-time dependency on Groovy libraries.
-   */
-  private static Object groovyConsole;
+  private static groovy.ui.Console groovyConsole;
 
   /**
    * add another dialog thread to the queue
@@ -3208,22 +3137,109 @@ public class Desktop extends jalview.jbgui.GDesktop implements
      * The dust settles...give focus to the tab we did this from.
      */
     myTopFrame.setDisplayedView(myTopFrame.alignPanel);
-
   }
 
-  public static AlignFrame getCurrentAlignFrame()
+  public static groovy.ui.Console getGroovyConsole()
   {
-    return currentAlignFrame;
+    return groovyConsole;
   }
 
-  public static void setCurrentAlignFrame(AlignFrame currentAlignFrame)
+  public static void transferFromDropTarget(List<String> files,
+          List<String> protocols, DropTargetDropEvent evt, Transferable t)
+          throws Exception
   {
-    Desktop.currentAlignFrame = currentAlignFrame;
-  }
 
-  public static Object getGroovyConsole()
-  {
-    return groovyConsole;
+    DataFlavor uriListFlavor = new DataFlavor(
+            "text/uri-list;class=java.lang.String");
+    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
+    {
+      // Works on Windows and MacOSX
+      Cache.log.debug("Drop handled as javaFileListFlavor");
+      evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
+      for (Object file : (List) t
+              .getTransferData(DataFlavor.javaFileListFlavor))
+      {
+        files.add(((File)file).toString());
+        protocols.add(FormatAdapter.FILE);
+      }
+    }
+    else
+    {
+      // Unix like behaviour
+      boolean added = false;
+      String data = null;
+      if (t.isDataFlavorSupported(uriListFlavor))
+      {
+        Cache.log.debug("Drop handled as uriListFlavor");
+        // This is used by Unix drag system
+        evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
+        data = (String) t.getTransferData(uriListFlavor);
+      }
+      if (data == null)
+      {
+        // fallback to text: workaround - on OSX where there's a JVM bug
+        Cache.log.debug("standard URIListFlavor failed. Trying text");
+        // try text fallback
+        data = (String) t.getTransferData(new DataFlavor(
+                "text/plain;class=java.lang.String"));
+        if (Cache.log.isDebugEnabled())
+        {
+          Cache.log.debug("fallback returned " + data);
+        }
+      }
+      while (protocols.size() < files.size())
+      {
+        Cache.log.debug("Adding missing FILE protocol for "
+                + files.get(protocols.size()));
+        protocols.add(FormatAdapter.FILE);
+      }
+      for (java.util.StringTokenizer st = new java.util.StringTokenizer(
+              data, "\r\n"); st.hasMoreTokens();)
+      {
+        added = true;
+        String s = st.nextToken();
+        if (s.startsWith("#"))
+        {
+          // the line is a comment (as per the RFC 2483)
+          continue;
+        }
+        java.net.URI uri = new java.net.URI(s);
+        if (uri.getScheme().toLowerCase().startsWith("http"))
+        {
+          protocols.add(FormatAdapter.URL);
+          files.add(uri.toString());
+        }
+        else
+        {
+          // otherwise preserve old behaviour: catch all for file objects
+          java.io.File file = new java.io.File(uri);
+          protocols.add(FormatAdapter.FILE);
+          files.add(file.toString());
+        }
+      }
+      if (Cache.log.isDebugEnabled())
+      {
+        if (data == null || !added)
+        {
+          Cache.log
+                  .debug("Couldn't resolve drop data. Here are the supported flavors:");
+          for (DataFlavor fl : t.getTransferDataFlavors())
+          {
+            Cache.log.debug("Supported transfer dataflavor: "
+                    + fl.toString());
+            evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
+            Object df = t.getTransferData(fl);
+            if (df != null)
+            {
+              Cache.log.debug("Retrieves: " + df);
+            }
+            else
+            {
+              Cache.log.debug("Retrieved nothing");
+            }
+          }
+        }
+      }
+    }
   }
-
 }