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.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
import envoy.client.event.EventBus;
import envoy.client.event.MessageCreationEvent;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.schema.Message; import envoy.schema.Message;
import envoy.schema.Message.Metadata.MessageState; import envoy.schema.Message.Metadata.MessageState;
@ -161,6 +163,9 @@ public class LocalDB {
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
// these are the unread Messages from the server // these are the unread Messages from the server
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); 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 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0

View File

@ -19,7 +19,7 @@ public class EventBus {
private EventBus() {} private EventBus() {}
public EventBus getInstance() { public static EventBus getInstance() {
if (eventBus == null) eventBus = new EventBus(); if (eventBus == null) eventBus = new EventBus();
return 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.Toolkit;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType; 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.exception.EnvoyException;
import envoy.schema.Message;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -20,7 +25,7 @@ import envoy.schema.Message;
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public class StatusTrayIcon { public class StatusTrayIcon implements EventHandler {
private TrayIcon trayIcon; private TrayIcon trayIcon;
@ -39,6 +44,7 @@ public class StatusTrayIcon {
popup.add(exitMenuItem); popup.add(exitMenuItem);
trayIcon.setPopupMenu(popup); trayIcon.setPopupMenu(popup);
EventBus.getInstance().register(this);
} }
/** /**
@ -55,12 +61,17 @@ public class StatusTrayIcon {
} }
/** /**
* Notifies the user of a message by displaying a popup. * Notifies the user of a message by displaying a pop-up.
*
* @param message the {@link Message} object to display in the popup
*/ */
public void displayMessageNotification(Message message) { @Override
// TODO: Add event listener public void handle(Event<?> event) {
trayIcon.displayMessage("New message received", message.getContent().get(0).getText(), MessageType.INFO); 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;
} }
} }