JAL-3048 wip externalise trigger from RunResponse
[jalview.git] / src / jalview / util / dialogrunner / DialogRunner.java
index 59f3c98..b6fd6a2 100644 (file)
@@ -26,23 +26,18 @@ import java.util.List;
 import java.util.Map;
 
 /**
- * daft gymnastics to allow Dialogs to extend from a Swing class and use this
- * class to implement chained Response run() definition and execution.
- * 
- * There is probably a better way of doing this.
+ * A helper class that executes registered Runnable actions corresponding to 
+ * user responses in a dialog. This is to enable dialog execution in JalviewJS,
+ * where everything must happen in a single thread of execution. That is, the
+ * dialog has to 'know' all actions that follow a user choice, rather than
+ * returning a response to allow a separate thread to decide the next action.
  * 
  * @author jprocter
- *
- * @param <T>
- *          the actual dialog that will be shown - which will also initiate the
- *          response chain.
  */
 public class DialogRunner implements DialogRunnerI
 {
   private Map<Object, List<RunResponse>> callbacks = new HashMap<>();
 
-  private boolean firstRunWasCalled = false;
-
   /**
    * Constructor
    */
@@ -50,61 +45,19 @@ public class DialogRunner implements DialogRunnerI
   {
   }
 
-  /**
-   * Reset so handleResponse will start response execution
-   */
-  public void resetResponses()
-  {
-    firstRunWasCalled = false;
-  }
-
   @Override
-  public DialogRunnerI addResponse(RunResponse action)
+  public DialogRunnerI addResponse(Object response, RunResponse action)
   {
-    addResponse(false, action);
-    return this;
-  }
-
-  /**
-   * 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 DialogRunnerI setFirstResponse(RunResponse action)
-  {
-    return addResponse(true, action);
-  }
-
-  protected DialogRunnerI addResponse(boolean prePend, RunResponse action)
-  {
-    List<RunResponse> actions = callbacks.get(action.getTrigger());
+       List<RunResponse> actions = callbacks.get(response);
     if (actions == null)
     {
       actions = new ArrayList<>();
-      callbacks.put(action.getTrigger(), actions);
-    }
-    if (prePend)
-    {
-      actions.add(0,action);
-    } else {
-      actions.add(action);
+      callbacks.put(response, actions);
     }
+    actions.add(action);
     return this;
   }
-
-  /**
-   * 
-   * @param action
-   * @return true if action is a registered callback
-   */
-  public boolean isRegistered(RunResponse action)
-  {
-    List<RunResponse> resp = callbacks.get(action.getTrigger());
-    return resp != null && resp.contains(action);
-  }
-
+  
   /**
    * Handles a response by running the chain of registered actions (if any).
    * Answers the list of responses run (in order).
@@ -124,15 +77,6 @@ public class DialogRunner implements DialogRunnerI
       return responsesRun;
     }
     
-    /*
-     * failsafe check for illegal duplicate call(?)
-     */
-    if (firstRunWasCalled)
-    {
-//      return responsesRun;
-    }
-    firstRunWasCalled = true;
-    
        runResponse(response, responsesRun);
     if (responsesRun.isEmpty())
     {