From ee9a37025c3bf1a79f11f53ff16bb5597ce0b52b Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Fri, 17 Jul 2020 13:43:39 +0100 Subject: [PATCH] JAL-3690 alternative servers menu fix --- src/jalview/gui/AlignFrame.java | 4 +- .../ws/jws2/PreferredServiceChangeListener.java | 9 ++ src/jalview/ws/jws2/PreferredServiceRegistry.java | 101 ++++++-------------- src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java | 3 - 4 files changed, 42 insertions(+), 75 deletions(-) create mode 100644 src/jalview/ws/jws2/PreferredServiceChangeListener.java diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 71f8c7d..9e0fe6f 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -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 index 0000000..ff959ca --- /dev/null +++ b/src/jalview/ws/jws2/PreferredServiceChangeListener.java @@ -0,0 +1,9 @@ +package jalview.ws.jws2; + +import jalview.ws.api.ServiceWithParameters; + +@FunctionalInterface +public interface PreferredServiceChangeListener +{ + public void preferredServiceChanged(ServiceWithParameters newValue); +} diff --git a/src/jalview/ws/jws2/PreferredServiceRegistry.java b/src/jalview/ws/jws2/PreferredServiceRegistry.java index 9e7438c..45af925 100644 --- a/src/jalview/ws/jws2/PreferredServiceRegistry.java +++ b/src/jalview/ws/jws2/PreferredServiceRegistry.java @@ -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 ourServices = new ArrayList(); + List ourServices = new ArrayList<>(); /** * forget about any known services @@ -48,81 +45,59 @@ public class PreferredServiceRegistry } public void populateWSMenuEntry(List 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 enumerableServices = new ArrayList<>(); - // jws2al.removeAll(); + + List oneshotServices = new ArrayList<>(); + List interactiveServices = new ArrayList<>(); Map preferredHosts = new HashMap<>(); Map> 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 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(), services); - }; - }).start(); - - } + itm.addActionListener(e -> { + setPreferredServiceFor(alignFrame, sv.getName(), sv.getServiceType(), sv); + changeListener.preferredServiceChanged(sv); }); } } diff --git a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java index a42b382..cd296c6 100644 --- a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java +++ b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java @@ -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; -- 1.7.10.2