Browse Source

Change highlights into proper displayable events.

Instead of wrapping the source event and modifying it, they are
now proper events that are displayed themselves (and the original
message is suppressed).

This allows highlights to be formatted differently.

Issue #669
pull/684/head
Chris Smith 8 years ago
parent
commit
46bcaecfc1

+ 4
- 0
res/com/dmdirc/ui/messages/format.yml View File

@@ -126,6 +126,8 @@ ChannelSelfActionEvent:
126 126
   colour: 6
127 127
 ChannelMessageEvent:
128 128
   format: "<{{client.modePrefixedNickname}}> {{message}}"
129
+ChannelHighlightEvent:
130
+  format: "<{{client.modePrefixedNickname}}> {{message}}"
129 131
 ChannelSelfMessageEvent:
130 132
   format: "<{{client.modePrefixedNickname}}> {{message}}"
131 133
 ChannelModeNoticeEvent:
@@ -197,6 +199,8 @@ QuerySelfActionEvent:
197 199
   colour: 6
198 200
 QueryMessageEvent:
199 201
   format: "<{{user.nickname}}> {{message}}"
202
+QueryHighlightEvent:
203
+  format: "<{{user.nickname}}> {{message}}"
200 204
 QuerySelfMessageEvent:
201 205
   format: "<{{user.nickname}}> {{message}}"
202 206
 QueryQuitEvent:

+ 1
- 7
src/com/dmdirc/Server.java View File

@@ -51,7 +51,6 @@ import com.dmdirc.parser.interfaces.SecureParser;
51 51
 import com.dmdirc.parser.interfaces.StringConverter;
52 52
 import com.dmdirc.tls.CertificateManager;
53 53
 import com.dmdirc.ui.input.TabCompletionType;
54
-import com.dmdirc.ui.messages.ColourManager;
55 54
 import com.dmdirc.ui.messages.Formatter;
56 55
 import com.dmdirc.ui.messages.HighlightManager;
57 56
 
@@ -209,11 +208,7 @@ public class Server implements Connection {
209 208
         windowModel.getConfigManager().addChangeListener("formatter", "serverName", configListener);
210 209
         windowModel.getConfigManager().addChangeListener("formatter", "serverTitle", configListener);
211 210
 
212
-        this.highlightManager = new HighlightManager(
213
-                windowModel,
214
-                windowModel.getConfigManager(),
215
-                new ColourManager(windowModel.getConfigManager()));
216
-        highlightManager.init();
211
+        highlightManager = new HighlightManager(windowModel);
217 212
         windowModel.getEventBus().subscribe(highlightManager);
218 213
         windowModel.getEventBus().subscribe(this);
219 214
     }
