📄 chatwindow.java
字号:
getChatDoc().insertString(getChatDoc().getLength(), message+ "\n", style);
} else {
checkForSmile(message, style);
getChatDoc().insertString(getChatDoc().getLength(), "\n", style);
}
Point pt1 = chatRoomTextPane1.getLocation();
Point pt2 = new Point((int)(0),
(int)(chatRoomTextPane1.getBounds().getHeight()));
chatRoomScrollPane.getViewport().setViewPosition(pt2);
} catch(Exception e) {
logger.warning("Error: "+e);
}
toFront();
}
protected void setEndSelection() {
int len = chatRoomTextPane1.getDocument().getLength();
chatRoomTextPane1.setSelectionStart(len);
chatRoomTextPane1.setSelectionEnd(len);
}
public DefaultStyledDocument getChatDoc() {
return chatDoc;
}
public void setChatDoc(DefaultStyledDocument chatDoc) {
this.chatDoc = chatDoc;
}
public void setResponse(String res) {
int msgType = JOptionPane.PLAIN_MESSAGE ;
if(res.startsWith("+OK"))
msgType = JOptionPane.INFORMATION_MESSAGE;
if(res.startsWith("-ERR"))
msgType = JOptionPane.ERROR_MESSAGE;
toFront();
JOptionPane.showMessageDialog(this,
res.substring(res.indexOf(" ")+1), "Response", msgType);
}
public void enableChat(boolean flag) {
sendText.setEnabled(flag);
sendButton.setEnabled(flag);
sendPrivateButton.setEnabled(flag);
loginMenuItem.setEnabled(!flag);
logoutMenuItem.setEnabled(flag);
changeRoomMenuItem.setEnabled(flag);
updateUserListMenuItem.setEnabled(flag);
if(flag==false) {
userListModel.clear();
setTitle("QuickChat - Not Connected");
} else {
chatRoomTextPane1.setText("");
chatRoom.processReceivedMsg();
}
glassPane.stop();
glassPane.setVisible(false);
}
public void addToUserList(String id) {
logger.fine("Got: "+id);
id = removeRoom(id);
getUserListModel().addElement(id);
}
public void removeFromUserList(String id) {
logger.fine("Got: "+id);
id = removeRoom(id);
getUserListModel().removeElement(id);
}
public DefaultListModel getUserListModel() {
return userListModel;
}
public void setUserListModel(DefaultListModel userListModel) {
this.userListModel = userListModel;
}
private String removeRoom(String id) {
if(id==null) return id;
if( id.endsWith("@"+chatRoom.getRoom()) ) {
id = id.substring(0, id.indexOf("@"+chatRoom.getRoom()) );
}
return id;
}
private void about() {
JOptionPane.showMessageDialog(this,
"QuickChat v 1.0\n\n"+
"GUI Client for ChatServer example of QuickServer.\n"+
"This is compliant with QuickServer v1.4.5 release.\n\n"+
"Copyright (C) 2005 QuickServer.org\n"+
"http://www.quickserver.org",
"About QuickChat",
JOptionPane.INFORMATION_MESSAGE,
logo);
}
private void changeRoom() {
String newRoom = (String) JOptionPane.showInputDialog(this,
"Chat Room:",
"Change Room", JOptionPane.PLAIN_MESSAGE, logo,
null, chatRoom.getRoom());
if(newRoom==null) return;
chatRoom.changeRoom(newRoom);
userListModel.clear();
chatRoom.updateUserList();
}
//-- build gui
private void buildChatPanel() {
chatRoomTextPane1 = new JTextPane(getChatDoc());
//chatRoomTextPane1.setDocument(getChatDoc());
chatRoomTextPane1.setEditable(false);
chatRoomTextPane1.setMinimumSize(new java.awt.Dimension(500, 200));
chatRoomTextPane1.setPreferredSize(new java.awt.Dimension(500, 300));
chatRoomScrollPane = new JScrollPane(chatRoomTextPane1);
chatRoomScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
private void buildSendMsg() {
jPanel2 = new javax.swing.JPanel();
jPanel2.setLayout(new java.awt.GridBagLayout());
jPanel2.setMinimumSize(new java.awt.Dimension(373, 40));
jPanel2.setPreferredSize(new java.awt.Dimension(373, 50));
sendText = new javax.swing.JTextField();
sendText.setEnabled(false);
sendText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
sendTextActionPerformed(evt);
}
});
}
});
java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
jPanel2.add(sendText, gridBagConstraints);
sendButton = new javax.swing.JButton();
sendButton.setText("Send");
sendButton.setEnabled(false);
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
sendButtonActionPerformed(evt);
}
});
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
jPanel2.add(sendButton, gridBagConstraints);
sendPrivateButton = new javax.swing.JButton();
sendPrivateButton.setText("Private Mesage");
sendPrivateButton.setEnabled(false);
sendPrivateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
sendPrivateButtonActionPerformed(evt);
}
});
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
jPanel2.add(sendPrivateButton, gridBagConstraints);
}
private void buildLogPanel() {
logTextScrollPane = new javax.swing.JScrollPane();
logTextScrollPane.setMinimumSize(new java.awt.Dimension(24, 50));
logTextScrollPane.setPreferredSize(new java.awt.Dimension(11, 50));
logTextPane = new JTextPane(getLogDoc());
//logTextPane.setDocument(getLogDoc());
logTextPane.setEditable(false);
logTextScrollPane.setViewportView(logTextPane);
}
private void buildUserListPanel() {
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane1.setPreferredSize(new java.awt.Dimension(70, 132));
userList = new javax.swing.JList(getUserListModel());
//userList.setModel(getUserListModel());
userList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(userList);
}
private void buildMenu() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu1.setText("Chat");
loginMenuItem = new JMenuItem("Login...");
loginMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginMenuItemActionPerformed(evt);
}
});
jMenu1.add(loginMenuItem);
JMenu optionsjMenu = new javax.swing.JMenu();
optionsjMenu.setText("Options");
updateUserListMenuItem = new JMenuItem("Update UserList");
updateUserListMenuItem.setEnabled(false);
updateUserListMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
updateUserList();
}
});
}
});
optionsjMenu.add(updateUserListMenuItem);
changeRoomMenuItem = new JMenuItem("Change Room...");
changeRoomMenuItem.setEnabled(false);
changeRoomMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
changeRoom();
}
});
}
});
optionsjMenu.add(changeRoomMenuItem);
logoutMenuItem = new JMenuItem("Logout");
logoutMenuItem.setEnabled(false);
logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
logoutMenuItemActionPerformed(evt);
}
});
jMenu1.add(logoutMenuItem);
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
});
jMenu1.add(exitMenuItem);
clearMenuItem = new JMenuItem("Clear Chat");
clearMenuItem.setEnabled(true);
clearMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chatRoomTextPane1.setText("");
logTextPane.setText("");
}
});
optionsjMenu.add(clearMenuItem);
jMenu2 = new javax.swing.JMenu();
jMenu2.setText("Help");
aboutMenuItem = new JMenuItem("About...");
aboutMenuItem.setEnabled(true);
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
about();
}
});
}
});
jMenu2.add(aboutMenuItem);
jMenuBar1.add(jMenu1);
jMenuBar1.add(optionsjMenu);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
}
private boolean checkForSmile(String message, AttributeSet style) throws BadLocationException {
if(message.length()==0) return false;
int loc = message.indexOf(":-)");
int start = 0;
String temp = null;
while(loc!=-1) {
if(loc!=start) {
temp = message.substring(start, loc);
if(checkForSad(temp, style)==false) {
getChatDoc().insertString(getChatDoc().getLength(), temp, style);
}
}
setEndSelection();
chatRoomTextPane1.insertIcon(smile);
loc = loc+3;
start = loc;
if(loc>=message.length()) break;
loc = message.indexOf(":-)", start);
}
if(start<message.length()) {
temp = message.substring(start, message.length());
if(checkForSad(temp, style)==false) {
getChatDoc().insertString(getChatDoc().getLength(), temp, style);
}
}
return true;
}
private boolean checkForSad(String message, AttributeSet style) throws BadLocationException {
int loc = message.indexOf(":-(");
if(message.length()==0) return false;
int start = 0;
String temp = null;
while(loc!=-1) {
if(loc!=start) {
temp = message.substring(start, loc);
getChatDoc().insertString(getChatDoc().getLength(), temp, style);
}
setEndSelection();
chatRoomTextPane1.insertIcon(sad);
loc = loc+3;
start = loc;
if(loc>=message.length()) break;
loc = message.indexOf(":-(", start);
}
if(start<message.length()) {
temp = message.substring(start, message.length());
getChatDoc().insertString(getChatDoc().getLength(), temp, style);
}
return true;
}
private void updateUserList() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
chatRoom.updateUserList();
userListModel.clear();
}
});
}
}
class UserListListener implements ListSelectionListener {
private String userSelected = null;
private JList list;
public UserListListener(JList list) {
this.list = list;
}
public String getSelecteduser() {
return userSelected;
}
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting() == false) {
int index = list.getSelectedIndex();
if(index==-1)
userSelected = null;
else
userSelected = list.getSelectedValue().toString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -