featureGroup show/hide and newView methods for applet's javascript API
[jalview.git] / src / jalview / bin / JalviewLite.java
index df11327..4d1c216 100755 (executable)
@@ -43,16 +43,22 @@ public class JalviewLite
   ///////////////////////////////////////////
   //The following public methods maybe called
   //externally, eg via javascript in HTML page
-
+  /**
+   * @return list of selected sequences separated by "¬"
+   */
   public String getSelectedSequences()
   {
+    return getSelectedSequences(currentAlignFrame);
+  }
+  public String getSelectedSequences(AlignFrame alf)
+  {
     StringBuffer result = new StringBuffer("");
 
-    if (initialAlignFrame.viewport.getSelectionGroup() != null)
+    if (alf.viewport.getSelectionGroup() != null)
     {
-      SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
+      SequenceI[] seqs = alf.viewport.getSelectionGroup().
           getSequencesInOrder(
-              initialAlignFrame.viewport.getAlignment());
+                  alf.viewport.getAlignment());
 
       for (int i = 0; i < seqs.length; i++)
       {
@@ -62,20 +68,27 @@ public class JalviewLite
 
     return result.toString();
   }
-
+  
   public String getAlignment(String format)
   {
-    return getAlignment(format, "true");
+    return getAlignment(currentAlignFrame, format, "true");
+  }
+  public String getAlignment(AlignFrame alf, String format)
+  {
+    return getAlignment(alf, format, "true");
   }
-
   public String getAlignment(String format, String suffix)
   {
+    return getAlignment(currentAlignFrame, format, suffix);
+  }
+  public String getAlignment(AlignFrame alf, String format, String suffix)
+  {
     try
     {
       boolean seqlimits = suffix.equalsIgnoreCase("true");
 
       String reply = new AppletFormatAdapter().formatSequences(format,
-          currentAlignFrame.viewport.getAlignment(), seqlimits);
+          alf.viewport.getAlignment(), seqlimits);
       return reply;
     }
     catch (Exception ex)
@@ -87,30 +100,63 @@ public class JalviewLite
 
   public void loadAnnotation(String annotation)
   {
+    loadAnnotation(currentAlignFrame, annotation);
+  }
+  public void loadAnnotation(AlignFrame alf, String annotation)
+  {
     if (new AnnotationFile().readAnnotationFile(
-        currentAlignFrame.getAlignViewport().getAlignment(), annotation,
+        alf.getAlignViewport().getAlignment(), annotation,
         AppletFormatAdapter.PASTE))
     {
-      currentAlignFrame.alignPanel.fontChanged();
-      currentAlignFrame.alignPanel.setScrollValues(0, 0);
+      alf.alignPanel.fontChanged();
+      alf.alignPanel.setScrollValues(0, 0);
     }
     else
     {
-      currentAlignFrame.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
+      alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
     }
   }
 
   public String getFeatures(String format)
   {
-    return currentAlignFrame.outputFeatures(false, format);
+    return getFeatures(currentAlignFrame, format);
+  }
+  public String getFeatures(AlignFrame alf, String format)
+  {
+    return alf.outputFeatures(false, format);
   }
-
   public String getAnnotation()
   {
-    return currentAlignFrame.outputAnnotations(false);
+    return getAnnotation(currentAlignFrame);
+  }
+  public String getAnnotation(AlignFrame alf)
+  {
+    return alf.outputAnnotations(false);
+  }
+  public AlignFrame newView()
+  {
+    return newView(currentAlignFrame);
+  }
+  public AlignFrame newView(String name)
+  {
+    return newView(currentAlignFrame, name);
   }
 
-  public void loadAlignment(String text, String title)
+  public AlignFrame newView(AlignFrame alf)
+  {
+    return alf.newView(null);
+  }
+  public AlignFrame newView(AlignFrame alf, String name)
+  {
+    return alf.newView(name);
+  }
+  /**
+   * 
+   * @param text alignment file as a string
+   * @param title window title 
+   * @return null or new alignment frame
+   */
+  public AlignFrame loadAlignment(String text, String title)
   {
     Alignment al = null;
     String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
@@ -121,13 +167,14 @@ public class JalviewLite
                                               format);
       if (al.getHeight() > 0)
       {
-        new AlignFrame(al, this, title, false);
+        return new AlignFrame(al, this, title, false);
       }
     }
     catch (java.io.IOException ex)
     {
       ex.printStackTrace();
     }
+    return null;
   }
 
   ////////////////////////////////////////////////
@@ -276,6 +323,7 @@ public class JalviewLite
         if (frame instanceof AlignFrame)
         {
           currentAlignFrame = (AlignFrame) frame;
+          System.err.println("Activated window "+frame);
         }
       }
 
@@ -643,4 +691,86 @@ public class JalviewLite
       return file;
     }
   }
+
+  public String[] tabbedListToArray(String list)
+  {
+    if (list==null || list.equals(""))
+      return null;
+    java.util.Vector jv = new Vector();
+    int cp=0,pos;
+    while ((pos=list.indexOf("\t",cp))>cp)
+    {
+      jv.addElement(list.substring(cp,pos));
+      cp = pos+1;
+    }
+    if (cp<list.length())
+    {
+      jv.addElement(list.substring(cp));
+    }
+    if (jv.size()>0)
+    { String[] v = new String[jv.size()];
+      jv.copyInto(v);
+      jv.removeAllElements();
+      System.err.println("Array from Tabbed List:\n"+v.length+"\n"+v.toString());
+      return v;
+    }
+    System.err.println("Empty Array from Tabbed List");
+    return null;
+  }
+
+  public String ArraytotabbedList(String[] list)
+  {
+    StringBuffer v = new StringBuffer();
+    if (list!=null)
+    {
+      for (int i=0,iSize=list.length-1;i<iSize;i++)
+      { 
+        if (list[i]!=null)
+        {  
+          v.append(list[i]); 
+        }
+        v.append("\t");
+      }
+      if (list[list.length-1]!=null)
+      { v.append(list[list.length-1]);
+      }
+      System.err.println("Tabbed List:\n"+v.toString());
+      return v.toString();
+    }
+    System.err.println("Empty Tabbed List\n");
+    return "";
+  }
+  /**
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroups()
+   */
+  public String getFeatureGroups()
+  {
+    String lst = ArraytotabbedList(currentAlignFrame.getFeatureGroups());
+    return lst;
+  }
+
+  /**
+   * @param visible
+   * @return
+   * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
+   */
+  public String getFeatureGroupsOfState(boolean visible)
+  {
+    return ArraytotabbedList(currentAlignFrame.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)
+  {
+    boolean st = state;//!(state==null || state.equals("") || state.toLowerCase().equals("false"));
+    alf.setFeatureGroupState(tabbedListToArray(groups), st);
+  }
+  public void setFeatureGroupState(String groups, boolean state)
+  {
+    setFeatureGroupState(currentAlignFrame, groups, state);
+  }
 }