Removed event handler casts, simplified logging statements

This commit is contained in:
Kai S. K. Engelbart 2020-02-12 06:12:04 +01:00
parent 8667409413
commit 836f0c39b8
9 changed files with 31 additions and 44 deletions

View File

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

View File

@ -30,9 +30,6 @@ public class MessageStatusChangeEventProcessor implements Consumer<MessageStatus
@Override
public void accept(MessageStatusChangeEvent evt) {
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.info("Received invalid message status change " + evt);
else {
logger.info("Received " + evt.toString());
EventBus.getInstance().dispatch(evt);
}
else EventBus.getInstance().dispatch(evt);
}
}

View File

@ -23,7 +23,6 @@ public class ReceivedMessageProcessor implements Consumer<Message> {
@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

View File

@ -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<UserStatusChangeEvent> {
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);
}
}

View File

@ -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();

View File

@ -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<User> contacts = (List<User>) evt.get();
final java.util.List<User> 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("");

View File

@ -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); });
}
/**

View File

@ -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);

View File

@ -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);