⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatroompanel.java

📁 JBother是纯Java开发的Jabber(即时消息开源软件)客户端。支持群组聊天
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**     * Adds a buddy to the nickname list     *     * @param buddy     *            the buddy to add     */    public void addBuddy(String buddy) {        if( nickList == null || buddy == null ) return;        nickList.addBuddy(buddy);    }    /**     * Removes a buddy from the nick list     *     * @param buddy     *            the buddy to remove     */    public void removeBuddy(String buddy) {        if( nickList == null || buddy == null ) return;        nickList.removeBuddy(buddy);    }    /**     * Opens the log file     */    public void startLog() {        // for loggingg        if (Settings.getInstance().getBoolean("keepLogs")) {            String logFileName = LogViewerDialog.getDateName() + ".log";            String logFileDir = JBother.profileDir                    + File.separatorChar                    + "logs"                    + File.separatorChar                    + getRoomName().replaceAll("@", "_at_").replaceAll("\\/",                            "-");            File logDir = new File(logFileDir);            if (!logDir.isDirectory() && !logDir.mkdirs())                Standard.warningMessage(this, resources.getString("log"),                        resources.getString("couldNotCreateLogDir"));            String logEnc =                Settings.getInstance().getProperty("keepLogsEncoding");            conversationArea.setLogFile(new File(logDir, logFileName), logEnc);        }    }    /**     * Gets the BuddyStatus represending a user in the room     *     * @param user     *            the BuddyStatus to get     * @return the requested BuddyStatus     */    public MUCBuddyStatus getBuddyStatus(String user) {        if (!buddyStatuses.containsKey(user)) {            MUCBuddyStatus buddy = new MUCBuddyStatus(user);            buddy.setMUC(chat);            buddy.setName(user.substring(user.indexOf("/") + 1, user.length()));            buddyStatuses.put(user, buddy);        }        return (MUCBuddyStatus) buddyStatuses.get(user);    }    /**     * Returns the tab name for the TabFramePanel     *     * @return the panel name     */    public String getPanelName() {        return getShortRoomName();    }    /**     * Returns the tooltip for the tab in the TabFrame     *     * @return the tooltip for this tab in the tab frame     */    public String getTooltip() {        return getRoomName();    }    /**     * Returns the window title     *     * @return the window title for the TabFrame when this tab is selected     */    public String getWindowTitle() {        return resources.getString("groupChat") + ": " + getRoomName();    }    /**     * Gets the short room name - for example, if you are talking in     * jdev@conference.jabber.org, it would return "jdev"     *     * @return short room name     */    public String getShortRoomName() {        return chatroom.replaceAll("\\@.*", "");    }    /**     * Gets the entire room name, server included     *     * @return gets the room address     */    public String getRoomName() {        return chatroom;    }    /**     * Returns the nickname of a user in a group chat.     *     * @param id     *            The full id of someone in a room, ie:     *            jdev@conference.jabber.org/synic     * @return the nickname of the person in the room     */    public String getNickname(String id) {        int index = id.indexOf("/");        if (index == -1)            return id;        return id.substring(index + 1);    }    public MultiUserChat getChat() {        return chat;    }    public GroupParticipantListener getParticipantListener() {        return participantListener;    }    /**     * Starts the groupchat. Sets up a thread to connect, and start that thread     */    public void startChat() {        if (!BuddyList.getInstance().checkConnection())            return;        serverNoticeMessage("Connecting to " + getRoomName() + " ... ");        BuddyList.getInstance().addTabPanel(this);        JoinChatThread t = new JoinChatThread();        t.start();        startLog();    }    private void leaveChat() {        if (chat == null)            return;        buddyStatuses.clear();        com.valhalla.Logger.debug("Leaving " + chat.getRoom());        Presence p = new Presence(Presence.Type.UNAVAILABLE);        p.setTo(chat.getRoom());        if (BuddyList.getInstance().checkConnection())            BuddyList.getInstance().getConnection().sendPacket(p);        chat.removeMessageListener(messageListener);        chat.removeParticipantListener(participantListener);        chat.removeSubjectUpdatedListener(subjectListener);        chat.removeParticipantStatusListener(statusListener);        chat.removeUserStatusListener(userStatusListener);        chat.removeInvitationRejectionListener(invitationRejectionPacketListener);        chat = null;        System.gc();    }    /**     * Asks for a new topic, then sets the new topic     */    private void topicHandler(final String subject) {        Thread thread = new Thread(new Runnable() {            public void run() {                try {                    chat.changeSubject(subject);                } catch (final XMPPException e) {                    SwingUtilities.invokeLater(new Runnable() {                        public void run() {                            String message = e.getMessage();                            serverErrorMessage(resources                                    .getString("errorSettingSubject")                                    + ": " + message);                        }                    });                }                SwingUtilities.invokeLater(new Runnable() {                    public void run() {                        subjectField.setEnabled(true);                    }                });            }        });        thread.start();    }    private class RunTaskThread implements Runnable {        private String err, method;        private Object param;        public RunTaskThread(String err, String method, Object param) {            this.err = err;            this.method = method;            this.param = param;        }        public void run() {            java.lang.reflect.Method m;            try {                m = chat.getClass().getMethod(this.method,                        new Class[] { param.getClass() });            } catch (Exception e) {                e.printStackTrace();                return;            }            try {                m.invoke(chat, new Object[] { param });            } catch (final java.lang.reflect.InvocationTargetException ex) {                final XMPPException xmpp = (XMPPException) ex.getCause();                if (xmpp == null)                    return;                SwingUtilities.invokeLater(new Runnable() {                    public void run() {                        serverErrorMessage(err                                + ": "                                + resources.getString("xmppError"                                        + xmpp.getXMPPError().getCode()));                    }                });            } catch (Exception e) {                e.printStackTrace();            }        }    }    /**     * Asks for a new nickname, and sends a nickname change request     */    private void changeNickHandler() {        String result = (String) JOptionPane.showInputDialog(null, resources                .getString("enterNickname"),                resources.getString("setNickname"),                JOptionPane.QUESTION_MESSAGE, null, null, chat.getNickname());        if (result != null && !result.equals("")) {            Thread thread = new Thread(new RunTaskThread(resources                    .getString("couldNotChangeNick"), "changeNickname", result));            thread.start();        }    }    /**     * Adds the event listeners for the various components in this chatwindows     */    public void addListeners() {        //set up the window so you can press enter in the text box and        //that will send the message.        Action SendMessageAction = new AbstractAction() {            public void actionPerformed(ActionEvent e) {                sendHandler();            }        };        Action nickCompletionAction = new AbstractAction() {            public void actionPerformed(ActionEvent e) {                nickCompletionHandler();            }        };        //set it up so that if they drag in the conversation window, it grabs        // the focus        conversationArea.getTextPane().addMouseMotionListener(new MouseMotionAdapter() {            public void mouseDragged(MouseEvent e) {                //conversationArea.grabFocus();            }        });        //set it up so that if there isn't any selected text in the        // conversation area        //the textentryarea grabs the focus.        conversationArea.getTextPane().addMouseListener(new MouseAdapter() {            public void mouseReleased(MouseEvent e) {                if (conversationArea.getSelectedText() == null) {                    textEntryArea.requestFocus();                }            }        });        Action closeAction = new AbstractAction() {            public void actionPerformed(ActionEvent e) {                BuddyList.getInstance().getTabFrame().removePanel(ChatRoomPanel.this);                BuddyList.getInstance().stopTabFrame();            }        };        subjectField.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                subjectField.setEnabled(false);                topicHandler(subjectField.getText());                setSubject(subject);            }        });        KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);        textEntryArea.getInputMap().put(enterStroke, SendMessageAction);        KeyStroke tabStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);        textEntryArea.getInputMap().put(tabStroke, nickCompletionAction);        textEntryArea.getInputMap().put(                KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit                        .getDefaultToolkit().getMenuShortcutKeyMask()),                closeAction);    }    /**     * Leaves this room and removes it from the groupchat frame     */    public void leave() {        closeLog();        leaveChat();    } //leave the chatroom    /**     * Gets the nickname currently being used in the chat room     *     * @return the nickname being used in the chatroom     */    public String getNickname() {        if (chat == null || chat.getNickname() == null)            return nickname;        return chat.getNickname();    }    /**     * Displays a server notice message     *     * @param message     *            the message to display     */    public void serverNoticeMessage(String message) {        conversationArea.append(getDate(null));        conversationArea.append(" -> " + message + "\n", ConversationArea.SERVER);    }

⌨️ 快捷键说明

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