1 package jalview.ws.jws2;
3 import jalview.bin.Cache;
4 import jalview.gui.AlignFrame;
5 import jalview.gui.Desktop;
6 import jalview.gui.JvSwingUtils;
7 import jalview.util.MessageManager;
8 import jalview.ws.api.ServiceWithParameters;
10 import java.awt.Color;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.beans.PropertyChangeSupport;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Hashtable;
19 import java.util.List;
21 import java.util.Vector;
23 import javax.swing.JMenu;
24 import javax.swing.JMenuItem;
26 public class PreferredServiceRegistry
29 private static PreferredServiceRegistry us = new PreferredServiceRegistry();
31 public static PreferredServiceRegistry getRegistry()
35 us = new PreferredServiceRegistry();
40 List<ServiceWithParameters> ourServices = new ArrayList();
43 * forget about any known services
45 public void clearServices()
50 public void populateWSMenuEntry(List<ServiceWithParameters> services,
51 PropertyChangeSupport changeSupport,
52 JMenu jws2al, final AlignFrame alignFrame, String typeFilter)
55 * eventually, JWS2 services will appear under the same align/etc submenus.
56 * for moment we keep them separate.
59 List<ServiceWithParameters> enumerableServices = new ArrayList<>();
60 // jws2al.removeAll();
61 Map<String, ServiceWithParameters> preferredHosts = new HashMap<>();
62 Map<String, List<ServiceWithParameters>> alternates = new HashMap<>();
63 for (ServiceWithParameters service : services)
65 ourServices.add(service);
66 // TODO: check this behaves with refactored serviceType to getName
67 if (!service.isInteractiveUpdate())
69 // add 'one shot' services to be displayed using the classic menu
71 enumerableServices.add(service);
75 if (!preferredHosts.containsKey(service.getName()))
77 ServiceWithParameters preferredInstance = getPreferredServiceFor(
78 alignFrame, service.getName());
79 if (preferredInstance != null)
81 preferredHosts.put(service.getName(), preferredInstance);
85 preferredHosts.put(service.getName(), service);
88 List<ServiceWithParameters> ph = alternates.get(service.getName());
89 if (preferredHosts.get(service.getName()) != service)
93 ph = new ArrayList<>();
96 alternates.put(service.getName(), ph);
102 // create GUI element for classic services
103 addEnumeratedServices(jws2al, alignFrame, enumerableServices);
104 // and the instantaneous services
105 for (final ServiceWithParameters service : preferredHosts.values())
107 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,
108 service.getServiceType());
110 if (atpoint.getItemCount() > 1)
112 // previous service of this type already present
113 atpoint.addSeparator();
115 atpoint.add(hitm = new JMenuItem(service.getHostURL()));
116 hitm.setForeground(Color.blue);
117 hitm.addActionListener(new ActionListener()
121 public void actionPerformed(ActionEvent e)
123 Desktop.showUrl(service.getHostURL());
126 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
127 MessageManager.getString("label.open_jabaws_web_page")));
129 service.attachWSMenuEntry(atpoint, alignFrame);
130 if (alternates.containsKey(service.getName()))
132 atpoint.add(hitm = new JMenu(
133 MessageManager.getString("label.switch_server")));
134 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
135 MessageManager.getString("label.choose_jabaws_server")));
136 for (final ServiceWithParameters sv : alternates
137 .get(service.getName()))
140 hitm.add(itm = new JMenuItem(sv.getHostURL()));
141 itm.setForeground(Color.blue);
142 itm.addActionListener(new ActionListener()
146 public void actionPerformed(ActionEvent arg0)
148 new Thread(new Runnable()
153 setPreferredServiceFor(alignFrame, sv.getName(),
154 sv.getServiceType(), sv);
155 changeSupport.firePropertyChange("services",
156 new Vector<ServiceWithParameters>(), services);
168 * add services using the Java 2.5/2.6/2.7 system which optionally creates
169 * submenus to index by host and service program type
171 private void addEnumeratedServices(final JMenu jws2al,
172 final AlignFrame alignFrame,
173 List<ServiceWithParameters> enumerableServices)
175 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false),
176 bytype = Cache.getDefault("WSMENU_BYTYPE", false);
178 * eventually, JWS2 services will appear under the same align/etc submenus.
179 * for moment we keep them separate.
183 List<String> hostLabels = new ArrayList<>();
184 Hashtable<String, String> lasthostFor = new Hashtable<>();
185 Hashtable<String, ArrayList<ServiceWithParameters>> hosts = new Hashtable<>();
186 ArrayList<String> hostlist = new ArrayList<>();
187 for (ServiceWithParameters service : enumerableServices)
189 ArrayList<ServiceWithParameters> hostservices = hosts
190 .get(service.getHostURL());
191 if (hostservices == null)
193 hosts.put(service.getHostURL(), hostservices = new ArrayList<>());
194 hostlist.add(service.getHostURL());
196 hostservices.add(service);
198 // now add hosts in order of the given array
199 for (String host : hostlist)
201 ServiceWithParameters orderedsvcs[] = hosts.get(host)
202 .toArray(new ServiceWithParameters[1]);
203 String sortbytype[] = new String[orderedsvcs.length];
204 for (int i = 0; i < sortbytype.length; i++)
206 sortbytype[i] = orderedsvcs[i].getName();
208 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
209 for (final ServiceWithParameters service : orderedsvcs)
211 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,
212 service.getAction());
213 String type = service.getName();
216 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
217 if (atpoint.getToolTipText() == null)
219 atpoint.setToolTipText(MessageManager
220 .formatMessage("label.services_at", new String[]
226 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
227 if (atpoint.getToolTipText() == null)
229 atpoint.setToolTipText(service.getActionText());
232 if (!byhost && !hostLabels.contains(
233 host + service.getName() + service.getActionText()))
234 // !hostLabels.contains(host + (bytype ?
235 // service.serviceType+service.getActionText() : "")))
237 // add a marker indicating where this service is hosted
238 // relies on services from the same host being listed in a
242 if (hostLabels.contains(host))
244 atpoint.addSeparator();
248 hostLabels.add(host);
250 if (lasthostFor.get(service.getAction()) == null
251 || !lasthostFor.get(service.getAction()).equals(host))
253 atpoint.add(hitm = new JMenuItem(host));
254 hitm.setForeground(Color.blue);
255 hitm.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent e)
261 Desktop.showUrl(service.getHostURL());
265 JvSwingUtils.wrapTooltip(true, MessageManager
266 .getString("label.open_jabaws_web_page")));
267 lasthostFor.put(service.getAction(), host);
270 .add(host + service.getName() + service.getActionText());
273 service.attachWSMenuEntry(atpoint, alignFrame);
279 * pick the user's preferred service based on a set of URLs (jaba server
280 * locations) and service URIs (specifying version and service interface
284 * @return null or best match for given uri/ls.
286 public ServiceWithParameters getPreferredServiceFor(String[] serviceURLs)
288 HashSet<String> urls = new HashSet<>();
289 urls.addAll(Arrays.asList(serviceURLs));
290 ServiceWithParameters match = null;
292 if (ourServices != null)
294 for (ServiceWithParameters svc : ourServices)
296 // TODO getNameURI Should return a versioned URI for the service, but
297 // doesn't as of 2.11
298 if (urls.contains(svc.getNameURI()))
302 // for moment we always pick service from server ordered first in
303 // user's preferences
306 if (urls.contains(svc.getUri()))
308 // stop and return - we've matched type URI and URI for service
318 Map<String, Map<String, String>> preferredServiceMap = new HashMap<>();
321 * get current preferred endpoint of the given Jabaws service, or global
325 * null or a specific alignFrame
327 * ServiceWithParameters.getName() for service
328 * @return null if no service of this type is available, the preferred service
329 * for the serviceType and af if specified and if defined.
331 public ServiceWithParameters getPreferredServiceFor(AlignFrame af,
334 String serviceurl = null;
335 synchronized (preferredServiceMap)
337 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
338 Map<String, String> prefmap = preferredServiceMap.get(afid);
339 if (afid.length() > 0 && prefmap == null)
341 // recover global setting, if any
342 prefmap = preferredServiceMap.get("");
346 serviceurl = prefmap.get(serviceName);
350 ServiceWithParameters response = null;
351 for (ServiceWithParameters svc : ourServices)
353 if (svc.getName().equals(serviceName))
355 if (serviceurl == null || serviceurl.equals(svc.getHostURL()))
365 public void setPreferredServiceFor(AlignFrame af, String serviceName,
366 String serviceAction, ServiceWithParameters selectedServer)
368 // TODO: pull out and generalise for the selectedServer's attributes
369 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
370 if (preferredServiceMap == null)
372 preferredServiceMap = new HashMap<>();
374 Map<String, String> prefmap = preferredServiceMap.get(afid);
377 prefmap = new HashMap<>();
378 preferredServiceMap.put(afid, prefmap);
380 prefmap.put(serviceName, selectedServer.getHostURL());
381 prefmap.put(serviceAction, selectedServer.getHostURL());
384 public void setPreferredServiceFor(String serviceType,
385 String serviceAction, ServiceWithParameters selectedServer)
387 setPreferredServiceFor(null, serviceType, serviceAction,
391 public boolean contains(ServiceWithParameters service)
393 return ourServices.contains(service);