Browse Source

Tidy up how windows are added/removed.

- Don't have FrameContainers add themselves to the WindowManager in
  the constructor.

- Don't make FrameContainers keep a reference to WindowManager just
  to remove themselves when they're closed

- Instead have WindowManager register a FrameClosed listener and use
  that to remove windows itself.

Change-Id: Iadbca8a28f7ef3372df7fda722a3cbba2a0fbad6
Depends-On: I0f60ac23438313cab49cdb86c089938f5ee44abc
Reviewed-on: http://gerrit.dmdirc.com/2842
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 years ago
parent
commit
43d0bfac36

+ 2
- 5
src/com/dmdirc/Channel.java View File

@@ -35,7 +35,6 @@ import com.dmdirc.parser.interfaces.ChannelClientInfo;
35 35
 import com.dmdirc.parser.interfaces.ChannelInfo;
36 36
 import com.dmdirc.parser.interfaces.ClientInfo;
37 37
 import com.dmdirc.ui.Colour;
38
-import com.dmdirc.ui.WindowManager;
39 38
 import com.dmdirc.ui.core.components.WindowComponent;
40 39
 import com.dmdirc.ui.input.TabCompleter;
41 40
 import com.dmdirc.ui.input.TabCompleterFactory;
@@ -106,19 +105,17 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
106 105
      * @param configMigrator The config migrator which provides the config for this channel.
107 106
      * @param tabCompleterFactory The factory to use to create tab completers.
108 107
      * @param messageSinkManager The sink manager to use to despatch messages.
109
-     * @param windowManager Window management
110 108
      */
111 109
     public Channel(
112 110
             final Server newServer,
113 111
             final ChannelInfo newChannelInfo,
114 112
             final ConfigProviderMigrator configMigrator,
115 113
             final TabCompleterFactory tabCompleterFactory,
116
-            final MessageSinkManager messageSinkManager,
117
-            final WindowManager windowManager) {
114
+            final MessageSinkManager messageSinkManager) {
118 115
         super("channel-inactive", newChannelInfo.getName(),
119 116
                 Styliser.stipControlCodes(newChannelInfo.getName()),
120 117
                 configMigrator.getConfigProvider(),
121
-                new ChannelCommandParser(newServer), messageSinkManager, windowManager,
118
+                new ChannelCommandParser(newServer), messageSinkManager,
122 119
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
123 120
                     WindowComponent.INPUTFIELD.getIdentifier(),
124 121
                     WindowComponent.TOPICBAR.getIdentifier(),

+ 3
- 9
src/com/dmdirc/CustomWindow.java View File

@@ -23,7 +23,6 @@
23 23
 package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.ui.WindowManager;
27 26
 import com.dmdirc.ui.core.components.WindowComponent;
28 27
 
29 28
 import java.util.Arrays;
@@ -43,9 +42,7 @@ public class CustomWindow extends FrameContainer {
43 42
     public CustomWindow(final String name, final String title,
44 43
             final FrameContainer parent) {
45 44
         super("custom", name, title, parent.getConfigManager(),
46
-                Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()), parent.getWindowManager());
47
-
48
-        parent.getWindowManager().addWindow(parent, this);
45
+                Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()));
49 46
     }
50 47
 
51 48
     /**
@@ -53,14 +50,11 @@ public class CustomWindow extends FrameContainer {
53 50
      *
54 51
      * @param name The name of this custom window
55 52
      * @param title The parent of this custom window
56
-     * @param windowManager For window management
57 53
      */
58
-    public CustomWindow(final String name, final String title, final WindowManager windowManager) {
54
+    public CustomWindow(final String name, final String title) {
59 55
         super("custom", name, title,
60 56
                 IdentityManager.getIdentityManager().getGlobalConfiguration(),
61
-                Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()), windowManager);
62
-
63
-        windowManager.addWindow(this);
57
+                Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()));
64 58
     }
65 59
 
66 60
     /** {@inheritDoc} */

+ 1
- 20
src/com/dmdirc/FrameContainer.java View File

