JAL-1365 unified GUI building for AACon and RNAAliFold service clients
[jalview.git] / src / jalview / ws / jws2 / SequenceAnnotationWSClient.java
index fea8f2a..cca6af3 100644 (file)
@@ -1,34 +1,49 @@
-/**
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
  * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
 package jalview.ws.jws2;
 
+import jalview.api.AlignCalcWorkerI;
+import jalview.bin.Cache;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.gui.JvSwingUtils;
+import jalview.ws.jws2.dm.AAConSettings;
+import jalview.ws.jws2.jabaws2.Jws2Instance;
+import jalview.ws.params.WsParamSetI;
+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.JOptionPane;
-
-import compbio.metadata.Argument;
-
-import jalview.api.AlignCalcWorkerI;
-import jalview.datamodel.AlignmentView;
-import jalview.gui.AlignFrame;
-import jalview.gui.Desktop;
-import jalview.gui.JalviewDialog;
-import jalview.gui.JvSwingUtils;
-import jalview.ws.jws2.jabaws2.Jws2Instance;
-import jalview.ws.params.WsParamSetI;
+import javax.swing.event.MenuEvent;
+import javax.swing.event.MenuListener;
 
 /**
- * @author jimp
+ * @author jprocter
  * 
  */
 public class SequenceAnnotationWSClient extends Jws2Client
 {
-
   /**
    * initialise a client so its attachWSMenuEntry method can be called.
    */
@@ -41,38 +56,62 @@ public class SequenceAnnotationWSClient extends Jws2Client
           AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
   {
     super(alignFrame, preset, null);
-    if (sh.serviceType.toLowerCase().contains("aaconws"))
+    initSequenceAnnotationWSClient(sh, alignFrame, preset, editParams);
+  }
+
+  // dan think. Do I need to change this method to run RNAalifold through the GUI
+  
+  public void initSequenceAnnotationWSClient(final Jws2Instance sh,
+          AlignFrame alignFrame, WsParamSetI preset, boolean editParams)
+  {
+       // dan changed! dan test. comment out if conditional
+//    if (alignFrame.getViewport().getAlignment().isNucleotide())
+//    {
+//      JOptionPane.showMessageDialog(Desktop.desktop, sh.serviceType
+//              + " can only be used\nfor amino acid alignments.",
+//              "Wrong type of sequences!", JOptionPane.WARNING_MESSAGE);
+//      return;
+//
+//    }
+    AlignAnalysisUIText aaui = sh.getAlignAnalysisUI();
+    if (aaui!=null)
     {
-      // Build an AACons client
-      if (alignFrame.getViewport().getAlignment().isNucleotide())
-      {
-        JOptionPane
-                .showMessageDialog(
-                        Desktop.desktop,
-                        "AACons can only be used\nfor amino acid alignments.\nSorry about that!",
-                        "Wrong type of sequences!",
-                        JOptionPane.WARNING_MESSAGE);
-        return;
+      Class clientClass = aaui.getClient();
+      
+      // Build an AACon style client - take alignment, return annotation for
+      // columns
 
-      }
       List<AlignCalcWorkerI> clnts = alignFrame.getViewport()
               .getCalcManager()
-              .getRegisteredWorkersOfClass(AAConsClient.class);
+              .getRegisteredWorkersOfClass(clientClass);
+      JabawsAlignCalcWorker worker;
       if (clnts == null || clnts.size() == 0)
       {
         if (!processParams(sh, editParams))
         {
           return;
         }
+        try {
+          worker = (JabawsAlignCalcWorker) (clientClass.getConstructor(
+                  new Class[] { Jws2Instance.class, 
+                          AlignFrame.class, WsParamSetI.class, 
+                          List.class }).newInstance(new Object[] { sh, alignFrame, this.preset, paramset}));
+        } catch (Exception x)
+        {
+          x.printStackTrace();
+          throw new Error("Implementation error",x);
+        }
         alignFrame
                 .getViewport()
                 .getCalcManager()
                 .registerWorker(
-                        new AAConsClient(sh, alignFrame, preset, paramset));
+                        worker);
+        alignFrame.getViewport().getCalcManager().startWorker(worker);
+
       }
       else
       {
-        AAConsClient worker = (AAConsClient) clnts.get(0);
+        worker = (JabawsAlignCalcWorker) clnts.get(0);
         if (editParams)
         {
           paramset = worker.getArguments();
@@ -80,15 +119,38 @@ public class SequenceAnnotationWSClient extends Jws2Client
         }
 
         if (!processParams(sh, editParams, true))
+        {
           return;
+        }
         // reinstate worker if it was blacklisted (might have happened due to
         // invalid parameters)
         alignFrame.getViewport().getCalcManager().workerMayRun(worker);
-        worker.updateParameters(preset, paramset);
-
+        worker.updateParameters(this.preset, paramset);
       }
     }
-    
+    if (sh.action.toLowerCase().contains("disorder"))
+    {
+      // build IUPred style client. take sequences, returns annotation per
+      // sequence.
+      if (!processParams(sh, editParams))
+      {
+        return;
+      }
+
+      alignFrame
+              .getViewport()
+              .getCalcManager()
+              .startWorker(
+                      new AADisorderClient(sh, alignFrame, preset, paramset));
+    }
+  }
+
+  public SequenceAnnotationWSClient(AAConSettings fave,
+          AlignFrame alignFrame, boolean b)
+  {
+    super(alignFrame, fave.getPreset(), fave.getJobArgset());
+    initSequenceAnnotationWSClient(fave.getService(), alignFrame,
+            fave.getPreset(), b);
   }
 
   /*
@@ -100,64 +162,319 @@ public class SequenceAnnotationWSClient extends Jws2Client
   public void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
           final AlignFrame alignFrame)
   {
-    if (service.serviceType.toLowerCase().contains("aaconws"))
+    if (registerAAConWSInstance(wsmenu, service, alignFrame)) {
+      // Alignment dependent analysis calculation WS gui
+      return;
+    }
+    boolean hasparams = service.hasParameters();
+    // Assume name ends in WS
+    String calcName = service.serviceType.substring(0,
+            service.serviceType.length() - 2);
+
+    JMenuItem annotservice = new JMenuItem(calcName + " Defaults");
+    annotservice.addActionListener(new ActionListener()
     {
-      boolean hasparams = service.hasParameters();
-      String calcName = "AACons";
-      JMenuItem aacons = new JMenuItem("AACons Defaults");
-      aacons.addActionListener(new ActionListener()
+
+      @Override
+      public void actionPerformed(ActionEvent e)
       {
+        new SequenceAnnotationWSClient(service, alignFrame, null, false);
+      }
+    });
+    wsmenu.add(annotservice);
+    if (hasparams)
+    {
+      // only add these menu options if the service has user-modifiable
+      // arguments
+      annotservice = new JMenuItem("Edit settings and run ...");
+      annotservice
+              .setToolTipText("View and change parameters before running calculation");
 
-        @Override
+      annotservice.addActionListener(new ActionListener()
+      {
         public void actionPerformed(ActionEvent e)
         {
-          new SequenceAnnotationWSClient(service, alignFrame, null, false);
+          new SequenceAnnotationWSClient(service, alignFrame, null, true);
         }
       });
-      wsmenu.add(aacons);
-      if (hasparams)
+      wsmenu.add(annotservice);
+      List<WsParamSetI> presets = service.getParamStore().getPresets();
+      if (presets != null && presets.size() > 0)
       {
-        // only add these menu options if the service has user-modifiable
-        // arguments
-        aacons = new JMenuItem("Edit settings and run ...");
-        aacons.setToolTipText("View and change the parameters before alignment.");
+        JMenu presetlist = new JMenu("Run " + calcName + "with preset");
 
-        aacons.addActionListener(new ActionListener()
+        for (final WsParamSetI preset : presets)
         {
-          public void actionPerformed(ActionEvent e)
+          final JMenuItem methodR = new JMenuItem(preset.getName());
+          methodR.setToolTipText("<html><p>"
+                  + JvSwingUtils.wrapTooltip("<strong>"
+                          + (preset.isModifiable() ? "User Preset"
+                                  : "Service Preset") + "</strong><br/>"
+                          + preset.getDescription() + "</p>") + "</html>");
+          methodR.addActionListener(new ActionListener()
           {
-            new SequenceAnnotationWSClient(service, alignFrame, null, true);
+            public void actionPerformed(ActionEvent e)
+            {
+              new SequenceAnnotationWSClient(service, alignFrame, preset,
+                      false);
+            }
+
+          });
+          presetlist.add(methodR);
+        }
+        wsmenu.add(presetlist);
+      }
+
+    }
+    else
+    {
+      annotservice = new JMenuItem("View documentation");
+      if (service.docUrl != null)
+      {
+        annotservice.addActionListener(new ActionListener()
+        {
+
+          @Override
+          public void actionPerformed(ActionEvent arg0)
+          {
+            Desktop.instance.showUrl(service.docUrl);
           }
         });
-        wsmenu.add(aacons);
-        List<WsParamSetI> presets = service.getParamStore().getPresets();
-        if (presets != null && presets.size() > 0)
-        {
-          JMenu presetlist = new JMenu("Run " + calcName + "with preset");
+        annotservice.setToolTipText("<html>"
+                + JvSwingUtils.wrapTooltip("View <a href=\""
+                        + service.docUrl + "\">" + service.docUrl + "</a>")
+                + "</html>");
+        wsmenu.add(annotservice);
+      }
+    }
+  }
 
-          for (final WsParamSetI preset : presets)
+  
+  private boolean registerAAConWSInstance(final JMenu wsmenu,
+          final Jws2Instance 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)
+      {
+        JabawsAlignCalcWorker worker = (JabawsAlignCalcWorker) aaconClient.get(0);
+        if (!worker.service.hosturl.equals(service.hosturl))
+        {
+          // javax.swing.SwingUtilities.invokeLater(new Runnable()
           {
-            final JMenuItem methodR = new JMenuItem(preset.getName());
-            methodR.setToolTipText("<html><p>"
-                    + JvSwingUtils.wrapTooltip("<strong>"
-                            + (preset.isModifiable() ? "User Preset"
-                                    : "Service Preset") + "</strong><br/>"
-                            + preset.getDescription() + "</p>") + "</html>");
-            methodR.addActionListener(new ActionListener()
+            // @Override
+            // public void run()
             {
-              public void actionPerformed(ActionEvent e)
-              {
-                new SequenceAnnotationWSClient(service, alignFrame, preset,
-                        false);
-              }
-
-            });
-            presetlist.add(methodR);
+              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());
+      wsmenu.addMenuListener(new MenuListener()
+      {
+
+        @Override
+        public void menuSelected(MenuEvent arg0)
+        {
+          // TODO: refactor to the implementing class.
+          wsmenu.setEnabled((alignFrame.getViewport().getAlignment()
+                  .isNucleotide() ? aaui.isNa() : aaui.isPr()));
+          List<AlignCalcWorkerI> aaconClient = alignFrame.getViewport()
+                  .getCalcManager()
+                  .getRegisteredWorkersOfClass(aaui.getClient());
+          if (aaconClient != null && aaconClient.size() > 0)
+          {
+            aaConEnabled.setSelected(true);
+          }
+          else
+          {
+            aaConEnabled.setSelected(false);
           }
-          wsmenu.add(presetlist);
         }
+
+        @Override
+        public void menuDeselected(MenuEvent arg0)
+        {
+          // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void menuCanceled(MenuEvent arg0)
+        {
+          // TODO Auto-generated method stub
+
+        }
+      });
+      aaConEnabled.setToolTipText("<html><p>"
+              + JvSwingUtils.wrapTooltip(aaui.getAAconToggleTooltip() + "</p>")
+              + "</html>");
+      aaConEnabled.addActionListener(new ActionListener()
+      {
+        @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
+          {
+            buildCurrentAAConWorkerFor(aaui, alignFrame);
+
+          }
+        }
+
+      });
+      wsmenu.add(aaConEnabled);
+      JMenuItem modifyParams = new JMenuItem(aaui.getAAeditSettings());
+      modifyParams.setToolTipText("<html><p>"
+              + JvSwingUtils.wrapTooltip(aaui.getAAeditSettingsTooltip() + "</p>")
+              + "</html>");
+      modifyParams.addActionListener(new ActionListener()
+      {
+
+        @Override
+        public void actionPerformed(ActionEvent arg0)
+        {
+          showAAConAnnotationSettingsFor(aaui, alignFrame);
+        }
+      });
+      wsmenu.add(modifyParams);
+
+    }
+    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
+    AAConSettings fave = (AAConSettings) 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,
+          Jws2Instance service)
+  {
+    /*
+     * preferred settings Whether AACon is automatically recalculated Which
+     * AACon server to use What parameters to use
+     */
+    AAConSettings fave = (AAConSettings) alignFrame.getViewport()
+            .getCalcIdSettingsFor(aaui.getCalcId());
+    if (fave == null)
+    {
+      fave = createDefaultAAConSettings(aaui, service);
+    }
+    else
+    {
+      if (service != null
+              && !fave.getService().hosturl.equals(service.hosturl))
+      {
+        Cache.log.debug("Changing AACon service to " + service.hosturl
+                + " from " + fave.getService().hosturl);
+        fave.setService(service);
       }
+    }
+    new SequenceAnnotationWSClient(fave, alignFrame, false);
+  }
 
