JAL-1028 utilities package for accessing synchronous web services with apache's HTTP...
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
index e629503..d72681d 100644 (file)
@@ -19,6 +19,7 @@ import javax.swing.event.MenuListener;
 import compbio.metadata.Argument;
 
 import jalview.api.AlignCalcWorkerI;
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentView;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
@@ -29,10 +30,10 @@ import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.WsParamSetI;
 
 /**
- * @author jimp
+ * @author jprocter 
  * 
  */
-public class SequenceAnnotationWSClient extends Jws2Client
+public class SequenceAnnotationWSClient extends Jws2Client 
 {
 
   public static final String AAConsCalcId = "jabaws2.AACons";
@@ -51,7 +52,7 @@ public class SequenceAnnotationWSClient extends Jws2Client
     super(alignFrame, preset, null);
     initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
   }
-  
+
   public void initSequenceAnnotationWSClient(final Jws2Instance sh,
           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
   {
@@ -82,8 +83,8 @@ public class SequenceAnnotationWSClient extends Jws2Client
                 .getViewport()
                 .getCalcManager()
                 .registerWorker(
-                        worker = new AAConsClient(sh, alignFrame, this.preset,
-                                paramset));
+                        worker = new AAConsClient(sh, alignFrame,
+                                this.preset, paramset));
         alignFrame.getViewport().getCalcManager().startWorker(worker);
 
       }
@@ -129,7 +130,8 @@ public class SequenceAnnotationWSClient extends Jws2Client
           AlignFrame alignFrame, boolean b)
   {
     super(alignFrame, fave.getPreset(), fave.getJobArgset());
-    initSequenceAnnotationWSClient(fave.getService(), alignFrame, fave.getPreset(), b);
+    initSequenceAnnotationWSClient(fave.getService(), alignFrame,
+            fave.getPreset(), b);
   }
 
   /*
@@ -213,8 +215,6 @@ public class SequenceAnnotationWSClient extends Jws2Client
           AAeditSettings = "Change AACons Settings...",
           AAeditSettingsTooltip = "Modify settings for AACons calculations.";
 
-  private SequenceAnnotationWSClient preferredAAConsClient = null;
-
   // private final enableAAConsCalculation(final AlignFrame alignFrame, )
   private void registerAAConsWSInstance(final JMenu wsmenu,
           final Jws2Instance service, final AlignFrame alignFrame)
@@ -230,8 +230,32 @@ public class SequenceAnnotationWSClient extends Jws2Client
         _aaConsEnabled = (JCheckBoxMenuItem) item;
       }
     }
+    // is there an aaCons worker already present - if so, set it to use the
+    // given service handle
+    {
+      List<AlignCalcWorkerI> aaconsClient = alignFrame.getViewport()
+              .getCalcManager()
+              .getRegisteredWorkersOfClass(AAConsClient.class);
+      if (aaconsClient != null && aaconsClient.size() > 0)
+      {
+        AAConsClient worker = (AAConsClient) aaconsClient.get(0);
+        if (!worker.service.hosturl.equals(service.hosturl))
+        {
+         // javax.swing.SwingUtilities.invokeLater(new Runnable()
+          {
+         //   @Override
+         //   public void run()
+            {
+              removeCurrentAAConsWorkerFor(alignFrame);
+              buildCurrentAAConsWorkerFor(alignFrame, service);
+            }
+          }//);
+        }
+      }
+    }
 
-    // is there a service already registered ?
+    // is there a service already registered ? there shouldn't be if we are
+    // being called correctly
     if (_aaConsEnabled == null)
     {
       final JCheckBoxMenuItem aaConsEnabled = new JCheckBoxMenuItem(
@@ -312,56 +336,101 @@ public class SequenceAnnotationWSClient extends Jws2Client
       wsmenu.add(modifyParams);
 
     }
-
   }
 
   private static void showAAConsAnnotationSettingsFor(AlignFrame alignFrame)
   {
     /*
-     * preferred settings
-     * Whether AACons is automatically recalculated
-     * Which AACons server to use
-     * What parameters to use
+     * preferred settings Whether AACons is automatically recalculated Which
+     * AACons server to use What parameters to use
      */
     // could actually do a class search for this too
