From 487c6a657360988a4acbb75c5b3e9b7e4ef72cdb Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 2 Apr 2020 09:23:47 +0200 Subject: [PATCH] Added BASIC group capability (#128) * Changed serialVersionUID everywhere to 0L * Added support for GroupResizeEvents and NameChangeEvents Co-authored-by: CyB3RC0nN0R --- src/main/java/envoy/client/data/Cache.java | 2 +- src/main/java/envoy/client/data/Chat.java | 21 +++++++--- src/main/java/envoy/client/data/LocalDB.java | 41 +++++++++++++++++-- src/main/java/envoy/client/net/Client.java | 6 +++ src/main/java/envoy/client/ui/Color.java | 2 +- .../client/ui/container/LoginDialog.java | 2 +- .../client/ui/primary/PrimaryScrollPane.java | 2 +- .../client/ui/primary/PrimaryTextArea.java | 2 +- .../ui/settings/GeneralSettingsPanel.java | 2 +- .../ui/settings/ThemeCustomizationPanel.java | 2 +- 10 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java index 5a92463..8060d2e 100644 --- a/src/main/java/envoy/client/data/Cache.java +++ b/src/main/java/envoy/client/data/Cache.java @@ -25,7 +25,7 @@ public class Cache implements Consumer, Serializable { private transient Consumer processor; private static final Logger logger = EnvoyLog.getLogger(Cache.class); - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; /** * Adds an element to the cache. diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 839daa7..a9148e2 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -6,9 +6,8 @@ import java.util.ArrayList; import java.util.List; import envoy.client.net.WriteProxy; -import envoy.data.Message; +import envoy.data.*; import envoy.data.Message.MessageStatus; -import envoy.data.User; import envoy.event.MessageStatusChangeEvent; /** @@ -26,7 +25,7 @@ import envoy.event.MessageStatusChangeEvent; */ public final class Chat implements Serializable { - private final User recipient; + private final Contact recipient; private final List messages = new ArrayList<>(); private static final long serialVersionUID = 1L; @@ -38,7 +37,7 @@ public final class Chat implements Serializable { * @param recipient the user who receives the messages * @since Envoy Client v0.1-alpha */ - public Chat(User recipient) { this.recipient = recipient; } + public Chat(Contact recipient) { this.recipient = recipient; } @Override public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); } @@ -82,5 +81,17 @@ public final class Chat implements Serializable { * @return the recipient of a message * @since Envoy Client v0.1-alpha */ - public User getRecipient() { return recipient; } + public Contact getRecipient() { return recipient; } + + /** + * @return whether this {@link Chat} points at a {@link User} + * @since Envoy Client v0.1-beta + */ + public boolean isUserChat() { return recipient instanceof User; } + + /** + * @return whether this {@link Chat} points at a {@link Group} + * @since Envoy Client v0.1-beta + */ + public boolean isGroupChat() { return recipient instanceof Group; } } diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 7de7383..256e701 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -2,10 +2,8 @@ package envoy.client.data; import java.util.*; -import envoy.data.IDGenerator; -import envoy.data.Message; -import envoy.data.User; -import envoy.event.MessageStatusChangeEvent; +import envoy.data.*; +import envoy.event.*; /** * Stores information about the current {@link User} and their {@link Chat}s. @@ -155,4 +153,39 @@ public abstract class LocalDB { public Message getMessage(long id) { return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null); } + + /** + * Performs a contact name change if the corresponding contact is present. + * + * @param event the {@link NameChangeEvent} to process + * @since Envoy Client v0.1-beta + */ + public void replaceContactName(NameChangeEvent event) { + chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == event.getId()).findAny().ifPresent(c -> c.setName(event.get())); + } + + /** + * Performs a group resize operation if the corresponding group is present. + * + * @param event the {@link GroupResizeEvent} to process + * @since Envoy Client v0.1-beta + */ + public void updateGroup(GroupResizeEvent event) { + chats.stream() + .map(Chat::getRecipient) + .filter(Group.class::isInstance) + .filter(g -> g.getID() == event.getGroupID() && g.getID() != user.getID()) + .map(Group.class::cast) + .findAny() + .ifPresent(group -> { + switch (event.getOperation()) { + case ADD: + group.getMemberIDs().add(event.get()); + break; + case REMOVE: + group.getMemberIDs().remove(event.get()); + break; + } + }); + } } diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index a07c0bc..a61d6de 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -145,12 +145,18 @@ public class Client implements Closeable { // Process message ID generation receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator); + // Process name changes + receiver.registerProcessor(NameChangeEvent.class, evt -> { localDB.replaceContactName(evt); EventBus.getInstance().dispatch(evt); }); + // Process contact searches receiver.registerProcessor(ContactSearchResult.class, EventBus.getInstance()::dispatch); receiver.registerProcessor(Contacts.class, contacts -> EventBus.getInstance().dispatch(new ContactOperationEvent(contacts.getContacts().get(0), Operation.ADD))); + // Process group size changes + receiver.registerProcessor(GroupResizeEvent.class, evt -> { localDB.updateGroup(evt); EventBus.getInstance().dispatch(evt); }); + // Send event EventBus.getInstance().register(SendEvent.class, evt -> { try { diff --git a/src/main/java/envoy/client/ui/Color.java b/src/main/java/envoy/client/ui/Color.java index bb89943..da58bbb 100644 --- a/src/main/java/envoy/client/ui/Color.java +++ b/src/main/java/envoy/client/ui/Color.java @@ -81,7 +81,7 @@ public class Color extends java.awt.Color { */ public static final Color blue = new Color(0, 0, 255); - private static final long serialVersionUID = -9166233199998257344L; + private static final long serialVersionUID = 0L; public Color(java.awt.Color other) { this(other.getRGB()); } diff --git a/src/main/java/envoy/client/ui/container/LoginDialog.java b/src/main/java/envoy/client/ui/container/LoginDialog.java index d2cad9f..0d9f034 100644 --- a/src/main/java/envoy/client/ui/container/LoginDialog.java +++ b/src/main/java/envoy/client/ui/container/LoginDialog.java @@ -65,7 +65,7 @@ public class LoginDialog extends JDialog { private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class); - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; /** * Displays a dialog enabling the user to enter their user name and password. diff --git a/src/main/java/envoy/client/ui/primary/PrimaryScrollPane.java b/src/main/java/envoy/client/ui/primary/PrimaryScrollPane.java index a27b075..e02502b 100644 --- a/src/main/java/envoy/client/ui/primary/PrimaryScrollPane.java +++ b/src/main/java/envoy/client/ui/primary/PrimaryScrollPane.java @@ -14,7 +14,7 @@ import envoy.client.ui.Theme; */ public class PrimaryScrollPane extends JScrollPane { - private static final long serialVersionUID = -4786837444056228439L; + private static final long serialVersionUID = 0L; private int verticalScrollBarMaximum = getVerticalScrollBar().getMaximum(); private boolean chatOpened = false; diff --git a/src/main/java/envoy/client/ui/primary/PrimaryTextArea.java b/src/main/java/envoy/client/ui/primary/PrimaryTextArea.java index 40bf167..37d05eb 100644 --- a/src/main/java/envoy/client/ui/primary/PrimaryTextArea.java +++ b/src/main/java/envoy/client/ui/primary/PrimaryTextArea.java @@ -16,7 +16,7 @@ import javax.swing.border.EmptyBorder; */ public class PrimaryTextArea extends JTextArea { - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; private int arcSize; /** diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index 5b88ce4..372478b 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -30,7 +30,7 @@ public class GeneralSettingsPanel extends SettingsPanel { private static final String[] items = { "onCloseMode", "enterToSend" }; private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class); - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; /** * This is the constructor for the General class. Here the user can set general diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 44eb026..b6eb832 100755 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -39,7 +39,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { private final Insets insets = new Insets(5, 5, 5, 5); private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class); - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; /** * Initializes a {@link ThemeCustomizationPanel} that enables the user to change