1. 程式人生 > >openfire+spark二次開發 spark聊天視窗的構建

openfire+spark二次開發 spark聊天視窗的構建


// 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);
        }
    }