1 14 15 package chatserver.client; 16 17 import javax.swing.text.*; 18 import java.awt.Color ; 19 import java.awt.Point ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.logging.*; 23 import javax.swing.*; 24 import javax.swing.event.ListSelectionEvent ; 25 import javax.swing.event.ListSelectionListener ; 26 import java.awt.Graphics ; 27 import java.awt.event.*; 28 29 33 public class ChatWindow extends javax.swing.JFrame { 34 private static Logger logger = Logger.getLogger(ChatWindow.class.getName()); 35 36 private ClassLoader classLoader = getClass().getClassLoader(); 37 public final ImageIcon logo = new ImageIcon( 38 classLoader.getResource("chatserver/client/face-smile.gif")); 39 public final ImageIcon smile = new ImageIcon( 40 classLoader.getResource("chatserver/client/smile.gif")); 41 public final ImageIcon sad = new ImageIcon( 42 classLoader.getResource("chatserver/client/sad.gif")); 43 44 private ChatRoom chatRoom; 45 private LoginDialog loginDialog; 46 private DefaultStyledDocument logDoc = null; 47 private DefaultStyledDocument chatDoc = null; 48 private DefaultListModel userListModel = null; 49 private UserListListener userListListener = null; 50 private Map styleMap = new HashMap (); 51 52 final String NORMALBLUE = "NormalBlue"; 53 final String BOLDBLUE = "BoldBlue"; 54 final String NORMALBLACK = "NormalBlack"; 55 final String ITALICBLACK = "ITALICBLACK"; 56 final String BOLDGREEN = "BoldGreen"; 57 final String NORMALRED = "NormalRed"; 58 final String ITALICRED = "ItalicRed"; 59 60 private InfiniteProgressPanel glassPane; 61 62 63 public ChatWindow(ChatRoom chatRoom, String args[]) { 64 this.chatRoom = chatRoom; 65 setLogDoc(new DefaultStyledDocument()); 66 setChatDoc(new DefaultStyledDocument()); 67 setUserListModel(new DefaultListModel()); 68 prepareAllStyles(); 69 initComponents(); 70 userListListener = new UserListListener(userList); 71 userList.addListSelectionListener(userListListener); 72 loginDialog = new LoginDialog(this, args); 73 } 74 75 private void initComponents() { 76 getContentPane().setLayout(new java.awt.BorderLayout (2, 2)); 77 setTitle("QuickChat - Please Login"); 78 79 buildChatPanel(); 80 buildSendMsg(); 81 buildLogPanel(); 82 buildUserListPanel(); 83 84 jPanel1 = new javax.swing.JPanel (); 86 jPanel1.setLayout(new java.awt.BorderLayout ()); 87 jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH); 89 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 90 chatRoomScrollPane, jScrollPane1); 91 splitPane.setOneTouchExpandable(true); 92 java.awt.Dimension minimumSize = new java.awt.Dimension (500, 20); 93 chatRoomScrollPane.setMinimumSize(minimumSize); 94 splitPane.setDividerLocation(500); 95 splitPane.setResizeWeight(0.9); 96 97 jPanel1.add(splitPane, java.awt.BorderLayout.CENTER); 99 jTabbedPane1 = new javax.swing.JTabbedPane (); 100 jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM); 101 jTabbedPane1.addTab("Chat Room", jPanel1); 102 jTabbedPane1.addTab("Logs", logTextScrollPane); 103 104 getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER); 105 106 buildMenu(); 107 setIconImage(logo.getImage()); 108 109 addWindowListener(new WindowAdapter() { 110 public void windowClosing(WindowEvent e) { 111 glassPane.interrupt(); 112 System.exit(0); 113 } 114 public void windowOpened(WindowEvent e) { 115 } 116 }); 117 pack(); 118 setLocationRelativeTo(null); 119 120 glassPane = new InfiniteProgressPanel("Logging to server.."); 121 setGlassPane(glassPane); 122 } 123 124 private void loginMenuItemActionPerformed(final java.awt.event.ActionEvent evt) { 125 Thread t = new Thread () { 126 public void run() { 127 while(true) { 128 if(login()==false) break; 129 logger.info("Calling login()"); 130 } 131 } 132 }; 133 t.setPriority(Thread.NORM_PRIORITY); 134 t.start(); 135 } 136 137 private boolean login() { 138 loginDialog.clearStatus(); 139 loginDialog.show(); 140 if(loginDialog.isOk()==true) { 141 String r[] = loginDialog.getValues(); 142 glassPane.start(); 143 try { 144 boolean flag = chatRoom.doLogin(r[0], Integer.parseInt(r[1]), 145 r[2], r[3]); 146 147 if(flag==true) { 148 enableChat(true); 149 } else { 150 enableChat(false); 151 return true; } 153 } catch(Exception ex) { 154 enableChat(false); 155 logger.warning("Error logging in : "+ex); 156 return true; 157 } 158 } 159 return false; 160 } 161 162 private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) { 163 Thread t = new Thread () { 164 public void run() { 165 try { 166 chatRoom.doLogout(); 167 } catch(Exception ex) { 168 loginMenuItem.setEnabled(true); 169 logoutMenuItem.setEnabled(false); 170 logger.warning("Error logging in : "+ex); 171 } 172 } 173 }; 174 t.setPriority(Thread.NORM_PRIORITY); 175 t.start(); 176 } 177 178 private void sendTextActionPerformed(java.awt.event.ActionEvent evt) { 179 sendButtonActionPerformed(evt); 180 } 181 182 private void sendPrivateButtonActionPerformed(java.awt.event.ActionEvent evt) { 183 if(userListListener.getSelecteduser()==null) { 184 setResponse("-ERR No User is selected!"); 185 return; 186 } 187 if(sendText.getText().length()==0) { 188 setResponse("-ERR No message to send!"); 189 return; 190 } 191 chatRoom.sendPrivateMessage(userListListener.getSelecteduser()+"@"+chatRoom.getRoom(), 192 sendText.getText()); 193 sendText.setText(""); 194 } 195 196 private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) { 197 if(sendText.getText().length()==0) { 198 setResponse("-ERR No message to send!"); 199 return; 200 } 201 chatRoom.sendMessage(sendText.getText()); 202 sendText.setText(""); 203 } 204 205 206 private javax.swing.JScrollPane chatRoomScrollPane; 208 private javax.swing.JTextPane chatRoomTextPane1; 209 private javax.swing.JMenu jMenu1, jMenu2; 210 private javax.swing.JMenuBar jMenuBar1; 211 private javax.swing.JPanel jPanel1; 212 private javax.swing.JPanel jPanel2; 213 private javax.swing.JScrollPane jScrollPane1; 214 private javax.swing.JTabbedPane jTabbedPane1; 215 private javax.swing.JTextPane logTextPane; 216 private javax.swing.JScrollPane logTextScrollPane; 217 private javax.swing.JMenuItem loginMenuItem; 218 private javax.swing.JMenuItem logoutMenuItem; 219 private javax.swing.JMenuItem exitMenuItem; 220 private javax.swing.JMenuItem changeRoomMenuItem; 221 private javax.swing.JMenuItem updateUserListMenuItem; 222 private javax.swing.JMenuItem clearMenuItem; 223 private javax.swing.JMenuItem aboutMenuItem; 224 private javax.swing.JButton sendButton; 225 private javax.swing.JButton sendPrivateButton; 226 private javax.swing.JTextField sendText; 227 private javax.swing.JList userList; 228 230 public void log(String msg) { 231 logger.fine("Got: "+msg); 232 try { 233 AttributeSet style = (AttributeSet)styleMap.get(NORMALBLACK); 234 getLogDoc().insertString(getLogDoc().getLength(), 235 msg + "\n" ,style); 236 237 Point pt2 = new Point ((int)(0), 239 (int)(logTextPane.getBounds().getHeight())); 240 logTextScrollPane.getViewport().setViewPosition(pt2); 241 } catch(Exception e) { 242 logger.warning("Error: "+e); 243 } 244 } 245 246 public DefaultStyledDocument getLogDoc() { 247 return logDoc; 248 } 249 250 public void setLogDoc(DefaultStyledDocument logDoc) { 251 this.logDoc = logDoc; 252 } 253 254 255 256 public void prepareAllStyles() { 257 SimpleAttributeSet aset = new SimpleAttributeSet(); 258 StyleConstants.setForeground(aset,Color.blue); 259 StyleConstants.setFontSize(aset,12); 260 StyleConstants.setFontFamily(aset,"Verdana"); 261 styleMap.put(NORMALBLUE,aset); 262 263 aset = new SimpleAttributeSet(); 264 StyleConstants.setForeground(aset,Color.blue); 265 StyleConstants.setFontSize(aset,12); 266 StyleConstants.setFontFamily(aset,"Verdana"); 267 StyleConstants.setBold(aset, true); 268 styleMap.put(BOLDBLUE,aset); 269 270 aset = new SimpleAttributeSet(); 271 StyleConstants.setForeground(aset,Color.black); 272 StyleConstants.setFontSize(aset,12); 273 StyleConstants.setFontFamily(aset,"Verdana"); 274 styleMap.put(NORMALBLACK,aset); 275 276 aset = new SimpleAttributeSet(); 277 StyleConstants.setForeground(aset,Color.black); 278 StyleConstants.setFontSize(aset,12); 279 StyleConstants.setFontFamily(aset,"Verdana"); 280 StyleConstants.setItalic(aset, true); 281 styleMap.put(ITALICBLACK,aset); 282 283 aset = new SimpleAttributeSet(); 284 StyleConstants.setForeground(aset, new Color (0, 128, 0)); 285 StyleConstants.setFontSize(aset,12); 286 StyleConstants.setFontFamily(aset,"Verdana"); 287 StyleConstants.setBold(aset, true); 288 styleMap.put(BOLDGREEN,aset); 289 290 aset = new SimpleAttributeSet(); 291 StyleConstants.setForeground(aset,Color.red); 292 StyleConstants.setFontSize(aset,12); 293 StyleConstants.setFontFamily(aset,"Verdana"); 294 styleMap.put(NORMALRED,aset); 295 296 aset = new SimpleAttributeSet(); 297 StyleConstants.setForeground(aset,Color.red); 298 StyleConstants.setFontSize(aset,12); 299 StyleConstants.setFontFamily(aset,"Verdana"); 300 StyleConstants.setItalic(aset, true); 301 styleMap.put(ITALICRED,aset); 302 } 303 304 public void addChatMessage(String message) { 305 logger.fine("Got: "+message); 306 if(message.startsWith("{system.help} ")) { 307 return; 308 } else if(message.startsWith("{system.debug} ")) { 309 return; 311 } 312 313 AttributeSet style = null; 314 String fromid = null; 315 String toid = null; 316 317 String msgType = null; 318 try { 319 if(message.startsWith("{system.msg} ")) { 320 msgType = "{system.msg}"; 321 message = message.substring(13); 322 style = (AttributeSet)styleMap.get(BOLDBLUE); 323 } else if(message.startsWith("{system.error} ")) { 324 msgType = "{system.error}"; 325 message = "Error: "+message.substring(15); 326 style = (AttributeSet)styleMap.get(NORMALRED); 327 } else if(message.startsWith("{user.msg:")) { 328 msgType = "{user.msg}"; 329 int j = message.indexOf(":", 10); int i = message.indexOf("} ", 10); 331 if(j!=-1) { 332 toid = message.substring(j+1, i); 333 } else { 334 j = i; 335 } 336 fromid = message.substring(10, j); 337 message = message.substring(i+2); 338 style = (AttributeSet)styleMap.get(NORMALBLUE); 339 } else if(message.startsWith("{msg.user:")) { msgType = "{msg.user}"; 341 int i = message.indexOf("} ", 10); 342 toid = message.substring(10, i); 343 message = message.substring(i+2); 344 style = (AttributeSet)styleMap.get(NORMALBLUE); 345 } else if(message.startsWith("{user.info:")) { 346 msgType = "{user.info}"; 347 int i = message.indexOf("} ", 11); 348 fromid = message.substring(11, i); 349 message = message.substring(i+2); 350 if(message.equals("LoggedIn")) { 351 addToUserList(fromid); 352 message = "joined the room"; 353 } else if(message.equals("LoggedOut")) { 354 removeFromUserList(fromid); 355 message = "left the room"; 356 } else 357 System.out.println("Unknown ->"+message+"<-"); 358 style = (AttributeSet)styleMap.get(ITALICBLACK); 359 } else { 360 msgType = "{unknown}"; 361 style = (AttributeSet)styleMap.get(NORMALBLACK); 362 } 363 364 365 if(msgType.equals("{user.msg}")) { 366 toid = removeRoom(toid); 367 fromid = removeRoom(fromid); 368 if(toid!=null && toid.length()==0) { getChatDoc().insertString(getChatDoc().getLength(), 370 fromid+": ", (AttributeSet)styleMap.get(NORMALRED)); 371 } else if(toid!=null) { 372 getChatDoc().insertString(getChatDoc().getLength(), 373 "PrvMsg From "+fromid+": ", (AttributeSet)styleMap.get(BOLDBLUE)); 374 } 375 } else if(msgType.equals("{msg.user}")) { 376 toid = removeRoom(toid); 377 getChatDoc().insertString(getChatDoc().getLength(), 378 "PrvMsg To "+toid+": ", (AttributeSet)styleMap.get(BOLDBLUE)); 379 } else if(msgType.equals("{user.info}")) { 380 fromid = removeRoom(fromid); 381 getChatDoc().insertString(getChatDoc().getLength(), 382 fromid+": ", (AttributeSet)styleMap.get(NORMALRED)); 383 } 384 385 if(message.indexOf(":-)")==-1 && message.indexOf(":-(")==-1) { 386 getChatDoc().insertString(getChatDoc().getLength(), message+ "\n", style); 387 } else { 388 checkForSmile(message, style); 389 getChatDoc().insertString(getChatDoc().getLength(), "\n", style); 390 } 391 392 Point pt1 = chatRoomTextPane1.getLocation(); 393 Point pt2 = new Point ((int)(0), 394 (int)(chatRoomTextPane1.getBounds().getHeight())); 395 chatRoomScrollPane.getViewport().setViewPosition(pt2); 396 } catch(Exception e) { 397 logger.warning("Error: "+e); 398 } 399 400 toFront(); 401 } 402 403 protected void setEndSelection() { 404 int len = chatRoomTextPane1.getDocument().getLength(); 405 chatRoomTextPane1.setSelectionStart(len); 406 chatRoomTextPane1.setSelectionEnd(len); 407 } 408 409 public DefaultStyledDocument getChatDoc() { 410 return chatDoc; 411 } 412 413 public void setChatDoc(DefaultStyledDocument chatDoc) { 414 this.chatDoc = chatDoc; 415 } 416 417 public void setResponse(String res) { 418 int msgType = JOptionPane.PLAIN_MESSAGE ; 419 if(res.startsWith("+OK")) 420 msgType = JOptionPane.INFORMATION_MESSAGE; 421 if(res.startsWith("-ERR")) 422 msgType = JOptionPane.ERROR_MESSAGE; 423 toFront(); 424 JOptionPane.showMessageDialog(this, 425 res.substring(res.indexOf(" ")+1), "Response", msgType); 426 } 427 428 public void enableChat(boolean flag) { 429 sendText.setEnabled(flag); 430 sendButton.setEnabled(flag); 431 sendPrivateButton.setEnabled(flag); 432 loginMenuItem.setEnabled(!flag); 433 logoutMenuItem.setEnabled(flag); 434 changeRoomMenuItem.setEnabled(flag); 435 updateUserListMenuItem.setEnabled(flag); 436 if(flag==false) { 437 userListModel.clear(); 438 setTitle("QuickChat - Not Connected"); 439 } else { 440 chatRoomTextPane1.setText(""); 441 chatRoom.processReceivedMsg(); 442 } 443 glassPane.stop(); 444 glassPane.setVisible(false); 445 } 446 447 public void addToUserList(String id) { 448 logger.fine("Got: "+id); 449 id = removeRoom(id); 450 getUserListModel().addElement(id); 451 } 452 453 public void removeFromUserList(String id) { 454 logger.fine("Got: "+id); 455 id = removeRoom(id); 456 getUserListModel().removeElement(id); 457 } 458 459 public DefaultListModel getUserListModel() { 460 return userListModel; 461 } 462 463 public void setUserListModel(DefaultListModel userListModel) { 464 this.userListModel = userListModel; 465 } 466 467 private String removeRoom(String id) { 468 if(id==null) return id; 469 if( id.endsWith("@"+chatRoom.getRoom()) ) { 470 id = id.substring(0, id.indexOf("@"+chatRoom.getRoom()) ); 471 } 472 return id; 473 } 474 475 private void about() { 476 JOptionPane.showMessageDialog(this, 477 "QuickChat v 1.0\n\n"+ 478 "GUI Client for ChatServer example of QuickServer.\n"+ 479 "This is compliant with QuickServer v1.4.5 release.\n\n"+ 480 "Copyright (C) 2005 QuickServer.org\n"+ 481 "http://www.quickserver.org", 482 "About QuickChat", 483 JOptionPane.INFORMATION_MESSAGE, 484 logo); 485 } 486 487 private void changeRoom() { 488 String newRoom = (String ) JOptionPane.showInputDialog(this, 489 "Chat Room:", 490 "Change Room", JOptionPane.PLAIN_MESSAGE, logo, 491 null, chatRoom.getRoom()); 492 if(newRoom==null) return; 493 chatRoom.changeRoom(newRoom); 494 userListModel.clear(); 495 chatRoom.updateUserList(); 496 } 497 498 private void buildChatPanel() { 500 chatRoomTextPane1 = new JTextPane(getChatDoc()); 501 chatRoomTextPane1.setEditable(false); 503 chatRoomTextPane1.setMinimumSize(new java.awt.Dimension (500, 200)); 504 chatRoomTextPane1.setPreferredSize(new java.awt.Dimension (500, 300)); 505 506 chatRoomScrollPane = new JScrollPane(chatRoomTextPane1); 507 chatRoomScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 508 } 509 510 private void buildSendMsg() { 511 jPanel2 = new javax.swing.JPanel (); 512 jPanel2.setLayout(new java.awt.GridBagLayout ()); 513 jPanel2.setMinimumSize(new java.awt.Dimension (373, 40)); 514 jPanel2.setPreferredSize(new java.awt.Dimension (373, 50)); 515 516 sendText = new javax.swing.JTextField (); 517 sendText.setEnabled(false); 518 sendText.addActionListener(new java.awt.event.ActionListener () { 519 public void actionPerformed(final java.awt.event.ActionEvent evt) { 520 java.awt.EventQueue.invokeLater(new Runnable () { 521 public void run() { 522 sendTextActionPerformed(evt); 523 } 524 }); 525 } 526 }); 527 528 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 529 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 530 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 531 gridBagConstraints.weightx = 1.0; 532 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 2); 533 jPanel2.add(sendText, gridBagConstraints); 534 535 sendButton = new javax.swing.JButton (); 536 sendButton.setText("Send"); 537 sendButton.setEnabled(false); 538 sendButton.addActionListener(new java.awt.event.ActionListener () { 539 public void actionPerformed(final java.awt.event.ActionEvent evt) { 540 java.awt.EventQueue.invokeLater(new Runnable () { 541 public void run() { 542 sendButtonActionPerformed(evt); 543 } 544 }); 545 } 546 }); 547 548 gridBagConstraints = new java.awt.GridBagConstraints (); 549 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 2); 550 jPanel2.add(sendButton, gridBagConstraints); 551 552 sendPrivateButton = new javax.swing.JButton (); 553 sendPrivateButton.setText("Private Mesage"); 554 sendPrivateButton.setEnabled(false); 555 sendPrivateButton.addActionListener(new java.awt.event.ActionListener () { 556 public void actionPerformed(final java.awt.event.ActionEvent evt) { 557 java.awt.EventQueue.invokeLater(new Runnable () { 558 public void run() { 559 sendPrivateButtonActionPerformed(evt); 560 } 561 }); 562 } 563 }); 564 565 gridBagConstraints = new java.awt.GridBagConstraints (); 566 gridBagConstraints.insets = new java.awt.Insets (0, 2, 0, 2); 567 jPanel2.add(sendPrivateButton, gridBagConstraints); 568 } 569 570 private void buildLogPanel() { 571 logTextScrollPane = new javax.swing.JScrollPane (); 572 logTextScrollPane.setMinimumSize(new java.awt.Dimension (24, 50)); 573 logTextScrollPane.setPreferredSize(new java.awt.Dimension (11, 50)); 574 575 logTextPane = new JTextPane(getLogDoc()); 576 logTextPane.setEditable(false); 578 logTextScrollPane.setViewportView(logTextPane); 579 } 580 581 private void buildUserListPanel() { 582 jScrollPane1 = new javax.swing.JScrollPane (); 583 jScrollPane1.setPreferredSize(new java.awt.Dimension (70, 132)); 584 585 userList = new javax.swing.JList (getUserListModel()); 586 userList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 588 jScrollPane1.setViewportView(userList); 589 } 590 591 private void buildMenu() { 592 jMenuBar1 = new javax.swing.JMenuBar (); 593 jMenu1 = new javax.swing.JMenu (); 594 jMenu1.setText("Chat"); 595 596 loginMenuItem = new JMenuItem("Login..."); 597 loginMenuItem.addActionListener(new java.awt.event.ActionListener () { 598 public void actionPerformed(java.awt.event.ActionEvent evt) { 599 loginMenuItemActionPerformed(evt); 600 } 601 }); 602 603 jMenu1.add(loginMenuItem); 604 605 606 JMenu optionsjMenu = new javax.swing.JMenu (); 607 optionsjMenu.setText("Options"); 608 609 updateUserListMenuItem = new JMenuItem("Update UserList"); 610 updateUserListMenuItem.setEnabled(false); 611 updateUserListMenuItem.addActionListener(new java.awt.event.ActionListener () { 612 public void actionPerformed(java.awt.event.ActionEvent evt) { 613 java.awt.EventQueue.invokeLater(new Runnable () { 614 public void run() { 615 updateUserList(); 616 } 617 }); 618 } 619 }); 620 optionsjMenu.add(updateUserListMenuItem); 621 622 changeRoomMenuItem = new JMenuItem("Change Room..."); 623 changeRoomMenuItem.setEnabled(false); 624 changeRoomMenuItem.addActionListener(new java.awt.event.ActionListener () { 625 public void actionPerformed(java.awt.event.ActionEvent evt) { 626 java.awt.EventQueue.invokeLater(new Runnable () { 627 public void run() { 628 changeRoom(); 629 } 630 }); 631 } 632 }); 633 optionsjMenu.add(changeRoomMenuItem); 634 635 636 logoutMenuItem = new JMenuItem("Logout"); 637 logoutMenuItem.setEnabled(false); 638 logoutMenuItem.addActionListener(new java.awt.event.ActionListener () { 639 public void actionPerformed(java.awt.event.ActionEvent evt) { 640 logoutMenuItemActionPerformed(evt); 641 } 642 }); 643 jMenu1.add(logoutMenuItem); 644 645 exitMenuItem = new JMenuItem("Exit"); 646 exitMenuItem.addActionListener(new java.awt.event.ActionListener () { 647 public void actionPerformed(java.awt.event.ActionEvent evt) { 648 System.exit(0); 649 } 650 }); 651 jMenu1.add(exitMenuItem); 652 653 clearMenuItem = new JMenuItem("Clear Chat"); 654 clearMenuItem.setEnabled(true); 655 clearMenuItem.addActionListener(new java.awt.event.ActionListener () { 656 public void actionPerformed(java.awt.event.ActionEvent evt) { 657 chatRoomTextPane1.setText(""); 658 logTextPane.setText(""); 659 } 660 }); 661 optionsjMenu.add(clearMenuItem); 662 663 jMenu2 = new javax.swing.JMenu (); 664 jMenu2.setText("Help"); 665 666 aboutMenuItem = new JMenuItem("About..."); 667 aboutMenuItem.setEnabled(true); 668 aboutMenuItem.addActionListener(new java.awt.event.ActionListener () { 669 public void actionPerformed(java.awt.event.ActionEvent evt) { 670 java.awt.EventQueue.invokeLater(new Runnable () { 671 public void run() { 672 about(); 673 } 674 }); 675 } 676 }); 677 jMenu2.add(aboutMenuItem); 678 679 jMenuBar1.add(jMenu1); 680 jMenuBar1.add(optionsjMenu); 681 jMenuBar1.add(jMenu2); 682 683 setJMenuBar(jMenuBar1); 684 } 685 686 private boolean checkForSmile(String message, AttributeSet style) throws BadLocationException { 687 if(message.length()==0) return false; 688 int loc = message.indexOf(":-)"); 689 690 int start = 0; 691 String temp = null; 692 while(loc!=-1) { 693 if(loc!=start) { 694 temp = message.substring(start, loc); 695 if(checkForSad(temp, style)==false) { 696 getChatDoc().insertString(getChatDoc().getLength(), temp, style); 697 } 698 } 699 setEndSelection(); 700 chatRoomTextPane1.insertIcon(smile); 701 loc = loc+3; 702 start = loc; 703 if(loc>=message.length()) break; 704 loc = message.indexOf(":-)", start); 705 } 706 if(start<message.length()) { 707 temp = message.substring(start, message.length()); 708 if(checkForSad(temp, style)==false) { 709 getChatDoc().insertString(getChatDoc().getLength(), temp, style); 710 } 711 } 712 return true; 713 } 714 715 private boolean checkForSad(String message, AttributeSet style) throws BadLocationException { 716 int loc = message.indexOf(":-("); 717 if(message.length()==0) return false; 718 719 int start = 0; 720 String temp = null; 721 while(loc!=-1) { 722 if(loc!=start) { 723 temp = message.substring(start, loc); 724 getChatDoc().insertString(getChatDoc().getLength(), temp, style); 725 } 726 setEndSelection(); 727 chatRoomTextPane1.insertIcon(sad); 728 loc = loc+3; 729 start = loc; 730 if(loc>=message.length()) break; 731 loc = message.indexOf(":-(", start); 732 } 733 if(start<message.length()) { 734 temp = message.substring(start, message.length()); 735 getChatDoc().insertString(getChatDoc().getLength(), temp, style); 736 } 737 return true; 738 } 739 740 private void updateUserList() { 741 java.awt.EventQueue.invokeLater(new Runnable () { 742 public void run() { 743 chatRoom.updateUserList(); 744 userListModel.clear(); 745 } 746 }); 747 } 748 } 749 750 class UserListListener implements ListSelectionListener { 751 private String userSelected = null; 752 private JList list; 753 754 public UserListListener(JList list) { 755 this.list = list; 756 } 757 public String getSelecteduser() { 758 return userSelected; 759 } 760 public void valueChanged(ListSelectionEvent e) { 761 if(e.getValueIsAdjusting() == false) { 762 int index = list.getSelectedIndex(); 763 if(index==-1) 764 userSelected = null; 765 else 766 userSelected = list.getSelectedValue().toString(); 767 } 768 } 769 } 770 | Popular Tags |