JAL-3851 merged to develop 2022-03-22
[jalview.git] / src / jalview / gui / AlignFrame.java
index d791842..168f6c5 100644 (file)
@@ -57,9 +57,12 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Deque;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Vector;
+import java.util.concurrent.CompletableFuture;
 
 import javax.swing.ButtonGroup;
 import javax.swing.JCheckBoxMenuItem;
@@ -369,8 +372,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       // modifyPID.setEnabled(false);
     }
 
-    String sortby = Cache.getDefault("SORT_ALIGNMENT",
-            "No sort");
+    String sortby = Cache.getDefault("SORT_ALIGNMENT", "No sort");
 
     if (sortby.equals("Id"))
     {
@@ -489,8 +491,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             });
     if (Cache.getDefault("VERSION", "DEVELOPMENT").toLowerCase(Locale.ROOT)
             .indexOf("devel") > -1
-            || Cache.getDefault("VERSION", "DEVELOPMENT").toLowerCase(Locale.ROOT)
-                    .indexOf("test") > -1)
+            || Cache.getDefault("VERSION", "DEVELOPMENT")
+                    .toLowerCase(Locale.ROOT).indexOf("test") > -1)
     {
       formatMenu.add(vsel);
     }
@@ -579,7 +581,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           }
           if (viewport.cursorMode)
           {
-            alignPanel.getSeqPanel().moveCursor(0, -1,evt.isShiftDown());
+            alignPanel.getSeqPanel().moveCursor(0, -1, evt.isShiftDown());
           }
 
           break;
@@ -4306,7 +4308,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                       jws2servs.attachWSMenuEntry(webService, me);
                       for (Jws2Instance sv : jws2servs.getServices())
                       {
-                        if (sv.description.toLowerCase(Locale.ROOT).contains("jpred"))
+                        if (sv.description.toLowerCase(Locale.ROOT)
+                                .contains("jpred"))
                         {
                           for (JMenuItem jmi : legacyItems)
                           {
@@ -4465,6 +4468,22 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return showp;
   }
 
+  public List<String> getProducts()
+  {
+    SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
+    AlignmentI dataset = viewport.getAlignment().getDataset();
+
+    boolean dna = viewport.getAlignment().isNucleotide();
+
+    if (seqs == null || seqs.length == 0)
+    {
+      // nothing to see here.
+      return null;
+    }
+
+    return new CrossRef(seqs, dataset).findXrefSourcesForSequences(dna);
+  }
+
   /**
    * Finds and displays cross-references for the selected sequences (protein
    * products for nucleotide sequences, dna coding sequences for peptides).
@@ -4476,12 +4495,37 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * @param source
    *          the database to show cross-references for
    */
+  /*
   protected void showProductsFor(final SequenceI[] sel, final boolean _odna,
           final String source)
   {
     new Thread(CrossRefAction.getHandlerFor(sel, _odna, source, this))
             .start();
   }
+  */
+  protected void showProductsFor(final SequenceI[] sel, final boolean _odna,
+          final String source)
+  {
+    showProductsFor(sel, _odna, source, false, null);
+  }
+
+  public CompletableFuture<Void> showProductsFor(final SequenceI[] sel,
+          final boolean _odna, final String source, boolean returnFuture,
+          String id)
+  {
+    CompletableFuture<Void> cf = CompletableFuture
+            .runAsync(() -> runCrossRefActionAndCacheAlignFrame(sel, _odna,
+                    source, returnFuture, id));
+    return returnFuture ? cf : null;
+  }
+
+  private void runCrossRefActionAndCacheAlignFrame(SequenceI[] sel,
+          boolean _odna, String source, boolean cacheAlignFrame, String id)
+  {
+    final AlignFrame af = this;
+    CrossRefAction.getHandlerFor(sel, _odna, source, af).run();
+    af.cacheAlignFrameFromRestId(id);
+  }
 
   /**
    * Construct and display a new frame containing the translation of this
@@ -4498,8 +4542,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       al = dna.translateCdna(codeTable);
     } catch (Exception ex)
     {
-      Console.error(
-              "Exception during translation. Please report this !", ex);
+      Console.error("Exception during translation. Please report this !",
+              ex);
       final String msg = MessageManager.getString(
               "label.error_when_translating_sequences_submit_bug_report");
       final String errorTitle = MessageManager
@@ -5419,6 +5463,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
               }
             }
           }
+
         });
       }
     }).start();
@@ -5905,6 +5950,27 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     return lastFeatureSettingsBounds;
   }
+
+  /*
+   * Caching hashmaps for jalview.rest.API
+   */
+  private static Map<String, AlignFrame> alignFrameMap = null;
+
+  public static AlignFrame getAlignFrameFromRestId(String id)
+  {
+    if (id == null || alignFrameMap == null)
+      return null;
+    return alignFrameMap.get(id);
+  }
+
+  public void cacheAlignFrameFromRestId(String id)
+  {
+    if (id == null)
+      return;
+    if (alignFrameMap == null)
+      alignFrameMap = new HashMap<>();
+    alignFrameMap.put(id, this);
+  }
 }
 
 class PrintThread extends Thread