diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java index 1b280f7ec..7db17978d 100755 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java @@ -11,7 +11,6 @@ import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; -import java.text.*; import java.util.*; import java.util.regex.*; @@ -43,7 +42,6 @@ public class ChatConversationPanel MouseListener, ClipboardOwner { - private static final Logger LOGGER = Logger .getLogger(ChatConversationPanel.class.getName()); @@ -81,14 +79,7 @@ public class ChatConversationPanel * private final int hrefPopupMaxWidth = 300; private final int * hrefPopupInitialHeight = 20; */ - - private Date lastIncomingMsgTimestamp = new Date(0); - - private Date pageFirstMsgTimestamp = new Date(Long.MAX_VALUE); - - private Date pageLastMsgTimestamp = new Date(0); - - private int messagesPerPage = 0; + private Date lastIncomingMsgTimestamp = new Date(0); private boolean isHistory = false; @@ -214,35 +205,19 @@ private void initEditor() public String processMessage(String contactName, Date date, String messageType, String message, String contentType) { - if (!isHistory - && (messageType.equals(Constants.HISTORY_INCOMING_MESSAGE) - || messageType.equals(Constants.HISTORY_OUTGOING_MESSAGE)) - && (date.compareTo(pageFirstMsgTimestamp) < 0)) - { - - pageFirstMsgTimestamp = date; - } - - if (!isHistory - && (messageType.equals(Constants.HISTORY_INCOMING_MESSAGE) - || messageType.equals(Constants.HISTORY_OUTGOING_MESSAGE)) - && (date.compareTo(pageLastMsgTimestamp) > 0)) - { - pageLastMsgTimestamp = date; - } - - String dateString = DateFormat.getDateTimeInstance(DateFormat.LONG, - DateFormat.LONG).format(date); + long msgDate = date.getTime(); + String msgID = "message"; + String msgHeaderID = "messageHeader"; String chatString = ""; String endHeaderTag = ""; String timeString = ""; - String startDivTag = "
"; + String startDivTag = "
"; String startHistoryDivTag - = "
"; + = "
"; String startSystemDivTag - = "
"; + = "
"; String endDivTag = "
"; String startPlainTextTag; @@ -268,9 +243,13 @@ public String processMessage(String contactName, Date date, } if (messageType.equals(Constants.INCOMING_MESSAGE)) - { - this.lastIncomingMsgTimestamp = new Date(); - chatString = "

"; + { + this.lastIncomingMsgTimestamp = new Date(msgDate); + chatString = "

"; + endHeaderTag = "

"; chatString += timeString + contactName + " at " @@ -280,7 +259,11 @@ public String processMessage(String contactName, Date date, } else if (messageType.equals(Constants.OUTGOING_MESSAGE)) { - chatString = "

"; + chatString = "

"; + endHeaderTag = "

"; chatString += timeString + Messages.getI18NString("me").getText() @@ -291,7 +274,8 @@ else if (messageType.equals(Constants.OUTGOING_MESSAGE)) } else if (messageType.equals(Constants.STATUS_MESSAGE)) { - chatString = "

"; + chatString = "

"; endHeaderTag = "

"; chatString += GuiUtils.formatTime(date) @@ -309,7 +293,11 @@ else if (messageType.equals(Constants.SYSTEM_MESSAGE)) } else if (messageType.equals(Constants.ERROR_MESSAGE)) { - chatString = "
"; + chatString = "
"; + endHeaderTag = "
"; String errorIcon = ""; + endHeaderTag = ""; chatString += timeString + contactName + " at " @@ -332,7 +324,11 @@ else if (messageType.equals(Constants.HISTORY_INCOMING_MESSAGE)) } else if (messageType.equals(Constants.HISTORY_OUTGOING_MESSAGE)) { - chatString = "

"; + chatString = "

"; + endHeaderTag = "

"; chatString += timeString + Messages.getI18NString("me").getText() @@ -342,8 +338,6 @@ else if (messageType.equals(Constants.HISTORY_OUTGOING_MESSAGE)) + endDivTag; } - messagesPerPage++; - return chatString; } @@ -432,51 +426,32 @@ public void insertMessageAfterStart(String chatString) private void ensureDocumentSize() { - if (messagesPerPage >= Constants.CHAT_BUFFER_SIZE) + if (document.getLength() > Constants.CHAT_BUFFER_SIZE) { - Element firstHeaderElement = this.document.getElement("header"); - Element firstMessageElement = this.document.getElement("message"); - try - { - if (firstHeaderElement != null) - this.document.remove(firstHeaderElement.getStartOffset(), - firstHeaderElement.getEndOffset() - - firstHeaderElement.getStartOffset()); - - if (firstMessageElement != null) - this.document.remove(firstMessageElement.getStartOffset(), - firstMessageElement.getEndOffset() - - firstMessageElement.getStartOffset()); - } - catch (BadLocationException e) - { - LOGGER.error("Error removing messages from chat: ", e); - } - - Element newHeaderElement = this.document.getElement("header"); - - String dateString = (String) newHeaderElement.getAttributes() - .getAttribute("date"); - + Element firstElement + = this.document.getDefaultRootElement().getElement(0); + try { - Date newFirstMsgTimestamp; - newFirstMsgTimestamp = DateFormat.getDateTimeInstance( - DateFormat.LONG, DateFormat.LONG).parse(dateString); - - // Update the start date for the last page. Needed in the main - // toolbar in order to load differently messages for the last - // page. - if (chatContainer instanceof ChatPanel) + if (firstElement != null) + this.document.remove(firstElement.getStartOffset(), + firstElement.getEndOffset() + - firstElement.getStartOffset()); + + String idAttr = (String) firstElement + .getAttributes().getAttribute("identifier"); + + if(idAttr != null && idAttr.equals("messageHeader")) { - - ((ChatPanel) chatContainer) - .setBeginLastPageTimeStamp(pageFirstMsgTimestamp); - - pageFirstMsgTimestamp = newFirstMsgTimestamp; + Element secondElement + = this.document.getDefaultRootElement().getElement(0); + + this.document.remove(secondElement.getStartOffset(), + secondElement.getEndOffset() + - secondElement.getStartOffset()); } } - catch (ParseException e) + catch (BadLocationException e) { LOGGER.error("Error removing messages from chat: ", e); } @@ -884,10 +859,6 @@ public void clear() { this.document = (HTMLDocument) editorKit.createDefaultDocument(); Constants.loadSimpleStyle(document.getStyleSheet()); - - pageFirstMsgTimestamp = new Date(System.currentTimeMillis()); - pageLastMsgTimestamp = new Date(0); - messagesPerPage = 0; } /** @@ -936,8 +907,30 @@ public ChatRightButtonMenu getRightButtonMenu() * @return the date of the first message in the current page */ public Date getPageFirstMsgTimestamp() - { - return pageFirstMsgTimestamp; + { + Element rootElement = this.document.getDefaultRootElement(); + + Element firstMessageElement = null; + + for(int i = 0; i < rootElement.getElementCount(); i ++) + { + String idAttr = (String) rootElement.getElement(i) + .getAttributes().getAttribute("identifier"); + + if (idAttr != null && idAttr.equals("messageHeader")) + { + firstMessageElement = rootElement.getElement(i); + break; + } + } + + if(firstMessageElement == null) + return null; + + String dateString = (String)firstMessageElement + .getAttributes().getAttribute("date"); + + return new Date(new Long(dateString).longValue()); } /** @@ -946,8 +939,30 @@ public Date getPageFirstMsgTimestamp() * @return the date of the last message in the current page */ public Date getPageLastMsgTimestamp() - { - return pageLastMsgTimestamp; + { + Element rootElement = this.document.getDefaultRootElement(); + + Element lastMessageElement = null; + + for(int i = rootElement.getElementCount() - 1; i >= 0; i --) + { + String idAttr = (String) rootElement.getElement(i) + .getAttributes().getAttribute("identifier"); + + if (idAttr != null && idAttr.equals("messageHeader")) + { + lastMessageElement = rootElement.getElement(i); + break; + } + } + + if(lastMessageElement == null) + return null; + + String dateString = (String) lastMessageElement + .getAttributes().getAttribute("date"); + + return new Date(new Long(dateString).longValue()); } /** diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java index b7a21f32f..869f5ab73 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java @@ -129,9 +129,9 @@ public ChatPanel(ChatWindow chatWindow) public abstract void loadHistory(String escapedMessageID); - public abstract void loadPreviousFromHistory(); + public abstract void loadPreviousPageFromHistory(); - public abstract void loadNextFromHistory(); + public abstract void loadNextPageFromHistory(); protected abstract void sendMessage(String text); @@ -171,7 +171,7 @@ public Window getConversationContainerWindow() { public void setStatusMessage(String statusMessage){ this.sendPanel.setStatusMessage(statusMessage); } - + public ChatConversationPanel getChatConversationPanel() { return this.conversationPanel; @@ -348,7 +348,11 @@ else if(o instanceof MessageReceivedEvent) { } } } + conversationPanel.insertMessageAfterStart(historyString); + + getChatWindow().getMainToolBar() + .changeHistoryButtonsState(this); } /** @@ -472,27 +476,7 @@ public void sendButtonDoClick() sendButton.requestFocus(); sendButton.doClick(); } - - /** - * Seys the first date from the last page in the chat. - * - * @param pageFirstMsgTimestamp the fist date from the last page of the chat - */ - public void setBeginLastPageTimeStamp(Date pageFirstMsgTimestamp) - { - this.beginLastPageTimeStamp = pageFirstMsgTimestamp; - } - - /** - * Returns the first date from the last chat page. - * - * @return the first date from the last chat page - */ - public Date getBeginLastPageTimeStamp() - { - return beginLastPageTimeStamp; - } - + /** * Returns TRUE if this chat panel is added to a container (window or * tabbed pane), which is shown on the screen, FALSE - otherwise. diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatWindow.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatWindow.java index e00df8a0c..54dae30fb 100755 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatWindow.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatWindow.java @@ -316,8 +316,6 @@ public void setCurrentChatPanel(ChatPanel chatPanel) this.chatTabbedPane.setSelectedComponent(chatPanel); this.setTitle(chatPanel.getChatName()); - - this.getMainToolBar().changeHistoryButtonsState(chatPanel); chatPanel.requestFocusInWriteArea(); } diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatPanel.java index 887396e0d..524bdd0d0 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatPanel.java @@ -76,15 +76,6 @@ public MetaContactChatPanel( ChatWindow chatWindow, //Add the contact to the list of contacts contained in this panel getChatContactListPanel().addContact(chatContact); - - //Load the history period, to initialize the firstMessageTimestamp and - //the lastMessageTimeStamp variables. Used to disable/enable history - //flash buttons in the chat window tool bar. - new Thread(){ - public void run(){ - loadHistoryPeriod(); - } - }.start(); //For each subcontact in the given MetaContact adds a //ContactPresenceStatusListener in order to have always the contact @@ -142,26 +133,37 @@ public void run(){ public void loadHistory() { new Thread() { - public void run() { + 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(); + + // Load the last N=CHAT_HISTORY_SIZE messages from history. Collection historyList = msgHistory.findLast( metaContact, Constants.CHAT_HISTORY_SIZE); if(historyList.size() > 0) { - class ProcessHistory implements Runnable { + class ProcessHistory implements Runnable + { Collection historyList; + ProcessHistory(Collection historyList) { this.historyList = historyList; } + public void run() { - processHistory(historyList, null); + processHistory(historyList, null); } } SwingUtilities.invokeLater(new ProcessHistory(historyList)); } } - }.start(); + }.start(); } /** @@ -189,46 +191,51 @@ private void loadHistoryPeriod() Collection firstMessage = msgHistory .findFirstMessagesAfter(metaContact, new Date(0), 1); - - if(firstMessage.size() > 0) { - + + if(firstMessage.size() > 0) + { Iterator i = firstMessage.iterator(); Object o = i.next(); - if(o instanceof MessageDeliveredEvent) { + if(o instanceof MessageDeliveredEvent) + { MessageDeliveredEvent evt = (MessageDeliveredEvent)o; - + this.firstHistoryMsgTimestamp = evt.getTimestamp(); } - else if(o instanceof MessageReceivedEvent) { + else if(o instanceof MessageReceivedEvent) + { MessageReceivedEvent evt = (MessageReceivedEvent)o; - + this.firstHistoryMsgTimestamp = evt.getTimestamp(); - } - - Collection lastMessage = msgHistory - .findLastMessagesBefore(metaContact, new Date(Long.MAX_VALUE), 1); - + } + } + + Collection lastMessage = msgHistory + .findLastMessagesBefore(metaContact, new Date(Long.MAX_VALUE), 1); + + if(lastMessage.size() > 0) + { Iterator i1 = lastMessage.iterator(); - + Object o1 = i1.next(); - - if(o1 instanceof MessageDeliveredEvent) { + + if(o1 instanceof MessageDeliveredEvent) + { MessageDeliveredEvent evt = (MessageDeliveredEvent)o1; - + this.lastHistoryMsgTimestamp = evt.getTimestamp(); } - else if(o1 instanceof MessageReceivedEvent) { + else if(o1 instanceof MessageReceivedEvent) + { MessageReceivedEvent evt = (MessageReceivedEvent)o1; - + this.lastHistoryMsgTimestamp = evt.getTimestamp(); } } - - getChatWindow().getMainToolBar().changeHistoryButtonsState(this); } /** @@ -284,6 +291,7 @@ public void contactPresenceStatusChanged( ContactPresenceStatusChangeEvent evt) { Contact sourceContact = evt.getSourceContact(); + PresenceStatus newStatus = evt.getNewStatus(); MetaContact sourceMetaContact = GuiActivator.getMetaContactListService() .findMetaContactByContact(sourceContact); @@ -295,10 +303,8 @@ public void contactPresenceStatusChanged( contactSelectorBox.updateContactStatus(sourceContact); // Update the status of the source meta contact in the contact details - // panel on the right. - - if(sourceMetaContact != null - && sourceMetaContact.getDefaultContact().equals(sourceContact)) + // panel on the right. + if(sourceMetaContact.getDefaultContact().equals(sourceContact)) { ChatContact chatContact = findChatContactByMetaContact(sourceMetaContact); @@ -310,17 +316,15 @@ public void contactPresenceStatusChanged( chatContactPanel.setStatusIcon( chatContact.getPresenceStatus()); } - - PresenceStatus status = contactSelectorBox - .getSelectedProtocolContact().getPresenceStatus(); - + // Show a status message to the user. String message = getChatConversationPanel().processMessage( sourceContact.getAddress(), new Date(System.currentTimeMillis()), Constants.STATUS_MESSAGE, Messages.getI18NString("statusChangedChatMessage", - new String[]{status.getStatusName()}).getText(), "text"); + new String[]{newStatus.getStatusName()}).getText(), + "text"); getChatConversationPanel().appendMessageToEnd(message); @@ -328,7 +332,7 @@ public void contactPresenceStatusChanged( { if (getChatWindow().getChatTabCount() > 0) { getChatWindow().setTabIcon(this, - new ImageIcon(Constants.getStatusIcon(status))); + new ImageIcon(Constants.getStatusIcon(newStatus))); } } } @@ -563,7 +567,7 @@ public void actionPerformed(ActionEvent e) * Implements ChatPanel.loadPreviousFromHistory. * Loads previous page from history. */ - public void loadPreviousFromHistory() + public void loadPreviousPageFromHistory() { new Thread() { public void run() @@ -574,44 +578,23 @@ public void run() ChatConversationPanel conversationPanel = getChatConversationPanel(); - Collection c = msgHistory.findLastMessagesBefore( + Date firstMsgDate + = conversationPanel.getPageFirstMsgTimestamp(); + + Collection c = null; + + if(firstMsgDate != null) + { + c = msgHistory.findLastMessagesBefore( metaContact, - conversationPanel.getPageFirstMsgTimestamp(), + firstMsgDate, MESSAGES_PER_PAGE); - - if(c.size() > 0) + } + + if(c !=null && c.size() > 0) { SwingUtilities.invokeLater( new HistoryMessagesLoader(c)); - - //Save the last before the last page - Iterator i = c.iterator(); - Object lastMessageObject = null; - Date lastMessageTimeStamp = null; - - while(i.hasNext()) { - Object o = i.next(); - - if(!i.hasNext()) { - lastMessageObject = o; - } - } - - if(lastMessageObject instanceof MessageDeliveredEvent) { - MessageDeliveredEvent evt - = (MessageDeliveredEvent)lastMessageObject; - - lastMessageTimeStamp = evt.getTimestamp(); - } - else if(lastMessageObject instanceof MessageReceivedEvent) { - MessageReceivedEvent evt - = (MessageReceivedEvent) lastMessageObject; - - lastMessageTimeStamp = evt.getTimestamp(); - } - - if(getBeginLastPageTimeStamp() == null) - setBeginLastPageTimeStamp(lastMessageTimeStamp); } } }.start(); @@ -621,31 +604,26 @@ else if(lastMessageObject instanceof MessageReceivedEvent) { * Implements ChatPanel.loadNextFromHistory. * Loads next page from history. */ - public void loadNextFromHistory() + public void loadNextPageFromHistory() { new Thread() { public void run(){ MessageHistoryService msgHistory = GuiActivator.getMsgHistoryService(); - - Collection c; - if(getBeginLastPageTimeStamp().compareTo( - getChatConversationPanel().getPageLastMsgTimestamp()) == 0) - { - c = msgHistory.findByPeriod( - metaContact, - getBeginLastPageTimeStamp(), - new Date(System.currentTimeMillis())); - } - else - { + + Date lastMsgDate + = getChatConversationPanel().getPageLastMsgTimestamp(); + System.out.println("LAST MSG DATE=============" + lastMsgDate); + Collection c = null; + if(lastMsgDate != null) + { c = msgHistory.findFirstMessagesAfter( metaContact, - getChatConversationPanel().getPageLastMsgTimestamp(), + lastMsgDate, MESSAGES_PER_PAGE); } - if(c.size() > 0) + if(c != null && c.size() > 0) SwingUtilities.invokeLater( new HistoryMessagesLoader(c)); } diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatPanel.java index 52b3e4485..0849af0db 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatPanel.java @@ -118,7 +118,7 @@ public void loadHistory(String escapedMessageID) *
* Loads the previous "page" in the history. */ - public void loadPreviousFromHistory() + public void loadPreviousPageFromHistory() { // TODO Auto-generated method stub } @@ -128,7 +128,7 @@ public void loadPreviousFromHistory() *
* Loads the next "page" in the history. */ - public void loadNextFromHistory() + public void loadNextPageFromHistory() { // TODO Auto-generated method stub } diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/toolBars/MainToolBar.java b/src/net/java/sip/communicator/impl/gui/main/chat/toolBars/MainToolBar.java index ff19dde3c..d5fd36f95 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/toolBars/MainToolBar.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/toolBars/MainToolBar.java @@ -86,10 +86,7 @@ public MainToolBar(ChatWindow messageWindow) { this.setRollover(true); this.setLayout(new FlowLayout(FlowLayout.LEFT, 3, 0)); this.setBorder(BorderFactory.createEmptyBorder(2, 2, 5, 2)); - - this.nextButton.setEnabled(false); - this.previousButton.setEnabled(false); - + this.add(saveButton); this.add(printButton); @@ -213,15 +210,11 @@ else if (buttonText.equalsIgnoreCase("paste")) { } else if (buttonText.equalsIgnoreCase("previous")) { - chatPanel.loadPreviousFromHistory(); - - changeHistoryButtonsState(chatPanel); + chatPanel.loadPreviousPageFromHistory(); } else if (buttonText.equalsIgnoreCase("next")) { - chatPanel.loadPreviousFromHistory(); - - changeHistoryButtonsState(chatPanel); + chatPanel.loadNextPageFromHistory(); } else if (buttonText.equalsIgnoreCase("sendFile")) { @@ -277,7 +270,7 @@ public boolean hasSelectedMenus() * current page is the first, the last page or a middle page. */ public void changeHistoryButtonsState(ChatPanel chatPanel) - { + { ChatConversationPanel convPanel = chatPanel.getChatConversationPanel(); Date firstMsgInHistory = chatPanel.getFirstHistoryMsgTimestamp(); @@ -285,24 +278,25 @@ public void changeHistoryButtonsState(ChatPanel chatPanel) Date firstMsgInPage = convPanel.getPageFirstMsgTimestamp(); Date lastMsgInPage = convPanel.getPageLastMsgTimestamp(); - if(firstMsgInHistory == null || lastMsgInHistory == null) { + if(firstMsgInHistory == null || lastMsgInHistory == null) + { previousButton.setEnabled(false); nextButton.setEnabled(false); return; } - if(firstMsgInHistory.compareTo(firstMsgInPage) < 0) { + if(firstMsgInHistory.compareTo(firstMsgInPage) < 0) previousButton.setEnabled(true); - } - else { + else previousButton.setEnabled(false); - } if(lastMsgInPage.getTime() > 0 - && (lastMsgInHistory.compareTo(lastMsgInPage) > 0)) { + && (lastMsgInHistory.compareTo(lastMsgInPage) > 0)) + { nextButton.setEnabled(true); } - else { + else + { nextButton.setEnabled(false); } } diff --git a/src/net/java/sip/communicator/impl/gui/utils/Constants.java b/src/net/java/sip/communicator/impl/gui/utils/Constants.java index 9464899f0..a8540a160 100755 --- a/src/net/java/sip/communicator/impl/gui/utils/Constants.java +++ b/src/net/java/sip/communicator/impl/gui/utils/Constants.java @@ -148,7 +148,7 @@ public class Constants { * The size of the buffer that indicates how many messages will be stored * in the conversation area in the chat window. */ - public static final int CHAT_BUFFER_SIZE = 50; + public static final int CHAT_BUFFER_SIZE = 3000; /** * The maximum width of the ConfigurationFrame.