chatroomimpl.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 688 行 · 第 1/2 页
JAVA
688 行
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.spark.ui.rooms;
import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromMatchesFilter;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.MessageEventManager;
import org.jivesoftware.smackx.packet.MessageEvent;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomButton;
import org.jivesoftware.spark.ui.ContactItem;
import org.jivesoftware.spark.ui.ContactList;
import org.jivesoftware.spark.ui.MessageEventListener;
import org.jivesoftware.spark.ui.RosterDialog;
import org.jivesoftware.spark.ui.VCardPanel;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.transcripts.ChatTranscript;
import org.jivesoftware.sparkimpl.plugin.transcripts.ChatTranscripts;
import org.jivesoftware.sparkimpl.plugin.transcripts.HistoryMessage;
import org.jivesoftware.sparkimpl.profile.VCardManager;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;
/**
* This is the Person to Person implementation of <code>ChatRoom</code>
* This room only allows for 1 to 1 conversations.
*/
public class ChatRoomImpl extends ChatRoom {
private List messageEventListeners = new ArrayList();
private String roomname;
private Icon tabIcon;
private String roomTitle;
private String tabTitle;
private String participantJID;
private String participantNickname;
private Presence presence;
private boolean offlineSent;
private Roster roster;
private long lastTypedCharTime;
private boolean sendNotification;
private TimerTask typingTimerTask;
private boolean sendTypingNotification;
private String threadID;
private long lastActivity;
private boolean active;
// Information button
private ChatRoomButton infoButton;
private ChatRoomButton addToRosterButton;
/**
* Constructs a 1-to-1 ChatRoom.
*
* @param participantJID the participants jid to chat with.
* @param participantNickname the nickname of the participant.
* @param title the title of the room.
*/
public ChatRoomImpl(final String participantJID, String participantNickname, String title) {
this.active = true;
this.participantJID = participantJID;
this.participantNickname = participantNickname;
// Loads the current history for this user.
loadHistory();
// Register PacketListeners
PacketFilter fromFilter = new FromMatchesFilter(participantJID);
PacketFilter orFilter = new OrFilter(new PacketTypeFilter(Presence.class), new PacketTypeFilter(Message.class));
PacketFilter andFilter = new AndFilter(orFilter, fromFilter);
SparkManager.getConnection().addPacketListener(this, andFilter);
// The roomname will be the participantJID
this.roomname = participantJID;
// Use the agents username as the Tab Title
this.tabTitle = title;
// The name of the room will be the node of the user jid + conversation.
final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
this.roomTitle = participantNickname;
// Add RoomInfo
this.getSplitPane().setRightComponent(null);
getSplitPane().setDividerSize(0);
presence = PresenceManager.getPresence(participantJID);
roster = SparkManager.getConnection().getRoster();
RosterEntry entry = roster.getEntry(participantJID);
tabIcon = PresenceManager.getIconFromPresence(presence);
// Create toolbar buttons.
infoButton = new ChatRoomButton("", SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_24x24));
infoButton.setToolTipText(Res.getString("message.view.information.about.this.user"));
// Create basic toolbar.
getToolBar().addChatRoomButton(infoButton);
// If the user is not in the roster, then allow user to add them.
addToRosterButton = new ChatRoomButton("", SparkRes.getImageIcon(SparkRes.ADD_IMAGE_24x24));
if (entry == null && !StringUtils.parseResource(participantJID).equals(participantNickname)) {
addToRosterButton.setToolTipText(Res.getString("message.add.this.user.to.your.roster"));
getToolBar().addChatRoomButton(addToRosterButton);
addToRosterButton.addActionListener(this);
}
// Show VCard.
infoButton.addActionListener(this);
// If this is a private chat from a group chat room, do not show toolbar.
if (StringUtils.parseResource(participantJID).equals(participantNickname)) {
getToolBar().setVisible(false);
}
typingTimerTask = new TimerTask() {
public void run() {
if (!sendTypingNotification) {
return;
}
long now = System.currentTimeMillis();
if (now - lastTypedCharTime > 2000) {
if (!sendNotification) {
// send cancel
SparkManager.getMessageEventManager().sendCancelledNotification(getParticipantJID(), threadID);
sendNotification = true;
}
}
}
};
TaskEngine.getInstance().scheduleAtFixedRate(typingTimerTask, 2000, 2000);
lastActivity = System.currentTimeMillis();
}
public void closeChatRoom() {
// If already closed, don't bother.
if (!active) {
return;
}
super.closeChatRoom();
// Remove info listener
infoButton.removeActionListener(this);
addToRosterButton.removeActionListener(this);
// Send a cancel notification event on closing if listening.
if (!sendNotification) {
// send cancel
SparkManager.getMessageEventManager().sendCancelledNotification(getParticipantJID(), threadID);
sendNotification = true;
}
SparkManager.getChatManager().removeChat(this);
SparkManager.getConnection().removePacketListener(this);
if (typingTimerTask != null) {
TaskEngine.getInstance().cancelScheduledTask(typingTimerTask);
typingTimerTask = null;
}
active = false;
}
public void sendMessage() {
String text = getChatInputEditor().getText();
sendMessage(text);
}
public void sendMessage(String text) {
final Message message = new Message();
if (threadID == null) {
threadID = StringUtils.randomString(6);
}
message.setThread(threadID);
// Set the body of the message using typedMessage
message.setBody(text);
// IF there is no body, just return and do nothing
if (!ModelUtil.hasLength(text)) {
return;
}
// Fire Message Filters
SparkManager.getChatManager().filterOutgoingMessage(this, message);
// Fire Global Filters
SparkManager.getChatManager().fireGlobalMessageSentListeners(this, message);
sendMessage(message);
sendNotification = true;
}
/**
* Sends a message to the appropriate jid. The message is automatically added to the transcript.
*
* @param message the message to send.
*/
public void sendMessage(Message message) {
lastActivity = System.currentTimeMillis();
try {
getTranscriptWindow().insertMessage(getNickname(), message, ChatManager.TO_COLOR);
getChatInputEditor().selectAll();
getTranscriptWindow().validate();
getTranscriptWindow().repaint();
getChatInputEditor().clear();
}
catch (Exception ex) {
Log.error("Error sending message", ex);
}
// Before sending message, let's add our full jid for full verification
message.setType(Message.Type.chat);
message.setTo(participantJID);
message.setFrom(SparkManager.getSessionManager().getJID());
// Notify users that message has been sent
fireMessageSent(message);
addToTranscript(message, false);
getChatInputEditor().setCaretPosition(0);
getChatInputEditor().requestFocusInWindow();
scrollToBottom();
// No need to request displayed or delivered as we aren't doing anything with this
// information.
MessageEventManager.addNotificationsRequests(message, true, false, false, true);
// Send the message that contains the notifications request
try {
fireOutgoingMessageSending(message);
SparkManager.getConnection().sendPacket(message);
}
catch (Exception ex) {
Log.error("Error sending message", ex);
}
}
public String getRoomname() {
return roomname;
}
public Icon getTabIcon() {
return tabIcon;
}
public void setTabIcon(Icon icon) {
this.tabIcon = icon;
}
public String getTabTitle() {
return tabTitle;
}
public void setTabTitle(String tabTitle) {
this.tabTitle = tabTitle;
}
public void setRoomTitle(String roomTitle) {
this.roomTitle = roomTitle;
}
public String getRoomTitle() {
return roomTitle;
}
public Message.Type getChatType() {
return Message.Type.chat;
}
public void leaveChatRoom() {
// There really is no such thing in Agent to Agent
}
public boolean isActive() {
return true;
}
public String getParticipantJID() {
return participantJID;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?