added bugfix todo
[jalview.git] / src / jalview / bin / JalviewLite.java
index c9977fd..4d45f8c 100755 (executable)
@@ -48,9 +48,9 @@ public class JalviewLite
    */
   public String getSelectedSequences()
   {
-    return getSelectedSequences(currentAlignFrame);
+    return getSelectedSequencesFrom(getDefaultTargetFrame());
   }
-  public String getSelectedSequences(AlignFrame alf)
+  public String getSelectedSequencesFrom(AlignFrame alf)
   {
     StringBuffer result = new StringBuffer("");
 
@@ -71,17 +71,17 @@ public class JalviewLite
   
   public String getAlignment(String format)
   {
-    return getAlignment(currentAlignFrame, format, "true");
+    return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
   }
-  public String getAlignment(AlignFrame alf, String format)
+  public String getAlignmentFrom(AlignFrame alf, String format)
   {
-    return getAlignment(alf, format, "true");
+    return getAlignmentFrom(alf, format, "true");
   }
   public String getAlignment(String format, String suffix)
   {
-    return getAlignment(currentAlignFrame, format, suffix);
+    return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
   }
-  public String getAlignment(AlignFrame alf, String format, String suffix)
+  public String getAlignmentFrom(AlignFrame alf, String format, String suffix)
   {
     try
     {
@@ -100,9 +100,9 @@ public class JalviewLite
 
   public void loadAnnotation(String annotation)
   {
-    loadAnnotation(currentAlignFrame, annotation);
+    loadAnnotationFrom(getDefaultTargetFrame(), annotation);
   }
-  public void loadAnnotation(AlignFrame alf, String annotation)
+  public void loadAnnotationFrom(AlignFrame alf, String annotation)
   {
     if (new AnnotationFile().readAnnotationFile(
         alf.getAlignViewport().getAlignment(), annotation,
@@ -119,34 +119,34 @@ public class JalviewLite
 
   public String getFeatures(String format)
   {
-    return getFeatures(currentAlignFrame, format);
+    return getFeaturesFrom(getDefaultTargetFrame(), format);
   }
-  public String getFeatures(AlignFrame alf, String format)
+  public String getFeaturesFrom(AlignFrame alf, String format)
   {
     return alf.outputFeatures(false, format);
   }
   public String getAnnotation()
   {
-    return getAnnotation(currentAlignFrame);
+    return getAnnotationFrom(getDefaultTargetFrame());
   }
-  public String getAnnotation(AlignFrame alf)
+  public String getAnnotationFrom(AlignFrame alf)
   {
     return alf.outputAnnotations(false);
   }
   public AlignFrame newView()
   {
-    return newView(currentAlignFrame);
+    return newViewFrom(getDefaultTargetFrame());
   }
   public AlignFrame newView(String name)
   {
-    return newView(currentAlignFrame, name);
+    return newViewFrom(getDefaultTargetFrame(), name);
   }
 
-  public AlignFrame newView(AlignFrame alf)
+  public AlignFrame newViewFrom(AlignFrame alf)
   {
     return alf.newView(null);
   }
-  public AlignFrame newView(AlignFrame alf, String name)
+  public AlignFrame newViewFrom(AlignFrame alf, String name)
   {
     return alf.newView(name);
   }
@@ -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;
@@ -210,6 +217,22 @@ public class JalviewLite
     {
       debug = dbg.toLowerCase().equals("true");
     }
+    /**
+     * get the separator parameter if present
+     */
+    String sep = getParameter("separator");
+    if (sep!=null)
+    {
+      if (sep.length()>0)
+      {      separator = sep;
+        if (debug)
+        {
+          System.err.println("Separator set to '"+separator+"'");
+        }
+      } else {
+        throw new Error("Invalid separator parameter - must be non-zero length");
+      }
+    }
     int r = 255;
     int g = 255;
     int b = 255;
@@ -320,10 +343,14 @@ public class JalviewLite
         }
         lastFrameX -= 40;
         lastFrameY -= 40;
+        if (frame instanceof EmbmenuFrame)
+        {
+          ((EmbmenuFrame) frame).destroyMenus();
+        }
         frame.setMenuBar(null);
         frame.dispose();
       }
-
+      
       public void windowActivated(WindowEvent e)
       {
         if (frame instanceof AlignFrame)
@@ -334,8 +361,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);
   }
@@ -449,18 +494,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");
@@ -727,17 +772,43 @@ public class JalviewLite
       return file;
     }
   }
-
-  public String[] tabbedListToArray(String list)
+  /**
+   * @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
+  /**
+   * parse the string into a list
+   * @param list
+   * @return elements separated by separator
+   */
+  public String[] separatorListToArray(String list)
   {
+    int seplen = separator.length();
     if (list==null || list.equals(""))
       return null;
     java.util.Vector jv = new Vector();
     int cp=0,pos;
-    while ((pos=list.indexOf("\t",cp))>cp)
+    while ((pos=list.indexOf(separator,cp))>cp)
     {
       jv.addElement(list.substring(cp,pos));
-      cp = pos+1;
+      cp = pos+seplen;
     }
     if (cp<list.length())
     {
@@ -745,22 +816,33 @@ public class JalviewLite
     }
     if (jv.size()>0)
     { String[] v = new String[jv.size()];
-      jv.copyInto(v);
+      for (int i=0; i<v.length; i++)
+      {
+        v[i] = (String) jv.elementAt(i);
+      }
       jv.removeAllElements();
       if (debug)
       {
-        System.err.println("Array from Tabbed List:\n"+v.length+"\n"+v.toString());
+        System.err.println("Array from '"+separator+"' separated List:\n"+v.length);
+        for (int i=0; i<v.length;i++)
+        {
+          System.err.println("item "+i+" '"+v[i]+"'");
+        }
       }
       return v;
     }
     if (debug)
     {
-      System.err.println("Empty Array from Tabbed List");
+      System.err.println("Empty Array from '"+separator+"' separated List");
     }
     return null;
   }
-
-  public String ArraytotabbedList(String[] list)
+  /**
+   * concatenate the list with separator
+   * @param list
+   * @return concatenated string
+   */
+  public String arrayToSeparatorList(String[] list)
   {
     StringBuffer v = new StringBuffer();
     if (list!=null)
@@ -771,20 +853,21 @@ public class JalviewLite
         {  
           v.append(list[i]); 
         }
-        v.append("\t");
+        v.append(separator);
       }
       if (list[list.length-1]!=null)
       { v.append(list[list.length-1]);
       }
       if (debug)
       {
-        System.err.println("Tabbed List:\n"+v.toString());
+        System.err.println("Returning '"+separator+"' separated List:\n");
+        System.err.println(v);
       }
       return v.toString();
     }
     if (debug)
     {
-      System.err.println("Empty Tabbed List\n");
+      System.err.println("Returning empty '"+separator+"' separated List\n");
     }
     return "";
   }
