|
@@ -28,8 +28,8 @@ import com.dmdirc.Precondition;
|
28
|
28
|
import com.dmdirc.logger.ErrorLevel;
|
29
|
29
|
import com.dmdirc.logger.Logger;
|
30
|
30
|
import com.dmdirc.ui.interfaces.FrameListener;
|
|
31
|
+import com.dmdirc.util.ListenerList;
|
31
|
32
|
|
32
|
|
-import java.util.ArrayList;
|
33
|
33
|
import java.util.Collection;
|
34
|
34
|
import java.util.Collections;
|
35
|
35
|
import java.util.List;
|
|
@@ -39,37 +39,29 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
39
|
39
|
* The WindowManager maintains a list of all open windows, and their
|
40
|
40
|
* parent/child relations.
|
41
|
41
|
*/
|
42
|
|
-public final class WindowManager {
|
|
42
|
+public class WindowManager {
|
|
43
|
+
|
|
44
|
+ /** Singleton instance of the WindowManager. */
|
|
45
|
+ private static final WindowManager INSTANCE = new WindowManager();
|
43
|
46
|
|
44
|
47
|
/** A list of root windows. */
|
45
|
|
- private static final List<FrameContainer> ROOT_WINDOWS
|
|
48
|
+ private final List<FrameContainer> rootWindows
|
46
|
49
|
= new CopyOnWriteArrayList<FrameContainer>();
|
47
|
50
|
|
48
|
51
|
/** A list of frame listeners. */
|
49
|
|
- private static final List<FrameListener> FRAME_LISTENERS
|
50
|
|
- = new ArrayList<FrameListener>();
|
51
|
|
-
|
52
|
|
- /**
|
53
|
|
- * Creates a new instance of WindowManager.
|
54
|
|
- */
|
55
|
|
- private WindowManager() {
|
56
|
|
- // Shouldn't be instansiated
|
57
|
|
- }
|
|
52
|
+ private final ListenerList listeners
|
|
53
|
+ = new ListenerList();
|
58
|
54
|
|
59
|
55
|
/**
|
60
|
56
|
* Registers a FrameListener with the WindowManager.
|
61
|
57
|
*
|
62
|
58
|
* @param frameListener The frame listener to be registered
|
63
|
59
|
*/
|
64
|
|
- @Precondition({
|
65
|
|
- "The specified FrameListener is not null",
|
66
|
|
- "The specified FrameListener has not already been added"
|
67
|
|
- })
|
68
|
|
- public static void addFrameListener(final FrameListener frameListener) {
|
|
60
|
+ @Precondition("The specified FrameListener is not null")
|
|
61
|
+ public void addListener(final FrameListener frameListener) {
|
69
|
62
|
Logger.assertTrue(frameListener != null);
|
70
|
|
- Logger.assertTrue(!FRAME_LISTENERS.contains(frameListener));
|
71
|
63
|
|
72
|
|
- FRAME_LISTENERS.add(frameListener);
|
|
64
|
+ listeners.add(FrameListener.class, frameListener);
|
73
|
65
|
}
|
74
|
66
|
|
75
|
67
|
/**
|
|
@@ -79,14 +71,11 @@ public final class WindowManager {
|
79
|
71
|
* @param frameListener The frame listener to be registered
|
80
|
72
|
* @since 0.6.6
|
81
|
73
|
*/
|
82
|
|
- @Precondition({
|
83
|
|
- "The specified FrameListener is not null",
|
84
|
|
- "The specified FrameListener has not already been added"
|
85
|
|
- })
|
86
|
|
- public static void addAndExecuteFrameListener(final FrameListener frameListener) {
|
87
|
|
- addFrameListener(frameListener);
|
|
74
|
+ @Precondition("The specified FrameListener is not null")
|
|
75
|
+ public void addListenerAndSync(final FrameListener frameListener) {
|
|
76
|
+ addListener(frameListener);
|
88
|
77
|
|
89
|
|
- for (FrameContainer root : ROOT_WINDOWS) {
|
|
78
|
+ for (FrameContainer root : rootWindows) {
|
90
|
79
|
frameListener.addWindow(root, true);
|
91
|
80
|
|
92
|
81
|
for (FrameContainer child : root.getChildren()) {
|
|
@@ -118,15 +107,8 @@ public final class WindowManager {
|
118
|
107
|
*
|
119
|
108
|
* @param frameListener The frame listener to be removed
|
120
|
109
|
*/
|
121
|
|
- @Precondition({
|
122
|
|
- "The specified FrameListener is not null",
|
123
|
|
- "The specified FrameListener has already been added and not removed"
|
124
|
|
- })
|
125
|
|
- public static void removeFrameListener(final FrameListener frameListener) {
|
126
|
|
- Logger.assertTrue(frameListener != null);
|
127
|
|
- Logger.assertTrue(FRAME_LISTENERS.contains(frameListener));
|
128
|
|
-
|
129
|
|
- FRAME_LISTENERS.remove(frameListener);
|
|
110
|
+ public void removeListener(final FrameListener frameListener) {
|
|
111
|
+ listeners.remove(FrameListener.class, frameListener);
|
130
|
112
|
}
|
131
|
113
|
|
132
|
114
|
/**
|
|
@@ -139,7 +121,7 @@ public final class WindowManager {
|
139
|
121
|
"The specified Window is not null",
|
140
|
122
|
"The specified Window has not already been added"
|
141
|
123
|
})
|
142
|
|
- public static void addWindow(final FrameContainer window) {
|
|
124
|
+ public void addWindow(final FrameContainer window) {
|
143
|
125
|
addWindow(window, true);
|
144
|
126
|
}
|
145
|
127
|
|
|
@@ -154,11 +136,11 @@ public final class WindowManager {
|
154
|
136
|
"The specified Window is not null",
|
155
|
137
|
"The specified Window has not already been added"
|
156
|
138
|
})
|
157
|
|
- public static void addWindow(final FrameContainer window, final boolean focus) {
|
|
139
|
+ public void addWindow(final FrameContainer window, final boolean focus) {
|
158
|
140
|
Logger.assertTrue(window != null);
|
159
|
|
- Logger.assertTrue(!ROOT_WINDOWS.contains(window));
|
|
141
|
+ Logger.assertTrue(!rootWindows.contains(window));
|
160
|
142
|
|
161
|
|
- ROOT_WINDOWS.add(window);
|
|
143
|
+ rootWindows.add(window);
|
162
|
144
|
|
163
|
145
|
fireAddWindow(window, focus);
|
164
|
146
|
}
|
|
@@ -171,7 +153,7 @@ public final class WindowManager {
|
171
|
153
|
* @since 0.6.4
|
172
|
154
|
*/
|
173
|
155
|
@Precondition("The specified Windows are not null")
|
174
|
|
- public static void addWindow(final FrameContainer parent,
|
|
156
|
+ public void addWindow(final FrameContainer parent,
|
175
|
157
|
final FrameContainer child) {
|
176
|
158
|
addWindow(parent, child, true);
|
177
|
159
|
}
|
|
@@ -189,7 +171,7 @@ public final class WindowManager {
|
189
|
171
|
"The specified parent is in the window hierarchy already",
|
190
|
172
|
"The specified child is NOT in the window hierarchy already"
|
191
|
173
|
})
|
192
|
|
- public static void addWindow(final FrameContainer parent,
|
|
174
|
+ public void addWindow(final FrameContainer parent,
|
193
|
175
|
final FrameContainer child, final boolean focus) {
|
194
|
176
|
Logger.assertTrue(parent != null);
|
195
|
177
|
Logger.assertTrue(isInHierarchy(parent));
|
|
@@ -210,8 +192,8 @@ public final class WindowManager {
|
210
|
192
|
* @param target The container to be tested
|
211
|
193
|
* @return True if the target is in the hierarchy, false otherise
|
212
|
194
|
*/
|
213
|
|
- protected static boolean isInHierarchy(final FrameContainer target) {
|
214
|
|
- return target != null && (ROOT_WINDOWS.contains(target)
|
|
195
|
+ protected boolean isInHierarchy(final FrameContainer target) {
|
|
196
|
+ return target != null && (rootWindows.contains(target)
|
215
|
197
|
|| isInHierarchy(target.getParent()));
|
216
|
198
|
}
|
217
|
199
|
|
|
@@ -228,7 +210,7 @@ public final class WindowManager {
|
228
|
210
|
"The specified window is not null",
|
229
|
211
|
"The specified window is in the window hierarchy"
|
230
|
212
|
})
|
231
|
|
- public static void removeWindow(final FrameContainer window) {
|
|
213
|
+ public void removeWindow(final FrameContainer window) {
|
232
|
214
|
Logger.assertTrue(isInHierarchy(window));
|
233
|
215
|
Logger.assertTrue(window != null);
|
234
|
216
|
|
|
@@ -236,9 +218,9 @@ public final class WindowManager {
|
236
|
218
|
child.close();
|
237
|
219
|
}
|
238
|
220
|
|
239
|
|
- if (ROOT_WINDOWS.contains(window)) {
|
|
221
|
+ if (rootWindows.contains(window)) {
|
240
|
222
|
fireDeleteWindow(window);
|
241
|
|
- ROOT_WINDOWS.remove(window);
|
|
223
|
+ rootWindows.remove(window);
|
242
|
224
|
} else {
|
243
|
225
|
final FrameContainer parent = window.getParent();
|
244
|
226
|
fireDeleteWindow(parent, window);
|
|
@@ -275,7 +257,7 @@ public final class WindowManager {
|
275
|
257
|
"The specified window is in the window hierarchy"
|
276
|
258
|
})
|
277
|
259
|
@Deprecated
|
278
|
|
- public static void removeWindow(final FrameContainer window, final boolean canWait) {
|
|
260
|
+ public void removeWindow(final FrameContainer window, final boolean canWait) {
|
279
|
261
|
removeWindow(window);
|
280
|
262
|
}
|
281
|
263
|
|
|
@@ -287,10 +269,10 @@ public final class WindowManager {
|
287
|
269
|
* @return The specified custom window, or null
|
288
|
270
|
*/
|
289
|
271
|
@Precondition("The specified window name is not null")
|
290
|
|
- public static FrameContainer findCustomWindow(final String name) {
|
|
272
|
+ public FrameContainer findCustomWindow(final String name) {
|
291
|
273
|
Logger.assertTrue(name != null);
|
292
|
274
|
|
293
|
|
- return findCustomWindow(ROOT_WINDOWS, name);
|
|
275
|
+ return findCustomWindow(rootWindows, name);
|
294
|
276
|
}
|
295
|
277
|
|
296
|
278
|
/**
|
|
@@ -306,7 +288,7 @@ public final class WindowManager {
|
306
|
288
|
"The specified parent window is not null",
|
307
|
289
|
"The specified parent window has been added to the Window Manager"
|
308
|
290
|
})
|
309
|
|
- public static FrameContainer findCustomWindow(final FrameContainer parent,
|
|
291
|
+ public FrameContainer findCustomWindow(final FrameContainer parent,
|
310
|
292
|
final String name) {
|
311
|
293
|
Logger.assertTrue(parent != null);
|
312
|
294
|
Logger.assertTrue(name != null);
|
|
@@ -322,7 +304,7 @@ public final class WindowManager {
|
322
|
304
|
* @param name The name of the custom window to search for
|
323
|
305
|
* @return The custom window if found, or null otherwise
|
324
|
306
|
*/
|
325
|
|
- private static FrameContainer findCustomWindow(final Collection<FrameContainer> windows,
|
|
307
|
+ private FrameContainer findCustomWindow(final Collection<FrameContainer> windows,
|
326
|
308
|
final String name) {
|
327
|
309
|
for (FrameContainer window : windows) {
|
328
|
310
|
if (window instanceof CustomWindow
|
|
@@ -340,8 +322,8 @@ public final class WindowManager {
|
340
|
322
|
* @since 0.6.4
|
341
|
323
|
* @return A collection of all known root windows.
|
342
|
324
|
*/
|
343
|
|
- public static Collection<FrameContainer> getRootWindows() {
|
344
|
|
- return Collections.unmodifiableCollection(ROOT_WINDOWS);
|
|
325
|
+ public Collection<FrameContainer> getRootWindows() {
|
|
326
|
+ return Collections.unmodifiableCollection(rootWindows);
|
345
|
327
|
}
|
346
|
328
|
|
347
|
329
|
/**
|
|
@@ -350,8 +332,8 @@ public final class WindowManager {
|
350
|
332
|
* @param window The window that was added
|
351
|
333
|
* @param focus Should this window become focused
|
352
|
334
|
*/
|
353
|
|
- private static void fireAddWindow(final FrameContainer window, final boolean focus) {
|
354
|
|
- for (FrameListener listener : FRAME_LISTENERS) {
|
|
335
|
+ private void fireAddWindow(final FrameContainer window, final boolean focus) {
|
|
336
|
+ for (FrameListener listener : listeners.get(FrameListener.class)) {
|
355
|
337
|
listener.addWindow(window, focus);
|
356
|
338
|
}
|
357
|
339
|
}
|
|
@@ -364,9 +346,9 @@ public final class WindowManager {
|
364
|
346
|
* @param focus Should this window become focused
|
365
|
347
|
*
|
366
|
348
|
*/
|
367
|
|
- private static void fireAddWindow(final FrameContainer parent,
|
|
349
|
+ private void fireAddWindow(final FrameContainer parent,
|
368
|
350
|
final FrameContainer child, final boolean focus) {
|
369
|
|
- for (FrameListener listener : FRAME_LISTENERS) {
|
|
351
|
+ for (FrameListener listener : listeners.get(FrameListener.class)) {
|
370
|
352
|
listener.addWindow(parent, child, focus);
|
371
|
353
|
}
|
372
|
354
|
}
|
|
@@ -376,8 +358,8 @@ public final class WindowManager {
|
376
|
358
|
*
|
377
|
359
|
* @param window The window that was removed
|
378
|
360
|
*/
|
379
|
|
- private static void fireDeleteWindow(final FrameContainer window) {
|
380
|
|
- for (FrameListener listener : FRAME_LISTENERS) {
|
|
361
|
+ private void fireDeleteWindow(final FrameContainer window) {
|
|
362
|
+ for (FrameListener listener : listeners.get(FrameListener.class)) {
|
381
|
363
|
listener.delWindow(window);
|
382
|
364
|
}
|
383
|
365
|
}
|
|
@@ -388,11 +370,19 @@ public final class WindowManager {
|
388
|
370
|
* @param parent The parent window
|
389
|
371
|
* @param child The child window that was removed
|
390
|
372
|
*/
|
391
|
|
- private static void fireDeleteWindow(final FrameContainer parent,
|
|
373
|
+ private void fireDeleteWindow(final FrameContainer parent,
|
392
|
374
|
final FrameContainer child) {
|
393
|
|
- for (FrameListener listener : FRAME_LISTENERS) {
|
|
375
|
+ for (FrameListener listener : listeners.get(FrameListener.class)) {
|
394
|
376
|
listener.delWindow(parent, child);
|
395
|
377
|
}
|
396
|
378
|
}
|
397
|
379
|
|
|
380
|
+ /**
|
|
381
|
+ * Returns a singleton instance of the WindowManager.
|
|
382
|
+ *
|
|
383
|
+ * @return A singleton WindowManager instance
|
|
384
|
+ */
|
|
385
|
+ public static WindowManager getWindowManager() {
|
|
386
|
+ return INSTANCE;
|
|
387
|
+ }
|
398
|
388
|
}
|