JAL-3690 alternative servers menu fix
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 17 Jul 2020 12:43:39 +0000 (13:43 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 20 Jul 2020 11:17:31 +0000 (12:17 +0100)
src/jalview/gui/AlignFrame.java
src/jalview/ws/jws2/PreferredServiceChangeListener.java [new file with mode: 0644]
src/jalview/ws/jws2/PreferredServiceRegistry.java
src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java

index 71f8c7d..9e0fe6f 100644 (file)
@@ -4410,6 +4410,7 @@ public class AlignFrame extends GAlignFrame
   public void buildWebServicesMenu()
   {
     SwingUtilities.invokeLater(() -> {
+      Cache.log.info("Rebuiling WS menu");
       webService.removeAll();
       if (Cache.getDefault("SHOW_SLIVKA_SERVICES", true))
       {
@@ -4461,7 +4462,8 @@ public class AlignFrame extends GAlignFrame
     if (discoverer.hasServices())
     {
       PreferredServiceRegistry.getRegistry().populateWSMenuEntry(
-              discoverer.getServices(), null, menu, this, null);
+              discoverer.getServices(), sv -> buildWebServicesMenu(), menu,
+              this, null);
     }
     if (discoverer.isRunning())
     {
diff --git a/src/jalview/ws/jws2/PreferredServiceChangeListener.java b/src/jalview/ws/jws2/PreferredServiceChangeListener.java
new file mode 100644 (file)
index 0000000..ff959ca
--- /dev/null
@@ -0,0 +1,9 @@
+package jalview.ws.jws2;
+
+import jalview.ws.api.ServiceWithParameters;
+
+@FunctionalInterface
+public interface PreferredServiceChangeListener
+{
+  public void preferredServiceChanged(ServiceWithParameters newValue);
+}
index 9e7438c..45af925 100644 (file)
@@ -10,7 +10,6 @@ import jalview.ws.api.ServiceWithParameters;
 import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.beans.PropertyChangeSupport;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -18,8 +17,6 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
-import java.util.Vector;
-
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 
@@ -37,7 +34,7 @@ public class PreferredServiceRegistry
     return us;
   }
 
-  List<ServiceWithParameters> ourServices = new ArrayList();
+  List<ServiceWithParameters> ourServices = new ArrayList<>();
 
   /**
    * forget about any known services
@@ -48,81 +45,59 @@ public class PreferredServiceRegistry
   }
 
   public void populateWSMenuEntry(List<ServiceWithParameters> services,
-          PropertyChangeSupport changeSupport,
-          JMenu jws2al, final AlignFrame alignFrame, String typeFilter)
+          PreferredServiceChangeListener changeListener, JMenu menu,
+          final AlignFrame alignFrame, String typeFilter)
   {
     /**
      * eventually, JWS2 services will appear under the same align/etc submenus.
      * for moment we keep them separate.
      */
+    ourServices.addAll(services);
     JMenu atpoint;
-    List<ServiceWithParameters> enumerableServices = new ArrayList<>();
-    // jws2al.removeAll();
+    
+    List<ServiceWithParameters> oneshotServices = new ArrayList<>();
+    List<ServiceWithParameters> interactiveServices = new ArrayList<>();
     Map<String, ServiceWithParameters> preferredHosts = new HashMap<>();
     Map<String, List<ServiceWithParameters>> alternates = new HashMap<>();
-    for (ServiceWithParameters service : services)
+    
+    for (var service : services) 
+    {
+      if (service.isInteractiveUpdate())
+        interactiveServices.add(service);
+      else
+        oneshotServices.add(service);
+    }
+    for (var service : interactiveServices)
     {
-      ourServices.add(service);
-      // TODO: check this behaves with refactored serviceType to getName
-      if (!service.isInteractiveUpdate())
+      if (!preferredHosts.containsKey(service.getName()))
       {
-        // add 'one shot' services to be displayed using the classic menu
-        // structure
-        enumerableServices.add(service);
+        var preferred = getPreferredServiceFor(alignFrame, service.getName());
+        preferredHosts.put(service.getName(), (preferred != null) ? preferred : service);
       }
-      else
+      var ph = alternates.getOrDefault(service.getName(), new ArrayList<>());
+      if (!preferredHosts.containsValue(service)) 
       {
-        if (!preferredHosts.containsKey(service.getName()))
-        {
-          ServiceWithParameters preferredInstance = getPreferredServiceFor(
-                  alignFrame, service.getName());
-          if (preferredInstance != null)
-          {
-            preferredHosts.put(service.getName(), preferredInstance);
-          }
-          else
-          {
-            preferredHosts.put(service.getName(), service);
-          }
-        }
-        List<ServiceWithParameters> ph = alternates.get(service.getName());
-        if (preferredHosts.get(service.getName()) != service)
-        {
-          if (ph == null)
-          {
-            ph = new ArrayList<>();
-          }
-          ph.add(service);
-          alternates.put(service.getName(), ph);
-        }
+        ph.add(service);
+        alternates.putIfAbsent(service.getName(), ph);
       }
-
     }
 
     // create GUI element for classic services
-    addEnumeratedServices(jws2al, alignFrame, enumerableServices);
+    addEnumeratedServices(menu, alignFrame, oneshotServices);
     // and the instantaneous services
     for (final ServiceWithParameters service : preferredHosts.values())
     {
-      atpoint = JvSwingUtils.findOrCreateMenu(jws2al,
+      atpoint = JvSwingUtils.findOrCreateMenu(menu,
               service.getServiceType());
-      JMenuItem hitm;
       if (atpoint.getItemCount() > 1)
       {
         // previous service of this type already present
         atpoint.addSeparator();
       }
+      JMenuItem hitm;
       atpoint.add(hitm = new JMenuItem(service.getHostURL()));
       hitm.setForeground(Color.blue);
-      hitm.addActionListener(new ActionListener()
-      {
-
-        @Override
-        public void actionPerformed(ActionEvent e)
-        {
-          Desktop.showUrl(service.getHostURL());
-        }
-      });
+      hitm.addActionListener(e -> Desktop.showUrl(service.getHostURL()));
       hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
               MessageManager.getString("label.open_jabaws_web_page")));
 
@@ -139,25 +114,9 @@ public class PreferredServiceRegistry
           JMenuItem itm;
           hitm.add(itm = new JMenuItem(sv.getHostURL()));
           itm.setForeground(Color.blue);
-          itm.addActionListener(new ActionListener()
-          {
-
-            @Override
-            public void actionPerformed(ActionEvent arg0)
-            {
-              new Thread(new Runnable()
-              {
-                @Override
-                public void run()
-                {
-                  setPreferredServiceFor(alignFrame, sv.getName(),
-                          sv.getServiceType(), sv);
-                  changeSupport.firePropertyChange("services",
-                          new Vector<ServiceWithParameters>(), services);
-                };
-              }).start();
-
-            }
+          itm.addActionListener(e -> {
+            setPreferredServiceFor(alignFrame, sv.getName(), sv.getServiceType(), sv);
+            changeListener.preferredServiceChanged(sv);
           });
         }
       }
index a42b382..cd296c6 100644 (file)
@@ -4,7 +4,6 @@ import jalview.bin.Cache;
 import jalview.ws.ServiceChangeListener;
 import jalview.ws.WSDiscovererI;
 import jalview.ws.api.ServiceWithParameters;
-import java.beans.PropertyChangeListener;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -18,8 +17,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.FutureTask;
-
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;