Skip to content

Commit

Permalink
Add a base class for events with a WindowModel source.
Browse files Browse the repository at this point in the history
For #662, it makes sense to have a standard way to
get the source from events we may wish to filter.

This introduces a SourcedEvent interface, and adapts classes
to use it.
  • Loading branch information
csmith committed Feb 24, 2016
1 parent 0e677f8 commit 8cd93c8
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/com/dmdirc/Server.java
Expand Up @@ -705,7 +705,7 @@ public ServerStatus getStatus() {

@Handler
private void handleClose(final FrameClosingEvent event) {
if (event.getContainer() == windowModel) {
if (event.getSource() == windowModel) {
synchronized (myStateLock) {
eventHandler.unregisterCallbacks();
windowModel.getConfigManager().removeListener(configListener);
Expand Down
4 changes: 2 additions & 2 deletions src/com/dmdirc/ServerManager.java
Expand Up @@ -222,8 +222,8 @@ public void joinDevChat() {

@Handler
void handleWindowClosing(final FrameClosingEvent event) {
if (event.getContainer() instanceof Server) {
unregisterServer((Server) event.getContainer());
if (event.getSource() instanceof Server) {
unregisterServer((Server) event.getSource());
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/com/dmdirc/events/DisplayableEvent.java
Expand Up @@ -22,15 +22,13 @@

package com.dmdirc.events;

import com.dmdirc.interfaces.WindowModel;

import java.time.LocalDateTime;
import java.util.Optional;

/**
* Describes an event which is rendered in the client to the user.
*/
public interface DisplayableEvent {
public interface DisplayableEvent extends SourcedEvent {

/**
* Sets a property relating to how this event should be displayed.
Expand Down Expand Up @@ -78,9 +76,4 @@ default boolean hasDisplayProperty(final DisplayProperty<?> property) {
*/
LocalDateTime getTimestamp();

/**
* Gets the source of the displayable event.
*/
WindowModel getSource();

}
14 changes: 8 additions & 6 deletions src/com/dmdirc/events/FrameEvent.java
Expand Up @@ -27,15 +27,17 @@
/**
* Base class for window related events in the client.
*/
public abstract class FrameEvent extends DMDircEvent {
public abstract class FrameEvent extends DMDircEvent implements SourcedEvent {

private final WindowModel container;
private final WindowModel source;

public FrameEvent(final WindowModel container) {
this.container = container;
public FrameEvent(final WindowModel source) {
this.source = source;
}

public WindowModel getContainer() {
return container;
@Override
public WindowModel getSource() {
return source;
}

}
37 changes: 37 additions & 0 deletions src/com/dmdirc/events/SourcedEvent.java
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.dmdirc.events;

import com.dmdirc.interfaces.WindowModel;

/**
* An event that is attached to a {@link WindowModel} source.
*/
public interface SourcedEvent {

/**
* Gets the source of the event.
*/
WindowModel getSource();

}
3 changes: 2 additions & 1 deletion src/com/dmdirc/events/UnreadStatusChangedEvent.java
Expand Up @@ -31,7 +31,7 @@
/**
* Event raised when the unread status of a window has changed.
*/
public class UnreadStatusChangedEvent extends DMDircEvent {
public class UnreadStatusChangedEvent extends DMDircEvent implements SourcedEvent {

private final WindowModel source;
private final UnreadStatusManager manager;
Expand All @@ -46,6 +46,7 @@ public UnreadStatusChangedEvent(final WindowModel source, final UnreadStatusMana
this.unreadCount = unreadCount;
}

@Override
public WindowModel getSource() {
return source;
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/dmdirc/ui/WindowManager.java
Expand Up @@ -411,7 +411,7 @@ private void fireDeleteWindow(final WindowModel parent, final WindowModel child)

@Handler
public void frameClosing(final FrameClosingEvent event) {
removeWindow(event.getContainer());
removeWindow(event.getSource());
}

}
2 changes: 1 addition & 1 deletion src/com/dmdirc/ui/input/InputHandler.java
Expand Up @@ -675,7 +675,7 @@ public void clearInputField() {

@Handler
void parentClosing(final FrameClosingEvent event) {
if (event.getContainer().equals(parentWindow)) {
if (event.getSource().equals(parentWindow)) {
executorService.shutdown();
eventBus.unsubscribe(this);
}
Expand Down

0 comments on commit 8cd93c8

Please sign in to comment.