Skip to content

Commit

Permalink
Merge pull request #666 from csmith/master
Browse files Browse the repository at this point in the history
Add source checks for eventbus handlers.
  • Loading branch information
greboid committed Feb 25, 2016
2 parents 3b2c211 + 648309f commit 6bb1b2b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
12 changes: 7 additions & 5 deletions src/com/dmdirc/GroupChatManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ public void handleConnected() {

@Handler
void handleChannelClosing(final ChannelClosedEvent event) {
final GroupChat channel = event.getChannel();
connection.getWindowModel().getInputModel().get().getTabCompleter()
.removeEntry(TabCompletionType.CHANNEL, channel.getName());
channels.remove(channel.getName());
channel.getEventBus().unsubscribe(this);
if (event.getChannel().getConnection().equals(connection)) {
final GroupChat channel = event.getChannel();
connection.getWindowModel().getInputModel().get().getTabCompleter()
.removeEntry(TabCompletionType.CHANNEL, channel.getName());
channels.remove(channel.getName());
channel.getEventBus().unsubscribe(this);
}
}

}
6 changes: 4 additions & 2 deletions src/com/dmdirc/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ public Server(
windowModel.getConfigManager().addChangeListener("formatter", "serverName", configListener);
windowModel.getConfigManager().addChangeListener("formatter", "serverTitle", configListener);

this.highlightManager = new HighlightManager(windowModel.getConfigManager(),
this.highlightManager = new HighlightManager(
windowModel,
windowModel.getConfigManager(),
new ColourManager(windowModel.getConfigManager()));
highlightManager.init();
windowModel.getEventBus().subscribe(highlightManager);
Expand Down Expand Up @@ -705,7 +707,7 @@ public ServerStatus getStatus() {

@Handler
private void handleClose(final FrameClosingEvent event) {
if (event.getSource() == windowModel) {
if (event.getSource().equals(windowModel)) {
synchronized (myStateLock) {
eventHandler.unregisterCallbacks();
windowModel.getConfigManager().removeListener(configListener);
Expand Down
32 changes: 21 additions & 11 deletions src/com/dmdirc/ui/messages/HighlightManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.dmdirc.events.ServerConnectedEvent;
import com.dmdirc.events.ServerNickChangeEvent;
import com.dmdirc.interfaces.User;
import com.dmdirc.interfaces.WindowModel;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.util.colours.Colour;

Expand All @@ -53,6 +54,7 @@ public class HighlightManager {
"(\\p{Space}|\\p{Punct}|$).*";

private final Collection<Pattern> patterns = new ArrayList<>();
private final WindowModel serverWindow;
private final AggregateConfigProvider configProvider;
private final ColourManager colourManager;

Expand All @@ -62,8 +64,10 @@ public class HighlightManager {
private Optional<Colour> foregroundColour = Optional.empty();

public HighlightManager(
final WindowModel serverWindow,
final AggregateConfigProvider configProvider,
final ColourManager colourManager) {
this.serverWindow = serverWindow;
this.configProvider = configProvider;
this.colourManager = colourManager;
}
Expand All @@ -78,37 +82,43 @@ public void stop() {

@Handler
void handleChannelMessage(final BaseChannelTextEvent event) {
if (patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
if (event.getChannel().getConnection().get().getWindowModel().equals(serverWindow)
&& patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
setColours(event);
event.getChannel().getEventBus().publishAsync(new ChannelHighlightEvent(event));
}
}

@Handler
void handleQueryMessage(final BaseQueryTextEvent event) {
if (patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
if (event.getUser().getConnection().getWindowModel().equals(serverWindow)
&& patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
setColours(event);
event.getQuery().getEventBus().publishAsync(new QueryHighlightEvent(event));
}
}

@Handler
void handleNickChange(final ServerNickChangeEvent event) {
setNickname(event.getNewNick());
if (event.getConnection().getWindowModel().equals(serverWindow)) {
setNickname(event.getNewNick());
}
}

@Handler
void handleConnected(final ServerConnectedEvent event) {
patterns.clear();
if (event.getConnection().getWindowModel().equals(serverWindow)) {
patterns.clear();

event.getConnection().getProfile().getHighlights()
.stream()
.map(this::compile)
.forEach(patterns::add);
event.getConnection().getProfile().getHighlights()
.stream()
.map(this::compile)
.forEach(patterns::add);

event.getConnection().getLocalUser()
.map(User::getNickname)
.ifPresent(this::setNickname);
event.getConnection().getLocalUser()
.map(User::getNickname)
.ifPresent(this::setNickname);
}
}

@ConfigBinding(domain = "ui", key = "highlightLineForegroundColour", required = false)
Expand Down
5 changes: 4 additions & 1 deletion test/com/dmdirc/ui/messages/HighlightManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.dmdirc.interfaces.Connection;
import com.dmdirc.interfaces.GroupChatUser;
import com.dmdirc.interfaces.User;
import com.dmdirc.interfaces.WindowModel;
import com.dmdirc.interfaces.config.AggregateConfigProvider;

import com.google.common.collect.Lists;
Expand All @@ -57,6 +58,7 @@
public class HighlightManagerTest {

@Mock private Connection connection;
@Mock private WindowModel windowModel;
@Mock private User user;
@Mock private Profile profile;

Expand All @@ -72,10 +74,11 @@ public class HighlightManagerTest {
public void setup() {
when(connection.getLocalUser()).thenReturn(Optional.of(user));
when(connection.getProfile()).thenReturn(profile);
when(connection.getWindowModel()).thenReturn(windowModel);

when(channel.getEventBus()).thenReturn(eventBus);

manager = new HighlightManager(configProvider, colourManager);
manager = new HighlightManager(windowModel, configProvider, colourManager);
}

@Test
Expand Down

0 comments on commit 6bb1b2b

Please sign in to comment.