X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2Fdialogrunner%2FDialogRunner.java;h=b6311288c0a58fec53f151f3ad69561380e190ff;hb=be9359e9a28efb6aade9bdac054d76b4f9452c47;hp=fc1a73d5ae1ca5d7d0e7f42a0812c7b1f7d83cb2;hpb=8b4803348445fde3efd484eabc39fadd1d9fd3e8;p=jalview.git diff --git a/src/jalview/util/dialogrunner/DialogRunner.java b/src/jalview/util/dialogrunner/DialogRunner.java index fc1a73d..b631128 100644 --- a/src/jalview/util/dialogrunner/DialogRunner.java +++ b/src/jalview/util/dialogrunner/DialogRunner.java @@ -39,7 +39,7 @@ import java.util.Map; public class DialogRunner implements DialogRunnerI { - private Map callbacks = new java.util.HashMap<>(); + private Map> callbacks = new java.util.HashMap<>(); public T dialog; @@ -54,9 +54,12 @@ public class DialogRunner implements DialogRunnerI */ public void resetResponses() { - for (RunResponse response : callbacks.values()) + for (List lr : callbacks.values()) { - response.reset(); + for (RunResponse response : lr) + { + response.reset(); + } } responses.clear(); firstRunWasCalled = false; @@ -65,11 +68,59 @@ public class DialogRunner implements DialogRunnerI @Override public T response(RunResponse action) { - callbacks.put(action.ourTrigger, action); + return addResponse(false, action); + } + + /** + * insert a response at the beginning of the chain for the action. Useful to add + * pre-action validations local to the Dialog class + * + * @param action + * @return + */ + public T firstResponse(RunResponse action) + { + return addResponse(true, action); + } + + protected T addResponse(boolean prePend, RunResponse action) + { + List laction = callbacks.get(action.ourTrigger); + if (laction == null) + { + laction = new ArrayList<>(); + callbacks.put(action.ourTrigger, laction); + } + if (prePend) + { + laction.add(0,action); + } else { + laction.add(action); + } return dialog; } /** + * + * @param action + * @return true if action is a registered callback + */ + public boolean isRegistered(RunResponse action) + { + List resp = callbacks.get(action.ourTrigger); + if (resp != null) + { + for (RunResponse r : resp) + { + if (r == action) + { + return true; + } + } + } + return false; + } + /** * handle a response * * @param responseCode @@ -125,24 +176,36 @@ public class DialogRunner implements DialogRunnerI private void run(Response response) { responses.add(response); - RunResponse action = callbacks.get(response); - if (action == null) + List laction = callbacks.get(response); + + if (laction == null) { System.err.println("Doing nothing for " + response); return; } - if (action.wasRun) + boolean wasRun = false; + int num = 0; + for (RunResponse action : laction) { - System.err - .println("IMPLEMENTATION ERROR: Cycle in DialogRunner ! for " - + action); + num++; + // find next action to execute + if (!action.wasRun) + { + System.err + .println("Executing action (" + num + ") for " + response); + wasRun = true; + action.wasRun = true; + action.run(); + if (action.returned != null) + { + run(action.returned); + } + break; + } } - System.err.println("Executing action for " + response); - action.wasRun = true; - action.run(); - if (action.returned != null) + if (!wasRun) { - run(action.returned); + System.err.println("Did nothing for " + response); } }