groupchatroom.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 1,132 行 · 第 1/3 页

SVN-BASE
1,132
字号
/**
 * $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.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
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.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.MessageEventManager;
import org.jivesoftware.smackx.MessageEventNotificationListener;
import org.jivesoftware.smackx.muc.DefaultParticipantStatusListener;
import org.jivesoftware.smackx.muc.DefaultUserStatusListener;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.SubjectUpdatedListener;
import org.jivesoftware.smackx.packet.DelayInformation;
import org.jivesoftware.smackx.packet.MUCUser;
import org.jivesoftware.smackx.packet.MUCUser.Destroy;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.ContextMenuListener;
import org.jivesoftware.spark.ui.ChatContainer;
import org.jivesoftware.spark.ui.ChatFrame;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomNotFoundException;
import org.jivesoftware.spark.ui.GroupChatRoomTransferHandler;
import org.jivesoftware.spark.ui.conferences.ConferenceUtils;
import org.jivesoftware.spark.ui.conferences.DataFormDialog;
import org.jivesoftware.spark.ui.conferences.GroupChatParticipantList;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.log.Log;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.DocumentEvent;

/**
 * GroupChatRoom is the conference chat room UI used to have Multi-User Chats.
 */
public final class GroupChatRoom extends ChatRoom {
    private final MultiUserChat chat;

    private final String roomname;
    private Icon tabIcon = SparkRes.getImageIcon(SparkRes.CONFERENCE_IMAGE_16x16);
    private String tabTitle;
    private final String roomTitle;
    private boolean isActive = true;
    private boolean showPresenceMessages = true;
    private SubjectPanel subjectPanel;

    private List currentUserList = new ArrayList();

    private String conferenceService;
    private List blockedUsers = new ArrayList();

    private ChatRoomMessageManager messageManager;
    private Timer typingTimer;
    private int typedChars;

    private GroupChatParticipantList roomInfo;

    private long lastActivity;
    private GroupChatRoomTransferHandler transferHandler;

