📄 chatdisplay.java
字号:
/**
* ChatDisplay.java
* @author Naizheng Bian
*/
package clientPackage;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import mediaPackage.*;
/**
* ChatDisplay is an user interface to display chat messages.
* User can add and remove a chat user, display a message in a ChatDisplay
* or send a message to the ChatDisplay. If there is an error, nothing happens.
*/
public class ChatDisplay extends JPanel implements Serializable, ActionListener, ChangeListener {
/** indicate the default user, who is sending messages */
private String mySelf;
/**manages the PropertyChangeListener and informs them on change of property */
private PropertyChangeSupport propertySupport;
/** main panel that contains the complete ChatDisplay */
private JPanel interfacePanel;
/** user panel mainly containing user combo box */
private JPanel userPanel;
/** the label "user" for the user selection combo box */
private JLabel userLabel;
/** combo box containing list of users */
private JComboBox userComboBox;
/** message panel that contains the components required to send messages */
private JPanel messagePanel;
/**send message text box label */
private JLabel messageLabel;
/** send message button */
private JButton sendButton;
/** textfield contains the message that the user wants to send */
private JTextField messageText;
/** tabbed pane includes public and private chat */
private JTabbedPane textTabbedPane;
/** The scroll pane contain the public pane */
private JScrollPane publicScrollPane;
/** The public chat pane */
private JTextPane publicTextPane;
/** The public text message */
private String publicText;
/** The scroll pane containg private chat pane */
private JScrollPane privateScrollPane;
/** The text pane containing private messages */
private JTextPane privateTextPane;
/** The private chat message*/
private String privateText;
/** type of the pane public or private chat */
private int paneType;
/** current user's Category */
private int myCategory;
/** user vector */
private Vector usersVector;
/**
* Constructor to create a new ChatDisplay
* @param userName The name of the user
* @param myCategory The category of the user
*/
public ChatDisplay(String userName, int myCategory) {
propertySupport = new PropertyChangeSupport (this);
initComponents();
mySelf = userName;
this.myCategory = myCategory;
usersVector = new Vector();
}
/**
* initialize the components of the ChatDisplay
*/
private void initComponents() {
interfacePanel = new JPanel();
userPanel = new JPanel();
userLabel = new JLabel();
userComboBox = new JComboBox();
messagePanel = new JPanel();
messageLabel = new JLabel();
sendButton = new JButton();
messageText = new JTextField();
textTabbedPane = new JTabbedPane();
publicScrollPane = new JScrollPane();
publicTextPane = new JTextPane();
publicText = new String("");
privateScrollPane = new JScrollPane();
privateTextPane = new JTextPane();
privateText = new String("");
setLayout(new BorderLayout());
interfacePanel.setLayout(new GridLayout(2, 1));
userPanel.setLayout(new BorderLayout());
userLabel.setText("User:");
userPanel.add(userLabel, BorderLayout.WEST);
userPanel.add(userComboBox, BorderLayout.CENTER);
interfacePanel.add(userPanel);
messagePanel.setLayout(new BorderLayout());
messageLabel.setText("Message:");
messagePanel.add(messageLabel, BorderLayout.WEST);
sendButton.setText("Send");
messagePanel.add(sendButton, BorderLayout.EAST);
sendButton.addActionListener(this);
messagePanel.add(messageText, BorderLayout.CENTER);
messageText.addActionListener(this);
interfacePanel.add(messagePanel);
add(interfacePanel, BorderLayout.SOUTH);
textTabbedPane.setTabPlacement(3);
publicTextPane.setEditable(false);
publicScrollPane.setViewportView(publicTextPane);
textTabbedPane.addTab("Public", publicScrollPane);
privateTextPane.setEditable(false);
privateTextPane.setName("rohit");
privateScrollPane.setViewportView(privateTextPane);
textTabbedPane.addTab("Private", privateScrollPane);
textTabbedPane.addChangeListener(this);
add(textTabbedPane, BorderLayout.CENTER);
paneType = Constants.PUBLIC_CHAT;
enableComponents();
}
/**
* receives messages from server
*
* @param v The message vector containing all the information about the message
*/
public void receiveMessage(Vector v) {
/**
* This is the message sent from the chatDisplay
*/
String msg = (String) v.get(0);
String fromName = (String) v.get(1);
String toName = (String) v.get(2); // receiver's name
int msgType = ((Integer) v.get(3)).intValue();// type of the message (public or private)
/* boolean flag = verifyUserName(fromName);
if (!flag) {
return;
}*/
// publicText+="testString\n";
if (!(toName.equals("") || toName.equals(mySelf))) return;
switch (msgType) {
case Constants.PUBLIC_CHAT:
publicText = publicText + fromName + ": " + msg + "\n";
publicTextPane.setText(publicText);
break;
case Constants.PRIVATE_CHAT:
privateText = privateText + fromName + ": " + msg + "\n";
privateTextPane.setText(privateText);
break;
}this.show();
}
/**
* adds a user to the user list
*
* @param userName The name of the user to be added
* @param userCategory The category of the user
* @return Returns a boolean indicating if the user is successfully added or not
*/
public boolean addUser(String userName, int userCategory) {
if (verifyUserName(userName)) {
return false;
}
userComboBox.addItem(makeObj(userName));
enableComponents();
if (!userName.equals(mySelf)) usersVector.addElement(userName);
return true;
}
/**
* adds a presenter
* @param userName The name of the presenter
*/
public void addPresenter(String userName) {
if (userName.equals(mySelf)) return;
userComboBox.removeAll();
userComboBox.addItem(makeObj(userName));
enableComponents();
}
/**
* removes a user from the user list
*
* @param userName The user to be removed from the list
*/
public void removeUser(String userName) {
// The number of users present
int num = userComboBox.getItemCount();
//Index variable to retrieve one user at a time from the combo box
int i = 0;
for (i = 0; i < num; i++) {
if (userName.equals(userComboBox.getItemAt(i).toString())) {
break;
}
}
if (i != num) userComboBox.removeItemAt(i);
usersVector.remove(userName);
enableComponents();
}
/**
* Verify if the user is present or not
*
* @param userName The name of the user to be verified
* @return true if the user is present in the list
* false if the user is not present in the list
*/
private boolean verifyUserName(String userName) {
int i = 0;
for (i = 0; i < usersVector.size(); i++) {
if (userName.equals(usersVector.elementAt(i))) {
return true;
}
}
return false;
}
/**
* sends the message to the Chat Network
*
* @param msg The message to be sent
*/
private void sendMessage(String msg) {
Vector v = new Vector();
v.addElement(msg);
v.addElement(mySelf);
switch (paneType) {
case Constants.PUBLIC_CHAT:
v.addElement("");
publicText = publicText + mySelf + ": " + msg + "\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -