Merge branch 'alpha/JAL-3066_Jalview_212_slivka-integration' into alpha/JAL-3362_Jalv...
[jalview.git] / src / jalview / ws / jws2 / Jws2ClientFactory.java
diff --git a/src/jalview/ws/jws2/Jws2ClientFactory.java b/src/jalview/ws/jws2/Jws2ClientFactory.java
new file mode 100644 (file)
index 0000000..6445cd5
--- /dev/null
@@ -0,0 +1,292 @@
+package jalview.ws.jws2;
+
+import jalview.api.AlignCalcWorkerI;
+import jalview.bin.Cache;
+import jalview.gui.AlignFrame;
+import jalview.gui.JvSwingUtils;
+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;
+import java.awt.event.ActionListener;
+import java.util.List;
+
+import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import javax.swing.event.MenuEvent;
+import javax.swing.event.MenuListener;
+
+public class Jws2ClientFactory
+{
+  static boolean registerAAConWSInstance(final JMenu wsmenu,
+          final ServiceWithParameters service, final AlignFrame alignFrame)
+  {
+    final AlignAnalysisUIText aaui = service.getAlignAnalysisUI(); // null
+                                                                        // ; //
+    // AlignAnalysisUIText.aaConGUI.get(service.serviceType.toString());
+    if (aaui == null)
+    {
+      // not an instantaneous calculation GUI type service
+      return false;
+    }
+    // create the instaneous calculation GUI bits and update state if existing
+    // GUI elements already present
+
+    JCheckBoxMenuItem _aaConEnabled = null;
+    for (int i = 0; i < wsmenu.getItemCount(); i++)
+    {
+      JMenuItem item = wsmenu.getItem(i);
+      if (item instanceof JCheckBoxMenuItem
+              && item.getText().equals(aaui.getAAconToggle()))
+      {
+        _aaConEnabled = (JCheckBoxMenuItem) item;
+      }
+    }
+    // is there an aaCon worker already present - if so, set it to use the
+    // given service handle
+    {
+      List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
+              .getCalcManager()
+              .getRegisteredWorkersOfClass(aaui.getClient());
+      if (aaconClient != null && aaconClient.size() > 0)
+      {
+        SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) aaconClient
+                .get(0);
+        if (!worker.service.getHostURL().equals(service.getHostURL()))
+        {
+          // javax.swing.SwingUtilities.invokeLater(new Runnable()
+          {
+            // @Override
+            // public void run()
+            {
+              removeCurrentAAConWorkerFor(aaui, alignFrame);
+              buildCurrentAAConWorkerFor(aaui, alignFrame, service);
+            }
+          } // );
+        }
+      }
+    }
+
+    // is there a service already registered ? there shouldn't be if we are
+    // being called correctly
+    if (_aaConEnabled == null)
+    {
+      final JCheckBoxMenuItem aaConEnabled = new JCheckBoxMenuItem(
+              aaui.getAAconToggle());
+
+      aaConEnabled.setToolTipText(
+              JvSwingUtils.wrapTooltip(true, aaui.getAAconToggleTooltip()));
+      aaConEnabled.addActionListener(new ActionListener()
+      {
+        @Override
+        public void actionPerformed(ActionEvent arg0)
+        {
+
+          List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
+                  .getCalcManager().getRegisteredWorkersOfClass(
+                          SeqAnnotationServiceCalcWorker.class);
+          if (aaconClient != null)
+          {
+            for (AlignCalcWorkerI worker : aaconClient)
+            {
+              if (((SeqAnnotationServiceCalcWorker) worker).getService()
+                      .getClass().equals(aaui.getClient()))
+              {
+                removeCurrentAAConWorkerFor(aaui, alignFrame);
+                return;
+              }
+            }
+          }
+          buildCurrentAAConWorkerFor(aaui, alignFrame);
+        }
+
+      });
+      wsmenu.add(aaConEnabled);
+      final JMenuItem modifyParams = new JMenuItem(
+              aaui.getAAeditSettings());
+      modifyParams.setToolTipText(JvSwingUtils.wrapTooltip(true,
+              aaui.getAAeditSettingsTooltip()));
+      modifyParams.addActionListener(new ActionListener()
+      {
+
+        @Override
+        public void actionPerformed(ActionEvent arg0)
+        {
+          showAAConAnnotationSettingsFor(aaui, alignFrame);
+        }
+      });
+      wsmenu.add(modifyParams);
+      wsmenu.addMenuListener(new MenuListener()
+      {
+
+        @Override
+        public void menuSelected(MenuEvent arg0)
+        {
+          // TODO: refactor to the implementing class.
+          if (alignFrame.getViewport().getAlignment().isNucleotide()
+                  ? aaui.isNa()
+                  : aaui.isPr())
+          {
+            aaConEnabled.setEnabled(true);
+            modifyParams.setEnabled(true);
+          }
+          else
+          {
+            aaConEnabled.setEnabled(false);
+            modifyParams.setEnabled(false);
+            return;
+          }
+          List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
+                  .getCalcManager().getRegisteredWorkersOfClass(
+                          SeqAnnotationServiceCalcWorker.class);
+
+          boolean serviceEnabled = false;
+          if (aaconClient != null)
+          {
+            // NB code duplicatino again!
+            for (AlignCalcWorkerI _worker : aaconClient)
+            {
+              SeqAnnotationServiceCalcWorker worker = (SeqAnnotationServiceCalcWorker) _worker;
+              // this could be cleaner ?
+              if (worker.hasService()
+                      && aaui.getClient()
+                              .equals(worker.getService().getClass()))
+              {
+                serviceEnabled = true;
+              }
+            }
+          }
+          aaConEnabled.setSelected(serviceEnabled);
+        }
+
+        @Override
+        public void menuDeselected(MenuEvent arg0)
+        {
+          // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void menuCanceled(MenuEvent arg0)
+        {
+          // TODO Auto-generated method stub
+
+        }
+      });
+
+    }
+    return true;
+  }
+
+  private static void showAAConAnnotationSettingsFor(
+          final AlignAnalysisUIText aaui, AlignFrame alignFrame)
+  {
+    /*
+     * preferred settings Whether AACon is automatically recalculated Which
+     * AACon server to use What parameters to use
+     */
+    // could actually do a class search for this too
+    AutoCalcSetting fave = alignFrame.getViewport()
+            .getCalcIdSettingsFor(aaui.getCalcId());
+    if (fave == null)
+    {
+      fave = createDefaultAAConSettings(aaui);
+    }
+    new SequenceAnnotationWSClient(fave, alignFrame, true);
+
+  }
+
+  private static void buildCurrentAAConWorkerFor(
+          final AlignAnalysisUIText aaui, AlignFrame alignFrame)
+  {
+    buildCurrentAAConWorkerFor(aaui, alignFrame, null);
+  }
+
+  private static void buildCurrentAAConWorkerFor(
+          final AlignAnalysisUIText aaui, AlignFrame alignFrame,
+          ServiceWithParameters service)
+  {
+    /*
+     * preferred settings Whether AACon is automatically recalculated Which
+     * AACon server to use What parameters to use
+     */
+    AutoCalcSetting fave = alignFrame.getViewport()
+            .getCalcIdSettingsFor(aaui.getCalcId());
+    if (fave == null)
+    {
+      fave = createDefaultAAConSettings(aaui, service);
+    }
+    else
+    {
+      if (service != null && !fave.getService().getHostURL()
+              .equals(service.getHostURL()))
+      {
+        Cache.log.debug("Changing AACon service to " + service.getHostURL()
+                + " from " + fave.getService().getHostURL());
+        fave.setService(service);
+      }
+    }
+    new SequenceAnnotationWSClient(fave, alignFrame, false);
+  }
+
+  private static AutoCalcSetting createDefaultAAConSettings(
+          AlignAnalysisUIText aaui)
+  {
+    return createDefaultAAConSettings(aaui, null);
+  }
+
+  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
+      {
+        // check service is actually in the list of currently avaialable
+        // services
+        if (!PreferredServiceRegistry.getRegistry().contains(service))
+        {
+          // it isn't ..
+          service = null;
+        }
+      }
+    }
+    if (service == null)
+    {
+      // get the default service for AACon
+      service = PreferredServiceRegistry.getRegistry().getPreferredServiceFor(null,
+              aaui.getServiceType());
+    }
+    if (service == null)
+    {
+      // TODO raise dialog box explaining error, and/or open the JABA
+      // preferences menu.
+      throw new Error(
+              MessageManager.getString("error.no_aacon_service_found"));
+    }
+    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());
+  }
+}
\ No newline at end of file