+  private static AAConSettings createDefaultAAConSettings(AlignAnalysisUIText aaui)
+  {
+    return createDefaultAAConSettings(aaui, null);
+  }
+
+  private static AAConSettings createDefaultAAConSettings(AlignAnalysisUIText aaui,
+          Jws2Instance service)
+  {
+    if (service != null)
+    {
+      if (!service.serviceType.toString().equals(
+              compbio.ws.client.Services.AAConWS.toString()))
+      {
+        Cache.log
+                .warn("Ignoring invalid preferred service for AACon calculations (service type was "
+                        + service.serviceType + ")");
+        service = null;
+      }
+      else
+      {
+        // 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)
+    {
+      // get the default service for AACon
+      service = Jws2Discoverer.getDiscoverer().getPreferredServiceFor(null,
+              aaui.getServiceType());
+    }
+    if (service == null)
+    {
+      // TODO raise dialog box explaining error, and/or open the JABA
+      // preferences menu.
+      throw new Error("No AACon service found.");
+    }
+    return new AAConSettings(true, service, null, null);
+  }
+
+  private static void removeCurrentAAConWorkerFor(AlignAnalysisUIText aaui, AlignFrame alignFrame)
+  {
+    alignFrame.getViewport().getCalcManager()
+            .removeRegisteredWorkersOfClass(aaui.getClient());
   }
 }