2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.util.dialogrunner;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
29 * daft gymnastics to allow Dialogs to extend from a Swing class and use this
30 * class to implement chained Response run() definition and execution.
32 * There is probably a better way of doing this.
37 * the actual dialog that will be shown - which will also initiate the
40 public class DialogRunner implements DialogRunnerI
42 private Map<Object, List<RunResponse>> callbacks = new HashMap<>();
44 private boolean firstRunWasCalled = false;
54 * Reset so handleResponse will start response execution
56 public void resetResponses()
58 firstRunWasCalled = false;
62 public DialogRunnerI addResponse(RunResponse action)
64 addResponse(false, action);
69 * insert a response at the beginning of the chain for the action. Useful to add
70 * pre-action validations local to the Dialog class
75 public DialogRunnerI setFirstResponse(RunResponse action)
77 return addResponse(true, action);
80 protected DialogRunnerI addResponse(boolean prePend, RunResponse action)
82 List<RunResponse> actions = callbacks.get(action.getTrigger());
85 actions = new ArrayList<>();
86 callbacks.put(action.getTrigger(), actions);
90 actions.add(0,action);
100 * @return true if action is a registered callback
102 public boolean isRegistered(RunResponse action)
104 List<RunResponse> resp = callbacks.get(action.getTrigger());
105 return resp != null && resp.contains(action);
109 * Handles a response by running the chain of registered actions (if any).
110 * Answers the list of responses run (in order).
115 public List<RunResponse> handleResponse(Object response)
117 List<RunResponse> responsesRun = new ArrayList<RunResponse>();
120 * this test is for NaN in Chrome
122 if (response != null && !response.equals(response))
128 * failsafe check for illegal duplicate call(?)
130 if (firstRunWasCalled)
132 // return responsesRun;
134 firstRunWasCalled = true;
136 runResponse(response, responsesRun);
137 if (responsesRun.isEmpty())
139 System.err.println("Did nothing for " + response);
146 * Runs any response handlers registered for the given response. If any
147 * response provides a return value, then the handler for that value is
148 * run next recursively. Handlers are only run once.
153 private void runResponse(Object response, List<RunResponse> alreadyRun)
156 * this test is for NaN in Chrome
158 if (response != null && !response.equals(response))
163 List<RunResponse> actions = response == null ? null : callbacks.get(response);
167 System.err.println("Doing nothing for " + response);
170 for (RunResponse action : actions)
172 if (!alreadyRun.contains(action))
174 action.setReturnValue(null);
176 alreadyRun.add(action);
177 Object returnValue = action.getReturnValue();
178 if (returnValue != null)
181 * RunResponse wants to chain another action
183 runResponse(returnValue, alreadyRun);