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
This commit is contained in:
Kai S. K. Engelbart 2019-12-04 22:26:24 +01:00
parent b5badae773
commit af7408142c
6 changed files with 88 additions and 16 deletions

View File

@ -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}.
* <br>
* 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(); }

View File

@ -8,7 +8,7 @@ import java.util.List;
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>EventBus.javaEvent.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
*
* @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;
}

View File

@ -0,0 +1,18 @@
package envoy.client.event;
import envoy.schema.Message;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>MessageCreationEvent.java</strong><br>
* Created: <strong>4 Dec 2019</strong><br>
*
* @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); }
}

View File

@ -0,0 +1,20 @@
package envoy.client.event;
import envoy.schema.Message;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>MessageCreationEvent.java</strong><br>
* Created: <strong>4 Dec 2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public class MessageEvent implements Event<Message> {
protected final Message message;
public MessageEvent(Message message) { this.message = message; }
@Override
public Message get() { return message; }
}

View File

@ -0,0 +1,18 @@
package envoy.client.event;
import envoy.schema.Message;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>MessageModificationEvent.java</strong><br>
* Created: <strong>4 Dec 2019</strong><br>
*
* @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); }
}

View File

@ -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: <strong>envoy-client</strong><br>
@ -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<Class<? extends Event<?>>> supports() {
Set<Class<? extends Event<?>>> supportedEvents = new HashSet<>();
supportedEvents.add(MessageCreationEvent.class);
return supportedEvents;
}
}