fc1a73d5ae1ca5d7d0e7f42a0812c7b1f7d83cb2
[jalview.git] / src / jalview / util / dialogrunner / DialogRunner.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *
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.
11  *
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.
16  *
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.
20  */
21 package jalview.util.dialogrunner;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 /**
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.
30  * 
31  * There is probably a better way of doing this.
32  * 
33  * @author jprocter
34  *
35  * @param <T>
36  *          the actual dialog that will be shown - which will also initiate the
37  *          response chain.
38  */
39 public class DialogRunner<T extends DialogRunnerI> implements DialogRunnerI
40 {
41
42   private Map<Response, RunResponse> callbacks = new java.util.HashMap<>();
43
44   public T dialog;
45
46   public DialogRunner(T ourDialog)
47   {
48     dialog = ourDialog;
49   }
50
51   /**
52    * clear all 'was ran' flags so responses can be called again, and firstRun will
53    * trigger response execution
54    */
55   public void resetResponses()
56   {
57     for (RunResponse response : callbacks.values())
58     {
59       response.reset();
60     }
61     responses.clear();
62     firstRunWasCalled = false;
63   }
64
65   @Override
66   public T response(RunResponse action)
67   {
68     callbacks.put(action.ourTrigger, action);
69     return dialog;
70   }
71
72   /**
73    * handle a response
74    * 
75    * @param responseCode
76    */
77   public void run(int responseCode)
78   {
79     run(new Response(responseCode));
80   }
81
82   public void run(String responseString)
83   {
84     run(new Response(responseString));
85   }
86
87   public void run(Object responseObj)
88   {
89     run(new Response(responseObj));
90   }
91
92   /**
93    * start of response handling.
94    * 
95    * @param responseCode
96    */
97   public void firstRun(int responseCode)
98   {
99     doFirstRun(new Response(responseCode));
100   }
101
102   public void firstRun(String responseString)
103   {
104     doFirstRun(new Response(responseString));
105   }
106
107   public void firstRun(Object responseObj)
108   {
109     doFirstRun(new Response(responseObj));
110   }
111
112
113   boolean firstRunWasCalled = false;
114
115   private void doFirstRun(Response response)
116   {
117     if (firstRunWasCalled)
118     {
119       return;
120     }
121     firstRunWasCalled = true;
122     run(response);
123   }
124
125   private void run(Response response)
126   {
127     responses.add(response);
128     RunResponse action = callbacks.get(response);
129     if (action == null)
130     {
131       System.err.println("Doing nothing for " + response);
132       return;
133     }
134     if (action.wasRun)
135     {
136       System.err
137               .println("IMPLEMENTATION ERROR: Cycle in DialogRunner ! for "
138                       + action);
139     }
140     System.err.println("Executing action for " + response);
141     action.wasRun = true;
142     action.run();
143     if (action.returned != null)
144     {
145       run(action.returned);
146     }
147   }
148
149   List<Response> responses = new ArrayList<>();
150
151 }