From af7408142c55560945685c8ce0cbcf55fec89b2c Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 4 Dec 2019 22:26:24 +0100 Subject: [PATCH] Added message events, triggering message creation event + Abstract MessageEvent class with MessageCreationEvent and MessageModificationEvent subclasses + Made StatusTrayIcon an event handler - Fixed EventBus#getInstance not being static --- src/main/java/envoy/client/LocalDB.java | 15 ++++++---- .../java/envoy/client/event/EventBus.java | 4 +-- .../client/event/MessageCreationEvent.java | 18 ++++++++++++ .../java/envoy/client/event/MessageEvent.java | 20 +++++++++++++ .../event/MessageModificationEvent.java | 18 ++++++++++++ .../java/envoy/client/ui/StatusTrayIcon.java | 29 +++++++++++++------ 6 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 src/main/java/envoy/client/event/MessageCreationEvent.java create mode 100644 src/main/java/envoy/client/event/MessageEvent.java create mode 100644 src/main/java/envoy/client/event/MessageModificationEvent.java diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 3cc96a2..bf6f01a 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -13,6 +13,8 @@ import java.util.List; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; +import envoy.client.event.EventBus; +import envoy.client.event.MessageCreationEvent; import envoy.exception.EnvoyException; import envoy.schema.Message; import envoy.schema.Message.Metadata.MessageState; @@ -112,7 +114,7 @@ public class LocalDB { /** * Creates a {@link Message} object serializable to XML. - * + * * @param textContent The content (text) of the message * @return prepared {@link Message} object */ @@ -161,6 +163,9 @@ public class LocalDB { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { // these are the unread Messages from the server unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); + + // Create and dispatch message creation event + EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i))); } if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0 @@ -225,7 +230,7 @@ public class LocalDB { /** * Adds the unread messages returned from the server in the latest sync to the * right chats in the LocalDB. - * + * * @param localDB * @since Envoy v0.1-alpha */ @@ -243,7 +248,7 @@ public class LocalDB { * {@code READ}. *
* Adds these messages to the {@code readMessages} {@link Sync} object. - * + * * @param currentChat * @since Envoy v0.1-alpha */ @@ -259,7 +264,7 @@ public class LocalDB { /** * Adds all messages with state {@code WAITING} from the {@link LocalDB} to the * {@link Sync} object. - * + * * @since Envoy v0.1-alpha */ private void addWaitingMessagesToSync() { @@ -273,7 +278,7 @@ public class LocalDB { /** * Clears the {@code unreadMessagesSync} {@link Sync} object. - * + * * @since Envoy v0.1-alpha */ public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } diff --git a/src/main/java/envoy/client/event/EventBus.java b/src/main/java/envoy/client/event/EventBus.java index c97844b..f1c40fc 100644 --- a/src/main/java/envoy/client/event/EventBus.java +++ b/src/main/java/envoy/client/event/EventBus.java @@ -8,7 +8,7 @@ import java.util.List; * Project: envoy-clientChess
* File: EventBus.javaEvent.java
* Created: 04.12.2019
- * + * * @author Kai S. K. Engelbart */ public class EventBus { @@ -19,7 +19,7 @@ public class EventBus { private EventBus() {} - public EventBus getInstance() { + public static EventBus getInstance() { if (eventBus == null) eventBus = new EventBus(); return eventBus; } diff --git a/src/main/java/envoy/client/event/MessageCreationEvent.java b/src/main/java/envoy/client/event/MessageCreationEvent.java new file mode 100644 index 0000000..28f1b58 --- /dev/null +++ b/src/main/java/envoy/client/event/MessageCreationEvent.java @@ -0,0 +1,18 @@ +package envoy.client.event; + +import envoy.schema.Message; + +/** + * Project: envoy-client
+ * File: MessageCreationEvent.java
+ * Created: 4 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MessageCreationEvent extends MessageEvent { + + /** + * @param message the {@link Message} that has been created + */ + public MessageCreationEvent(Message message) { super(message); } +} diff --git a/src/main/java/envoy/client/event/MessageEvent.java b/src/main/java/envoy/client/event/MessageEvent.java new file mode 100644 index 0000000..014a7bb --- /dev/null +++ b/src/main/java/envoy/client/event/MessageEvent.java @@ -0,0 +1,20 @@ +package envoy.client.event; + +import envoy.schema.Message; + +/** + * Project: envoy-client
+ * File: MessageCreationEvent.java
+ * Created: 4 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MessageEvent implements Event { + + protected final Message message; + + public MessageEvent(Message message) { this.message = message; } + + @Override + public Message get() { return message; } +} diff --git a/src/main/java/envoy/client/event/MessageModificationEvent.java b/src/main/java/envoy/client/event/MessageModificationEvent.java new file mode 100644 index 0000000..0b83ef0 --- /dev/null +++ b/src/main/java/envoy/client/event/MessageModificationEvent.java @@ -0,0 +1,18 @@ +package envoy.client.event; + +import envoy.schema.Message; + +/** + * Project: envoy-client
+ * File: MessageModificationEvent.java
+ * Created: 4 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MessageModificationEvent extends MessageEvent { + + /** + * @param message the {@link Message} that has been modified + */ + public MessageModificationEvent(Message message) { super(message); } +} diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 869e678..ab668e1 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -8,9 +8,14 @@ import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.TrayIcon.MessageType; +import java.util.HashSet; +import java.util.Set; +import envoy.client.event.Event; +import envoy.client.event.EventBus; +import envoy.client.event.EventHandler; +import envoy.client.event.MessageCreationEvent; import envoy.exception.EnvoyException; -import envoy.schema.Message; /** * Project: envoy-client
@@ -20,7 +25,7 @@ import envoy.schema.Message; * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ -public class StatusTrayIcon { +public class StatusTrayIcon implements EventHandler { private TrayIcon trayIcon; @@ -39,11 +44,12 @@ public class StatusTrayIcon { popup.add(exitMenuItem); trayIcon.setPopupMenu(popup); + EventBus.getInstance().register(this); } /** * Makes this {@link StatusTrayIcon} appear in the system tray. - * + * * @throws EnvoyException */ public void show() throws EnvoyException { @@ -55,12 +61,17 @@ public class StatusTrayIcon { } /** - * Notifies the user of a message by displaying a popup. - * - * @param message the {@link Message} object to display in the popup + * Notifies the user of a message by displaying a pop-up. */ - public void displayMessageNotification(Message message) { - // TODO: Add event listener - trayIcon.displayMessage("New message received", message.getContent().get(0).getText(), MessageType.INFO); + @Override + public void handle(Event event) { + trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO); + } + + @Override + public Set>> supports() { + Set>> supportedEvents = new HashSet<>(); + supportedEvents.add(MessageCreationEvent.class); + return supportedEvents; } }