From 836f0c39b8a7014f14853f9026f5b78b27407ed7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 06:12:04 +0100 Subject: [PATCH] Removed event handler casts, simplified logging statements --- src/main/java/envoy/client/net/Client.java | 4 ++-- .../MessageStatusChangeEventProcessor.java | 5 +--- .../client/net/ReceivedMessageProcessor.java | 1 - .../client/net/UserStatusChangeProcessor.java | 15 ++++-------- .../java/envoy/client/net/WriteProxy.java | 4 ++-- src/main/java/envoy/client/ui/ChatWindow.java | 23 +++++++++---------- .../java/envoy/client/ui/StatusTrayIcon.java | 7 ++---- .../client/ui/settings/SettingsScreen.java | 2 +- .../ui/settings/ThemeCustomizationPanel.java | 14 +++++------ 9 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index b9985c1..8a62925 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -110,7 +110,7 @@ public class Client implements Closeable { receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor()); // Process user status changes - receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(this)); + receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(localDb)); // Process message ID generation receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); @@ -124,7 +124,7 @@ public class Client implements Closeable { // Send event EventBus.getInstance().register(SendEvent.class, evt -> { try { - sendEvent(((SendEvent) evt).get()); + sendEvent(evt.get()); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java index 1cf457f..b81561c 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java @@ -30,9 +30,6 @@ public class MessageStatusChangeEventProcessor implements Consumer { @Override public void accept(Message message) { - logger.info("Received message object " + message); if (message.getStatus() != MessageStatus.SENT) logger.warning("The message has the unexpected status " + message.getStatus()); else { // Update status to RECEIVED diff --git a/src/main/java/envoy/client/net/UserStatusChangeProcessor.java b/src/main/java/envoy/client/net/UserStatusChangeProcessor.java index 744e6e8..69a36d4 100644 --- a/src/main/java/envoy/client/net/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/client/net/UserStatusChangeProcessor.java @@ -1,9 +1,8 @@ package envoy.client.net; import java.util.function.Consumer; -import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; +import envoy.client.data.LocalDb; import envoy.event.EventBus; import envoy.event.UserStatusChangeEvent; @@ -17,21 +16,17 @@ import envoy.event.UserStatusChangeEvent; */ public class UserStatusChangeProcessor implements Consumer { - private Client client; - - private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class.getSimpleName()); + private final LocalDb localDb; /** - * @param client the {@link Client} who receives an - * {@link UserStatusChangeEvent} + * @param localDb the local database in which status updates will by applied * @since Envoy v0.3-alpha */ - public UserStatusChangeProcessor(Client client) { this.client = client; } + public UserStatusChangeProcessor(LocalDb localDb) { this.localDb = localDb; } @Override public void accept(UserStatusChangeEvent evt) { - logger.info("Received " + evt); - client.getContacts().getContacts().stream().filter((user) -> user.getId() == evt.getId()).findFirst().get().setStatus(evt.get()); + localDb.getUsers().values().stream().filter(u -> u.getId() == evt.getId()).findFirst().get().setStatus(evt.get()); EventBus.getInstance().dispatch(evt); } } diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 98206ae..93e67d8 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -45,12 +45,14 @@ public class WriteProxy { // Initialize cache processors for messages and message status change events localDb.getMessageCache().setProcessor(msg -> { try { + logger.info("Sending cached " + msg); client.sendMessage(msg); } catch (IOException e) { logger.log(Level.SEVERE, "Could not send cached message", e); } }); localDb.getStatusCache().setProcessor(evt -> { + logger.info("Sending cached " + evt); try { client.sendEvent(evt); } catch (IOException e) { @@ -66,8 +68,6 @@ public class WriteProxy { * @since Envoy v0.3-alpha */ public void flushCache() { - logger.info("Sending cached messages and message status change events..."); - // Send messages localDb.getMessageCache().relay(); diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 17c0583..5aa18c5 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -4,7 +4,6 @@ import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -346,14 +345,14 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); // Listen to theme changes - EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get())); + EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get())); // Listen to user status changes - EventBus.getInstance().register(UserStatusChangeEvent.class, (evt) -> { userList.revalidate(); userList.repaint(); }); + EventBus.getInstance().register(UserStatusChangeEvent.class, evt -> { userList.revalidate(); userList.repaint(); }); // Listen to received messages - EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { - Message message = ((MessageCreationEvent) evt).get(); + EventBus.getInstance().register(MessageCreationEvent.class, evt -> { + Message message = evt.get(); Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); chat.appendMessage(message); @@ -365,9 +364,9 @@ public class ChatWindow extends JFrame { }); // Listen to message status changes - EventBus.getInstance().register(MessageStatusChangeEvent.class, (evt) -> { - final long id = ((MessageStatusChangeEvent) evt).getId(); - final MessageStatus status = (MessageStatus) evt.get(); + EventBus.getInstance().register(MessageStatusChangeEvent.class, evt -> { + final long id = evt.getId(); + final MessageStatus status = evt.get(); for (Chat c : localDb.getChats()) for (Message m : c.getModel()) @@ -388,9 +387,9 @@ public class ChatWindow extends JFrame { }); // Listen to contact search results - EventBus.getInstance().register(ContactSearchResult.class, (evt) -> { + EventBus.getInstance().register(ContactSearchResult.class, evt -> { contactsModel.clear(); - final java.util.List contacts = (List) evt.get(); + final java.util.List contacts = evt.get(); logger.info("Received contact search result " + contacts); contacts.forEach(contactsModel::add); revalidate(); @@ -398,8 +397,8 @@ public class ChatWindow extends JFrame { }); // Add new contacts to the contact list - EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { - User contact = (User) evt.get(); + EventBus.getInstance().register(ContactOperationEvent.class, evt -> { + User contact = evt.get(); // Clearing the search field and the searchResultList searchField.setText(""); diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 0cfda6a..4513aea 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -74,13 +74,10 @@ public class StatusTrayIcon { trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); // Start processing message events + // TODO: Handle other message types EventBus.getInstance() .register(MessageCreationEvent.class, - (evt) -> { - // TODO: Handle other message types - if (displayMessages) - trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getText(), MessageType.INFO); - }); + evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); }); } /** diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index 533200f..af68e8e 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -159,7 +159,7 @@ public class SettingsScreen extends JDialog { applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); // Respond to theme changes - EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); + EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get())); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModal(true); diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 9d4c6be..8df3491 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -47,7 +47,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { * Initializes a {@link ThemeCustomizationPanel} that enables the user to change * the current {@link Theme} and create new themes as part of the * {@link SettingsScreen}. - * + * * @param parent the {@link SettingsScreen} as a part of which this * {@link SettingsPanel} is displayed * @since Envoy v0.2-alpha @@ -119,8 +119,8 @@ public class ThemeCustomizationPanel extends SettingsPanel { // Respond to theme changes EventBus.getInstance() .register(ThemeChangeEvent.class, - (evt) -> { - final Theme currentTheme = ((ThemeChangeEvent) evt).get(); + evt -> { + final Theme currentTheme = evt.get(); temporaryTheme = new Theme("temporaryTheme", currentTheme); applyTheme(currentTheme); }); @@ -130,7 +130,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { public ActionListener getOkButtonAction() { return (evt) -> { if (themeChanged) { - new NewThemeScreen(parent, (name) -> { + new NewThemeScreen(parent, name -> { // Create new theme logger.log(Level.FINEST, name); Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); @@ -140,12 +140,12 @@ public class ThemeCustomizationPanel extends SettingsPanel { // Select new theme name themes.setSelectedIndex(themesModel.getSize() - 1); - }, (name) -> { + }, name -> { // Modify theme Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme)); - if(themes.getSelectedItem().equals(name)) { + if (themes.getSelectedItem().equals(name)) { EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name))); - }else { + } else { themes.setSelectedItem(name); } }).setVisible(true);