Implement chat room history in chat window.

cusax-fix
Yana Stamcheva 19 years ago
parent d7fd29562b
commit 0a5074d34e

@ -418,15 +418,18 @@ public boolean isWriteAreaEmpty()
* @param escapedMessageID The incoming message needed to be ignored if
* contained in history.
*/
public void processHistory(Collection historyList,
String escapedMessageID)
public void processHistory( Collection historyList,
String escapedMessageID)
{
Iterator iterator = historyList.iterator();
String historyString = "";
while (iterator.hasNext()) {
while (iterator.hasNext())
{
Object o = iterator.next();
if(o instanceof MessageDeliveredEvent) {
if(o instanceof MessageDeliveredEvent)
{
MessageDeliveredEvent evt
= (MessageDeliveredEvent)o;
@ -441,7 +444,8 @@ public void processHistory(Collection historyList,
evt.getSourceMessage().getContent(),
evt.getSourceMessage().getContentType());
}
else if(o instanceof MessageReceivedEvent) {
else if(o instanceof MessageReceivedEvent)
{
MessageReceivedEvent evt = (MessageReceivedEvent)o;
if(!evt.getSourceMessage().getMessageUID()
@ -454,6 +458,38 @@ else if(o instanceof MessageReceivedEvent) {
evt.getSourceMessage().getContentType());
}
}
else if(o instanceof ChatRoomMessageDeliveredEvent)
{
ChatRoomMessageDeliveredEvent evt
= (ChatRoomMessageDeliveredEvent)o;
ProtocolProviderService protocolProvider = evt
.getSourceChatRoom().getParentProvider();
historyString += processHistoryMessage(
getChatWindow().getMainFrame()
.getAccount(protocolProvider),
evt.getTimestamp(),
Constants.HISTORY_OUTGOING_MESSAGE,
evt.getMessage().getContent(),
evt.getMessage().getContentType());
}
else if(o instanceof ChatRoomMessageReceivedEvent)
{
ChatRoomMessageReceivedEvent evt
= (ChatRoomMessageReceivedEvent) o;
if(!evt.getMessage().getMessageUID()
.equals(escapedMessageID))
{
historyString += processHistoryMessage(
evt.getSourceChatRoomMember().getName(),
evt.getTimestamp(),
Constants.HISTORY_INCOMING_MESSAGE,
evt.getMessage().getContent(),
evt.getMessage().getContentType());
}
}
}
conversationPanel.insertMessageAfterStart(historyString);

@ -381,6 +381,20 @@ public ConferenceChatPanel getMultiChat(ChatRoomWrapper chatRoomWrapper)
* @return the chat panel corresponding to the given chat room
*/
public ConferenceChatPanel getMultiChat(ChatRoom chatRoom)
{
return getMultiChat(chatRoom, null);
}
/**
* Returns the chat panel corresponding to the given chat room.
*
* @param chatRoom the chat room, for which the chat panel is about
* @param escapedMessageID the message ID of the message that should be
* excluded from the history when the last one is loaded in the chat
* @return the chat panel corresponding to the given chat room
*/
public ConferenceChatPanel getMultiChat(ChatRoom chatRoom,
String escapedMessageID)
{
synchronized (syncChat)
{
@ -409,7 +423,7 @@ public ConferenceChatPanel getMultiChat(ChatRoom chatRoom)
if(chatRoomWrapper == null)
chatRoomWrapper = new ChatRoomWrapper(chatRoom);
return createChat(chatRoomWrapper);
return createChat(chatRoomWrapper, escapedMessageID);
}
}
@ -547,12 +561,27 @@ private MetaContactChatPanel createChat(MetaContact contact,
/**
* Creates a <tt>ChatPanel</tt> for the given <tt>ChatRoom</tt> and saves it
* in the list ot created <tt>ChatPanel</tt>s.
* in the list of created <tt>ChatPanel</tt>s.
*
* @param chatRoom the <tt>ChatRoom</tt>, for which the chat will be created
* @return The <code>ChatPanel</code> newly created.
*/
private ConferenceChatPanel createChat(ChatRoomWrapper chatRoomWrapper)
private ConferenceChatPanel createChat( ChatRoomWrapper chatRoomWrapper)
{
return createChat(chatRoomWrapper, null);
}
/**
* Creates a <tt>ChatPanel</tt> for the given <tt>ChatRoom</tt> and saves it
* in the list of created <tt>ChatPanel</tt>s.
*
* @param chatRoom the <tt>ChatRoom</tt>, for which the chat will be created
* @param escapedMessageID the message ID of the message that should be
* excluded from the history when the last one is loaded in the chat.
* @return The <code>ChatPanel</code> newly created.
*/
private ConferenceChatPanel createChat( ChatRoomWrapper chatRoomWrapper,
String escapedMessageID)
{
ChatWindow chatWindow;
@ -585,6 +614,11 @@ private ConferenceChatPanel createChat(ChatRoomWrapper chatRoomWrapper)
this.chats.put(chatRoomWrapper, chatPanel);
}
if(escapedMessageID != null)
chatPanel.loadHistory(escapedMessageID);
else
chatPanel.loadHistory();
return chatPanel;
}

@ -9,7 +9,6 @@
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
@ -135,13 +134,13 @@ class ProcessHistory implements Runnable
public void run()
{
processHistory(historyList, null);
processHistory(historyList, null);
}
}
SwingUtilities.invokeLater(new ProcessHistory(historyList));
}
}
}.start();
}.start();
}
/**

@ -12,9 +12,11 @@
import javax.swing.*;
import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.i18n.*;
import net.java.sip.communicator.impl.gui.main.chat.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.msghistory.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
@ -34,6 +36,10 @@ public class ConferenceChatPanel
private Logger logger = Logger.getLogger(ConferenceChatPanel.class);
private Date firstHistoryMsgTimestamp;
private Date lastHistoryMsgTimestamp;
private ChatRoomWrapper chatRoomWrapper;
/**
@ -107,7 +113,48 @@ public ImageIcon getChatStatusIcon()
*/
public void loadHistory()
{
if (chatRoomWrapper.getChatRoom() == null)
return;
final MessageHistoryService msgHistory
= GuiActivator.getMsgHistoryService();
if (msgHistory == null)
return;
new Thread() {
public void run()
{
// Load the history period, which initializes the
// firstMessageTimestamp and the lastMessageTimeStamp variables.
// Used to disable/enable history flash buttons in the chat
// window tool bar.
loadHistoryPeriod(chatRoomWrapper.getChatRoom());
// Load the last N=CHAT_HISTORY_SIZE messages from history.
Collection historyList
= msgHistory.findLast( chatRoomWrapper.getChatRoom(),
Constants.CHAT_HISTORY_SIZE);
if(historyList.size() > 0) {
class ProcessHistory implements Runnable
{
Collection historyList;
ProcessHistory(Collection historyList)
{
this.historyList = historyList;
}
public void run()
{
processHistory(historyList, null);
}
}
SwingUtilities.invokeLater(new ProcessHistory(historyList));
}
}
}.start();
}
/**
@ -118,7 +165,20 @@ public void loadHistory()
*/
public void loadHistory(String escapedMessageID)
{
// TODO Auto-generated method stub
if (chatRoomWrapper.getChatRoom() == null)
return;
MessageHistoryService msgHistory
= GuiActivator.getMsgHistoryService();
if (msgHistory == null)
return;
Collection historyList
= msgHistory.findLast( chatRoomWrapper.getChatRoom(),
Constants.CHAT_HISTORY_SIZE);
processHistory(historyList, escapedMessageID);
}
/**
@ -128,7 +188,38 @@ public void loadHistory(String escapedMessageID)
*/
public void loadPreviousPageFromHistory()
{
// TODO Auto-generated method stub
if (chatRoomWrapper.getChatRoom() == null)
return;
new Thread() {
public void run()
{
MessageHistoryService msgHistory
= GuiActivator.getMsgHistoryService();
ChatConversationPanel conversationPanel
= getChatConversationPanel();
Date firstMsgDate
= conversationPanel.getPageFirstMsgTimestamp();
Collection c = null;
if(firstMsgDate != null)
{
c = msgHistory.findLastMessagesBefore(
chatRoomWrapper.getChatRoom(),
firstMsgDate,
MESSAGES_PER_PAGE);
}
if(c !=null && c.size() > 0)
{
SwingUtilities.invokeLater(
new HistoryMessagesLoader(c));
}
}
}.start();
}
/**
@ -138,7 +229,33 @@ public void loadPreviousPageFromHistory()
*/
public void loadNextPageFromHistory()
{
// TODO Auto-generated method stub
if (chatRoomWrapper.getChatRoom() == null)
return;
new Thread()
{
public void run()
{
MessageHistoryService msgHistory
= GuiActivator.getMsgHistoryService();
Date lastMsgDate
= getChatConversationPanel().getPageLastMsgTimestamp();
Collection c = null;
if(lastMsgDate != null)
{
c = msgHistory.findFirstMessagesAfter(
chatRoomWrapper.getChatRoom(),
lastMsgDate,
MESSAGES_PER_PAGE);
}
if(c != null && c.size() > 0)
SwingUtilities.invokeLater(
new HistoryMessagesLoader(c));
}
}.start();
}
/**
@ -222,7 +339,7 @@ public int sendTypingNotification(int typingState)
*/
public Date getFirstHistoryMsgTimestamp()
{
return null;
return firstHistoryMsgTimestamp;
}
/**
@ -230,7 +347,7 @@ public Date getFirstHistoryMsgTimestamp()
*/
public Date getLastHistoryMsgTimestamp()
{
return null;
return lastHistoryMsgTimestamp;
}
public void chatRoomChanged(ChatRoomPropertyChangeEvent event)
@ -398,4 +515,83 @@ public void updateChatRoomStatus(String status)
}
}
}
/**
* Loads history period dates for the current chat.
*/
private void loadHistoryPeriod(ChatRoom chatRoom)
{
MessageHistoryService msgHistory
= GuiActivator.getMsgHistoryService();
Collection firstMessage = msgHistory
.findFirstMessagesAfter(chatRoom, new Date(0), 1);
if(firstMessage.size() > 0)
{
Iterator i = firstMessage.iterator();
Object o = i.next();
if(o instanceof MessageDeliveredEvent)
{
MessageDeliveredEvent evt
= (MessageDeliveredEvent)o;
this.firstHistoryMsgTimestamp = evt.getTimestamp();
}
else if(o instanceof MessageReceivedEvent)
{
MessageReceivedEvent evt = (MessageReceivedEvent)o;
this.firstHistoryMsgTimestamp = evt.getTimestamp();
}
}
Collection lastMessage = msgHistory
.findLastMessagesBefore(chatRoom, new Date(Long.MAX_VALUE), 1);
if(lastMessage.size() > 0)
{
Iterator i1 = lastMessage.iterator();
Object o1 = i1.next();
if(o1 instanceof MessageDeliveredEvent)
{
MessageDeliveredEvent evt
= (MessageDeliveredEvent)o1;
this.lastHistoryMsgTimestamp = evt.getTimestamp();
}
else if(o1 instanceof MessageReceivedEvent)
{
MessageReceivedEvent evt = (MessageReceivedEvent)o1;
this.lastHistoryMsgTimestamp = evt.getTimestamp();
}
}
}
/**
* From a given collection of messages shows the history in the chat window.
*/
private class HistoryMessagesLoader implements Runnable
{
private Collection msgHistory;
public HistoryMessagesLoader(Collection msgHistory)
{
this.msgHistory = msgHistory;
}
public void run()
{
getChatConversationPanel().clear();
processHistory(msgHistory, "");
getChatConversationPanel().setDefaultContent();
}
}
}

@ -163,12 +163,14 @@ else if (evt.getEventType()
= chatRoomList.findServerWrapperFromProvider(
sourceChatRoom.getParentProvider());
chatPanel = chatWindowManager.getMultiChat(
serverWrapper.getSystemRoomWrapper());
chatPanel
= chatWindowManager.getMultiChat(
serverWrapper.getSystemRoomWrapper());
}
else
{
chatPanel = chatWindowManager.getMultiChat(sourceChatRoom);
chatPanel = chatWindowManager
.getMultiChat(sourceChatRoom, message.getMessageUID());
}
chatPanel.processMessage(

Loading…
Cancel
Save