groupchatroom.java.svn-base

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

SVN-BASE
1,132
字号
    public String getNickname() {
        return chat.getNickname();
    }

    /**
     * Sets the icon to use on the tab.
     *
     * @param tabIcon the icon to use on the tab.
     */
    public void setTabIcon(Icon tabIcon) {
        this.tabIcon = tabIcon;
    }

    /**
     * Return the Icon that should be used in the tab of this GroupChat Pane.
     *
     * @return the Icon to use in tab.
     */
    public Icon getTabIcon() {
        return tabIcon;
    }

    /**
     * Return the title that should be used in the tab.
     *
     * @return the title to be used on the tab.
     */
    public String getTabTitle() {
        return tabTitle;
    }

    /**
     * Return the title of this room.
     *
     * @return the title of this room.
     */
    public String getRoomTitle() {
        return getTabTitle();
    }

    /**
     * Return the type of chat we are in.
     *
     * @return the type of chat we are in.
     */
    public Message.Type getChatType() {
        return Message.Type.groupchat;
    }

    /**
     * Implementation of leaveChatRoom.
     */
    public void leaveChatRoom() {
        if (!isActive) {
            return;
        }

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

        // Disable Send Field
        getChatInputEditor().showAsDisabled();

        // Do not allow other to try and invite or transfer chat
        disableToolbar();

        getToolBar().setVisible(false);

        // Update Room Notice To Inform Agent that he has left the chat.
        getTranscriptWindow().insertNotificationMessage(Res.getString("message.user.left.room", getNickname()), ChatManager.NOTIFICATION_COLOR);

        // Leave the Chat.
        try {
            chat.leave();
        }
        catch (Exception e) {
            Log.error("Closing Group Chat Room error.", e);
        }

        // Set window as greyed out.
        getTranscriptWindow().showWindowDisabled();

        // Update Notification Label
        getNotificationLabel().setText(Res.getString("message.chat.session.ended", SparkManager.DATE_SECOND_FORMATTER.format(new java.util.Date())));
        getNotificationLabel().setIcon(null);
        getNotificationLabel().setEnabled(false);

        getSplitPane().setRightComponent(null);
        getSplitPane().setDividerSize(0);

        isActive = false;
    }

    /**
     * If true, will display all presence messages. Set to false to turn off presence
     * notifications.
     *
     * @param showMessages true to display presence messages, otherwise false.
     */
    public void showPresenceMessages(boolean showMessages) {
        showPresenceMessages = showMessages;
    }

    /**
     * Returns whether or not this ChatRoom is active. To be active
     * means to have the agent still engaged in a conversation with a
     * customer.
     *
     * @return true if the ChatRoom is active.
     */
    public boolean isActive() {
        return isActive;
    }

    /**
     * Returns the number of participants in this room.
     *
     * @return the number of participants in this room.
     */
    public int getParticipantCount() {
        if (!isActive) {
            return 0;
        }
        return chat.getOccupantsCount();
    }

    /**
     * Implementation of processPacket to handle muc related packets.
     *
     * @param packet the packet.
     */
    public void processPacket(final Packet packet) {
        super.processPacket(packet);
        if (packet instanceof Presence) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    handlePresencePacket(packet);
                }
            });

        }
        if (packet instanceof Message) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    handleMessagePacket(packet);

                    // Set last activity
                    lastActivity = System.currentTimeMillis();
                }
            });

        }
    }

    /**
     * Handle all MUC related packets.
     *
     * @param packet the packet.
     */
    private void handleMessagePacket(Packet packet) {
        // Do something with the incoming packet here.
        final Message message = (Message)packet;
        if (message.getType() == Message.Type.groupchat) {
            DelayInformation inf = (DelayInformation)message.getExtension("x", "jabber:x:delay");
            Date sentDate;
            if (inf != null) {
                sentDate = inf.getStamp();
            }
            else {
                sentDate = new Date();
            }


            final boolean hasPacketID = packetIDExists(message.getPacketID());

            // Do not accept Administrative messages.
            String host = SparkManager.getSessionManager().getServerAddress();
            if (host.equals(message.getFrom())) {
                return;
            }

            String messageNickname = StringUtils.parseResource(message.getFrom());

            boolean isFromMe = messageNickname.equals(getNickname()) && inf == null;

            // If the message is not from the current user. Append to chat.
            if (ModelUtil.hasLength(message.getBody()) && !isFromMe) {
                // Update transcript
                super.insertMessage(message);

                String from = StringUtils.parseResource(message.getFrom());

                if (inf != null) {
                    getTranscriptWindow().insertHistoryMessage(from, message.getBody(), sentDate);
                }
                else {
                    if (isBlocked(message.getFrom())) {
                        return;
                    }

                    boolean isFromRoom = message.getFrom().indexOf("/") == -1;

                    if (!SparkManager.getUserManager().hasVoice(this, StringUtils.parseResource(message.getFrom())) && !isFromRoom) {
                        return;
                    }

                    getTranscriptWindow().insertMessage(from, message, getColor(from));
                }

                if (typingTimer != null) {
                    showDefaultTabIcon();
                }
            }
        }
        else if (message.getType() == Message.Type.chat) {
            ChatRoom chatRoom = null;
            try {
                chatRoom = SparkManager.getChatManager().getChatContainer().getChatRoom(message.getFrom());
            }
            catch (ChatRoomNotFoundException e) {
                String userNickname = StringUtils.parseResource(message.getFrom());
                String roomTitle = userNickname + " - " + StringUtils.parseName(getRoomname());

                // Create new room
                chatRoom = new ChatRoomImpl(message.getFrom(), userNickname, roomTitle);
                SparkManager.getChatManager().getChatContainer().addChatRoom(chatRoom);

                SparkManager.getChatManager().getChatContainer().activateChatRoom(chatRoom);

                // Check to see if this is a message notification.
                if (message.getBody() != null) {
                    chatRoom.insertMessage(message);
                }
            }

        }
        else if (message.getError() != null) {
            String errorMessage = "";

            if (message.getError().getCode() == 403 && message.getSubject() != null) {
                errorMessage = Res.getString("message.subject.change.error");
            }

            else if (message.getError().getCode() == 403) {
                errorMessage = Res.getString("message.forbidden.error");
            }

            if (ModelUtil.hasLength(errorMessage)) {
                getTranscriptWindow().insertNotificationMessage(errorMessage, ChatManager.ERROR_COLOR);
            }
        }
    }

    /**
     * Handle all presence packets being sent to this Group Chat Room.
     *
     * @param packet the presence packet.
     */
    private void handlePresencePacket(Packet packet) {
        Presence presence = (Presence)packet;
        if (presence.getError() != null) {
            return;
        }

        final String from = presence.getFrom();
        final String nickname = StringUtils.parseResource(from);

        MUCUser mucUser = (MUCUser)packet.getExtension("x", "http://jabber.org/protocol/muc#user");
        String code = "";
        if (mucUser != null) {
            code = mucUser.getStatus() != null ? mucUser.getStatus().getCode() : "";

            Destroy destroy = mucUser.getDestroy();
            if (destroy != null) {
                String reason = destroy.getReason();
                JOptionPane.showMessageDialog(this, Res.getString("message.room.destroyed", reason), Res.getString("title.room.destroyed"), JOptionPane.INFORMATION_MESSAGE);
                leaveChatRoom();
                return;
            }
        }


        if (presence.getType() == Presence.Type.unavailable && !"303".equals(code)) {
            if (currentUserList.contains(from)) {
                if (showPresenceMessages) {
                    getTranscriptWindow().insertNotificationMessage(Res.getString("message.user.left.room", nickname), ChatManager.NOTIFICATION_COLOR);
                    scrollToBottom();
                }
                currentUserList.remove(from);
            }
        }
        else {
            if (!currentUserList.contains(from)) {
                currentUserList.add(from);
                getChatInputEditor().setEnabled(true);
                if (showPresenceMessages) {
                    getTranscriptWindow().insertNotificationMessage(Res.getString("message.user.joined.room", nickname), ChatManager.NOTIFICATION_COLOR);
                    scrollToBottom();
                }
            }
        }
    }

    /**
     * Set up the participant listeners and status change listeners.
     */
    private void setupListeners() {
        chat.addParticipantStatusListener(new DefaultParticipantStatusListener() {
            public void kicked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.kicked.from.room", nickname));
            }

            public void voiceGranted(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.given.voice", nickname));
            }

            public void voiceRevoked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.voice.revoked", nickname));
            }

            public void banned(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.banned", nickname));
            }

            public void membershipGranted(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.granted.membership", nickname));
            }

            public void membershipRevoked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.revoked.membership", nickname));
            }

            public void moderatorGranted(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.granted.moderator", nickname));
            }

            public void moderatorRevoked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.revoked.moderator", nickname));
            }

            public void ownershipGranted(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.granted.owner", nickname));
            }

            public void ownershipRevoked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.revoked.owner", nickname));
            }

            public void adminGranted(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.granted.admin", nickname));
            }

            public void adminRevoked(String participant) {
                String nickname = StringUtils.parseResource(participant);
                insertText(Res.getString("message.user.revoked.admin", nickname));
            }

            public void nicknameChanged(String participant, String nickname) {
                insertText(Res.getString("message.user.nickname.changed", StringUtils.parseResource(participant), nickname));
            }
        });


        chat.addUserStatusListener(new DefaultUserStatusListener() {
            public void kicked(String s, String reason) {
                if (ModelUtil.hasLength(reason)) {
                    insertText(reason);

⌨️ 快捷键说明

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