JAL-3691 automatic insertion of Locale.ROOT to toUpperCase() and toLowerCase() and...
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
index ebd826e..8ed4a36 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.ws.jws2;
 
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.AlignmentView;
-import jalview.datamodel.SequenceI;
-import jalview.gui.AlignFrame;
-import jalview.gui.Desktop;
-import jalview.gui.JvSwingUtils;
-import jalview.util.MessageManager;
-import jalview.ws.jws2.jabaws2.Jws2Instance;
-import jalview.ws.params.WsParamSetI;
+import java.util.Locale;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
 import java.util.List;
 
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
-import javax.swing.JOptionPane;
+import javax.swing.ToolTipManager;
 
 import compbio.data.msa.MsaWS;
 import compbio.metadata.Argument;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.AlignmentView;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.gui.JvSwingUtils;
+import jalview.util.MessageManager;
+import jalview.ws.jws2.jabaws2.Jws2Instance;
+import jalview.ws.params.WsParamSetI;
 
 /**
  * DOCUMENT ME!
@@ -57,7 +59,7 @@ public class MsaWSClient extends Jws2Client
 
   public MsaWSClient(Jws2Instance sh, String altitle,
           jalview.datamodel.AlignmentView msa, boolean submitGaps,
-          boolean preserveOrder, Alignment seqdataset,
+          boolean preserveOrder, AlignmentI seqdataset,
           AlignFrame _alignFrame)
   {
     this(sh, null, null, false, altitle, msa, submitGaps, preserveOrder,
@@ -67,7 +69,7 @@ public class MsaWSClient extends Jws2Client
 
   public MsaWSClient(Jws2Instance sh, WsParamSetI preset, String altitle,
           jalview.datamodel.AlignmentView msa, boolean submitGaps,
-          boolean preserveOrder, Alignment seqdataset,
+          boolean preserveOrder, AlignmentI seqdataset,
           AlignFrame _alignFrame)
   {
     this(sh, preset, null, false, altitle, msa, submitGaps, preserveOrder,
@@ -94,7 +96,7 @@ public class MsaWSClient extends Jws2Client
   public MsaWSClient(Jws2Instance sh, WsParamSetI preset,
           List<Argument> arguments, boolean editParams, String altitle,
           jalview.datamodel.AlignmentView msa, boolean submitGaps,
-          boolean preserveOrder, Alignment seqdataset,
+          boolean preserveOrder, AlignmentI seqdataset,
           AlignFrame _alignFrame)
   {
     super(_alignFrame, preset, arguments);
@@ -106,23 +108,28 @@ public class MsaWSClient extends Jws2Client
     if (!(sh.service instanceof MsaWS))
     {
       // redundant at mo - but may change
-      JOptionPane
-              .showMessageDialog(
-                      Desktop.desktop,
-                      MessageManager.formatMessage("label.service_called_is_not_msa_service", new String[]{sh.serviceType}),
-                      MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
+      JvOptionPane.showMessageDialog(Desktop.desktop,
+              MessageManager.formatMessage(
+                      "label.service_called_is_not_msa_service",
+                      new String[]
+                      { sh.serviceType }),
+              MessageManager.getString("label.internal_jalview_error"),
+              JvOptionPane.WARNING_MESSAGE);
 
       return;
     }
     server = (MsaWS) sh.service;
     if ((wsInfo = setWebService(sh, false)) == null)
     {
-      JOptionPane.showMessageDialog(Desktop.desktop,
-                 MessageManager.formatMessage("label.msa_service_is_unknown", new String[]{sh.serviceType}),
-                 MessageManager.getString("label.internal_jalview_error"), JOptionPane.WARNING_MESSAGE);
+      JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager
+              .formatMessage("label.msa_service_is_unknown", new String[]
+              { sh.serviceType }),
+              MessageManager.getString("label.internal_jalview_error"),
+              JvOptionPane.WARNING_MESSAGE);
 
       return;
     }
+
     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
 
   }
@@ -134,7 +141,7 @@ public class MsaWSClient extends Jws2Client
   }
 
   private void startMsaWSClient(String altitle, AlignmentView msa,
-          boolean submitGaps, boolean preserveOrder, Alignment seqdataset)
+          boolean submitGaps, boolean preserveOrder, AlignmentI seqdataset)
   {
     // if (!locateWebService())
     // {
@@ -143,12 +150,11 @@ public class MsaWSClient extends Jws2Client
 
     wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")
             + " of " + altitle + "\nJob details\n");
-    String jobtitle = WebServiceName.toLowerCase();
+    String jobtitle = WebServiceName.toLowerCase(Locale.ROOT);
     if (jobtitle.endsWith("alignment"))
     {
-      if (submitGaps
-              && (!jobtitle.endsWith("realignment") || jobtitle
-                      .indexOf("profile") == -1))
+      if (submitGaps && (!jobtitle.endsWith("realignment")
+              || jobtitle.indexOf("profile") == -1))
       {
         int pos = jobtitle.indexOf("alignment");
         jobtitle = WebServiceName.substring(0, pos) + "re-alignment of "
@@ -165,61 +171,25 @@ public class MsaWSClient extends Jws2Client
               + "alignment of " + altitle;
     }
 
-    MsaWSThread msathread = new MsaWSThread(server, preset, paramset,
-            WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
-            submitGaps, preserveOrder, seqdataset);
-    wsInfo.setthisService(msathread);
-    if (isValidAlignment(alignFrame.getCurrentView().getAlignment()))
+    MsaWSThread msathread = new MsaWSThread(server, preset, paramset, WsURL,
+            wsInfo, alignFrame, WebServiceName, jobtitle, msa, submitGaps,
+            preserveOrder, seqdataset);
+    if (msathread.hasValidInput())
     {
-
-    msathread.start();
+      wsInfo.setthisService(msathread);
+      wsInfo.setVisible(true);
+      msathread.start();
     }
-  }
-
-  private boolean isValidAlignment(AlignmentI seqdataset)
-  {
-    String header = wsInfo.getInfoText()
-            + "\nValidating submited Alignment...";
-    wsInfo.setInfoText(header);
-    int validSeqCount = 0;
-    List<SequenceI> seqs = seqdataset.getSequences();
-    if (seqs.size() < 2)
-    {
-      wsInfo.setInfoText(header
-              + "\nA minimum of two sequences is required to perform this operation");
-      return false;
-    }
-
-    for (SequenceI seq : seqs)
+    else
     {
-
-      if (seq.getSequenceAsString().matches(
-              "([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"))
-      {
-        ++validSeqCount;
-      }
-      if (validSeqCount > 1)
-      {
-        wsInfo.setInfoText(header
-                + "\nAlignment sequences was successfully validated");
-        return true;
-      }
+      wsInfo.setVisible(false);
+      JvOptionPane.showMessageDialog(alignFrame,
+              MessageManager.getString("info.invalid_msa_input_mininfo"),
+              MessageManager.getString("info.invalid_msa_notenough"),
+              JvOptionPane.INFORMATION_MESSAGE);
     }
-
-    wsInfo.setInfoText(header
-            + "\nA minimum of two sequences with at least one non-gap character in each sequence is required to perform this operation");
-    return false;
-  }
-
-  public static void main(String[] args)
-  {
-    System.out
-            .println("A"
-                    .matches("([(a-zA-Z?)(-?)(.?)]+)?[a-zA-Z]([(a-zA-Z?)(-?)(.?)]+)?"));
   }
 
-
-
   protected String getServiceActionKey()
   {
     return "MsaWS";
@@ -243,10 +213,11 @@ public class MsaWSClient extends Jws2Client
     return (WebServiceName.indexOf("lustal") > -1); // cheat!
   }
 
+  @Override
   public void attachWSMenuEntry(JMenu rmsawsmenu,
-          final Jws2Instance service, final AlignFrame alignFrame)
+          final Jws2Instance service, final AlignFrame af)
   {
-    if (registerAAConWSInstance(rmsawsmenu, service, alignFrame))
+    if (registerAAConWSInstance(rmsawsmenu, service, af))
     {
       // Alignment dependent analysis calculation WS gui
       return;
@@ -273,8 +244,8 @@ public class MsaWSClient extends Jws2Client
       if (submitGaps == true)
       {
         action = "Realign ";
-        msawsmenu = new JMenu(MessageManager.formatMessage(
-                "label.realign_with_params", new String[]
+        msawsmenu = new JMenu(MessageManager
+                .formatMessage("label.realign_with_params", new String[]
                 { svcname }));
         msawsmenu.setToolTipText(MessageManager
                 .getString("label.align_sequences_to_existing_alignment"));
@@ -291,12 +262,18 @@ public class MsaWSClient extends Jws2Client
 
       method.addActionListener(new ActionListener()
       {
+        @Override
         public void actionPerformed(ActionEvent e)
         {
-          AlignmentView msa = alignFrame.gatherSequencesForAlignment();
-          new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
-                  true, alignFrame.getViewport().getAlignment()
-                          .getDataset(), alignFrame);
+          AlignmentView msa = af.gatherSequencesForAlignment();
+
+          if (msa != null)
+          {
+            new MsaWSClient(service, af.getTitle(), msa, withGaps,
+                    true,
+                    af.getViewport().getAlignment().getDataset(),
+                    af);
+          }
 
         }
       });
@@ -307,17 +284,19 @@ public class MsaWSClient extends Jws2Client
         // arguments
         method = new JMenuItem(
                 MessageManager.getString("label.edit_settings_and_run"));
-        method.setToolTipText(MessageManager
-                .getString("label.view_and_change_parameters_before_alignment"));
+        method.setToolTipText(MessageManager.getString(
+                "label.view_and_change_parameters_before_alignment"));
 
         method.addActionListener(new ActionListener()
         {
+          @Override
           public void actionPerformed(ActionEvent e)
           {
-            AlignmentView msa = alignFrame.gatherSequencesForAlignment();
-            new MsaWSClient(service, null, null, true, alignFrame
-                    .getTitle(), msa, withGaps, true, alignFrame
-                    .getViewport().getAlignment().getDataset(), alignFrame);
+            AlignmentView msa = af.gatherSequencesForAlignment();
+            if (msa != null)
+            {
+              startJob(service, af, withGaps, msa);
+            }
 
           }
         });
@@ -329,22 +308,54 @@ public class MsaWSClient extends Jws2Client
                   "label.run_with_preset_params", new String[]
                   { calcName }));
 
-          for (final WsParamSetI preset : presets)
+          final int showToolTipFor = ToolTipManager.sharedInstance()
+                  .getDismissDelay();
+          for (final WsParamSetI preSet : presets)
           {
-            final JMenuItem methodR = new JMenuItem(preset.getName());
-            methodR.setToolTipText(JvSwingUtils.wrapTooltip(true, "<p><strong>"
-                            + (preset.isModifiable() ? MessageManager.getString("label.user_preset")
-                                    : MessageManager.getString("label.service_preset")) + "</strong><br/>"
-                            + preset.getDescription() + "</p>"));
+            final JMenuItem methodR = new JMenuItem(preSet.getName());
+            final int QUICK_TOOLTIP = 1500;
+            // JAL-1582 shorten tooltip display time in these menu items as
+            // they can obscure other options
+            methodR.addMouseListener(new MouseAdapter()
+            {
+              @Override
+              public void mouseEntered(MouseEvent e)
+              {
+                ToolTipManager.sharedInstance()
+                        .setDismissDelay(QUICK_TOOLTIP);
+              }
+
+              @Override
+              public void mouseExited(MouseEvent e)
+              {
+                ToolTipManager.sharedInstance()
+                        .setDismissDelay(showToolTipFor);
+              }
+
+            });
+            String tooltip = JvSwingUtils.wrapTooltip(true, "<strong>"
+                    + (preSet.isModifiable()
+                            ? MessageManager.getString("label.user_preset")
+                            : MessageManager
+                                    .getString("label.service_preset"))
+                    + "</strong><br/>" + preSet.getDescription());
+            methodR.setToolTipText(tooltip);
             methodR.addActionListener(new ActionListener()
             {
+              @Override
               public void actionPerformed(ActionEvent e)
               {
-                AlignmentView msa = alignFrame
+                AlignmentView msa = af
                         .gatherSequencesForAlignment();
-                new MsaWSClient(service, preset, alignFrame.getTitle(),
-                        msa, false, true, alignFrame.getViewport()
-                                .getAlignment().getDataset(), alignFrame);
+
+                if (msa != null)
+                {
+                  MsaWSClient msac = new MsaWSClient(service, preSet,
+                          af.getTitle(), msa, false, true,
+                          af.getViewport().getAlignment()
+                                  .getDataset(),
+                          af);
+                }
 
               }
 
@@ -365,4 +376,20 @@ public class MsaWSClient extends Jws2Client
       }
     } while (!finished);
   }
+
+  protected void startJob(final Jws2Instance service, final AlignFrame af,
+          final boolean withGaps, AlignmentView msa)
+  {
+    try {
+    new MsaWSClient(service, null, null, true,
+            af.getTitle(), msa, withGaps, true,
+            af.getViewport().getAlignment().getDataset(),
+            af);
+    } catch (Exception e) {
+      JvOptionPane.showMessageDialog(alignFrame, e.getMessage(),
+              MessageManager.getString("label.state_job_error"),
+              JvOptionPane.WARNING_MESSAGE);
+
+    }
+  }
 }