@@ -106,9 +106,6 @@ public abstract class FrameContainer {
106 106
     /** Object used to synchronise styliser access. */
107 107
     private final Object documentSync = new Object();
108 108
 
109
-    /** Window management. */
110
-    private final WindowManager windowManager;
111
-
112 109
     /**
113 110
      * Instantiate new frame container.
114 111
      *
@@ -117,15 +114,12 @@ public abstract class FrameContainer {
117 114
      * @param title The title of this container
118 115
      * @param config The config manager for this container
119 116
      * @param components The UI components that this frame requires
120
-     * @param windowManager Window manager to register from
121 117
      * @since 0.6.4
122 118
      */
123 119
     protected FrameContainer(
124 120
             final String icon, final String name,
125 121
             final String title, final AggregateConfigProvider config,
126
-            final Collection<String> components,
127
-            final WindowManager windowManager) {
128
-        this.windowManager = windowManager;
122
+            final Collection<String> components) {
129 123
         this.configManager = config;
130 124
         this.name = name;
131 125
         this.title = title;
@@ -223,17 +217,6 @@ public abstract class FrameContainer {
223 217
         return new IconManager(getConfigManager(), urlBuilder);
224 218
     }
225 219
 
226
-    /**
227
-     * Gets the window manager associated with this container.
228
-     *
229
-     * @return This container's window manager.
230
-     * @deprecated Window managers should be supplied to classes that need them.
231
-     */
232
-    @Deprecated
233
-    public WindowManager getWindowManager() {
234
-        return windowManager;
235
-    }
236
-
237 220
     /**
238 221
      * Retrieves the {@link IRCDocument} used to store this frame's content.
239 222
      *
@@ -323,8 +306,6 @@ public abstract class FrameContainer {
323 306
         }
324 307
 
325 308
         windowClosing();
326
-
327
-        windowManager.removeWindow(this);
328 309
     }
329 310
 
330 311
     /**

+ 5
- 7
src/com/dmdirc/GlobalWindow.java View File

@@ -61,24 +61,20 @@ public class GlobalWindow extends WritableFrameContainer {
61 61
      *
62 62
      * @param config The ConfigManager to retrieve settings from.
63 63
      * @param parser The command parser to use to parse input.
64
-     * @param windowManager The window manager.
65 64
      * @param tabCompleterFactory The factory to use to create tab completers.
66 65
      * @param messageSinkManager The sink manager to use to despatch messages.
67 66
      */
68 67
     public GlobalWindow(
69 68
             final AggregateConfigProvider config,
70 69
             final CommandParser parser,
71
-            final WindowManager windowManager,
72 70
             final TabCompleterFactory tabCompleterFactory,
73 71
             final MessageSinkManager messageSinkManager) {
74
-        super("icon", "Global", "(Global)", config, parser, messageSinkManager, windowManager,
72
+        super("icon", "Global", "(Global)", config, parser, messageSinkManager,
75 73
                 Arrays.asList(
76 74
                         WindowComponent.TEXTAREA.getIdentifier(),
77 75
                         WindowComponent.INPUTFIELD.getIdentifier()));
78 76
 
79 77
         tabCompleter = tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL);
80
-
81
-        windowManager.addWindow(this);
82 78
     }
83 79
 
84 80
     /** {@inheritDoc} */
@@ -176,9 +172,11 @@ public class GlobalWindow extends WritableFrameContainer {
176 172
                 if (globalConfig.getOptionBool("general", "showglobalwindow")) {
177 173
                     if (globalWindow == null) {
178 174
                         globalWindow = new GlobalWindow(globalConfig,
179
-                                new GlobalCommandParser(globalConfig, commandControllerProvider.get()),
180
-                                windowManagerProvider.get(), tabCompleterFactory,
175
+                                new GlobalCommandParser(globalConfig,
176
+                                commandControllerProvider.get()),
177
+                                tabCompleterFactory,
181 178
                                 messageSinkManagerProvider.get());
179
+                        windowManagerProvider.get().addWindow(globalWindow);
182 180
                     }
183 181
                 } else {
184 182
                     if (globalWindow != null) {

+ 2
- 4
src/com/dmdirc/MessageTarget.java View File

@@ -25,7 +25,6 @@ package com.dmdirc;
25 25
 import com.dmdirc.commandparser.parsers.CommandParser;
26 26
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
27 27
 import com.dmdirc.messages.MessageSinkManager;
28
-import com.dmdirc.ui.WindowManager;
29 28
 
30 29
 import java.util.Collection;
31 30
 
@@ -45,14 +44,13 @@ public abstract class MessageTarget extends WritableFrameContainer {
45 44
      * @param parser The command parser for this container
46 45
      * @param messageSinkManager The sink manager to use to despatch messages.
47 46
      * @param components The UI components that this frame requires
48
-     * @param windowManager Window manager
49 47
      * @since 0.6.4
50 48
      */
51 49
     public MessageTarget(final String icon, final String name,
52 50
             final String title, final AggregateConfigProvider config,
53 51
             final CommandParser parser, final MessageSinkManager messageSinkManager,
54
-            final WindowManager windowManager, final Collection<String> components) {
55
-        super(icon, name, title, config, parser, messageSinkManager, windowManager, components);
52
+            final Collection<String> components) {
53
+        super(icon, name, title, config, parser, messageSinkManager, components);
56 54
     }
57 55
 
58 56
     /**

+ 2
- 5
src/com/dmdirc/Query.java View File

@@ -39,7 +39,6 @@ import com.dmdirc.parser.interfaces.callbacks.NickChangeListener;
39 39
 import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
40 40
 import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
41 41
 import com.dmdirc.parser.interfaces.callbacks.QuitListener;
42
-import com.dmdirc.ui.WindowManager;
43 42
 import com.dmdirc.ui.core.components.WindowComponent;
44 43
 import com.dmdirc.ui.input.TabCompleter;
45 44
 import com.dmdirc.ui.input.TabCompleterFactory;
@@ -74,16 +73,14 @@ public class Query extends MessageTarget implements PrivateActionListener,
74 73
      * @param newServer The server object that this Query belongs to
75 74
      * @param tabCompleterFactory The factory to use to create tab completers.
76 75
      * @param messageSinkManager The sink manager to use to despatch messages.
77
-     * @param windowManager Window management
78 76
      */
79 77
     public Query(final Server newServer, final String newHost,
80 78
             final TabCompleterFactory tabCompleterFactory,
81
-            final MessageSinkManager messageSinkManager,
82
-            final WindowManager windowManager) {
79
+            final MessageSinkManager messageSinkManager) {
83 80
         super("query", newServer.parseHostmask(newHost)[0],
84 81
                 newServer.parseHostmask(newHost)[0],
85 82
                 newServer.getConfigManager(), new QueryCommandParser(newServer),
86
-                messageSinkManager, windowManager,
83
+                messageSinkManager,
87 84
                 Arrays.asList(
88 85
                         WindowComponent.TEXTAREA.getIdentifier(),
89 86
                         WindowComponent.INPUTFIELD.getIdentifier()));

+ 2
- 5
src/com/dmdirc/Raw.java View File

@@ -30,7 +30,6 @@ import com.dmdirc.parser.common.CallbackNotFoundException;
30 30
 import com.dmdirc.parser.interfaces.Parser;
31 31
 import com.dmdirc.parser.interfaces.callbacks.DataInListener;
32 32
 import com.dmdirc.parser.interfaces.callbacks.DataOutListener;
33
-import com.dmdirc.ui.WindowManager;
34 33
 import com.dmdirc.ui.core.components.WindowComponent;
35 34
 import com.dmdirc.ui.input.TabCompleter;
36 35
 
@@ -52,13 +51,11 @@ public class Raw extends WritableFrameContainer
52 51
      *
53 52
      * @param newServer the server to monitor
54 53
      * @param messageSinkManager The sink manager to use to despatch messages.
55
-     * @param windowManager Window management
56 54
      */
57
-    public Raw(final Server newServer, final MessageSinkManager messageSinkManager,
58
-            final WindowManager windowManager) {
55
+    public Raw(final Server newServer, final MessageSinkManager messageSinkManager) {
59 56
         super("raw", "Raw", "(Raw log)", newServer.getConfigManager(),
60 57
                 new ServerCommandParser(newServer.getConfigManager()),
61
-                messageSinkManager, windowManager,
58
+                messageSinkManager,
62 59
                 Arrays.asList(
63 60
                     WindowComponent.TEXTAREA.getIdentifier(),
64 61
                     WindowComponent.INPUTFIELD.getIdentifier()));

+ 3
- 5
src/com/dmdirc/Server.java View File

@@ -240,7 +240,6 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
240 240
                 configMigrator.getConfigProvider(),
241 241
                 commandParser,
242 242
                 messageSinkManager,
243
-                windowManager,
244 243
                 Arrays.asList(
245 244
                     WindowComponent.TEXTAREA.getIdentifier(),
246 245
                     WindowComponent.INPUTFIELD.getIdentifier(),
@@ -567,8 +566,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
567 566
         final String lnick = converter.toLowerCase(nick);
568 567
 
569 568
         if (!queries.containsKey(lnick)) {
570
-            final Query newQuery = new Query(this, host, tabCompleterFactory,
571
-                    messageSinkManager, windowManager);
569
+            final Query newQuery = new Query(this, host, tabCompleterFactory, messageSinkManager);
572 570
 
573 571
             windowManager.addWindow(this, newQuery, focus);
574 572
             ActionManager.getActionManager().triggerEvent(
@@ -608,7 +606,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
608 606
     @Override
609 607
     public void addRaw() {
610 608
         if (raw == null) {
611
-            raw = new Raw(this, messageSinkManager, windowManager);
609
+            raw = new Raw(this, messageSinkManager);
612 610
             windowManager.addWindow(raw, this);
613 611
 
614 612
             try {
@@ -668,7 +666,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
668 666
             final ConfigProviderMigrator channelConfig = identityFactory.createMigratableConfig(
669 667
                     getProtocol(), getIrcd(), getNetwork(), getAddress(), chan.getName());
670 668
             final Channel newChan = new Channel(this, chan, channelConfig,
671
-                    tabCompleterFactory, messageSinkManager, windowManager);
669
+                    tabCompleterFactory, messageSinkManager);
672 670
 
673 671
             windowManager.addWindow(this, newChan, focus);
674 672
             ActionManager.getActionManager().triggerEvent(CoreActionType.CHANNEL_OPENED, null,

+ 1
- 4
src/com/dmdirc/WritableFrameContainer.java View File

@@ -28,7 +28,6 @@ import com.dmdirc.interfaces.actions.ActionType;
28 28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 import com.dmdirc.messages.MessageSinkManager;
30 30
 import com.dmdirc.parser.common.CompositionState;
31
-import com.dmdirc.ui.WindowManager;
32 31
 import com.dmdirc.ui.input.TabCompleter;
33 32
 
34 33
 import java.util.ArrayList;
@@ -65,7 +64,6 @@ public abstract class WritableFrameContainer extends FrameContainer {
65 64
      * @param parser The command parser for this container
66 65
      * @param messageSinkManager The sink manager to use to despatch messages
67 66
      * @param components The UI components that this frame requires
68
-     * @param windowManager For window management
69 67
      * @since 0.6.4
70 68
      */
71 69
     public WritableFrameContainer(
@@ -75,9 +73,8 @@ public abstract class WritableFrameContainer extends FrameContainer {
75 73
             final AggregateConfigProvider config,
76 74
             final CommandParser parser,
77 75
             final MessageSinkManager messageSinkManager,
78
-            final WindowManager windowManager,
79 76
             final Collection<String> components) {
80
-        super(icon, name, title, config, components, windowManager);
77
+        super(icon, name, title, config, components);
81 78
 
82 79
         this.commandParser = parser;
83 80
         this.messageSinkManager = messageSinkManager;

+ 5
- 2
src/com/dmdirc/commandparser/commands/global/OpenWindow.java View File

@@ -100,10 +100,13 @@ public class OpenWindow extends Command implements IntelligentCommand {
100 100
                 ? args.getArgumentsAsString(start + 1) : args.getArguments()[start];
101 101
 
102 102
             if (window == null) {
103
+                CustomWindow newWindow;
103 104
                 if (parent == null) {
104
-                    new CustomWindow(args.getArguments()[start], title, windowManager);
105
+                    newWindow = new CustomWindow(args.getArguments()[start], title);
106
+                    windowManager.addWindow(newWindow);
105 107
                 } else {
106
-                    new CustomWindow(args.getArguments()[start], title, parent);
108
+                    newWindow = new CustomWindow(args.getArguments()[start], title, parent);
109
+                    windowManager.addWindow(parent, newWindow);
107 110
                 }
108 111
             } else {
109 112
                 sendLine(origin, args.isSilent(), FORMAT_ERROR,

+ 1
- 0
src/com/dmdirc/messages/CustomWindowMessageSink.java View File

@@ -67,6 +67,7 @@ public class CustomWindowMessageSink implements MessageSink {
67 67
         if (targetWindow == null) {
68 68
             targetWindow = new CustomWindow(patternMatches[0],
69 69
                     patternMatches[0], source.getServer());
70
+            windowManager.addWindow(source.getServer(), targetWindow);
70 71
         }
71 72
 
72 73
         targetWindow.addLine(messageType, date, args);

+ 21
- 0
src/com/dmdirc/ui/WindowManager.java View File

@@ -25,6 +25,7 @@ package com.dmdirc.ui;
25 25
 import com.dmdirc.CustomWindow;
26 26
 import com.dmdirc.FrameContainer;
27 27
 import com.dmdirc.Precondition;
28
+import com.dmdirc.interfaces.FrameCloseListener;
28 29
 import com.dmdirc.interfaces.ui.FrameListener;
29 30
 import com.dmdirc.logger.Logger;
30 31
 import com.dmdirc.util.collections.ListenerList;
@@ -50,6 +51,9 @@ public class WindowManager {
50 51
     /** A list of frame listeners. */
51 52
     private final ListenerList listeners = new ListenerList();
52 53
 
54
+    /** Listener for frame close events. */
55
+    private final FrameCloseListener closeListener = new CloseListener();
56
+
53 57
     /**
54 58
      * Creates a new instance of {@link WindowManager}.
55 59
      */
@@ -148,6 +152,8 @@ public class WindowManager {
148 152
         rootWindows.add(window);
149 153
 
150 154
         fireAddWindow(window, focus);
155
+
156
+        window.addCloseListener(closeListener);
151 157
     }
152 158
 
153 159
     /**
@@ -186,6 +192,8 @@ public class WindowManager {
186 192
         parent.addChild(child);
187 193
 
188 194
         fireAddWindow(parent, child, focus);
195
+
196
+        child.addCloseListener(closeListener);
189 197
     }
190 198
 
191 199
     /**
@@ -351,4 +359,17 @@ public class WindowManager {
351 359
         }
352 360
     }
353 361
 
362
+    /**
363
+     * Frame close listener that removes the window from this manager.
364
+     */
365
+    private class CloseListener implements FrameCloseListener {
366
+
367
+        /** {@inheritDoc} */
368
+        @Override
369
+        public void windowClosing(final FrameContainer window) {
370
+            removeWindow(window);
371
+        }
372
+
373
+    }
374
+
354 375
 }

+ 2
- 4
test/com/dmdirc/WritableFrameContainerTest.java View File

@@ -27,7 +27,6 @@ import com.dmdirc.config.ConfigBinder;
27 27
 import com.dmdirc.harness.TestWritableFrameContainer;
28 28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 import com.dmdirc.messages.MessageSinkManager;
30
-import com.dmdirc.ui.WindowManager;
31 30
 
32 31
 import java.util.Arrays;
33 32
 
@@ -43,7 +42,6 @@ public class WritableFrameContainerTest {
43 42
 
44 43
     @Mock private AggregateConfigProvider acp;
45 44
     @Mock private ServerManager serverManager;
46
-    @Mock private WindowManager windowManager;
47 45
     @Mock private MessageSinkManager messageSinkManager;
48 46
     private CommandManager commands;
49 47
 
@@ -61,7 +59,7 @@ public class WritableFrameContainerTest {
61 59
     @Test
62 60
     public void testGetNumLines() {
63 61
         final WritableFrameContainer container10
64
-                = new TestWritableFrameContainer(10, acp, commands, messageSinkManager, windowManager);
62
+                = new TestWritableFrameContainer(10, acp, commands, messageSinkManager);
65 63
 
66 64
         final int res0a = container10.getNumLines("");
67 65
         final int res0b = container10.getNumLines("\r");
@@ -91,7 +89,7 @@ public class WritableFrameContainerTest {
91 89
     @Test
92 90
     public void testSplitLine() {
93 91
         final WritableFrameContainer container10
94
-                = new TestWritableFrameContainer(10, acp, commands, messageSinkManager, windowManager);
92
+                = new TestWritableFrameContainer(10, acp, commands, messageSinkManager);
95 93
         final String[][][] tests = new String[][][]{
96 94
             {{""}, {""}},
97 95
             {{"0123456789"}, {"0123456789"}},

+ 2
- 3
test/com/dmdirc/harness/TestWritableFrameContainer.java View File

@@ -28,7 +28,6 @@ import com.dmdirc.commandparser.CommandManager;
28 28
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
29 29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30 30
 import com.dmdirc.messages.MessageSinkManager;
31
-import com.dmdirc.ui.WindowManager;
32 31
 import com.dmdirc.ui.input.TabCompleter;
33 32
 
34 33
 import java.util.Collections;
@@ -39,9 +38,9 @@ public class TestWritableFrameContainer extends WritableFrameContainer {
39 38
 
40 39
     public TestWritableFrameContainer(final int lineLength,
41 40
             final AggregateConfigProvider cm, final CommandManager commandManager,
42
-            final MessageSinkManager messageSinkManager, final WindowManager windowManager) {
41
+            final MessageSinkManager messageSinkManager) {
43 42
         super("raw", "Raw", "(Raw)", cm,
44
-                new GlobalCommandParser(cm, commandManager), messageSinkManager, windowManager,
43
+                new GlobalCommandParser(cm, commandManager), messageSinkManager,
45 44
                 Collections.<String>emptySet());
46 45
 
47 46
         this.lineLength = lineLength;

Loading…
Cancel
Save