fixed applet API calls so they operate on the embedded applet instance if no other...
authorjprocter <Jim Procter>
Fri, 26 Oct 2007 11:14:17 +0000 (11:14 +0000)
committerjprocter <Jim Procter>
Fri, 26 Oct 2007 11:14:17 +0000 (11:14 +0000)
src/jalview/bin/JalviewLite.java

index 5b103c5..a1f9e8e 100755 (executable)
@@ -48,7 +48,7 @@ public class JalviewLite
    */
   public String getSelectedSequences()
   {
-    return getSelectedSequencesFrom(currentAlignFrame);
+    return getSelectedSequencesFrom(getDefaultTargetFrame());
   }
   public String getSelectedSequencesFrom(AlignFrame alf)
   {
@@ -71,7 +71,7 @@ public class JalviewLite
   
   public String getAlignment(String format)
   {
-    return getAlignmentFrom(currentAlignFrame, format, "true");
+    return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
   }
   public String getAlignmentFrom(AlignFrame alf, String format)
   {
@@ -79,7 +79,7 @@ public class JalviewLite
   }
   public String getAlignment(String format, String suffix)
   {
-    return getAlignmentFrom(currentAlignFrame, format, suffix);
+    return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
   }
   public String getAlignmentFrom(AlignFrame alf, String format, String suffix)
   {
@@ -100,7 +100,7 @@ public class JalviewLite
 
   public void loadAnnotation(String annotation)
   {
-    loadAnnotationFrom(currentAlignFrame, annotation);
+    loadAnnotationFrom(getDefaultTargetFrame(), annotation);
   }
   public void loadAnnotationFrom(AlignFrame alf, String annotation)
   {
@@ -119,7 +119,7 @@ public class JalviewLite
 
   public String getFeatures(String format)
   {
-    return getFeaturesFrom(currentAlignFrame, format);
+    return getFeaturesFrom(getDefaultTargetFrame(), format);
   }
   public String getFeaturesFrom(AlignFrame alf, String format)
   {
@@ -127,7 +127,7 @@ public class JalviewLite
   }
   public String getAnnotation()
   {
-    return getAnnotationFrom(currentAlignFrame);
+    return getAnnotationFrom(getDefaultTargetFrame());
   }
   public String getAnnotationFrom(AlignFrame alf)
   {
@@ -135,11 +135,11 @@ public class JalviewLite
   }
   public AlignFrame newView()
   {
-    return newViewFrom(currentAlignFrame);
+    return newViewFrom(getDefaultTargetFrame());
   }
   public AlignFrame newView(String name)
   {
-    return newViewFrom(currentAlignFrame, name);
+    return newViewFrom(getDefaultTargetFrame(), name);
   }
 
   public AlignFrame newViewFrom(AlignFrame alf)
@@ -188,11 +188,18 @@ public class JalviewLite
   String file = "No file";
   Button launcher = new Button("Start Jalview");
 
-  //The currentAlignFrame is static, it will change
-  //if and when the user selects a new window
+  /**
+   * The currentAlignFrame is static, it will change
+   * if and when the user selects a new window.
+   * Note that it will *never* point back to the embedded AlignFrame 
+   * if the applet is started as embedded on the page and then afterwards a new view is created.
+   */
   public static AlignFrame currentAlignFrame;
 
-  //This is the first frame to be displayed, and does not change
+  /** 
+   * This is the first frame to be displayed, and does not change.
+   * API calls will default to this instance if currentAlignFrame is null.
+   */
   AlignFrame initialAlignFrame;
 
   boolean embedded = false;
@@ -339,7 +346,7 @@ public class JalviewLite
         frame.setMenuBar(null);
         frame.dispose();
       }
-
+      
       public void windowActivated(WindowEvent e)
       {
         if (frame instanceof AlignFrame)
@@ -350,8 +357,26 @@ public class JalviewLite
             System.err.println("Activated window "+frame);
           }
         }
+        // be good.
+        super.windowActivated(e);
       }
-
+      /* Probably not necessary to do this - see TODO above.
+       * (non-Javadoc)
+       * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
+       *
+      public void windowDeactivated(WindowEvent e)
+      {
+        if (currentAlignFrame == frame)
+        {
+          currentAlignFrame = null;
+          if (debug)
+          {
+            System.err.println("Deactivated window "+frame);
+          }
+        }
+        super.windowDeactivated(e);
+      }
+       */
     });
     frame.setVisible(true);
   }
@@ -465,18 +490,18 @@ public class JalviewLite
       }
       if ( (al != null) && (al.getHeight() > 0))
       {
-        currentAlignFrame = new AlignFrame(al,
+        initialAlignFrame =  new AlignFrame(al,
                                            applet,
                                            file,
                                            embedded);
+        // update the focus.
+        currentAlignFrame = initialAlignFrame;
 
         if (protocol == jalview.io.AppletFormatAdapter.PASTE)
         {
           currentAlignFrame.setTitle("Sequences from " + getDocumentBase());
         }
 
-        initialAlignFrame = currentAlignFrame;
-
         currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
 
         String treeFile = applet.getParameter("tree");
@@ -744,6 +769,23 @@ public class JalviewLite
     }
   }
   /**
+   * @return the default alignFrame acted on by the public applet methods.
+   * May return null with an error message on System.err indicating the fact. 
+   */
+  protected AlignFrame getDefaultTargetFrame()
+  {
+    if (currentAlignFrame!=null)
+    {
+      return currentAlignFrame;
+    }
+    if (initialAlignFrame!=null)
+    {
+      return initialAlignFrame;
+    }
+    System.err.println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
+    return null;
+  }
+  /**
    * separator used for separatorList
    */
   protected String separator = "|"; // this is a safe(ish) separator - tabs don't work for firefox
@@ -831,7 +873,7 @@ public class JalviewLite
    */
   public String getFeatureGroups()
   {
-    String lst = arrayToSeparatorList(currentAlignFrame.getFeatureGroups());
+    String lst = arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroups());
     return lst;
   }
   /**
@@ -851,7 +893,7 @@ public class JalviewLite
    */
   public String getFeatureGroupsOfState(boolean visible)
   {
-    return arrayToSeparatorList(currentAlignFrame.getFeatureGroupsOfState(visible));
+    return arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroupsOfState(visible));
   }
   /**
    * @param alf align frame to get groups of state visible
@@ -874,7 +916,7 @@ public class JalviewLite
   }
   public void setFeatureGroupState(String groups, boolean state)
   {
-    setFeatureGroupStateOn(currentAlignFrame, groups, state);
+    setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
   }
   /**
    * List separator string