Reading current chat when a new message is received
This commit is contained in:
parent
58b9ac8081
commit
8f4cf1428a
@ -29,7 +29,7 @@ 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 {
|
||||||
logger.info("Received " + evt.toString());
|
logger.info("Received " + evt.toString());
|
||||||
EventBus.getInstance().dispatch(evt);
|
EventBus.getInstance().dispatch(evt);
|
||||||
|
@ -216,7 +216,20 @@ public class ChatWindow extends JFrame {
|
|||||||
// 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 = ((MessageCreationEvent) evt).get();
|
||||||
localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get().appendMessage(message);
|
Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
|
||||||
|
chat.appendMessage(message);
|
||||||
|
|
||||||
|
// Read message and update UI if in current chat
|
||||||
|
if (chat == currentChat) {
|
||||||
|
try {
|
||||||
|
currentChat.read(client);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
logger.log(Level.WARNING, "Could notify server about message status change", e);
|
||||||
|
}
|
||||||
|
messageList.synchronizeModel();
|
||||||
|
}
|
||||||
|
|
||||||
revalidate();
|
revalidate();
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
@ -66,6 +66,18 @@ public class ComponentList<E> extends JPanel {
|
|||||||
synchronizeModel();
|
synchronizeModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all child components and then adds all components representing the
|
||||||
|
* elements of the {@link ComponentListModel}.
|
||||||
|
*
|
||||||
|
* @since Envoy v0.3-alpha
|
||||||
|
*/
|
||||||
|
public void synchronizeModel() {
|
||||||
|
removeAll();
|
||||||
|
if (model != null) for (E elem : model)
|
||||||
|
add(elem);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an object to the list by rendering it with the current
|
* Adds an object to the list by rendering it with the current
|
||||||
* {@link ComponentListCellRenderer}.
|
* {@link ComponentListCellRenderer}.
|
||||||
@ -76,16 +88,4 @@ public class ComponentList<E> extends JPanel {
|
|||||||
void add(E elem) {
|
void add(E elem) {
|
||||||
add(renderer.getListCellComponent(this, elem, false));
|
add(renderer.getListCellComponent(this, elem, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes all child components and then adds all components representing the
|
|
||||||
* elements of the {@link ComponentListModel}.
|
|
||||||
*
|
|
||||||
* @since Envoy v0.3-alpha
|
|
||||||
*/
|
|
||||||
void synchronizeModel() {
|
|
||||||
removeAll();
|
|
||||||
if (model != null) for (E elem : model)
|
|
||||||
add(elem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user