|
@@ -0,0 +1,140 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.ui;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.Main;
|
|
26
|
+import com.dmdirc.logger.Logger;
|
|
27
|
+import com.dmdirc.ui.interfaces.FrameManager;
|
|
28
|
+import com.dmdirc.ui.interfaces.Window;
|
|
29
|
+
|
|
30
|
+import java.util.ArrayList;
|
|
31
|
+import java.util.HashMap;
|
|
32
|
+import java.util.List;
|
|
33
|
+import java.util.Map;
|
|
34
|
+import java.util.Map.Entry;
|
|
35
|
+
|
|
36
|
+public class WindowManager {
|
|
37
|
+
|
|
38
|
+ private final static List<Window> rootWindows = new ArrayList<Window>();
|
|
39
|
+
|
|
40
|
+ private final static Map<Window, List<Window>> childWindows = new HashMap<Window, List<Window>>();
|
|
41
|
+
|
|
42
|
+ private final static List<FrameManager> frameManagers = new ArrayList<FrameManager>();
|
|
43
|
+
|
|
44
|
+ static {
|
|
45
|
+ frameManagers.add(Main.getUI().getMainWindow().getFrameManager());
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ private WindowManager() {
|
|
49
|
+ // Shouldn't be instansiated
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public static void addFrameManager(final FrameManager frameManager) {
|
|
53
|
+ Logger.doAssertion(frameManager != null);
|
|
54
|
+ Logger.doAssertion(!frameManagers.contains(frameManager));
|
|
55
|
+
|
|
56
|
+ frameManagers.add(frameManager);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public static void removeFrameManager(final FrameManager frameManager) {
|
|
60
|
+ Logger.doAssertion(frameManager != null);
|
|
61
|
+ Logger.doAssertion(frameManagers.contains(frameManager));
|
|
62
|
+
|
|
63
|
+ frameManagers.remove(frameManager);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public static void addWindow(final Window window) {
|
|
67
|
+ Logger.doAssertion(window != null);
|
|
68
|
+ Logger.doAssertion(!rootWindows.contains(window));
|
|
69
|
+
|
|
70
|
+ rootWindows.add(window);
|
|
71
|
+ childWindows.put(window, new ArrayList<Window>());
|
|
72
|
+
|
|
73
|
+ fireAddWindow(window);
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public static void addWindow(final Window parent, final Window child) {
|
|
77
|
+ Logger.doAssertion(parent != null, child != null);
|
|
78
|
+ Logger.doAssertion(childWindows.containsKey(parent), !childWindows.containsKey(child));
|
|
79
|
+
|
|
80
|
+ childWindows.get(parent).add(child);
|
|
81
|
+ childWindows.put(child, new ArrayList<Window>());
|
|
82
|
+
|
|
83
|
+ fireAddWindow(parent, child);
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public static void removeWindow(final Window window) {
|
|
87
|
+ Logger.doAssertion(window != null);
|
|
88
|
+ Logger.doAssertion(childWindows.containsKey(window));
|
|
89
|
+ Logger.doAssertion(childWindows.get(window).isEmpty());
|
|
90
|
+
|
|
91
|
+ childWindows.remove(window);
|
|
92
|
+
|
|
93
|
+ if (rootWindows.contains(window)) {
|
|
94
|
+ rootWindows.remove(window);
|
|
95
|
+
|
|
96
|
+ fireDeleteWindow(window);
|
|
97
|
+ } else {
|
|
98
|
+ final Window parent = getParent(window);
|
|
99
|
+
|
|
100
|
+ childWindows.get(parent).remove(window);
|
|
101
|
+
|
|
102
|
+ fireDeleteWindow(parent, window);
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ private static Window getParent(final Window window) {
|
|
107
|
+ for (Entry<Window, List<Window>> entry : childWindows.entrySet()) {
|
|
108
|
+ if (entry.getValue().contains(window)) {
|
|
109
|
+ return entry.getKey();
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ return null;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ private static void fireAddWindow(final Window window) {
|
|
117
|
+ for (FrameManager manager : frameManagers) {
|
|
118
|
+ manager.addWindow(window.getContainer());
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ private static void fireAddWindow(final Window parent, final Window child) {
|
|
123
|
+ for (FrameManager manager : frameManagers) {
|
|
124
|
+ manager.addWindow(parent.getContainer(), child.getContainer());
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ private static void fireDeleteWindow(final Window window) {
|
|
129
|
+ for (FrameManager manager : frameManagers) {
|
|
130
|
+ manager.delWindow(window.getContainer());
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ private static void fireDeleteWindow(final Window parent, final Window child) {
|
|
135
|
+ for (FrameManager manager : frameManagers) {
|
|
136
|
+ manager.delWindow(parent.getContainer(), child.getContainer());
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+}
|