Removed event handler casts, simplified logging statements
This commit is contained in:
parent
48e1d791c6
commit
84e350c02e
@ -110,7 +110,7 @@ public class Client implements Closeable {
|
|||||||
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor());
|
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor());
|
||||||
|
|
||||||
// Process user status changes
|
// Process user status changes
|
||||||
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(this));
|
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(localDb));
|
||||||
|
|
||||||
// Process message ID generation
|
// Process message ID generation
|
||||||
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
|
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
|
||||||
@ -124,7 +124,7 @@ public class Client implements Closeable {
|
|||||||
// Send event
|
// Send event
|
||||||
EventBus.getInstance().register(SendEvent.class, evt -> {
|
EventBus.getInstance().register(SendEvent.class, evt -> {
|
||||||
try {
|
try {
|
||||||
sendEvent(((SendEvent) evt).get());
|
sendEvent(evt.get());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,6 @@ public class MessageStatusChangeEventProcessor implements Consumer<MessageStatus
|
|||||||
@Override
|
@Override
|
||||||
public void accept(MessageStatusChangeEvent evt) {
|
public void accept(MessageStatusChangeEvent evt) {
|
||||||
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.info("Received invalid message status change " + evt);
|
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.info("Received invalid message status change " + evt);
|
||||||
else {
|
else EventBus.getInstance().dispatch(evt);
|
||||||
logger.info("Received " + evt.toString());
|
|
||||||
EventBus.getInstance().dispatch(evt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ public class ReceivedMessageProcessor implements Consumer<Message> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(Message message) {
|
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());
|
if (message.getStatus() != MessageStatus.SENT) logger.warning("The message has the unexpected status " + message.getStatus());
|
||||||
else {
|
else {
|
||||||
// Update status to RECEIVED
|
// Update status to RECEIVED
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package envoy.client.net;
|
package envoy.client.net;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
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.EventBus;
|
||||||
import envoy.event.UserStatusChangeEvent;
|
import envoy.event.UserStatusChangeEvent;
|
||||||
|
|
||||||
@ -17,21 +16,17 @@ import envoy.event.UserStatusChangeEvent;
|
|||||||
*/
|
*/
|
||||||
public class UserStatusChangeProcessor implements Consumer<UserStatusChangeEvent> {
|
public class UserStatusChangeProcessor implements Consumer<UserStatusChangeEvent> {
|
||||||
|
|
||||||
private Client client;
|
private final LocalDb localDb;
|
||||||
|
|
||||||
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class.getSimpleName());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param client the {@link Client} who receives an
|
* @param localDb the local database in which status updates will by applied
|
||||||
* {@link UserStatusChangeEvent}
|
|
||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public UserStatusChangeProcessor(Client client) { this.client = client; }
|
public UserStatusChangeProcessor(LocalDb localDb) { this.localDb = localDb; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(UserStatusChangeEvent evt) {
|
public void accept(UserStatusChangeEvent evt) {
|
||||||
logger.info("Received " + evt);
|
localDb.getUsers().values().stream().filter(u -> u.getId() == evt.getId()).findFirst().get().setStatus(evt.get());
|
||||||
client.getContacts().getContacts().stream().filter((user) -> user.getId() == evt.getId()).findFirst().get().setStatus(evt.get());
|
|
||||||
EventBus.getInstance().dispatch(evt);
|
EventBus.getInstance().dispatch(evt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,12 +45,14 @@ public class WriteProxy {
|
|||||||
// Initialize cache processors for messages and message status change events
|
// Initialize cache processors for messages and message status change events
|
||||||
localDb.getMessageCache().setProcessor(msg -> {
|
localDb.getMessageCache().setProcessor(msg -> {
|
||||||
try {
|
try {
|
||||||
|
logger.info("Sending cached " + msg);
|
||||||
client.sendMessage(msg);
|
client.sendMessage(msg);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.log(Level.SEVERE, "Could not send cached message", e);
|
logger.log(Level.SEVERE, "Could not send cached message", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
localDb.getStatusCache().setProcessor(evt -> {
|
localDb.getStatusCache().setProcessor(evt -> {
|
||||||
|
logger.info("Sending cached " + evt);
|
||||||
try {
|
try {
|
||||||
client.sendEvent(evt);
|
client.sendEvent(evt);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -66,8 +68,6 @@ public class WriteProxy {
|
|||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public void flushCache() {
|
public void flushCache() {
|
||||||
logger.info("Sending cached messages and message status change events...");
|
|
||||||
|
|
||||||
// Send messages
|
// Send messages
|
||||||
localDb.getMessageCache().relay();
|
localDb.getMessageCache().relay();
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import java.awt.*;
|
|||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -346,14 +345,14 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.revalidate();
|
contentPane.revalidate();
|
||||||
|
|
||||||
// Listen to theme changes
|
// 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
|
// 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
|
// Listen to received messages
|
||||||
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
|
EventBus.getInstance().register(MessageCreationEvent.class, evt -> {
|
||||||
Message message = ((MessageCreationEvent) evt).get();
|
Message message = evt.get();
|
||||||
Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
|
Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
|
||||||
chat.appendMessage(message);
|
chat.appendMessage(message);
|
||||||
|
|
||||||
@ -365,9 +364,9 @@ public class ChatWindow extends JFrame {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Listen to message status changes
|
// Listen to message status changes
|
||||||
EventBus.getInstance().register(MessageStatusChangeEvent.class, (evt) -> {
|
EventBus.getInstance().register(MessageStatusChangeEvent.class, evt -> {
|
||||||
final long id = ((MessageStatusChangeEvent) evt).getId();
|
final long id = evt.getId();
|
||||||
final MessageStatus status = (MessageStatus) evt.get();
|
final MessageStatus status = evt.get();
|
||||||
|
|
||||||
for (Chat c : localDb.getChats())
|
for (Chat c : localDb.getChats())
|
||||||
for (Message m : c.getModel())
|
for (Message m : c.getModel())
|
||||||
@ -388,9 +387,9 @@ public class ChatWindow extends JFrame {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Listen to contact search results
|
// Listen to contact search results
|
||||||
EventBus.getInstance().register(ContactSearchResult.class, (evt) -> {
|
EventBus.getInstance().register(ContactSearchResult.class, evt -> {
|
||||||
contactsModel.clear();
|
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);
|
logger.info("Received contact search result " + contacts);
|
||||||
contacts.forEach(contactsModel::add);
|
contacts.forEach(contactsModel::add);
|
||||||
revalidate();
|
revalidate();
|
||||||
@ -398,8 +397,8 @@ public class ChatWindow extends JFrame {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Add new contacts to the contact list
|
// Add new contacts to the contact list
|
||||||
EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> {
|
EventBus.getInstance().register(ContactOperationEvent.class, evt -> {
|
||||||
User contact = (User) evt.get();
|
User contact = evt.get();
|
||||||
|
|
||||||
// Clearing the search field and the searchResultList
|
// Clearing the search field and the searchResultList
|
||||||
searchField.setText("");
|
searchField.setText("");
|
||||||
|
@ -74,13 +74,10 @@ public class StatusTrayIcon {
|
|||||||
trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); });
|
trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); });
|
||||||
|
|
||||||
// Start processing message events
|
// Start processing message events
|
||||||
|
// TODO: Handle other message types
|
||||||
EventBus.getInstance()
|
EventBus.getInstance()
|
||||||
.register(MessageCreationEvent.class,
|
.register(MessageCreationEvent.class,
|
||||||
(evt) -> {
|
evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); });
|
||||||
// TODO: Handle other message types
|
|
||||||
if (displayMessages)
|
|
||||||
trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getText(), MessageType.INFO);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,7 +159,7 @@ public class SettingsScreen extends JDialog {
|
|||||||
applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
||||||
|
|
||||||
// Respond to theme changes
|
// 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);
|
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||||
setModal(true);
|
setModal(true);
|
||||||
|
@ -119,8 +119,8 @@ public class ThemeCustomizationPanel extends SettingsPanel {
|
|||||||
// Respond to theme changes
|
// Respond to theme changes
|
||||||
EventBus.getInstance()
|
EventBus.getInstance()
|
||||||
.register(ThemeChangeEvent.class,
|
.register(ThemeChangeEvent.class,
|
||||||
(evt) -> {
|
evt -> {
|
||||||
final Theme currentTheme = ((ThemeChangeEvent) evt).get();
|
final Theme currentTheme = evt.get();
|
||||||
temporaryTheme = new Theme("temporaryTheme", currentTheme);
|
temporaryTheme = new Theme("temporaryTheme", currentTheme);
|
||||||
applyTheme(currentTheme);
|
applyTheme(currentTheme);
|
||||||
});
|
});
|
||||||
@ -130,7 +130,7 @@ public class ThemeCustomizationPanel extends SettingsPanel {
|
|||||||
public ActionListener getOkButtonAction() {
|
public ActionListener getOkButtonAction() {
|
||||||
return (evt) -> {
|
return (evt) -> {
|
||||||
if (themeChanged) {
|
if (themeChanged) {
|
||||||
new NewThemeScreen(parent, (name) -> {
|
new NewThemeScreen(parent, name -> {
|
||||||
// Create new theme
|
// Create new theme
|
||||||
logger.log(Level.FINEST, name);
|
logger.log(Level.FINEST, name);
|
||||||
Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme));
|
Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme));
|
||||||
@ -140,12 +140,12 @@ public class ThemeCustomizationPanel extends SettingsPanel {
|
|||||||
|
|
||||||
// Select new theme name
|
// Select new theme name
|
||||||
themes.setSelectedIndex(themesModel.getSize() - 1);
|
themes.setSelectedIndex(themesModel.getSize() - 1);
|
||||||
}, (name) -> {
|
}, name -> {
|
||||||
// Modify theme
|
// Modify theme
|
||||||
Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme));
|
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)));
|
EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name)));
|
||||||
}else {
|
} else {
|
||||||
themes.setSelectedItem(name);
|
themes.setSelectedItem(name);
|
||||||
}
|
}
|
||||||
}).setVisible(true);
|
}).setVisible(true);
|
||||||
|
Reference in New Issue
Block a user