    /**
     * Creates a GroupChatRoom from a <code>MultiUserChat</code>.
     *
     * @param chat the MultiUserChat to create a GroupChatRoom from.
     */
    public GroupChatRoom(final MultiUserChat chat) {
        this.chat = chat;

        // Create the filter and register with the current connection
        // making sure to filter by room
        PacketFilter fromFilter = new FromContainsFilter(chat.getRoom());
        PacketFilter orFilter = new OrFilter(new PacketTypeFilter(Presence.class), new PacketTypeFilter(Message.class));
        PacketFilter andFilter = new AndFilter(orFilter, fromFilter);

        // Add packet Listener.
        SparkManager.getConnection().addPacketListener(this, andFilter);

        // Thie Room Name is the same as the ChatRoom name
        roomname = chat.getRoom();
        roomTitle = roomname;

        // We are just using a generic Group Chat.
        tabTitle = StringUtils.parseName(StringUtils.unescapeNode(roomname));

        // Room Information
        roomInfo = new GroupChatParticipantList();
        getSplitPane().setRightComponent(roomInfo.getGUI());

        roomInfo.setChatRoom(this);
        getSplitPane().setResizeWeight(.60);
        getSplitPane().setDividerLocation(.60);

        setupListeners();


        conferenceService = StringUtils.parseServer(chat.getRoom());

        subjectPanel = new SubjectPanel();

        // Do not show top toolbar
        getToolBar().add(subjectPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 2, 0, 2), 0, 0));

        // Add ContextMenuListener
        getTranscriptWindow().addContextMenuListener(new ContextMenuListener() {
            public void poppingUp(Object component, JPopupMenu popup) {
                popup.addSeparator();
                Action inviteAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        ConferenceUtils.inviteUsersToRoom(conferenceService, getRoomname(), null);
                    }
                };

                inviteAction.putValue(Action.NAME, Res.getString("menuitem.invite.users"));
                inviteAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE));

                popup.add(inviteAction);


                Action configureAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        try {
                            ChatFrame chatFrame = SparkManager.getChatManager().getChatContainer().getChatFrame();
                            Form form = chat.getConfigurationForm().createAnswerForm();
                            new DataFormDialog(chatFrame, chat, form);
                        }
                        catch (XMPPException e) {
                            Log.error("Error configuring room.", e);
                        }
                    }
                };

                configureAction.putValue(Action.NAME, Res.getString("title.configure.room"));
                configureAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_ALL_AGENTS_IMAGE));
                if (SparkManager.getUserManager().isOwner((GroupChatRoom)getChatRoom(), chat.getNickname())) {
                    popup.add(configureAction);
                }

                Action subjectAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        String newSubject = JOptionPane.showInputDialog(getChatRoom(), Res.getString("message.enter.new.subject") + ":", Res.getString("title.change.subject"), JOptionPane.QUESTION_MESSAGE);
                        if (ModelUtil.hasLength(newSubject)) {
                            try {
                                chat.changeSubject(newSubject);
                            }
                            catch (XMPPException e) {
                                Log.error(e);
                            }
                        }
                    }
                };

                subjectAction.putValue(Action.NAME, Res.getString("menuitem.change.subject"));
                subjectAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_EDIT_IMAGE));
                popup.add(subjectAction);

                // Define actions to modify/view room information
                Action destroyRoomAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        int ok = JOptionPane.showConfirmDialog(getChatRoom(), Res.getString("message.confirm.destruction.of.room"), Res.getString("title.confirmation"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (ok == JOptionPane.NO_OPTION) {
                            return;
                        }

                        String reason = JOptionPane.showInputDialog(getChatRoom(), Res.getString("message.room.destruction.reason"), Res.getString("title.enter.reason"), JOptionPane.QUESTION_MESSAGE);
                        if (ModelUtil.hasLength(reason)) {
                            try {
                                chat.destroy(reason, null);
                                getChatRoom().leaveChatRoom();
                            }
                            catch (XMPPException e1) {
                                Log.warning("Unable to destroy room", e1);
                            }
                        }
                    }
                };

                destroyRoomAction.putValue(Action.NAME, Res.getString("menuitem.destroy.room"));
                destroyRoomAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_DELETE));
                if (SparkManager.getUserManager().isOwner((GroupChatRoom)getChatRoom(), getNickname())) {
                    popup.add(destroyRoomAction);
                }


            }

            public void poppingDown(JPopupMenu popup) {

            }

            public boolean handleDefaultAction(MouseEvent e) {
                return false;
            }
        });


        messageManager = new ChatRoomMessageManager();

        // set last activity to be right now
        lastActivity = System.currentTimeMillis();

        transferHandler = new GroupChatRoomTransferHandler(this);
        getTranscriptWindow().setTransferHandler(transferHandler);
    }

    /**
     * Sets the title to use on the tab describing the Conference room.
     *
     * @param tabTitle the title to use on the tab.
     */
    public void setTabTitle(String tabTitle) {
        this.tabTitle = tabTitle;
    }

    /**
     * Call this method if you wish to hide the participant list.
     */
    public void hideParticipantList() {
        getSplitPane().setRightComponent(null);
    }

    /**
     * Have the user leave this chat room and then close it.
     */
    public void closeChatRoom() {
        // Specify the end time.
        super.closeChatRoom();

        // Remove Listener
        SparkManager.getConnection().removePacketListener(this);

        ChatContainer container = SparkManager.getChatManager().getChatContainer();
        container.leaveChatRoom(this);
        container.closeTab(this);
    }

    /**
     * Sends a message.
     *
     * @param message - the message to send.
     */
    public void sendMessage(Message message) {
        try {
            message.setTo(chat.getRoom());
            message.setType(Message.Type.groupchat);
            MessageEventManager.addNotificationsRequests(message, true, true, true, true);
            // Add packetID to list
            addPacketID(message.getPacketID());

            // Fire Message Filters
            SparkManager.getChatManager().filterOutgoingMessage(this, message);

            // Fire Global Listeners
            SparkManager.getChatManager().fireGlobalMessageSentListeners(this, message);

            chat.sendMessage(message);
        }
        catch (XMPPException ex) {
            Log.error("Unable to send message in conference chat.", ex);
        }

        try {
            getTranscriptWindow().insertMessage(getNickname(), message, getColor(getNickname()));
            getChatInputEditor().selectAll();

            getTranscriptWindow().validate();
            getTranscriptWindow().repaint();
            getChatInputEditor().clear();
        }
        catch (Exception ex) {
            Log.error("Error sending message", ex);
        }

        // Notify users that message has been sent
        fireMessageSent(message);

        addToTranscript(message, false);

        getChatInputEditor().setCaretPosition(0);
        getChatInputEditor().requestFocusInWindow();
        scrollToBottom();

        lastActivity = System.currentTimeMillis();
    }

    /**
     * Sends a message.
     *
     * @param message - the message to send.
     */
    public void sendMessageWithoutNotification(Message message) {
        try {
            message.setTo(chat.getRoom());
            message.setType(Message.Type.groupchat);
            MessageEventManager.addNotificationsRequests(message, true, true, true, true);
            // Add packetID to list
            addPacketID(message.getPacketID());

            chat.sendMessage(message);
        }
        catch (XMPPException ex) {
            Log.error("Unable to send message in conference chat.", ex);
        }

        try {
            getTranscriptWindow().insertMessage(getNickname(), message, getColor(getNickname()));
            getChatInputEditor().selectAll();

            getTranscriptWindow().validate();
            getTranscriptWindow().repaint();
            getChatInputEditor().clear();
        }
        catch (Exception ex) {
            Log.error("Error sending message", ex);
        }

        addToTranscript(message, false);

        getChatInputEditor().setCaretPosition(0);
        getChatInputEditor().requestFocusInWindow();
        scrollToBottom();

        lastActivity = System.currentTimeMillis();
    }

    /**
     * Return name of the room specified when the room was created.
     *
     * @return the roomname.
     */
    public String getRoomname() {
        return roomname;
    }

    /**
     * Retrieve the nickname of the user in this groupchat.
     *
     * @return the nickname of the agent in this groupchat
     */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?