-    AAConsSettings fave= (AAConsSettings) alignFrame.getViewport().getCalcIdSettingsFor(AAConsCalcId);
-    if (fave==null)
+    AAConsSettings fave = (AAConsSettings) alignFrame.getViewport()
+            .getCalcIdSettingsFor(AAConsCalcId);
+    if (fave == null)
     {
       fave = createDefaultAAConsSettings();
     }
     new SequenceAnnotationWSClient(fave, alignFrame, true);
 
   }
+
   private static void buildCurrentAAConsWorkerFor(AlignFrame alignFrame)
   {
+    buildCurrentAAConsWorkerFor(alignFrame, null);
+  }
+
+  private static void buildCurrentAAConsWorkerFor(AlignFrame alignFrame,
+          Jws2Instance service)
+  {
     /*
-     * preferred settings
-     * Whether AACons is automatically recalculated
-     * Which AACons server to use
-     * What parameters to use
+     * preferred settings Whether AACons is automatically recalculated Which
+     * AACons server to use What parameters to use
      */
-    AAConsSettings fave= (AAConsSettings) alignFrame.getViewport().getCalcIdSettingsFor(AAConsCalcId);
-    if (fave==null)
+    AAConsSettings fave = (AAConsSettings) alignFrame.getViewport()
+            .getCalcIdSettingsFor(AAConsCalcId);
+    if (fave == null)
     {
-      fave = createDefaultAAConsSettings();
+      fave = createDefaultAAConsSettings(service);
+    }
+    else
+    {
+      if (service != null
+              && !fave.getService().hosturl.equals(service.hosturl))
+      {
+        Cache.log.debug("Changing AACons service to " + service.hosturl
+                + " from " + fave.getService().hosturl);
+        fave.setService(service);
+      }
     }
     new SequenceAnnotationWSClient(fave, alignFrame, false);
   }
 
   private static AAConsSettings createDefaultAAConsSettings()
   {
-    Jws2Instance service=null;
-    // set sensible defaults
-    for (Jws2Instance sv: Jws2Discoverer.getDiscoverer().getServices()) {
-      if (sv.serviceType.toString().equals(compbio.ws.client.Services.AAConWS.toString()))
+    return createDefaultAAConsSettings(null);
+  }
+
+  private static AAConsSettings createDefaultAAConsSettings(
+          Jws2Instance service)
+  {
+    if (service != null)
+    {
+      if (!service.serviceType.toString().equals(
+              compbio.ws.client.Services.AAConWS.toString()))
+      {
+        Cache.log
+                .warn("Ignoring invalid preferred service for AACons calculations (service type was "
+                        + service.serviceType + ")");
+        service = null;
+      }
+      else
       {
-        service = sv;
-        break;
+        // check service is actually in the list of currently avaialable
+        // services
+        if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
+        {
+          // it isn't ..
+          service = null;
+        }
       }
     }
-    if (service==null)
+    if (service == null)
+    {
+      // get the default service for AACons
+      service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
+              compbio.ws.client.Services.AAConWS.toString());
+      /*
+       * for (Jws2Instance sv : Jws2Discoverer.getDiscoverer().getServices()) {
+       * if (sv.serviceType.toString().equals(
+       * compbio.ws.client.Services.AAConWS.toString())) { service = sv; break;
+       * } }
+       */
+    }
+    if (service == null)
     {
-      // TODO raise dialog box explaining error, and/or open the JABA preferences menu.
+      // TODO raise dialog box explaining error, and/or open the JABA
+      // preferences menu.
       throw new Error("No AACons service found.");
     }
     return new AAConsSettings(true, service, null, null);