Explorar el Código

Merge pull request #649 from csmith/master

Separate InputModel functions from FrameContainer.
pull/650/head
Greg Holmes hace 8 años
padre
commit
61ee61f5ef

+ 8
- 12
src/com/dmdirc/Channel.java Ver fichero

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
-import com.dmdirc.commandparser.CommandType;
26 25
 import com.dmdirc.config.ConfigBinding;
27 26
 import com.dmdirc.events.ChannelClosedEvent;
28 27
 import com.dmdirc.events.ChannelSelfActionEvent;
@@ -45,7 +44,6 @@ import com.dmdirc.parser.interfaces.ChannelClientInfo;
45 44
 import com.dmdirc.parser.interfaces.ChannelInfo;
46 45
 import com.dmdirc.parser.interfaces.Parser;
47 46
 import com.dmdirc.ui.core.components.WindowComponent;
48
-import com.dmdirc.ui.input.TabCompleterFactory;
49 47
 import com.dmdirc.ui.input.TabCompletionType;
50 48
 import com.dmdirc.ui.messages.BackBufferFactory;
51 49
 import com.dmdirc.ui.messages.Styliser;
@@ -99,13 +97,11 @@ public class Channel extends FrameContainer implements GroupChat {
99 97
      * @param connection          The connection object that this channel belongs to
100 98
      * @param newChannelInfo      The parser's channel object that corresponds to this channel
101 99
      * @param configMigrator      The config migrator which provides the config for this channel.
102
-     * @param tabCompleterFactory The factory to use to create tab completers.
103 100
      */
104 101
     public Channel(
105 102
             final Connection connection,
106 103
             final ChannelInfo newChannelInfo,
107 104
             final ConfigProviderMigrator configMigrator,
108
-            final TabCompleterFactory tabCompleterFactory,
109 105
             final BackBufferFactory backBufferFactory,
110 106
             final GroupChatUserManager groupChatUserManager) {
111 107
         super("channel-inactive",
@@ -113,10 +109,6 @@ public class Channel extends FrameContainer implements GroupChat {
113 109
                 Styliser.stipControlCodes(newChannelInfo.getName()),
114 110
                 configMigrator.getConfigProvider(),
115 111
                 backBufferFactory,
116
-                tabCompleterFactory.getTabCompleter(
117
-                        connection.getWindowModel().getInputModel().get().getTabCompleter(),
118
-                        configMigrator.getConfigProvider(), CommandType.TYPE_CHANNEL,
119
-                        CommandType.TYPE_CHAT),
120 112
                 connection.getWindowModel().getEventBus(),
121 113
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
122 114
                         WindowComponent.INPUTFIELD.getIdentifier(),
@@ -168,10 +160,14 @@ public class Channel extends FrameContainer implements GroupChat {
168 160
         }
169 161
 
170 162
         final GroupChatUser me = getUser(connection.getLocalUser().get()).get();
171
-        splitLine(line).stream().filter(part -> !part.isEmpty()).forEach(part -> {
172
-            getEventBus().publishAsync(new ChannelSelfMessageEvent(this, me, part));
173
-            channelInfo.sendMessage(part);
174
-        });
163
+        getInputModel().get()
164
+                .splitLine(line)
165
+                .stream()
166
+                .filter(part -> !part.isEmpty())
167
+                .forEach(part -> {
168
+                    getEventBus().publishAsync(new ChannelSelfMessageEvent(this, me, part));
169
+                    channelInfo.sendMessage(part);
170
+                });
175 171
     }
176 172
 
177 173
     @Override

+ 15
- 3
src/com/dmdirc/ChannelFactory.java Ver fichero

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.commandparser.CommandType;
25 26
 import com.dmdirc.commandparser.parsers.ChannelCommandParser;
26 27
 import com.dmdirc.events.ChannelOpenedEvent;
27 28
 import com.dmdirc.interfaces.CommandController;
@@ -65,9 +66,20 @@ public class ChannelFactory {
65 66
             final ChannelInfo channelInfo,
66 67
             final ConfigProviderMigrator configMigrator) {
67 68
         final Channel channel = new Channel(connection, channelInfo, configMigrator,
68
-                tabCompleterFactory, backBufferFactory, groupChatUserManager);
69
-        channel.setCommandParser(new ChannelCommandParser(connection.getWindowModel(),
70
-                commandController, eventBus, channel));
69
+                backBufferFactory, groupChatUserManager);
70
+        channel.setInputModel(new DefaultInputModel(
71
+                channel::sendLine,
72
+                new ChannelCommandParser(
73
+                        connection.getWindowModel(),
74
+                        commandController,
75
+                        eventBus,
76
+                        channel),
77
+                tabCompleterFactory.getTabCompleter(
78
+                        connection.getWindowModel().getInputModel().get().getTabCompleter(),
79
+                        configMigrator.getConfigProvider(),
80
+                        CommandType.TYPE_CHANNEL,
81
+                        CommandType.TYPE_CHAT),
82
+                channel::getMaxLineLength));
71 83
         windowManager.addWindow(connection.getWindowModel(), channel);
72 84
         connection.getWindowModel().getEventBus().publish(new ChannelOpenedEvent(channel));
73 85
         return channel;

+ 118
- 0
src/com/dmdirc/DefaultInputModel.java Ver fichero

@@ -0,0 +1,118 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
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;
24
+
25
+import com.dmdirc.commandparser.parsers.CommandParser;
26
+import com.dmdirc.interfaces.InputModel;
27
+import com.dmdirc.ui.input.TabCompleter;
28
+
29
+import java.util.ArrayList;
30
+import java.util.List;
31
+import java.util.function.Consumer;
32
+import java.util.function.Supplier;
33
+
34
+/**
35
+ * Basic implementation of {@link InputModel}.
36
+ */
37
+public class DefaultInputModel implements InputModel {
38
+
39
+    private final Consumer<String> lineConsumer;
40
+    private final CommandParser commandParser;
41
+    private final TabCompleter tabCompleter;
42
+    private final Supplier<Integer> lineLengthSupplier;
43
+
44
+    public DefaultInputModel(final Consumer<String> lineConsumer, final CommandParser commandParser,
45
+            final TabCompleter tabCompleter, final Supplier<Integer> lineLengthSupplier) {
46
+        this.lineConsumer = lineConsumer;
47
+        this.commandParser = commandParser;
48
+        this.tabCompleter = tabCompleter;
49
+        this.lineLengthSupplier = lineLengthSupplier;
50
+    }
51
+
52
+    @Override
53
+    public void sendLine(final String line) {
54
+        lineConsumer.accept(line);
55
+    }
56
+
57
+    @Override
58
+    public CommandParser getCommandParser() {
59
+        return commandParser;
60
+    }
61
+
62
+    @Override
63
+    public TabCompleter getTabCompleter() {
64
+        return tabCompleter;
65
+    }
66
+
67
+    @Override
68
+    public int getMaxLineLength() {
69
+        return lineLengthSupplier.get();
70
+    }
71
+
72
+    @Override
73
+    public List<String> splitLine(final String line) {
74
+        final List<String> result = new ArrayList<>();
75
+
76
+        if (line.indexOf('\n') > -1) {
77
+            for (String part : line.split("\n")) {
78
+                result.addAll(splitLine(part));
79
+            }
80
+        } else {
81
+            final StringBuilder remaining = new StringBuilder(line);
82
+
83
+            while (getMaxLineLength() > -1 && remaining.toString().getBytes().length
84
+                    > getMaxLineLength()) {
85
+                int number = Math.min(remaining.length(), getMaxLineLength());
86
+
87
+                while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
88
+                    number--;
89
+                }
90
+
91
+                result.add(remaining.substring(0, number));
92
+                remaining.delete(0, number);
93
+            }
94
+
95
+            result.add(remaining.toString());
96
+        }
97
+
98
+        return result;
99
+    }
100
+
101
+    @Override
102
+    public final int getNumLines(final String line) {
103
+        final String[] splitLines = line.split("(\n|\r\n|\r)", Integer.MAX_VALUE);
104
+        int lines = 0;
105
+
106
+        for (String splitLine : splitLines) {
107
+            if (getMaxLineLength() <= 0) {
108
+                lines++;
109
+            } else {
110
+                lines += (int) Math.ceil(splitLine.getBytes().length
111
+                        / (double) getMaxLineLength());
112
+            }
113
+        }
114
+
115
+        return lines;
116
+    }
117
+
118
+}

+ 15
- 138
src/com/dmdirc/FrameContainer.java Ver fichero

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
-import com.dmdirc.commandparser.parsers.CommandParser;
26 25
 import com.dmdirc.events.FrameClosingEvent;
27 26
 import com.dmdirc.events.FrameComponentAddedEvent;
28 27
 import com.dmdirc.events.FrameComponentRemovedEvent;
@@ -34,28 +33,25 @@ import com.dmdirc.interfaces.WindowModel;
34 33
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
35 34
 import com.dmdirc.interfaces.config.ConfigChangeListener;
36 35
 import com.dmdirc.parser.common.CompositionState;
37
-import com.dmdirc.ui.input.TabCompleter;
38 36
 import com.dmdirc.ui.messages.BackBuffer;
39 37
 import com.dmdirc.ui.messages.BackBufferFactory;
40 38
 import com.dmdirc.ui.messages.UnreadStatusManager;
41 39
 import com.dmdirc.util.ChildEventBusManager;
42 40
 import com.dmdirc.util.collections.ListenerList;
43 41
 
44
-import java.util.ArrayList;
45 42
 import java.util.Collection;
46 43
 import java.util.Collections;
47 44
 import java.util.HashSet;
48
-import java.util.List;
49 45
 import java.util.Optional;
50 46
 import java.util.Set;
51 47
 
52
-import static com.google.common.base.Preconditions.checkState;
48
+import javax.annotation.Nullable;
53 49
 
54 50
 /**
55 51
  * The frame container implements basic methods that should be present in all objects that handle a
56 52
  * frame.
57 53
  */
58
-public abstract class FrameContainer implements WindowModel, InputModel {
54
+public abstract class FrameContainer implements WindowModel {
59 55
 
60 56
     /** Listeners not yet using ListenerSupport. */
61 57
     protected final ListenerList listeners = new ListenerList();
@@ -77,26 +73,12 @@ public abstract class FrameContainer implements WindowModel, InputModel {
77 73
     private final DMDircMBassador eventBus;
78 74
     /** The manager handling this frame's unread status. */
79 75
     private final UnreadStatusManager unreadStatusManager;
80
-    /** Whether or not this container is writable. */
81
-    private final boolean writable;
82 76
     /** The back buffer factory. */
83 77
     private final BackBufferFactory backBufferFactory;
84 78
     /** The back buffer for this container. */
85 79
     private BackBuffer backBuffer;
86
-
87
-    /**
88
-     * The tab completer to use.
89
-     * <p>
90
-     * Only defined if this container is {@link #writable}.
91
-     */
92
-    private final Optional<TabCompleter> tabCompleter;
93
-
94
-    /**
95
-     * The command parser used for commands in this container.
96
-     * <p>
97
-     * Only defined if this container is {@link #writable}.
98
-     */
99
-    private Optional<CommandParser> commandParser = Optional.empty();
80
+    /** The input model for this container. */
81
+    private Optional<InputModel> inputModel = Optional.empty();
100 82
 
101 83
     /**
102 84
      * Instantiate new frame container.
@@ -113,38 +95,6 @@ public abstract class FrameContainer implements WindowModel, InputModel {
113 95
         this.name = name;
114 96
         this.title = title;
115 97
         this.components = new HashSet<>(components);
116
-        this.writable = false;
117
-        this.tabCompleter = Optional.empty();
118
-        this.backBufferFactory = backBufferFactory;
119
-
120
-        eventBusManager = new ChildEventBusManager(eventBus);
121
-        eventBusManager.connect();
122
-        this.eventBus = eventBusManager.getChildBus();
123
-        this.unreadStatusManager = new UnreadStatusManager(this);
124
-        this.eventBus.subscribe(unreadStatusManager);
125
-        configManager.getBinder().bind(unreadStatusManager, UnreadStatusManager.class);
126
-
127
-        setIcon(icon);
128
-    }
129
-
130
-    /**
131
-     * Instantiate new frame container that accepts user input.
132
-     */
133
-    protected FrameContainer(
134
-            final String icon,
135
-            final String name,
136
-            final String title,
137
-            final AggregateConfigProvider config,
138
-            final BackBufferFactory backBufferFactory,
139
-            final TabCompleter tabCompleter,
140
-            final DMDircMBassador eventBus,
141
-            final Collection<String> components) {
142
-        this.configManager = config;
143
-        this.name = name;
144
-        this.title = title;
145
-        this.components = new HashSet<>(components);
146
-        this.writable = true;
147
-        this.tabCompleter = Optional.of(tabCompleter);
148 98
         this.backBufferFactory = backBufferFactory;
149 99
 
150 100
         eventBusManager = new ChildEventBusManager(eventBus);
@@ -162,10 +112,6 @@ public abstract class FrameContainer implements WindowModel, InputModel {
162 112
         backBuffer.startAddingEvents();
163 113
     }
164 114
 
165
-    public void setCommandParser(final CommandParser commandParser) {
166
-        this.commandParser = Optional.ofNullable(commandParser);
167
-    }
168
-
169 115
     @Override
170 116
     public String getIcon() {
171 117
         return icon;
@@ -258,81 +204,6 @@ public abstract class FrameContainer implements WindowModel, InputModel {
258 204
         return backBuffer;
259 205
     }
260 206
 
261
-    @Override
262
-    public void sendLine(final String line) {
263
-        throw new UnsupportedOperationException("Container doesn't override sendLine");
264
-    }
265
-
266
-    @Override
267
-    public CommandParser getCommandParser() {
268
-        checkState(writable);
269
-        return commandParser.get();
270
-    }
271
-
272
-    @Override
273
-    public TabCompleter getTabCompleter() {
274
-        checkState(writable);
275
-        return tabCompleter.get();
276
-    }
277
-
278
-    @Override
279
-    public int getMaxLineLength() {
280
-        throw new UnsupportedOperationException("Container doesn't override getMaxLineLength");
281
-    }
282
-
283
-    /**
284
-     * Splits the specified line into chunks that contain a number of bytes less than or equal to
285
-     * the value returned by {@link #getMaxLineLength()}.
286
-     *
287
-     * @param line The line to be split
288
-     *
289
-     * @return An ordered list of chunks of the desired length
290
-     */
291
-    protected List<String> splitLine(final String line) {
292
-        final List<String> result = new ArrayList<>();
293
-
294
-        if (line.indexOf('\n') > -1) {
295
-            for (String part : line.split("\n")) {
296
-                result.addAll(splitLine(part));
297
-            }
298
-        } else {
299
-            final StringBuilder remaining = new StringBuilder(line);
300
-
301
-            while (getMaxLineLength() > -1 && remaining.toString().getBytes().length
302
-                    > getMaxLineLength()) {
303
-                int number = Math.min(remaining.length(), getMaxLineLength());
304
-
305
-                while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
306
-                    number--;
307
-                }
308
-
309
-                result.add(remaining.substring(0, number));
310
-                remaining.delete(0, number);
311
-            }
312
-
313
-            result.add(remaining.toString());
314
-        }
315
-
316
-        return result;
317
-    }
318
-
319
-    @Override
320
-    public final int getNumLines(final String line) {
321
-        final String[] splitLines = line.split("(\n|\r\n|\r)", Integer.MAX_VALUE);
322
-        int lines = 0;
323
-
324
-        for (String splitLine : splitLines) {
325
-            if (getMaxLineLength() <= 0) {
326
-                lines++;
327
-            } else {
328
-                lines += (int) Math.ceil(splitLine.getBytes().length
329
-                        / (double) getMaxLineLength());
330
-            }
331
-        }
332
-
333
-        return lines;
334
-    }
335
-
336 207
     /**
337 208
      * Sets the composition state for the local user for this chat.
338 209
      *
@@ -345,11 +216,17 @@ public abstract class FrameContainer implements WindowModel, InputModel {
345 216
 
346 217
     @Override
347 218
     public Optional<InputModel> getInputModel() {
348
-        if (writable) {
349
-            return Optional.of(this);
350
-        } else {
351
-            return Optional.empty();
352
-        }
219
+        return inputModel;
220
+    }
221
+
222
+    /**
223
+     * Sets an input model for this window. If a window does not have an input model, then it
224
+     * will not accept user input.
225
+     *
226
+     * @param inputModel The new input model to use (null to disallow input).
227
+     */
228
+    public void setInputModel(@Nullable final InputModel inputModel) {
229
+        this.inputModel = Optional.ofNullable(inputModel);
353 230
     }
354 231
 
355 232
     @Override

+ 9
- 7
src/com/dmdirc/GlobalWindow.java Ver fichero

@@ -54,12 +54,16 @@ public class GlobalWindow extends FrameContainer {
54 54
     public GlobalWindow(@GlobalConfig final AggregateConfigProvider config,
55 55
             final GlobalCommandParser parser, final TabCompleterFactory tabCompleterFactory,
56 56
             final DMDircMBassador eventBus, final BackBufferFactory backBufferFactory) {
57
-        super("icon", "Global", "(Global)", config, backBufferFactory,
58
-                tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL), eventBus,
57
+        super("icon", "Global", "(Global)", config, backBufferFactory, eventBus,
59 58
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
60 59
                         WindowComponent.INPUTFIELD.getIdentifier()));
61 60
         initBackBuffer();
62
-        setCommandParser(parser);
61
+        setInputModel(new DefaultInputModel(
62
+                this::sendLine,
63
+                parser,
64
+                tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL),
65
+                this::getMaxLineLength
66
+        ));
63 67
     }
64 68
 
65 69
     @Override
@@ -67,14 +71,12 @@ public class GlobalWindow extends FrameContainer {
67 71
         return Optional.empty();
68 72
     }
69 73
 
70
-    @Override
71
-    public void sendLine(final String line) {
74
+    private void sendLine(final String line) {
72 75
         getEventBus().publishAsync(
73 76
                 new CommandErrorEvent(this, "You may only enter commands in the global window."));
74 77
     }
75 78
 
76
-    @Override
77
-    public int getMaxLineLength() {
79
+    private int getMaxLineLength() {
78 80
         return -1;
79 81
     }
80 82
 

+ 9
- 12
src/com/dmdirc/Query.java Ver fichero

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
-import com.dmdirc.commandparser.CommandType;
26 25
 import com.dmdirc.events.CommandErrorEvent;
27 26
 import com.dmdirc.events.QueryActionEvent;
28 27
 import com.dmdirc.events.QueryClosedEvent;
@@ -45,7 +44,6 @@ import com.dmdirc.parser.events.QuitEvent;
45 44
 import com.dmdirc.parser.interfaces.ClientInfo;
46 45
 import com.dmdirc.parser.interfaces.Parser;
47 46
 import com.dmdirc.ui.core.components.WindowComponent;
48
-import com.dmdirc.ui.input.TabCompleterFactory;
49 47
 import com.dmdirc.ui.messages.BackBufferFactory;
50 48
 
51 49
 import java.awt.Toolkit;
@@ -69,17 +67,12 @@ public class Query extends FrameContainer implements PrivateChat {
69 67
     public Query(
70 68
             final Connection connection,
71 69
             final User user,
72
-            final TabCompleterFactory tabCompleterFactory,
73 70
             final BackBufferFactory backBufferFactory) {
74 71
         super("query",
75 72
                 user.getNickname(),
76 73
                 user.getNickname(),
77 74
                 connection.getWindowModel().getConfigManager(),
78 75
                 backBufferFactory,
79
-                tabCompleterFactory.getTabCompleter(
80
-                        connection.getWindowModel().getInputModel().get().getTabCompleter(),
81
-                        connection.getWindowModel().getConfigManager(),
82
-                        CommandType.TYPE_QUERY, CommandType.TYPE_CHAT),
83 76
                 connection.getWindowModel().getEventBus(),
84 77
                 Arrays.asList(
85 78
                         WindowComponent.TEXTAREA.getIdentifier(),
@@ -103,11 +96,15 @@ public class Query extends FrameContainer implements PrivateChat {
103 96
             return;
104 97
         }
105 98
 
106
-        splitLine(line).stream().filter(part -> !part.isEmpty()).forEach(part -> {
107
-            connection.getParser().get().sendMessage(target, part);
108
-            getEventBus().publishAsync(new QuerySelfMessageEvent(this,
109
-                    connection.getLocalUser().get(), part));
110
-        });
99
+        getInputModel().get()
100
+                .splitLine(line)
101
+                .stream()
102
+                .filter(part -> !part.isEmpty())
103
+                .forEach(part -> {
104
+                    connection.getParser().get().sendMessage(target, part);
105
+                    getEventBus().publishAsync(new QuerySelfMessageEvent(this,
106
+                            connection.getLocalUser().get(), part));
107
+                });
111 108
     }
112 109
 
113 110
     @Override

+ 15
- 3
src/com/dmdirc/QueryFactory.java Ver fichero

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.commandparser.CommandType;
25 26
 import com.dmdirc.commandparser.parsers.QueryCommandParser;
26 27
 import com.dmdirc.events.QueryOpenedEvent;
27 28
 import com.dmdirc.interfaces.CommandController;
@@ -58,9 +59,20 @@ public class QueryFactory {
58 59
     }
59 60
 
60 61
     public Query getQuery(final Connection connection, final User user) {
61
-        final Query query = new Query(connection, user, tabCompleterFactory, backBufferFactory);
62
-        query.setCommandParser(new QueryCommandParser(connection.getWindowModel(),
63
-                commandController, connection.getWindowModel().getEventBus(), query));
62
+        final Query query = new Query(connection, user, backBufferFactory);
63
+        query.setInputModel(new DefaultInputModel(
64
+                query::sendLine,
65
+                new QueryCommandParser(
66
+                        connection.getWindowModel(),
67
+                        commandController,
68
+                        connection.getWindowModel().getEventBus(),
69
+                        query),
70
+                tabCompleterFactory.getTabCompleter(
71
+                        connection.getWindowModel().getInputModel().get().getTabCompleter(),
72
+                        connection.getWindowModel().getConfigManager(),
73
+                        CommandType.TYPE_QUERY,
74
+                        CommandType.TYPE_CHAT),
75
+                query::getMaxLineLength));
64 76
         windowManager.addWindow(connection.getWindowModel(), query);
65 77
         connection.getWindowModel().getEventBus().publish(new QueryOpenedEvent(query));
66 78
         return query;

+ 0
- 6
src/com/dmdirc/Server.java Ver fichero

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
-import com.dmdirc.commandparser.CommandType;
26 25
 import com.dmdirc.config.profiles.Profile;
27 26
 import com.dmdirc.events.ServerConnectErrorEvent;
28 27
 import com.dmdirc.events.ServerConnectedEvent;
@@ -51,7 +50,6 @@ import com.dmdirc.parser.interfaces.SecureParser;
51 50
 import com.dmdirc.parser.interfaces.StringConverter;
52 51
 import com.dmdirc.tls.CertificateManager;
53 52
 import com.dmdirc.ui.core.components.WindowComponent;
54
-import com.dmdirc.ui.input.TabCompleterFactory;
55 53
 import com.dmdirc.ui.input.TabCompletionType;
56 54
 import com.dmdirc.ui.messages.BackBufferFactory;
57 55
 import com.dmdirc.ui.messages.ColourManager;
@@ -174,7 +172,6 @@ public class Server extends FrameContainer implements Connection {
174 172
     public Server(
175 173
             final ConfigProviderMigrator configMigrator,
176 174
             final ParserFactory parserFactory,
177
-            final TabCompleterFactory tabCompleterFactory,
178 175
             final IdentityFactory identityFactory,
179 176
             final QueryFactory queryFactory,
180 177
             final DMDircMBassador eventBus,
@@ -191,8 +188,6 @@ public class Server extends FrameContainer implements Connection {
191 188
                 getHost(uri),
192 189
                 configMigrator.getConfigProvider(),
193 190
                 backBufferFactory,
194
-                tabCompleterFactory.getTabCompleter(configMigrator.getConfigProvider(),
195
-                        CommandType.TYPE_SERVER, CommandType.TYPE_GLOBAL),
196 191
                 eventBus,
197 192
                 Arrays.asList(
198 193
                         WindowComponent.TEXTAREA.getIdentifier(),
@@ -621,7 +616,6 @@ public class Server extends FrameContainer implements Connection {
621 616
         }
622 617
     }
623 618
 
624
-    @Override
625 619
     public int getMaxLineLength() {
626 620
         return withParserReadLock(Parser::getMaxLength, -1);
627 621
     }

+ 14
- 3
src/com/dmdirc/ServerFactoryImpl.java Ver fichero

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.commandparser.CommandType;
25 26
 import com.dmdirc.commandparser.parsers.ServerCommandParser;
26 27
 import com.dmdirc.config.profiles.Profile;
27 28
 import com.dmdirc.interfaces.CommandController;
@@ -88,11 +89,21 @@ public class ServerFactoryImpl {
88 89
             final URI uri,
89 90
             final Profile profile) {
90 91
         final Server server = new Server(configMigrator, parserFactory,
91
-                tabCompleterFactory, identityFactory, queryFactory.get(), eventBus,
92
+                identityFactory, queryFactory.get(), eventBus,
92 93
                 messageEncoderFactory, userSettings, groupChatManagerFactory, executorService,
93 94
                 uri, profile, backBufferFactory, userManager);
94
-        server.setCommandParser(new ServerCommandParser(server.getConfigManager(),
95
-                commandController.get(), eventBus, server));
95
+        server.setInputModel(new DefaultInputModel(
96
+                server::sendLine,
97
+                new ServerCommandParser(
98
+                        server.getConfigManager(),
99
+                        commandController.get(),
100
+                        eventBus,
101
+                        server),
102
+                tabCompleterFactory.getTabCompleter(
103
+                        configMigrator.getConfigProvider(),
104
+                        CommandType.TYPE_SERVER,
105
+                        CommandType.TYPE_GLOBAL),
106
+                server::getMaxLineLength));
96 107
         return server;
97 108
     }
98 109
 }

+ 12
- 0
src/com/dmdirc/interfaces/InputModel.java Ver fichero

@@ -25,6 +25,8 @@ package com.dmdirc.interfaces;
25 25
 import com.dmdirc.commandparser.parsers.CommandParser;
26 26
 import com.dmdirc.ui.input.TabCompleter;
27 27
 
28
+import java.util.List;
29
+
28 30
 /**
29 31
  * Models the input functionality of a {@link WindowModel}.
30 32
  */
@@ -59,6 +61,16 @@ public interface InputModel {
59 61
      */
60 62
     int getMaxLineLength();
61 63
 
64
+    /**
65
+     * Splits the specified line into chunks that contain a number of bytes less than or equal to
66
+     * the value returned by {@link #getMaxLineLength()}.
67
+     *
68
+     * @param line The line to be split
69
+     *
70
+     * @return An ordered list of chunks of the desired length
71
+     */
72
+    List<String> splitLine(String line);
73
+
62 74
     /**
63 75
      * Returns the number of lines that the specified string would be sent as, if it were passed
64 76
      * to {@link #sendLine(String)}.

+ 12
- 10
test/com/dmdirc/WritableFrameContainerTest.java Ver fichero

@@ -61,22 +61,23 @@ public class WritableFrameContainerTest {
61 61
         commands = new CommandManager(connectionManager, globalWindowProvider);
62 62
     }
63 63
 
64
+    // TODO: Move this test to DefaultInputModel.
64 65
     @Test
65 66
     public void testGetNumLines() {
66 67
         final FrameContainer container10 = new TestWritableFrameContainer(10, acp, commands,
67 68
                 eventBus, backBufferFactory);
68 69
 
69
-        final int res0a = container10.getNumLines("");
70
-        final int res0b = container10.getNumLines("\r");
71
-        final int res0c = container10.getNumLines("\r\n");
70
+        final int res0a = container10.getInputModel().get().getNumLines("");
71
+        final int res0b = container10.getInputModel().get().getNumLines("\r");
72
+        final int res0c = container10.getInputModel().get().getNumLines("\r\n");
72 73
 
73
-        final int res1a = container10.getNumLines("0123456789");
74
-        final int res1b = container10.getNumLines("\r\n123456789");
75
-        final int res1c = container10.getNumLines("qaaa");
74
+        final int res1a = container10.getInputModel().get().getNumLines("0123456789");
75
+        final int res1b = container10.getInputModel().get().getNumLines("\r\n123456789");
76
+        final int res1c = container10.getInputModel().get().getNumLines("qaaa");
76 77
 
77
-        final int res2a = container10.getNumLines("01234567890");
78
-        final int res2b = container10.getNumLines("012345\r\n\r\n34567890");
79
-        final int res2c = container10.getNumLines("01234567890\r\n\r\n");
78
+        final int res2a = container10.getInputModel().get().getNumLines("01234567890");
79
+        final int res2b = container10.getInputModel().get().getNumLines("012345\r\n\r\n34567890");
80
+        final int res2c = container10.getInputModel().get().getNumLines("01234567890\r\n\r\n");
80 81
 
81 82
         assertEquals(0, res0a);
82 83
         assertEquals(0, res0b);
@@ -91,6 +92,7 @@ public class WritableFrameContainerTest {
91 92
         assertEquals(2, res2c);
92 93
     }
93 94
 
95
+    // TODO: Move this test to DefaultInputModel.
94 96
     @Test
95 97
     public void testSplitLine() {
96 98
         final FrameContainer container10 = new TestWritableFrameContainer(10, acp, commands,
@@ -108,7 +110,7 @@ public class WritableFrameContainerTest {
108 110
         };
109 111
 
110 112
         for (String[][] test : tests) {
111
-            final String[] res = container10.splitLine(test[0][0]).toArray(new String[0]);
113
+            final String[] res = container10.getInputModel().get().splitLine(test[0][0]).toArray(new String[0]);
112 114
             assertTrue('\'' + test[0][0] + "' → "
113 115
                     + Arrays.toString(res) + " (expected: " + Arrays.toString(test[1]) + ')',
114 116
                     Arrays.equals(res, test[1]));

+ 7
- 15
test/com/dmdirc/harness/TestWritableFrameContainer.java Ver fichero

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.harness;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.DefaultInputModel;
26 27
 import com.dmdirc.FrameContainer;
27 28
 import com.dmdirc.commandparser.CommandManager;
28 29
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
@@ -36,29 +37,20 @@ import java.util.Optional;
36 37
 
37 38
 public class TestWritableFrameContainer extends FrameContainer {
38 39
 
39
-    private final int lineLength;
40
-
41 40
     public TestWritableFrameContainer(final int lineLength,
42 41
             final AggregateConfigProvider cm, final CommandManager commandManager,
43 42
             final DMDircMBassador eventBus,
44 43
             final BackBufferFactory backBufferFactory) {
45 44
         super("raw", "Raw", "(Raw)", cm, backBufferFactory,
46
-                new TabCompleter(cm),
47 45
                 eventBus,
48 46
                 Collections.<String>emptySet());
49 47
 
50
-        setCommandParser(new GlobalCommandParser(cm, commandManager, eventBus));
51
-        this.lineLength = lineLength;
52
-    }
53
-
54
-    @Override
55
-    public void sendLine(final String line) {
56
-        // Do nothing
57
-    }
58
-
59
-    @Override
60
-    public int getMaxLineLength() {
61
-        return lineLength;
48
+        setInputModel(
49
+                new DefaultInputModel(
50
+                        line -> {},
51
+                        new GlobalCommandParser(cm, commandManager, eventBus),
52
+                        new TabCompleter(cm),
53
+                        () -> lineLength));
62 54
     }
63 55
 
64 56
     @Override

Loading…
Cancelar
Guardar