Merge branch 'improvement/JAL-3783_upgrade_to_groovy-4' into bug/JAL-4298_java_consol...
[jalview.git] / src / jalview / gui / Desktop.java
index 0ebc58b..b1f9000 100644 (file)
@@ -114,6 +114,8 @@ import jalview.bin.Cache;
 import jalview.bin.Jalview;
 import jalview.bin.Jalview.ExitCode;
 import jalview.bin.argparser.Arg;
+import jalview.bin.groovy.JalviewObject;
+import jalview.bin.groovy.JalviewObjectI;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.Sequence;
@@ -158,7 +160,7 @@ import jalview.ws.utils.UrlDownloadClient;
  */
 public class Desktop extends jalview.jbgui.GDesktop
         implements DropTargetListener, ClipboardOwner, IProgressIndicator,
-        jalview.api.StructureSelectionManagerProvider
+        jalview.api.StructureSelectionManagerProvider, JalviewObjectI
 {
   private static final String CITATION;
   static
@@ -1056,7 +1058,7 @@ public class Desktop extends jalview.jbgui.GDesktop
         {
           if (itf instanceof AlignFrame)
           {
-            Jalview.setCurrentAlignFrame((AlignFrame) itf);
+            Jalview.getInstance().setCurrentAlignFrame((AlignFrame) itf);
           }
           itf.requestFocus();
         }
@@ -1679,7 +1681,7 @@ public class Desktop extends jalview.jbgui.GDesktop
       {
       }
     }
-    Jalview.setCurrentAlignFrame(null);
+    Jalview.getInstance().setCurrentAlignFrame(null);
     jalview.bin.Console.info("ALL CLOSED");
 
     /*
@@ -2138,7 +2140,7 @@ public class Desktop extends jalview.jbgui.GDesktop
       return null;
     }
     List<AlignmentPanel> aps = new ArrayList<>();
-    AlignFrame[] frames = getAlignFrames();
+    AlignFrame[] frames = Desktop.getDesktopAlignFrames();
     if (frames == null)
     {
       return null;
@@ -2175,7 +2177,7 @@ public class Desktop extends jalview.jbgui.GDesktop
     List<AlignmentViewport> viewp = new ArrayList<>();
     if (desktop != null)
     {
-      AlignFrame[] frames = Desktop.getAlignFrames();
+      AlignFrame[] frames = Desktop.getDesktopAlignFrames();
 
       for (AlignFrame afr : frames)
       {
@@ -2536,12 +2538,12 @@ public class Desktop extends jalview.jbgui.GDesktop
    * 
    * @return an array of AlignFrame, or null if none found
    */
-  public static AlignFrame[] getAlignFrames()
+  @Override
+  public AlignFrame[] getAlignFrames()
   {
-    if (Jalview.isHeadlessMode())
+    if (desktop == null)
     {
-      // Desktop.desktop is null in headless mode
-      return new AlignFrame[] { Jalview.currentAlignFrame };
+      return null;
     }
 
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
@@ -2583,6 +2585,25 @@ public class Desktop extends jalview.jbgui.GDesktop
   }
 
   /**
+   * static version
+   */
+  public static AlignFrame[] getDesktopAlignFrames()
+  {
+    if (Jalview.isHeadlessMode())
+    {
+      // Desktop.desktop is null in headless mode
+      return Jalview.getInstance().getAlignFrames();
+    }
+
+    if (instance != null && desktop != null)
+    {
+      return instance.getAlignFrames();
+    }
+
+    return null;
+  }
+
+  /**
    * Returns an array of any AppJmol frames in the Desktop (or null if none).
    * 
    * @return
@@ -2640,8 +2661,10 @@ public class Desktop extends jalview.jbgui.GDesktop
   {
     if (groovyConsole == null)
     {
+      JalviewObjectI j = new JalviewObject(this);
       groovyConsole = new groovy.console.ui.Console();
-      groovyConsole.setVariable("Jalview", this);
+      groovyConsole.setVariable("Jalview", j);
+      groovyConsole.setVariable("currentAlFrame", getCurrentAlignFrame());
       groovyConsole.run();
 
       /*
@@ -2714,7 +2737,7 @@ public class Desktop extends jalview.jbgui.GDesktop
      */
     groovyShell.setEnabled(!enabled);
 
-    AlignFrame[] alignFrames = getAlignFrames();
+    AlignFrame[] alignFrames = getDesktopAlignFrames();
     if (alignFrames != null)
     {
       for (AlignFrame af : alignFrames)
@@ -3845,4 +3868,9 @@ public class Desktop extends jalview.jbgui.GDesktop
             { button }, button, modal, new JButton[] { jb }, false);
   }
 
+  @Override
+  public AlignFrame getCurrentAlignFrame()
+  {
+    return Jalview.getInstance().getCurrentAlignFrame();
+  }
 }