openfire+spark二次开发 spark聊天窗口的构建

时间:2020-12-09 19:47:09
// Create new room
                    // 创建新的房间
                    ChatRoom chatRoom = new ChatRoomImpl( message.getFrom(), userNickname, roomTitle );
                    SparkManager.getChatManager().getChatContainer().addChatRoom( chatRoom );

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

GroupChatRoom.java    private void handleMessagePacket( Stanza stanza )

SparkManager.java

 public static ChatManager getChatManager() {
        if (chatManager == null) {
            chatManager = ChatManager.getInstance();
        }
        return chatManager;
    }

ChatManager.java

 public ChatContainer getChatContainer() {
        return chatContainer;
    }

ChatContainer.java

 /**
     * Adds a new ChatRoom to Spark.
     * 给Spark添加了一个新的聊天室。
     *
     * @param room the ChatRoom to add.
     */
    public synchronized void addChatRoom(final ChatRoom room) {
        createFrameIfNeeded();
        // 设置边框,高亮灰
        room.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
        AndFilter presenceFilter = new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.createBare( room.getRoomname()));

        // Next, create a packet listener. We use an anonymous inner class for brevity.
        StanzaListener myListener = stanza -> SwingUtilities.invokeLater( () -> handleRoomPresence((Presence)stanza) );

        room.registeredToFrame(chatFrame);
        
        SparkManager.getConnection().addAsyncStanzaListener(myListener, presenceFilter);

        // Add to PresenceMap
        presenceMap.put(room.getRoomname(), myListener);

        String tooltip;
        if (room instanceof ChatRoomImpl) {
            tooltip = ((ChatRoomImpl)room).getParticipantJID();
            String nickname = SparkManager.getUserManager().getUserNicknameFromJID(((ChatRoomImpl)room).getParticipantJID());

            tooltip = "<html><body><b>Contact: </b>" + nickname + "<br><b>JID: </b>" + tooltip;
        }
        else {
            tooltip = room.getRoomname();
        }

        // Create ChatRoom UI and dock
        SparkTab tab = addTab(room.getTabTitle(), room.getTabIcon(), room, tooltip);
        tab.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                checkTabPopup(e);
            }

            public void mousePressed(MouseEvent e) {
                checkTabPopup(e);
            }
        });

        room.addMessageListener(this);

        // Remove brand panel
        final String title = getTabAt(0).getActualText();
        if (title.equals(WELCOME_TITLE)) {
            chatFrame.setTitle(room.getRoomTitle());
        }


        final TimerTask visibleTask = new SwingTimerTask() {
            public void doRun() {
                checkVisibility(room);
            }
        };

        TaskEngine.getInstance().schedule(visibleTask, 100);

        // Add to ChatRoomList
        chatRoomList.add(room);

        // Notify users that the chat room has been opened.
        // 通知用户聊天室已经打开。
        fireChatRoomOpened(room);

        // Focus Chat
        focusChat();

        // Add Room listeners to override issue with input maps and keybinding on the mac.
        // 添加房间监听器以覆盖在mac上的输入映射和键绑定问题。
        if (Spark.isMac()) {
            room.getChatInputEditor().addKeyListener(this);
        }
    }