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.List;
28 * daft gymnastics to allow Dialogs to extend from a Swing class and use this
29 * class to implement chained Response run() definition and execution.
31 * There is probably a better way of doing this.
36 * the actual dialog that will be shown - which will also initiate the
39 public class DialogRunner<T extends DialogRunnerI> implements DialogRunnerI
42 private Map<Response, List<RunResponse>> callbacks = new java.util.HashMap<>();
46 public DialogRunner(T ourDialog)
52 * clear all 'was ran' flags so responses can be called again, and firstRun will
53 * trigger response execution
55 public void resetResponses()
57 for (List<RunResponse> lr : callbacks.values())
59 for (RunResponse response : lr)
65 firstRunWasCalled = false;
69 public T response(RunResponse action)
71 return addResponse(false, action);
75 * insert a response at the beginning of the chain for the action. Useful to add
76 * pre-action validations local to the Dialog class
81 public T firstResponse(RunResponse action)
83 return addResponse(true, action);
86 protected T addResponse(boolean prePend, RunResponse action)
88 List<RunResponse> laction = callbacks.get(action.ourTrigger);
91 laction = new ArrayList<>();
92 callbacks.put(action.ourTrigger, laction);
96 laction.add(0,action);
106 * @return true if action is a registered callback
108 public boolean isRegistered(RunResponse action)
110 List<RunResponse> resp = callbacks.get(action.ourTrigger);
113 for (RunResponse r : resp)
126 * @param responseCode
128 public void run(int responseCode)
130 run(new Response(responseCode));
133 public void run(String responseString)
135 run(new Response(responseString));
138 public void run(Object responseObj)
140 run(new Response(responseObj));
144 * start of response handling.
146 * @param responseCode
148 public void firstRun(int responseCode)
150 doFirstRun(new Response(responseCode));
153 public void firstRun(String responseString)
155 doFirstRun(new Response(responseString));
158 public void firstRun(Object responseObj)
160 if (responseObj != null && !responseObj.equals(responseObj))
162 // NaN is an object in Chrome - catch this weirdness
163 // this so we don't cause issues later
166 doFirstRun(new Response(responseObj));
170 boolean firstRunWasCalled = false;
172 private void doFirstRun(Response response)
174 if (firstRunWasCalled)
178 firstRunWasCalled = true;
182 private void run(Response response)
184 if (response.objresp != null
185 && !response.objresp.equals(response.objresp))
187 // NaN is an object in Chrome - catch this weirdness
188 // this so we don't cause issues later
191 responses.add(response);
192 List<RunResponse> laction = callbacks.get(response);
196 System.err.println("Doing nothing for " + response);
199 boolean wasRun = false;
201 for (RunResponse action : laction)
204 // find next action to execute
208 .println("Executing action (" + num + ") for " + response);
210 action.wasRun = true;
212 if (action.returned != null)
214 run(action.returned);
221 System.err.println("Did nothing for " + response);
225 List<Response> responses = new ArrayList<>();