forked from IbcAlpha/IBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultConfigDialogManager.java
204 lines (175 loc) · 7.15 KB
/
DefaultConfigDialogManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// This file is part of IBC.
// Copyright (C) 2004 Steven M. Kearns ([email protected] )
// Copyright (C) 2004 - 2018 Richard L King ([email protected])
// For conditions of distribution and use, see copyright notice in COPYING.txt
// IBC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// IBC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with IBC. If not, see <http://www.gnu.org/licenses/>.
package ibcalpha.ibc;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
public class DefaultConfigDialogManager extends ConfigDialogManager {
private volatile JDialog configDialog = null;
private volatile GetConfigDialogTask configDialogTask;
private final Object futureCreationLock = new Object();
private Future<JDialog> configDialogFuture;
/* records the number of 'things' (including possibly the user) that
* are currently accessing the config dialog
*/
private int usageCount;
@Override
public void logDiagnosticMessage(){
Utils.logToConsole("using default config dialog manager");
}
/**
* Records the fact that the config dialog has closed.
*/
@Override
public void clearConfigDialog() {
openedByUser = false;
configDialog = null;
}
/**
* Returns the Global Configuration dialog, if necessary blocking the calling thread until
* either it is available or a specified timeout has elapsed.
*
* If the Global Configuration dialog is currently open, it is returned immediately without blocking
* the calling thread.
*
* Calling this method from the Swing event dispatch thread results in an IllegalStateException
* being thrown.
*
* @param timeout
* the length of time to wait for the Global Configuration dialog to become available. If this is
* negative, the calling thread blocks until the dialog becomes available.
* @param unit
* the time units for the timeout parameter
* @return
* null if the timeout expires before the Global Configuration dialog becomes available; otherwise
* the Global Configuration dialog
* @throws IllegalStateException
* the method has been called from the Swing event dispatch thread
*/
@Override
public JDialog getConfigDialog(long timeout, TimeUnit unit) throws IllegalStateException {
/* Note that caching a config dialog doesn't work, since they seem to
* be one-time-only. So we have to go via the menu each time this
* method is called (if it isn't currently open or being opened).
*/
if (SwingUtilities.isEventDispatchThread()) throw new IllegalStateException();
Utils.logToConsole("Getting config dialog");
incrementUsage();
if (configDialog != null) {
Utils.logToConsole("Config dialog already found");
return configDialog;
}
synchronized(futureCreationLock) {
if (configDialogFuture != null) {
Utils.logToConsole("Waiting for config dialog future to complete");
} else {
Utils.logToConsole("Creating config dialog future");
configDialogTask = new GetConfigDialogTask();
ExecutorService exec = Executors.newSingleThreadExecutor();
configDialogFuture = exec.submit((Callable<JDialog>)configDialogTask);
exec.shutdown();
}
}
try {
if (timeout < 0) {
configDialog = configDialogFuture.get();
} else {
configDialog = configDialogFuture.get(timeout, unit);
}
Utils.logToConsole("Got config dialog from future");
return configDialog;
} catch (TimeoutException | InterruptedException e) {
return null;
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof IbcException) {
Utils.logError("getConfigDialog could not find " + t.getMessage());
return null;
}
if (t instanceof RuntimeException) throw (RuntimeException)t;
if (t instanceof Error) throw (Error)t;
throw new IllegalStateException(t);
}
}
/**
* Returns the Global Configuration dialog, if necessary blocking the calling thread until
* it is available.
*
* If the Global Configuration dialog is currently open, it is returned immediately without blocking
* the calling thread.
*
* Calling this method from the Swing event dispatch thread results in an IllegalStateException
* being thrown.
*
* @return
* the Global Configuration dialog, or null if the relevant menu entries cannot be found
* @throws IllegalStateException
* the method has been called from the Swing event dispatch thread
*/
@Override
public JDialog getConfigDialog() throws IllegalStateException {
return getConfigDialog(-1, TimeUnit.MILLISECONDS);
}
private boolean openedByUser;
@Override
public void setConfigDialog(JDialog window) {
configDialog = window;
if (configDialogTask == null) {
// config dialog opened by user
openedByUser = true;
} else {
configDialogTask.setConfigDialog(window);
configDialogTask = null;
configDialogFuture = null;
}
}
private boolean apiConfigChangeConfirmationExpected;
@Override
public boolean getApiConfigChangeConfirmationExpected() {
return apiConfigChangeConfirmationExpected;
}
@Override
public void releaseConfigDialog() {
decrementUsage();
}
@Override
public void setApiConfigChangeConfirmationExpected() {
apiConfigChangeConfirmationExpected = true;
}
@Override
public void setApiConfigChangeConfirmationHandled() {
apiConfigChangeConfirmationExpected = false;
}
private synchronized void incrementUsage() {
usageCount++;
}
private synchronized void decrementUsage() {
usageCount--;
if (openedByUser) return;
if (usageCount == 0){
GuiDeferredExecutor.instance().execute(() -> {
Utils.logToConsole("Configuration tasks completed");
SwingUtils.clickButton(configDialog, "OK");
GuiDeferredExecutor.instance().execute(() -> MainWindowManager.mainWindowManager().iconizeIfRequired());
});
}
}
}