JAL-3851 merged to develop 2022-03-22
[jalview.git] / src / jalview / gui / AlignFrame.java
index 07baa2e..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;
@@ -4289,9 +4291,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                   webService.add(me.webServiceNoServices);
                 }
                 // TODO: move into separate menu builder class.
-                boolean new_sspred = false;
-                if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
                 {
+                  // logic for 2.11.1.4 is
+                  // always look to see if there is a discover. if there isn't
+                  // we can't show any Jws2 services
+                  // if there are services available, show them - regardless of
+                  // the 'show JWS2 preference'
+                  // if the discoverer is running then say so
+                  // otherwise offer to trigger discovery if 'show JWS2' is not
+                  // enabled
                   Jws2Discoverer jws2servs = Jws2Discoverer.getDiscoverer();
                   if (jws2servs != null)
                   {
@@ -4300,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)
                           {
@@ -4308,8 +4317,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                           }
                         }
                       }
-
                     }
+
                     if (jws2servs.isRunning())
                     {
                       JMenuItem tm = new JMenuItem(
@@ -4317,6 +4326,26 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                       tm.setEnabled(false);
                       webService.add(tm);
                     }
+                    else if (!Cache.getDefault("SHOW_JWS2_SERVICES", true))
+                    {
+                      JMenuItem enableJws2 = new JMenuItem(
+                              "Discover Web Services");
+                      enableJws2.setToolTipText(
+                              "Select to start JABA Web Service discovery (or enable option in Web Service preferences)");
+                      enableJws2.setEnabled(true);
+                      enableJws2.addActionListener(new ActionListener()
+                      {
+
+                        @Override
+                        public void actionPerformed(ActionEvent e)
+                        {
+                          // start service discoverer, but ignore preference
+                          Desktop.instance.startServiceDiscovery(false,
+                                  true);
+                        }
+                      });
+                      webService.add(enableJws2);
+                    }
                   }
                 }
                 build_urlServiceMenu(me.webService);
@@ -4439,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).
@@ -4450,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
@@ -4472,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
@@ -5393,6 +5463,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
               }
             }
           }
+
         });
       }
     }).start();
@@ -5879,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