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

📄 bschatwindow.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.ou.kmi.buddyspace.gui;

/*
 * BSChatWindow.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 24 October 2002, 10:06
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.utils.*;
import edu.ou.kmi.buddyspace.xml.*;

/**
 * Window including whole GUI for chat.
 * Uses <code>BSChatWinManager</code> to receive and send messages.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSChatWindow extends AlertsDockableWindow
                          implements ActionListener, KeyListener, ItemListener,
                          BSVideoConferenceListener {

    private JPanel mainPanel;

    private JSplitPane splitPane;
    //private AutoScrollTextArea chatTextArea;
    private BSAutoScrollTextPane chatTextPane;
    private JScrollPane chatScrollPane;
    private JTextArea writeTextArea;
    private JScrollPane writeScrollPane;
    private JButton closeButton;
    private JButton historyButton;
    private JButton sendButton;
    private JLabel typingLabel;
    private JButton urlButton;
    private JPanel buttonPanel;
    private JButton dockButton;
    private JCheckBox enterSendsCheckBox;
    private JPanel checkBoxesPanel;
    private JComboBox smiliesCombo;
    private ImageIcon emptyIcon;
    private ImageIcon typingIcon;
    private JButton videoButton;

    //private BSChatWinManager winMan;
    //private Frame frame;
    private JID jid;
    private String nick;
    private String local;
    private boolean showResource;

    // message events
    private boolean sendComposingEvents = false;
    private javax.swing.Timer composingTimer = null;
    private String id = null;
    public int COMPOSING_TIME_OUT = 5 * 1000;

    private boolean enterSends = true;
    private String thread = null;

    private BSVideoConferenceBean videoBean = null;

    //private Image newMsgIcon;

    /** Constructor */
    BSChatWindow(Frame parent, BSChatWinManager winMan, JID jid, String title,
                 Image icon, String nick, String local, boolean docked, boolean showResource) {

        //super(jid.toString(), title, icon, docked, winMan);
        super(jid.normalize(), title, icon, docked, winMan);

        this.jid = jid;
        this.nick = nick;
        this.local = local;
        this.showResource = showResource;

        initComponents();
        chatTextPane.setMainFrame(winMan.mainFrame);

        thread = "chat_" + Integer.toString(BSCore.getNextID());

        videoBean = winMan.mainFrame.getCore().getVideoConferenceBean();
        if (videoBean != null)
        {
            videoBean.addVideoConferenceListener(this);
            videoBean.requestAuthorization(this);
        }
        focusToWriteArea();
    }

    protected void focusToWriteArea() {
        // request focus for the txtName field
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                writeTextArea.requestFocus();
            }
        });
    }

    /** Inits components */
    private void initComponents() {
        mainPanel = new JPanel(new BorderLayout());

        chatScrollPane = new JScrollPane();
        chatScrollPane.setAutoscrolls(true);

        //chatTextArea = new AutoScrollTextArea();
        chatTextPane = new BSAutoScrollTextPane(true);
        //chatTextArea.setEditable(false);
        //chatTextArea.setLineWrap(true);
        //chatTextArea.setWrapStyleWord(true);
        //*** based on text size...
        //chatTextArea.setPreferredSize(new Dimension(200, getFont().getSize()*20));
        chatScrollPane.setViewportView(chatTextPane);

        writeScrollPane = new JScrollPane();
        writeScrollPane.setAutoscrolls(true);
        writeTextArea = new JTextArea();
        writeTextArea.setEditable(true);
        writeTextArea.setLineWrap(true);
        writeTextArea.setWrapStyleWord(true);
        //*** based on text size...
        //writeTextArea.setPreferredSize(new Dimension(200, getFont().getSize()*2));
        writeScrollPane.setViewportView(writeTextArea);
        writeTextArea.addKeyListener(this);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, chatScrollPane, writeScrollPane);
        splitPane.setBorder(null);
        splitPane.setDividerSize(8);
        splitPane.setResizeWeight(0.8);

        urlButton = new JButton("URL");
        urlButton.addActionListener(this);

        smiliesCombo = new JComboBox();
        emptyIcon = new ImageIcon(ClassLoader.getSystemResource("images/empty.gif"));
        emptyIcon.setDescription(":-)");
        smiliesCombo.addItem(emptyIcon);
        for (int i=0; i<chatTextPane.smileyIcons.length; i++)
            smiliesCombo.addItem(chatTextPane.smileyIcons[i]);
        smiliesCombo.addItemListener(this);
        ImgIconListCellRenderer renderer = new ImgIconListCellRenderer();
        smiliesCombo.setRenderer(renderer);

        JPanel buttonPanel = new JPanel();
        closeButton = new JButton("Close");
        closeButton.addActionListener(this);
        sendButton = new JButton("Send");
        sendButton.addActionListener(this);

        typingIcon = new ImageIcon(ClassLoader.getSystemResource("images/typing3.gif"));
        typingLabel = new JLabel(emptyIcon);
        typingLabel.setPreferredSize(new Dimension(34, 18));

        Icon videoIcon = new ImageIcon(ClassLoader.getSystemResource("images/flashmeeting-grey.gif"));
        videoButton = new JButton(videoIcon);
        videoButton.setToolTipText("Start FlashMeeting");
        videoButton.setMargin(new Insets(1, 1, 1, 1));
        videoButton.addActionListener(this);

        buttonPanel.add(typingLabel);
        buttonPanel.add(urlButton);
        buttonPanel.add(smiliesCombo);
        buttonPanel.add(sendButton);
        buttonPanel.add(closeButton);

        JPanel checkBoxesPanel = new JPanel();
        Icon icon = new ImageIcon(ClassLoader.getSystemResource(!docked?
                                "images/dock.gif" : "images/float.gif"));
        dockButton = new JButton(icon);
        dockButton.setToolTipText(docked? "Float" : "Dock");
        dockButton.addActionListener(this);
        if (!OSVersion.isJava1Point4orHigher())
            dockButton.setEnabled(false);
        enterSendsCheckBox = new JCheckBox("Enter = Send", enterSends);
        enterSendsCheckBox.addActionListener(this);
        historyButton = new JButton("Load history");
        historyButton.addActionListener(this);
        checkBoxesPanel.add(enterSendsCheckBox);
        checkBoxesPanel.add(dockButton);
        checkBoxesPanel.add(historyButton);
        checkBoxesPanel.add(videoButton);

        /*buttonPanel.add(dockButton);
        buttonPanel.add(enterSendsCheckBox);
        buttonPanel.add(sendButton);
        buttonPanel.add(historyButton);
        buttonPanel.add(closeButton);*/

        mainPanel.add(checkBoxesPanel, BorderLayout.NORTH);
        mainPanel.add(splitPane, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setLayout(new BorderLayout());
        add(mainPanel, BorderLayout.CENTER);

        MouseAdapter ma = new MouseAdapter() {
            public void mouseClicked(MouseEvent mouseEvent) {
                if (newMsg) {
                    setNewMsg(false);
                    ((BSChatWinManager)winMan).updateNewMessageFlags(BSChatWindow.this, false);
                }
            }
        };
        addMouseListener(ma);
        writeTextArea.addMouseListener(ma);
        chatTextPane.addMouseListener(ma);
        frame.addMouseListener(ma);
        frame.getContentPane().addMouseListener(ma);

        //icon = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/chat.gif"));
        //newMsgIcon = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/chat-new.gif"));

        setNewMsg(newMsg);
    }

    /** Sets nick of peer */
    public void setNick(String nick) {
        this.nick = nick;
    }

    /** Returns if the resource of buddy's JID is shown */
    public boolean isShowResource() {
        return showResource;
    }

    /** Sends message */
    private void sendMessage() {
        String msg = writeTextArea.getText();
        sendMessage(msg, true);
    }

    private void sendMessage(String msg, boolean composing) {
        // requests the composing events
        try {
            XEventBuilder eb = new XEventBuilder();
            eb.setIsComposing(composing);
            MessageBuilder mb = new MessageBuilder();
            mb.setToAddress(jid); mb.setBody(msg); mb.setThread(thread);
            mb.setType("chat");
            mb.addExtension(eb.build());
            if (winMan != null)
                ((BSChatWinManager)winMan).sendMessage((Message)mb.build());
        } catch (InstantiationException e) {
            ((BSChatWinManager)winMan).sendMessage(jid, msg, thread);
        }

        addMessageToWindow(msg);
    }

    private void addMessageToWindow(String msg)
    {
        // adds that into the window
        writeTextArea.setText("");
        chatTextPane.append("<" + local + "> ", BSAutoScrollTextPane.MY_NICK_STYLE);
        chatTextPane.append(msg + "\n", BSAutoScrollTextPane.REGULAR_STYLE);

        // stores msg to history
        History.storeMessage(new JID(BSMainFrame.username, BSMainFrame.server, null),
                             jid, local, null, msg);
    }

    /** Returns jid of peer */
    public JID getJID() {
        return jid;
    }

    /** Loads message history */
    protected void loadHistory() {
        Thread t = new Thread() {
            public void run() {
                String history = History.getMessages(
                    new JID(BSMainFrame.username, BSMainFrame.server, null), jid);
                if (history != null) {
                    chatTextPane.clear();
                    chatTextPane.append(history + "\n", BSAutoScrollTextPane.REGULAR_STYLE);
                }
            }
        };
        t.start();
    }


    /** Adds message into chat pane **/
    protected void addMessageIntoChatPane(Message msg, String nick, boolean incoming) {
        if (msg == null) return;

        String body = msg.getBody();
        String timeStamp = null;
        String oobURL = null;
        String oobDesc = null;
        XEvent event = null;
        Enumeration extensions = msg.Extensions();
        while (extensions.hasMoreElements()) {
            Object o = extensions.nextElement();
            if (o instanceof XDelay)
                timeStamp = History.getTimeStamp((XDelay)o);
            else if (o instanceof OOB) {
                oobURL = ((OOB)o).getURL();
                oobDesc = ((OOB)o).getDescription();
            }
            else if (o instanceof XEvent) {
                if (((XEvent)o).getID() != null) {
                    event = (XEvent)o;
                }
            }
        }
        // sets the chat thread
        thread = msg.getThread();
        // handles the message event
        if (event != null && incoming) {
            if (event.getID() == null)
                ;// this is request for sending of message events
            else if (event.isOffline())
                chatTextPane.append("message was stored offline", BSAutoScrollTextPane.PRESENCE_STYLE);
            else if (event.isDelivered())
                chatTextPane.append("message was delivered", BSAutoScrollTextPane.PRESENCE_STYLE);
            else if (event.isDisplayed())
                chatTextPane.append("message was displayed", BSAutoScrollTextPane.PRESENCE_STYLE);
            else if (event.isComposing()) {
                typingLabel.setIcon(typingIcon);
                typingLabel.setToolTipText("buddy is composing a message");
            }
            else {
                typingLabel.setIcon(emptyIcon);
                typingLabel.setToolTipText("buddy cancelled composing of message");
            }
        }
        // if empty message
        if (body == null && oobURL == null && oobDesc == null) {
            // does nothing
        }
        else {
            if (incoming)
                chatTextPane.append("<" + nick + ">", BSAutoScrollTextPane.BUDDY_NICK_STYLE);

⌨️ 快捷键说明

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