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:
parent
b5badae773
commit
af7408142c
@ -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;
|
||||
@ -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
|
||||
|
@ -19,7 +19,7 @@ public class EventBus {
|
||||
|
||||
private EventBus() {}
|
||||
|
||||
public EventBus getInstance() {
|
||||
public static EventBus getInstance() {
|
||||
if (eventBus == null) eventBus = new EventBus();
|
||||
return eventBus;
|
||||
}
|
||||
|
18
src/main/java/envoy/client/event/MessageCreationEvent.java
Normal file
18
src/main/java/envoy/client/event/MessageCreationEvent.java
Normal 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); }
|
||||
}
|
20
src/main/java/envoy/client/event/MessageEvent.java
Normal file
20
src/main/java/envoy/client/event/MessageEvent.java
Normal 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; }
|
||||
}
|
@ -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); }
|
||||
}
|
@ -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,6 +44,7 @@ public class StatusTrayIcon {
|
||||
popup.add(exitMenuItem);
|
||||
|
||||
trayIcon.setPopupMenu(popup);
|
||||
EventBus.getInstance().register(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user