@@ -794,10 +877,19 @@ public class JalviewLite
    */
   public String getFeatureGroups()
   {
-    String lst = ArraytotabbedList(currentAlignFrame.getFeatureGroups());
+    String lst = arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroups());
+    return lst;
+  }
+  /**
+   * @param alf alignframe to get feature groups on
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroups()
+   */
+  public String getFeatureGroupsOn(AlignFrame alf)
+  {
+    String lst = arrayToSeparatorList(alf.getFeatureGroups());
     return lst;
   }
-
   /**
    * @param visible
    * @return
@@ -805,20 +897,45 @@ public class JalviewLite
    */
   public String getFeatureGroupsOfState(boolean visible)
   {
-    return ArraytotabbedList(currentAlignFrame.getFeatureGroupsOfState(visible));
+    return arrayToSeparatorList(getDefaultTargetFrame().getFeatureGroupsOfState(visible));
   }
   /**
+   * @param alf align frame to get groups of state visible
+   * @param visible
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
+   */
+  public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
+  {
+    return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
+  }  /**
    * @param groups tab separated list of group names 
    * @param state true or false
    * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[], boolean)
    */
-  public void setFeatureGroupState(AlignFrame alf, String groups, boolean state)
+  public void setFeatureGroupStateOn(AlignFrame alf, String groups, boolean state)
   {
     boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
-    alf.setFeatureGroupState(tabbedListToArray(groups), st);
+    alf.setFeatureGroupState(separatorListToArray(groups), st);
   }
   public void setFeatureGroupState(String groups, boolean state)
   {
-    setFeatureGroupState(currentAlignFrame, groups, state);
+    setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
+  }
+  /**
+   * List separator string
+   * @return the separator
+   */
+  public String getSeparator()
+  {
+    return separator;
+  }
+  /**
+   * List separator string
+   * @param separator the separator to set
+   */
+  public void setSeparator(String separator)
+  {
+    this.separator = separator;
   }
 }