SO option true for Java (from Jalview-JS/develop)
[jalview.git] / src / jalview / bin / JalviewAppLoader.java
index 921ccdf..a7c5042 100644 (file)
@@ -1,12 +1,12 @@
 package jalview.bin;
 
-import jalview.api.AlignFrameI;
 import jalview.api.JalviewApp;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
@@ -17,6 +17,7 @@ import jalview.gui.Desktop;
 import jalview.io.AnnotationFile;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.DataSourceType;
+import jalview.io.FeaturesFile;
 import jalview.io.FileFormat;
 import jalview.io.FileFormatI;
 import jalview.io.FileFormats;
@@ -24,6 +25,7 @@ import jalview.io.IdentifyFile;
 import jalview.io.JPredFile;
 import jalview.io.JnetAnnotationMaker;
 import jalview.io.NewickFile;
+import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.HttpUtils;
 import jalview.util.MessageManager;
@@ -758,14 +760,14 @@ public class JalviewAppLoader
     return arrayToSeparatorList(array, separator);
   }
 
-  public String getSelectedSequencesFrom(AlignFrameI alf, String sep)
+  public String getSelectedSequencesFrom(AlignFrame alf, String sep)
   {
     StringBuffer result = new StringBuffer("");
     if (sep == null || sep.length() == 0)
     {
       sep = separator; // "+0x00AC;
     }
-    AlignViewport v = ((AlignFrame) alf).getViewport();
+    AlignViewport v = alf.getViewport();
     if (v.getSelectionGroup() != null)
     {
       SequenceI[] seqs = v.getSelectionGroup()
@@ -781,7 +783,7 @@ public class JalviewAppLoader
     return result.toString();
   }
 
-  public void setFeatureGroupStateOn(final AlignFrameI alf,
+  public void setFeatureGroupStateOn(final AlignFrame alf,
           final String groups, boolean state)
   {
     java.awt.EventQueue.invokeLater(new Runnable()
@@ -789,19 +791,19 @@ public class JalviewAppLoader
       @Override
       public void run()
       {
-        ((AlignFrame) alf).setFeatureGroupState(
+        alf.setFeatureGroupState(
                 separatorListToArray(groups, separator), state);
       }
     });
   }
 
-  public String getFeatureGroupsOfStateOn(AlignFrameI alf, boolean visible)
+  public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
   {
     return arrayToSeparatorList(
-            ((AlignFrame) alf).getFeatureGroupsOfState(visible));
+            alf.getFeatureGroupsOfState(visible));
   }
 
-  public void scrollViewToIn(final AlignFrameI alf, final String topRow,
+  public void scrollViewToIn(final AlignFrame alf, final String topRow,
           final String leftHandColumn)
   {
     java.awt.EventQueue.invokeLater(new Runnable()
@@ -811,8 +813,8 @@ public class JalviewAppLoader
       {
         try
         {
-          ((AlignFrame) alf).scrollTo(new Integer(topRow).intValue(),
-                  new Integer(leftHandColumn).intValue());
+          alf.scrollTo(Integer.valueOf(topRow).intValue(),
+                  Integer.valueOf(leftHandColumn).intValue());
 
         } catch (Exception ex)
         {
@@ -825,7 +827,7 @@ public class JalviewAppLoader
     });
   }
 
-  public void scrollViewToRowIn(final AlignFrameI alf, final String topRow)
+  public void scrollViewToRowIn(final AlignFrame alf, final String topRow)
   {
 
     java.awt.EventQueue.invokeLater(new Runnable()
@@ -835,7 +837,7 @@ public class JalviewAppLoader
       {
         try
         {
-          ((AlignFrame) alf).scrollToRow(new Integer(topRow).intValue());
+          alf.scrollToRow(Integer.valueOf(topRow).intValue());
 
         } catch (Exception ex)
         {
@@ -848,7 +850,7 @@ public class JalviewAppLoader
     });
   }
 
-  public void scrollViewToColumnIn(final AlignFrameI alf,
+  public void scrollViewToColumnIn(final AlignFrame alf,
           final String leftHandColumn)
   {
     java.awt.EventQueue.invokeLater(new Runnable()
@@ -859,8 +861,9 @@ public class JalviewAppLoader
       {
         try
         {
-          ((AlignFrame) alf)
-                  .scrollToColumn(new Integer(leftHandColumn).intValue());
+          alf
+                  .scrollToColumn(
+                          Integer.valueOf(leftHandColumn).intValue());
 
         } catch (Exception ex)
         {
@@ -874,10 +877,10 @@ public class JalviewAppLoader
 
   }
 
-  public boolean addPdbFile(AlignFrameI alf, String sequenceId,
+  public boolean addPdbFile(AlignFrame alf, String sequenceId,
           String pdbEntryString, String pdbFile)
   {
-    AlignFrame alFrame = (AlignFrame) alf;
+    AlignFrame alFrame = alf;
     SequenceI toaddpdb = alFrame.getViewport().getAlignment()
             .findName(sequenceId);
     boolean needtoadd = false;
@@ -927,7 +930,7 @@ public class JalviewAppLoader
     return true;
   }
 
-  public AlignFrameI loadAlignment(String text, int width, int height,
+  public AlignFrame loadAlignment(String text, int width, int height,
           String title)
   {
     AlignmentI al = null;
@@ -949,18 +952,18 @@ public class JalviewAppLoader
     return null;
   }
 
-  public String getFeatureGroupsOn(AlignFrameI alf)
+  public String getFeatureGroupsOn(AlignFrame alf)
   {
     return arrayToSeparatorList(
-            ((AlignFrame) alf).getFeatureGroups());
+            alf.getFeatureGroups());
   }
 
-  public void highlightIn(final AlignFrameI alf, final String sequenceId,
+  public void highlightIn(final AlignFrame alf, final String sequenceId,
           final String position, final String alignedPosition)
   {
     // TODO: could try to highlight in all alignments if alf==null
     jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
-            ((AlignFrame) alf).getViewport().getAlignment()
+            alf.getViewport().getAlignment()
                     .getSequencesArray());
     final SequenceI sq = matcher.findIdMatch(sequenceId);
     if (sq != null)
@@ -968,7 +971,7 @@ public class JalviewAppLoader
       int apos = -1;
       try
       {
-        apos = new Integer(position).intValue();
+        apos = Integer.valueOf(position).intValue();
         apos--;
       } catch (NumberFormatException ex)
       {
@@ -1006,7 +1009,7 @@ public class JalviewAppLoader
     }
   }
 
-  public void selectIn(final AlignFrameI alf, String sequenceIds,
+  public void selectIn(final AlignFrame alf, String sequenceIds,
           String columns, String sep)
   {
     if (sep == null || sep.length() == 0)
@@ -1026,9 +1029,9 @@ public class JalviewAppLoader
     String[] cols = JalviewAppLoader.separatorListToArray(columns, sep);
     final SequenceGroup sel = new SequenceGroup();
     final ColumnSelection csel = new ColumnSelection();
-    AlignmentI al = ((AlignFrame) alf).getViewport().getAlignment();
+    AlignmentI al = alf.getViewport().getAlignment();
     jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
-            ((AlignFrame) alf).getViewport().getAlignment()
+            alf.getViewport().getAlignment()
                     .getSequencesArray());
     int start = 0, end = al.getWidth(), alw = al.getWidth();
     boolean seqsfound = true;
@@ -1066,7 +1069,7 @@ public class JalviewAppLoader
           int from = -1, to = -1;
           try
           {
-            from = new Integer(cl.substring(0, p)).intValue();
+            from = Integer.valueOf(cl.substring(0, p)).intValue();
             from--;
           } catch (NumberFormatException ex)
           {
@@ -1077,7 +1080,7 @@ public class JalviewAppLoader
           }
           try
           {
-            to = new Integer(cl.substring(p + 1)).intValue();
+            to = Integer.valueOf(cl.substring(p + 1)).intValue();
             to--;
           } catch (NumberFormatException ex)
           {
@@ -1137,7 +1140,7 @@ public class JalviewAppLoader
           int r = -1;
           try
           {
-            r = new Integer(cl).intValue();
+            r = Integer.valueOf(cl).intValue();
             r--;
           } catch (NumberFormatException ex)
           {
@@ -1213,16 +1216,16 @@ public class JalviewAppLoader
         @Override
         public void run()
         {
-          ((AlignFrame) alf).select(sel, csel, ((AlignFrame) alf)
+          alf.select(sel, csel, alf
                   .getCurrentView().getAlignment().getHiddenColumns());
         }
       });
     }
   }
 
-  public String getAlignmentOrderFrom(AlignFrameI alf, String sep)
+  public String getAlignmentOrderFrom(AlignFrame alf, String sep)
   {
-    AlignmentI alorder = ((AlignFrame) alf).getViewport().getAlignment();
+    AlignmentI alorder = alf.getViewport().getAlignment();
     String[] order = new String[alorder.getHeight()];
     for (int i = 0; i < order.length; i++)
     {
@@ -1231,12 +1234,12 @@ public class JalviewAppLoader
     return arrayToSeparatorList(order, sep);
   }
 
-  public String getSelectedSequencesAsAlignmentFrom(AlignFrameI alf,
+  public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
           String format, String suffix)
   {
     try
     {
-      AlignViewport vp = ((AlignFrame) alf).getViewport();
+      AlignViewport vp = alf.getViewport();
       FileFormatI theFormat = FileFormats.getInstance().forName(format);
       boolean seqlimits = (suffix == null
               || suffix.equalsIgnoreCase("true"));
@@ -1259,7 +1262,7 @@ public class JalviewAppLoader
     return "";
   }
 
-  public String orderAlignmentBy(AlignFrameI alf, String order,
+  public String orderAlignmentBy(AlignFrame alf, String order,
           String undoName, String sep)
   {
     if (sep == null || sep.length() == 0)
@@ -1271,7 +1274,7 @@ public class JalviewAppLoader
     if (ids != null && ids.length > 0)
     {
       jalview.analysis.SequenceIdMatcher matcher = new jalview.analysis.SequenceIdMatcher(
-              ((AlignFrame) alf).getViewport().getAlignment()
+              alf.getViewport().getAlignment()
                       .getSequencesArray());
       int s = 0;
       sqs = new SequenceI[ids.length];
@@ -1312,10 +1315,10 @@ public class JalviewAppLoader
     final String _undoName = undoName;
     // TODO: deal with synchronization here: cannot raise any events until after
     // this has returned.
-    return ((AlignFrame) alf).sortBy(aorder, _undoName) ? "true" : "";
+    return alf.sortBy(aorder, _undoName) ? "true" : "";
   }
 
-  public String getAlignmentFrom(AlignFrameI alf, String format,
+  public String getAlignmentFrom(AlignFrame alf, String format,
           String suffix)
   {
     try
@@ -1325,7 +1328,7 @@ public class JalviewAppLoader
 
       FileFormatI theFormat = FileFormats.getInstance().forName(format);
       String reply = new AppletFormatAdapter().formatSequences(theFormat,
-              ((AlignFrame) alf).getViewport().getAlignment(), seqlimits);
+              alf.getViewport().getAlignment(), seqlimits);
       return reply;
     } catch (IllegalArgumentException ex)
     {
@@ -1335,4 +1338,146 @@ public class JalviewAppLoader
     }
   }
 
+  public void loadAnnotationFrom(AlignFrame alf, String annotation)
+  {
+    if (new AnnotationFile().annotateAlignmentView(
+            alf.getViewport(), annotation,
+            DataSourceType.PASTE))
+    {
+      alf.alignPanel.fontChanged();
+      alf.alignPanel.setScrollValues(0, 0);
+    }
+    else
+    {
+      alf.parseFeaturesFile(annotation,
+              DataSourceType.PASTE);
+    }
+  }
+
+  public boolean loadFeaturesFrom(AlignFrame alf, String features,
+          boolean autoenabledisplay)
+  {
+    boolean ret = alf.parseFeaturesFile(features,
+            DataSourceType.PASTE);
+    if (!ret)
+    {
+      return false;
+    }
+    if (autoenabledisplay)
+    {
+      alf.getViewport().setShowSequenceFeatures(true);
+      // this next was for a checkbox in JalviewLite
+      // ((AlignFrame) alf).getViewport().sequenceFeatures.setState(true);
+    }
+    return true;
+  }
+
+  public String getFeaturesFrom(AlignFrame alf, String format)
+  {
+    AlignFrame f = (alf);
+
+    String features;
+    FeaturesFile formatter = new FeaturesFile();
+    if (format.equalsIgnoreCase("Jalview"))
+    {
+      features = formatter.printJalviewFormat(
+              f.getViewport().getAlignment().getSequencesArray(),
+              f.alignPanel.getFeatureRenderer(), true);
+    }
+    else
+    {
+      features = formatter.printGffFormat(
+              f.getViewport().getAlignment().getSequencesArray(),
+              f.alignPanel.getFeatureRenderer(), true);
+    }
+
+    if (features == null)
+    {
+      features = "";
+    }
+    return features;
+
+  }
+
+  public String getAnnotationFrom(AlignFrame alf)
+  {
+    AlignFrame f = alf;
+    String annotation = new AnnotationFile()
+            .printAnnotationsForView(f.getViewport());
+    return annotation;
+  }
+
+  // public AlignFrame newViewFrom(AlignFrame alf, String name)
+  // {
+  // return (AlignFrame) alf.newView(name, true);
+  // }
+  //
+  public String[] separatorListToArray(String list)
+  {
+    return separatorListToArray(list, separator);
+  }
+
+  public Object[] getSelectionForListener(AlignFrame currentFrame,
+          SequenceGroup seqsel, ColumnSelection colsel,
+          HiddenColumns hidden, SelectionSource source, Object alignFrame)
+  {
+    // System.err.println("Testing selection event relay to
+    // jsfunction:"+_listener);
+    String setid = "";
+    AlignFrame src = (AlignFrame) alignFrame;
+    if (source != null)
+    {
+      if (source instanceof AlignViewport
+              && currentFrame.getViewport() == source)
+      {
+        // should be valid if it just generated an event!
+        src = currentFrame;
+
+      }
+    }
+    String[] seqs = new String[] {};
+    String[] cols = new String[] {};
+    int strt = 0, end = (src == null) ? -1
+            : src.alignPanel.av.getAlignment().getWidth();
+    if (seqsel != null && seqsel.getSize() > 0)
+    {
+      seqs = new String[seqsel.getSize()];
+      for (int i = 0; i < seqs.length; i++)
+      {
+        seqs[i] = seqsel.getSequenceAt(i).getName();
+      }
+      if (strt < seqsel.getStartRes())
+      {
+        strt = seqsel.getStartRes();
+      }
+      if (end == -1 || end > seqsel.getEndRes())
+      {
+        end = seqsel.getEndRes();
+      }
+    }
+    if (colsel != null && !colsel.isEmpty())
+    {
+      if (end == -1)
+      {
+        end = colsel.getMax() + 1;
+      }
+      cols = new String[colsel.getSelected().size()];
+      for (int i = 0; i < cols.length; i++)
+      {
+        cols[i] = "" + (1 + colsel.getSelected().get(i).intValue());
+      }
+    }
+    else
+    {
+      if (seqsel != null && seqsel.getSize() > 0)
+      {
+        // send a valid range, otherwise we send the empty selection
+        cols = new String[2];
+        cols[0] = "" + (1 + strt) + "-" + (1 + end);
+      }
+    }
+    return new Object[] { src, setid, arrayToSeparatorList(seqs),
+        arrayToSeparatorList(cols) };
+  }
+
 }
\ No newline at end of file