JAL - 3690 AlignCalc rebuilt - FutureTask-based manager
[jalview.git] / src / jalview / ws / jws2 / Jws2ClientFactory.java
index 2a3b4a5..2456528 100644 (file)
@@ -8,6 +8,7 @@ import jalview.util.MessageManager;
 import jalview.ws.api.ServiceWithParameters;
 import jalview.ws.jws2.dm.AAConSettings;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
+import jalview.ws.params.AutoCalcSetting;
 import jalview.ws.uimodel.AlignAnalysisUIText;
 
 import java.awt.event.ActionEvent;
@@ -25,8 +26,7 @@ public class Jws2ClientFactory
   static boolean registerAAConWSInstance(final JMenu wsmenu,
           final ServiceWithParameters service, final AlignFrame alignFrame)
   {
-    Jws2Instance jaba_service = (Jws2Instance) service;
-    final AlignAnalysisUIText aaui = jaba_service.getAlignAnalysisUI(); // null
+    final AlignAnalysisUIText aaui = service.getAlignAnalysisUI(); // null
                                                                         // ; //
     // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
     if (aaui == null)
@@ -52,10 +52,10 @@ public class Jws2ClientFactory
     {
       List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
               .getCalcManager()
-              .getRegisteredWorkersOfClass(aaui.getClient());
+              .getWorkersOfClass(aaui.getClient());
       if (aaconClient != null && aaconClient.size() > 0)
       {
-        AbstractJabaCalcWorker worker = (AbstractJabaCalcWorker) aaconClient
+        SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) aaconClient
                 .get(0);
         if (!worker.service.getHostURL().equals(service.getHostURL()))
         {
@@ -65,7 +65,7 @@ public class Jws2ClientFactory
             // public void run()
             {
               removeCurrentAAConWorkerFor(aaui, alignFrame);
-              buildCurrentAAConWorkerFor(aaui, alignFrame, jaba_service);
+              buildCurrentAAConWorkerFor(aaui, alignFrame, service);
             }
           } // );
         }
@@ -86,18 +86,23 @@ public class Jws2ClientFactory
         @Override
         public void actionPerformed(ActionEvent arg0)
         {
+
           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
                   .getCalcManager()
-                  .getRegisteredWorkersOfClass(aaui.getClient());
-          if (aaconClient != null && aaconClient.size() > 0)
-          {
-            removeCurrentAAConWorkerFor(aaui, alignFrame);
-          }
-          else
+                  .getWorkersOfClass(SeqAnnotationServiceCalcWorker.class);
+          if (aaconClient != null)
           {
-            buildCurrentAAConWorkerFor(aaui, alignFrame);
-
+            for (AlignCalcWorkerI worker : aaconClient)
+            {
+              if (((SeqAnnotationServiceCalcWorker) worker).getService()
+                      .getClass().equals(aaui.getClient()))
+              {
+                removeCurrentAAConWorkerFor(aaui, alignFrame);
+                return;
+              }
+            }
           }
+          buildCurrentAAConWorkerFor(aaui, alignFrame);
         }
 
       });
@@ -137,15 +142,16 @@ public class Jws2ClientFactory
             return;
           }
           List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
-                  .getCalcManager().getRegisteredWorkersOfClass(
-                          AbstractJabaCalcWorker.class);
+                  .getCalcManager()
+                  .getWorkersOfClass(SeqAnnotationServiceCalcWorker.class);
 
           boolean serviceEnabled = false;
           if (aaconClient != null)
           {
+            // NB code duplicatino again!
             for (AlignCalcWorkerI _worker : aaconClient)
             {
-              AbstractJabaCalcWorker worker = (AbstractJabaCalcWorker) _worker;
+              SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) _worker;
               // this could be cleaner ?
               if (worker.hasService()
                       && aaui.getClient()
@@ -185,7 +191,7 @@ public class Jws2ClientFactory
      * AACon server to use What parameters to use
      */
     // could actually do a class search for this too
-    AAConSettings fave = (AAConSettings) alignFrame.getViewport()
+    AutoCalcSetting fave = alignFrame.getViewport()
             .getCalcIdSettingsFor(aaui.getCalcId());
     if (fave == null)
     {
@@ -203,13 +209,13 @@ public class Jws2ClientFactory
 
   private static void buildCurrentAAConWorkerFor(
           final AlignAnalysisUIText aaui, AlignFrame alignFrame,
-          Jws2Instance service)
+          ServiceWithParameters service)
   {
     /*
      * preferred settings Whether AACon is automatically recalculated Which
      * AACon server to use What parameters to use
      */
-    AAConSettings fave = (AAConSettings) alignFrame.getViewport()
+    AutoCalcSetting fave = alignFrame.getViewport()
             .getCalcIdSettingsFor(aaui.getCalcId());
     if (fave == null)
     {
@@ -228,30 +234,31 @@ public class Jws2ClientFactory
     new SequenceAnnotationWSClient(fave, alignFrame, false);
   }
 
-  private static AAConSettings createDefaultAAConSettings(
+  private static AutoCalcSetting createDefaultAAConSettings(
           AlignAnalysisUIText aaui)
   {
     return createDefaultAAConSettings(aaui, null);
   }
 
-  private static AAConSettings createDefaultAAConSettings(
-          AlignAnalysisUIText aaui, Jws2Instance service)
+  private static AutoCalcSetting createDefaultAAConSettings(
+          AlignAnalysisUIText aaui, ServiceWithParameters service)
   {
     if (service != null)
     {
-      if (!service.getServiceType()
-              .equals(compbio.ws.client.Services.AAConWS.toString()))
-      {
-        Cache.log.warn(
-                "Ignoring invalid preferred service for AACon calculations (service type was "
-                        + service.getServiceType() + ")");
-        service = null;
-      }
-      else
+      // if (!service.getServiceType()
+      // .equals(compbio.ws.client.Services.AAConWS.toString()))
+      // {
+      // Cache.log.warn(
+      // "Ignoring invalid preferred service for AACon calculations (service
+      // type was "
+      // + service.getServiceType() + ")");
+      // service = null;
+      // }
+      // else
       {
         // check service is actually in the list of currently avaialable
         // services
-        if (!Jws2Discoverer.getDiscoverer().getServices().contains(service))
+        if (!PreferredServiceRegistry.getRegistry().contains(service))
         {
           // it isn't ..
           service = null;
@@ -261,7 +268,7 @@ public class Jws2ClientFactory
     if (service == null)
     {
       // get the default service for AACon
-      service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
+      service = PreferredServiceRegistry.getRegistry().getPreferredServiceFor(null,
               aaui.getServiceType());
     }
     if (service == null)
@@ -271,13 +278,15 @@ public class Jws2ClientFactory
       throw new Error(
               MessageManager.getString("error.no_aacon_service_found"));
     }
-    return new AAConSettings(true, service, null, null);
+    return service instanceof Jws2Instance
+            ? new AAConSettings(true, service, null, null)
+            : new AutoCalcSetting(service, null, null, true);
   }
 
   private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui,
           AlignFrame alignFrame)
   {
     alignFrame.getViewport().getCalcManager()
-            .removeRegisteredWorkersOfClass(aaui.getClient());
+            .removeWorkersOfClass(aaui.getClient());
   }
 }
\ No newline at end of file