import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
-import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.CompletionStage;
import jalview.ws.params.ArgumentI;
import jalview.ws.params.ParamDatastoreI;
import jalview.ws.params.WsParamSetI;
+import jalview.ws2.actions.BaseTask;
+import jalview.ws2.actions.PollingTaskExecutor;
import jalview.ws2.actions.alignment.AlignmentAction;
+import jalview.ws2.actions.alignment.AlignmentResult;
+import jalview.ws2.actions.annotation.AlignCalcWorkerAdapter;
import jalview.ws2.actions.annotation.AnnotationAction;
import jalview.ws2.actions.api.ActionI;
import jalview.ws2.actions.api.TaskI;
import jalview.ws2.api.WebService;
import jalview.ws2.client.api.WebServiceProviderI;
-import static java.lang.String.format;
-
public class WebServicesMenuManager
{
private final JMenu menu;
noServicesItem.setEnabled(false);
}
- private Map<String, WeakReference<TaskI<?>>> interactiveTasks = new HashMap<>();
-
public WebServicesMenuManager(String name, AlignFrame frame)
{
this.frame = frame;
// sort actions by name pulling nulls to the front
actions.sort(Comparator.comparing(
ActionI::getName, Comparator.nullsFirst(Comparator.naturalOrder())));
- for (int i = 0; i < actions.size(); i++) {
+ for (int i = 0; i < actions.size(); i++)
+ {
addEntriesForAction(actions.get(i), atMenu, atMenu == menu);
}
}
serviceItem.removeActionListener(l);
WebService<?> service = action.getWebService();
serviceItem.addActionListener(e -> {
- if (serviceItem.getState())
- {
- cancelAndRunInteractive(action, frame.getCurrentView(), arguments,
- Credentials.empty());
- }
- else
- {
- cancelInteractive(service.getName());
- }
+ runAction(action, frame.getCurrentView(), arguments,
+ Credentials.empty());
});
serviceItem.setSelected(true);
lastPreset[0] = null;
arguments.clear();
arguments.addAll(args);
- cancelAndRunInteractive(action, frame.getCurrentView(),
+ runAction(action, frame.getCurrentView(),
arguments, Credentials.empty());
}
});
var item = new JMenuItem(preset.getName());
item.addActionListener(e -> {
lastPreset[0] = preset;
- cancelAndRunInteractive(action, frame.getCurrentView(),
+ runAction(action, frame.getCurrentView(),
preset.getArguments(), Credentials.empty());
});
presetsMenu.add(item);
}
}
- cancelAndRunInteractive(action, frame.getCurrentView(), arguments,
+ runAction(action, frame.getCurrentView(), arguments,
Credentials.empty());
}
}
}
- private void cancelInteractive(String wsName)
- {
- var taskRef = interactiveTasks.get(wsName);
- if (taskRef != null && taskRef.get() != null)
- taskRef.get().cancel();
- interactiveTasks.put(wsName, null);
- }
-
- private void cancelAndRunInteractive(ActionI<?> action,
- AlignmentViewport viewport, List<ArgumentI> args, Credentials credentials)
- {
- var wsName = action.getWebService().getName();
- cancelInteractive(wsName);
- var task = runAction(action, viewport, args, credentials);
- interactiveTasks.put(wsName, new WeakReference<>(task));
- }
- private TaskI<?> runAction(ActionI<?> action, AlignmentViewport viewport,
+ private void runAction(ActionI<?> action, AlignmentViewport viewport,
List<ArgumentI> args, Credentials credentials)
{
// casting and instance checks can be avoided with some effort,
// TODO: test if selection contains enough sequences
var _action = (AlignmentAction) action;
var handler = new AlignmentServiceGuiHandler(_action, frame);
- return _action.perform(viewport, args, credentials, handler);
+ BaseTask<?, AlignmentResult> task = _action.createTask(viewport, args, credentials);
+ var executor = PollingTaskExecutor.fromPool(viewport.getServiceExecutor());
+ task.addTaskEventListener(handler);
+ var future = executor.submit(task);
+ task.setCancelAction(() -> { future.cancel(true); });
+ return;
}
if (action instanceof AnnotationAction)
{
+ var calcManager = viewport.getCalcManager();
+ var oldWorkers = calcManager.getWorkersOfClass(AlignCalcWorkerAdapter.class);
+ for (var worker : oldWorkers)
+ {
+ calcManager.cancelWorker(worker);
+ calcManager.removeWorker(worker);
+ }
+
var _action = (AnnotationAction) action;
+ var worker = new AlignCalcWorkerAdapter(viewport, frame.alignPanel,
+ _action, args, credentials);
var handler = new AnnotationServiceGuiHandler(_action, frame);
- return _action.perform(viewport, args, credentials, handler);
+ worker.addTaskEventListener(handler);
+ if (action.getWebService().isInteractive())
+ calcManager.registerWorker(worker);
+ else
+ calcManager.startWorker(worker);
+ return;
}
throw new IllegalArgumentException(
String.format("Illegal action type %s", action.getClass().getName()));