JAL-3878 Create proper menu for interactive services.
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 21 Oct 2021 14:53:50 +0000 (16:53 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 21 Oct 2021 14:53:50 +0000 (16:53 +0200)
src/jalview/ws2/operations/AnnotationOperation.java
src/jalview/ws2/operations/AnnotationServiceWorker.java

index 31ac0a8..884d327 100644 (file)
@@ -4,15 +4,20 @@ import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CompletionStage;
 
+import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.event.MenuEvent;
+import javax.swing.event.MenuListener;
 
+import jalview.api.AlignCalcManagerI2;
 import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
 import jalview.gui.WsJobParameters;
 import jalview.io.FeaturesFile;
+import jalview.util.Format;
 import jalview.util.MessageManager;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
@@ -139,10 +144,13 @@ public class AnnotationOperation implements Operation
   @Override
   public MenuEntryProviderI getMenuBuilder()
   {
-    return this::buildMenu;
+    if (isInteractive())
+      return this::buildInteractiveMenu;
+    else
+      return this::buildClassicMenu;
   }
 
-  protected void buildMenu(JMenu parent, AlignFrame frame)
+  protected void buildClassicMenu(JMenu parent, AlignFrame frame)
   {
     final var calcName = service.getName();
     PollingTaskExecutor wsExecutor = frame.getViewport().getWSExecutor();
@@ -151,16 +159,8 @@ public class AnnotationOperation implements Operation
       var item = new JMenuItem(MessageManager.formatMessage(
           "label.calcname_with_default_settings", calcName));
       item.addActionListener((event) -> {
-        /* What is the purpose of AlignViewport and AlignmentViewPanel? */
-        AlignViewport viewport = frame.getCurrentView();
-        AlignmentViewPanel alignPanel = frame.alignPanel;
-        var worker = new AnnotationServiceWorker(this, service,
-            Collections.emptyList(), viewport, alignPanel, frame, frame,
-            calcManager);
-        if (isInteractive())
-          calcManager.registerWorker(worker);
-        else
-          calcManager.startWorker(worker);
+        var worker = createWorker(Collections.emptyList(), frame, calcManager);
+        calcManager.startWorker(worker);
       });
       parent.add(item);
     }
@@ -175,15 +175,8 @@ public class AnnotationOperation implements Operation
             .thenAcceptAsync((arguments) -> {
               if (arguments != null)
               {
-                AlignViewport viewport = frame.getCurrentView();
-                AlignmentViewPanel alignPanel = frame.alignPanel;
-                var worker = new AnnotationServiceWorker(
-                    AnnotationOperation.this, service, arguments, viewport,
-                    alignPanel, frame, frame, calcManager);
-                if (isInteractive())
-                  calcManager.registerWorker(worker);
-                else
-                  calcManager.startWorker(worker);
+                var worker = createWorker(arguments, frame, calcManager);
+                calcManager.startWorker(worker);
               }
             });
       });
@@ -191,6 +184,77 @@ public class AnnotationOperation implements Operation
     }
   }
 
+  protected void buildInteractiveMenu(JMenu parent, AlignFrame frame)
+  {
+    final var calcName = service.getName();
+    final var calcManager = frame.getViewport().getCalcManager();
+    final JCheckBoxMenuItem runItem;
+    {
+      // TODO use MessageManager and set tool tip text
+      runItem = new JCheckBoxMenuItem(
+          String.format("%s calculations", calcName));
+      runItem.addActionListener((event) -> {
+        calcManager.removeWorkersForName(calcName);
+        var worker = createWorker(Collections.emptyList(), frame, calcManager);
+        calcManager.registerWorker(worker);
+      });
+      parent.add(runItem);
+    }
+    JMenuItem _editItem = null;
+    if (service.hasParameters())
+    {
+      // TODO use MessageManager and set tool tip text
+      _editItem = new JMenuItem(
+          String.format("Edit %s settings", calcName));
+      _editItem.addActionListener((event) -> {
+        openEditParamsDialog(service, null, null)
+            .thenAcceptAsync((arguments) -> {
+              if (arguments != null)
+              {
+                calcManager.removeWorkersForName(calcName);
+                var worker = createWorker(arguments, frame, calcManager);
+                calcManager.registerWorker(worker);
+              }
+            });
+      });
+      parent.add(_editItem);
+    }
+    final var editItem = _editItem;
+
+    parent.addMenuListener(new MenuListener()
+      {
+        @Override
+        public void menuSelected(MenuEvent e)
+        {
+          var isNuc = frame.getViewport().getAlignment().isNucleotide();
+          var menuEnabled = (isNuc && isNucleotideOperation()) ||
+              (!isNuc && isProteinOperation());
+          runItem.setEnabled(menuEnabled);
+          if (editItem != null)
+            editItem.setEnabled(menuEnabled);
+          boolean currentlyRunning = calcManager.getWorkersForName(calcName).size() > 0;
+          runItem.setSelected(currentlyRunning);
+        }
+
+        @Override
+        public void menuDeselected(MenuEvent e) {}
+
+        @Override
+        public void menuCanceled(MenuEvent e) {}
+      });
+  }
+
+  private AnnotationServiceWorker createWorker(
+      List<ArgumentI> arguments, AlignFrame frame, AlignCalcManagerI2 calcManager)
+  {
+    /* What is the purpose of AlignViewport and AlignmentViewPanel? */
+    AlignViewport viewport = frame.getCurrentView();
+    AlignmentViewPanel alignPanel = frame.alignPanel;
+    return new AnnotationServiceWorker(this, service,
+        arguments, viewport, alignPanel, frame, frame,
+        calcManager);
+  }
+
   private CompletionStage<List<ArgumentI>> openEditParamsDialog(
       WebServiceI service, WsParamSetI preset, List<ArgumentI> arguments)
   {
index 7f026a7..034b581 100644 (file)
@@ -123,7 +123,7 @@ public class AnnotationServiceWorker implements PollableAlignCalcWorkerI
   @Override
   public boolean isDeletable()
   {
-    return false;
+    return true;
   }
 
   @Override