@@ -690,7 +685,6 @@ public class Server implements Connection {
690 685
             synchronized (myStateLock) {
691 686
                 eventHandler.unregisterCallbacks();
692 687
                 windowModel.getConfigManager().removeListener(configListener);
693
-                highlightManager.stop();
694 688
                 windowModel.getEventBus().unsubscribe(highlightManager);
695 689
                 executorService.shutdown();
696 690
 

+ 12
- 8
src/com/dmdirc/events/ChannelHighlightEvent.java View File

@@ -22,20 +22,24 @@
22 22
 
23 23
 package com.dmdirc.events;
24 24
 
25
+import com.dmdirc.interfaces.GroupChat;
26
+import com.dmdirc.interfaces.GroupChatUser;
27
+
28
+import java.time.LocalDateTime;
29
+
25 30
 /**
26 31
  * Event raised when a highlight is detected in a channel.
27 32
  */
28
-public class ChannelHighlightEvent extends ChannelEvent {
29
-
30
-    private final ChannelEvent cause;
33
+public class ChannelHighlightEvent extends ChannelMessageEvent {
31 34
 
32
-    public ChannelHighlightEvent(final ChannelEvent cause) {
33
-        super(cause.getTimestamp(), cause.getChannel());
34
-        this.cause = cause;
35
+    public ChannelHighlightEvent(final LocalDateTime timestamp, final GroupChat channel,
36
+            final GroupChatUser client, final String message) {
37
+        super(timestamp, channel, client, message);
35 38
     }
36 39
 
37
-    public ChannelEvent getCause() {
38
-        return cause;
40
+    public ChannelHighlightEvent(final GroupChat channel, final GroupChatUser client,
41
+            final String message) {
42
+        super(channel, client, message);
39 43
     }
40 44
 
41 45
 }

+ 11
- 8
src/com/dmdirc/events/QueryHighlightEvent.java View File

@@ -22,20 +22,23 @@
22 22
 
23 23
 package com.dmdirc.events;
24 24
 
25
+import com.dmdirc.Query;
26
+import com.dmdirc.interfaces.User;
27
+
28
+import java.time.LocalDateTime;
29
+
25 30
 /**
26 31
  * Event raised when a highlight is detected in a query.
27 32
  */
28
-public class QueryHighlightEvent extends QueryEvent {
29
-
30
-    private final QueryEvent cause;
33
+public class QueryHighlightEvent extends QueryMessageEvent {
31 34
 
32
-    public QueryHighlightEvent(final QueryEvent cause) {
33
-        super(cause.getTimestamp(), cause.getQuery());
34
-        this.cause = cause;
35
+    public QueryHighlightEvent(final LocalDateTime timestamp, final Query query, final User user,
36
+            final String message) {
37
+        super(timestamp, query, user, message);
35 38
     }
36 39
 
37
-    public QueryEvent getCause() {
38
-        return cause;
40
+    public QueryHighlightEvent(final Query query, final User user, final String message) {
41
+        super(query, user, message);
39 42
     }
40 43
 
41 44
 }

+ 17
- 60
src/com/dmdirc/ui/messages/HighlightManager.java View File

@@ -22,21 +22,15 @@
22 22
 
23 23
 package com.dmdirc.ui.messages;
24 24
 
25
-import com.dmdirc.config.ConfigBinding;
26
-import com.dmdirc.events.BaseChannelTextEvent;
27
-import com.dmdirc.events.BaseQueryTextEvent;
28 25
 import com.dmdirc.events.ChannelHighlightEvent;
26
+import com.dmdirc.events.ChannelMessageEvent;
29 27
 import com.dmdirc.events.DisplayProperty;
30
-import com.dmdirc.events.DisplayableEvent;
31 28
 import com.dmdirc.events.QueryHighlightEvent;
29
+import com.dmdirc.events.QueryMessageEvent;
32 30
 import com.dmdirc.events.ServerConnectedEvent;
33 31
 import com.dmdirc.events.ServerNickChangeEvent;
34 32
 import com.dmdirc.interfaces.User;
35 33
 import com.dmdirc.interfaces.WindowModel;
36
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
37
-import com.dmdirc.util.colours.Colour;
38
-
39
-import com.google.common.base.Strings;
40 34
 
41 35
 import java.util.ArrayList;
42 36
 import java.util.Collection;
@@ -55,46 +49,34 @@ public class HighlightManager {
55 49
 
56 50
     private final Collection<Pattern> patterns = new ArrayList<>();
57 51
     private final WindowModel serverWindow;
58
-    private final AggregateConfigProvider configProvider;
59
-    private final ColourManager colourManager;
60 52
 
61 53
     private Optional<Pattern> nicknamePattern = Optional.empty();
62 54
 
63
-    private Optional<Colour> backgroundColour = Optional.empty();
64
-    private Optional<Colour> foregroundColour = Optional.empty();
65
-
66
-    public HighlightManager(
67
-            final WindowModel serverWindow,
68
-            final AggregateConfigProvider configProvider,
69
-            final ColourManager colourManager) {
55
+    public HighlightManager(final WindowModel serverWindow) {
70 56
         this.serverWindow = serverWindow;
71
-        this.configProvider = configProvider;
72
-        this.colourManager = colourManager;
73
-    }
74
-
75
-    public void init() {
76
-        configProvider.getBinder().bind(this, HighlightManager.class);
77 57
     }
78 58
 
79
-    public void stop() {
80
-        configProvider.getBinder().unbind(this);
81
-    }
82
-
83
-    @Handler
84
-    void handleChannelMessage(final BaseChannelTextEvent event) {
59
+    @Handler(rejectSubtypes = true)
60
+    void handleChannelMessage(final ChannelMessageEvent event) {
85 61
         if (event.getChannel().getConnection().get().getWindowModel().equals(serverWindow)
86 62
                 && patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
87
-            setColours(event);
88
-            event.getChannel().getEventBus().publishAsync(new ChannelHighlightEvent(event));
63
+            event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
64
+            event.getChannel().getEventBus().publishAsync(
65
+                    new ChannelHighlightEvent(
66
+                            event.getTimestamp(), event.getChannel(), event.getClient(),
67
+                            event.getMessage()));
89 68
         }
90 69
     }
91 70
 
92
-    @Handler
93
-    void handleQueryMessage(final BaseQueryTextEvent event) {
71
+    @Handler(rejectSubtypes = true)
72
+    void handleQueryMessage(final QueryMessageEvent event) {
94 73
         if (event.getUser().getConnection().getWindowModel().equals(serverWindow)
95 74
                 && patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
96
-            setColours(event);
97
-            event.getQuery().getEventBus().publishAsync(new QueryHighlightEvent(event));
75
+            event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
76
+            event.getQuery().getEventBus().publishAsync(
77
+                    new QueryHighlightEvent(
78
+                            event.getTimestamp(), event.getQuery(), event.getUser(),
79
+                            event.getMessage()));
98 80
         }
99 81
     }
100 82
 
@@ -121,31 +103,6 @@ public class HighlightManager {
121 103
         }
122 104
     }
123 105
 
124
-    @ConfigBinding(domain = "ui", key = "highlightLineForegroundColour", required = false)
125
-    void handleForegroundColour(final String value) {
126
-        if (Strings.isNullOrEmpty(value)) {
127
-            foregroundColour = Optional.empty();
128
-        } else {
129
-            foregroundColour = Optional.ofNullable(colourManager.getColourFromString(value, null));
130
-        }
131
-    }
132
-
133
-    @ConfigBinding(domain = "ui", key = "highlightLineBackgroundColour", required = false)
134
-    void handleBackgroundColour(final String value) {
135
-        if (Strings.isNullOrEmpty(value)) {
136
-            backgroundColour = Optional.empty();
137
-        } else {
138
-            backgroundColour = Optional.ofNullable(colourManager.getColourFromString(value, null));
139
-        }
140
-    }
141
-
142
-    private void setColours(final DisplayableEvent event) {
143
-        backgroundColour.ifPresent(
144
-                c -> event.setDisplayProperty(DisplayProperty.BACKGROUND_COLOUR, c));
145
-        foregroundColour.ifPresent(
146
-                c -> event.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, c));
147
-    }
148
-
149 106
     private void setNickname(final String newNick) {
150 107
         nicknamePattern.ifPresent(patterns::remove);
151 108
         nicknamePattern = Optional.of(compile(newNick));

+ 2
- 2
src/com/dmdirc/ui/messages/UnreadStatusManager.java View File

@@ -82,14 +82,14 @@ public class UnreadStatusManager {
82 82
 
83 83
     @Handler
84 84
     public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
85
-        if (event.getCause().getChannel().equals(container)) {
85
+        if (event.getChannel().equals(container)) {
86 86
             updateStatus(highlightColour);
87 87
         }
88 88
     }
89 89
 
90 90
     @Handler
91 91
     public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
92
-        if (event.getCause().getQuery().equals(container)) {
92
+        if (event.getQuery().equals(container)) {
93 93
             updateStatus(highlightColour);
94 94
         }
95 95
     }

+ 14
- 9
test/com/dmdirc/ui/messages/HighlightManagerTest.java View File

@@ -33,7 +33,6 @@ import com.dmdirc.interfaces.Connection;
33 33
 import com.dmdirc.interfaces.GroupChatUser;
34 34
 import com.dmdirc.interfaces.User;
35 35
 import com.dmdirc.interfaces.WindowModel;
36
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
37 36
 
38 37
 import com.google.common.collect.Lists;
39 38
 
@@ -47,7 +46,7 @@ import org.mockito.ArgumentCaptor;
47 46
 import org.mockito.Mock;
48 47
 import org.mockito.runners.MockitoJUnitRunner;
49 48
 
50
-import static org.junit.Assert.assertSame;
49
+import static org.junit.Assert.assertEquals;
51 50
 import static org.mockito.Matchers.any;
52 51
 import static org.mockito.Mockito.never;
53 52
 import static org.mockito.Mockito.only;
@@ -66,8 +65,6 @@ public class HighlightManagerTest {
66 65
     @Mock private GroupChatUser channelUser;
67 66
     @Mock private DMDircMBassador eventBus;
68 67
 
69
-    @Mock private ColourManager colourManager;
70
-    @Mock private AggregateConfigProvider configProvider;
71 68
     private HighlightManager manager;
72 69
 
73 70
     @Before
@@ -79,7 +76,7 @@ public class HighlightManagerTest {
79 76
         when(channel.getEventBus()).thenReturn(eventBus);
80 77
         when(channel.getConnection()).thenReturn(Optional.of(connection));
81 78
 
82
-        manager = new HighlightManager(windowModel, configProvider, colourManager);
79
+        manager = new HighlightManager(windowModel);
83 80
     }
84 81
 
85 82
     @Test
@@ -97,7 +94,9 @@ public class HighlightManagerTest {
97 94
                 ArgumentCaptor.forClass(ChannelHighlightEvent.class);
98 95
 
99 96
         verify(eventBus).publishAsync(captor.capture());
100
-        assertSame(event, captor.getValue().getCause());
97
+        assertEquals(channel, captor.getValue().getChannel());
98
+        assertEquals(channelUser, captor.getValue().getClient());
99
+        assertEquals("Hi, nickName!", captor.getValue().getMessage());
101 100
     }
102 101
 
103 102
     @Test
@@ -116,7 +115,9 @@ public class HighlightManagerTest {
116 115
                 ArgumentCaptor.forClass(ChannelHighlightEvent.class);
117 116
 
118 117
         verify(eventBus).publishAsync(captor.capture());
119
-        assertSame(event, captor.getValue().getCause());
118
+        assertEquals(channel, captor.getValue().getChannel());
119
+        assertEquals(channelUser, captor.getValue().getClient());
120
+        assertEquals("Hi, newName!", captor.getValue().getMessage());
120 121
     }
121 122
 
122 123
     @Test
@@ -149,7 +150,9 @@ public class HighlightManagerTest {
149 150
                 ArgumentCaptor.forClass(ChannelHighlightEvent.class);
150 151
 
151 152
         verify(eventBus).publishAsync(captor.capture());
152
-        assertSame(event, captor.getValue().getCause());
153
+        assertEquals(channel, captor.getValue().getChannel());
154
+        assertEquals(channelUser, captor.getValue().getClient());
155
+        assertEquals("DMDirc is great.", captor.getValue().getMessage());
153 156
     }
154 157
 
155 158
     @Test
@@ -168,7 +171,9 @@ public class HighlightManagerTest {
168 171
                 ArgumentCaptor.forClass(ChannelHighlightEvent.class);
169 172
 
170 173
         verify(eventBus, only()).publishAsync(captor.capture());
171
-        assertSame(event, captor.getValue().getCause());
174
+        assertEquals(channel, captor.getValue().getChannel());
175
+        assertEquals(channelUser, captor.getValue().getClient());
176
+        assertEquals("DMDirc is great.", captor.getValue().getMessage());
172 177
     }
173 178
 
174 179
 }

Loading…
Cancel
Save