1 29 38 39 package org.planetamessenger.mos.ui; 40 41 import org.planetamessenger.mos.forms.*; 42 import org.planetamessenger.mos.engine.*; 43 import org.planetamessenger.plugin.*; 44 import java.util.*; 45 import java.awt.*; 46 import javax.swing.*; 47 import javax.swing.border.*; 48 49 50 public class JContactList extends JList implements JContactListInterface, java.awt.event.MouseListener , java.awt.event.ActionListener { 51 52 ArrayList<JMenuListener> menuListener = null; 53 JContactListModel listModel = null; 54 JPopupMenu popMenu = null; 55 JMenuItem messageItem = null; 56 JMenuItem contactDetailsItem = null; 57 JMenuItem updateNameItem = null; 58 JMenuItem removeContactItem = null; 59 JMenuItem renameContactItem = null; 60 JMenuItem blockUnblockItem = null; 61 JMenuItem addContactToListItem = null; 62 63 64 68 public JContactList() { 69 70 super(); 71 72 addMouseListener( this ); 73 74 menuListener = new ArrayList<JMenuListener>(); 75 listModel = new JContactListModel(); 76 setModel( listModel ); 77 getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); 78 79 JContactListItem onlineItem = new JContactListItem( -1, JSharedObjects.getLanguageManager().getStringEx( "ONLINE" ), JSharedObjects.getLanguageManager().getStringEx( "ONLINE" ) ); 80 JContactListItem offlineItem = new JContactListItem( -2, JSharedObjects.getLanguageManager().getStringEx( "OFFLINE" ), JSharedObjects.getLanguageManager().getStringEx( "OFFLINE" ) ); 81 82 onlineItem.setStatus( org.planetamessenger.plugin.JPlugin.STATUS_ONLINE ); 83 offlineItem.setStatus( org.planetamessenger.plugin.JPlugin.STATUS_OFFLINE ); 84 85 addItem( onlineItem ); 86 addItem( offlineItem ); 87 88 popMenu = new JPopupMenu(); 89 90 messageItem = new JMenuItem(); 91 messageItem.setName( "message" ); 92 messageItem.setFont( JSystemFonts.FONT ); 93 messageItem.setIcon( JSharedObjects.getResources().getIcon( JResources.MESSAGE_ICON ) ); 94 messageItem.addActionListener( this ); 95 popMenu.add( messageItem ); 96 97 contactDetailsItem = new JMenuItem(); 98 contactDetailsItem.setName( "contact_details" ); 99 contactDetailsItem.setFont( JSystemFonts.FONT ); 100 contactDetailsItem.setIcon( JSharedObjects.getResources().getIcon( JResources.USER_DETAIL_ICON ) ); 101 contactDetailsItem.addActionListener( this ); 102 popMenu.add( contactDetailsItem ); 103 104 popMenu.addSeparator(); 105 106 updateNameItem = new JMenuItem(); 107 updateNameItem.setName( "update_contact_name" ); 108 updateNameItem.setFont( JSystemFonts.FONT ); 109 updateNameItem.setIcon( JSharedObjects.getResources().getIcon( JResources.UPDATE_NAME_ICON ) ); 110 updateNameItem.addActionListener( this ); 111 popMenu.add( updateNameItem ); 112 113 blockUnblockItem = new JMenuItem(); 114 blockUnblockItem.setName( "block_unblock_contact" ); 115 blockUnblockItem.setFont( JSystemFonts.FONT ); 116 blockUnblockItem.addActionListener( this ); 117 popMenu.add( blockUnblockItem ); 118 119 addContactToListItem = new JMenuItem(); 120 addContactToListItem.setName( "add_contact" ); 121 addContactToListItem.setFont( JSystemFonts.FONT ); 122 addContactToListItem.setIcon( JSharedObjects.getResources().getIcon( JResources.ADD_INVITE_ICON ) ); 123 addContactToListItem.addActionListener( this ); 124 popMenu.add( addContactToListItem ); 125 126 removeContactItem = new JMenuItem(); 127 removeContactItem.setName( "remove_contact" ); 128 removeContactItem.setFont( JSystemFonts.FONT ); 129 removeContactItem.setIcon( JSharedObjects.getResources().getIcon( JResources.REMOVE_ICON ) ); 130 removeContactItem.addActionListener( this ); 131 popMenu.add( removeContactItem ); 132 133 renameContactItem = new JMenuItem(); 134 renameContactItem.setName( "rename_contact" ); 135 renameContactItem.setFont( JSystemFonts.FONT ); 136 renameContactItem.setIcon( JSharedObjects.getResources().getIcon( JResources.RENAME_ICON ) ); 137 renameContactItem.addActionListener( this ); 138 popMenu.add( renameContactItem ); 139 140 updateLanguage(); 141 setCellRenderer( new JListCellRenderer() ); 142 setToolTipText( "JContactList" ); } 144 145 148 public void updateLanguage() { 149 150 messageItem.setText( JSharedObjects.getLanguageManager().getStringEx( "NEW_MESSAGE" ) ); 151 contactDetailsItem.setText( JSharedObjects.getLanguageManager().getStringEx( "CONTACT_DETAILS" ) ); 152 updateNameItem.setText( JSharedObjects.getLanguageManager().getStringEx( "UPDATE_SCREEN_NAME" ) ); 153 removeContactItem.setText( JSharedObjects.getLanguageManager().getStringEx( "REMOVE_CONTACT" ) ); 154 renameContactItem.setText( JSharedObjects.getLanguageManager().getStringEx( "RENAME_CONTACT" ) ); 155 addContactToListItem.setText( JSharedObjects.getLanguageManager().getStringEx( "ADD_TO_CONTACT_LIST" ) ); 156 updateUI(); 157 } 158 159 164 public void updateUI() { 165 166 super.updateUI(); 167 168 if( popMenu != null ) { 169 javax.swing.SwingUtilities.updateComponentTreeUI( popMenu ); 170 popMenu.invalidate(); 171 popMenu.validate(); 172 popMenu.repaint(); 173 } 174 } 175 176 182 public String getToolTipText( java.awt.event.MouseEvent evt ) { 183 184 int nIndex = locationToIndex( evt.getPoint() ); 185 186 if( nIndex >= 0 ) { 187 JContactListItem item = ( JContactListItem ) getModel().getElementAt( nIndex ); 188 JPlugin plugin = ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ); 189 190 if( plugin != null ) 191 return "<html><b>" + plugin.toString() + 192 "</b><br><br><i><b>" + 193 JSharedObjects.getLanguageManager().getStringEx( "USER" ) + "</b></i> " + 194 item.getUserId() + "<br><i><b>" + 195 JSharedObjects.getLanguageManager().getStringEx( "NICK_NAME" ) + "</b></i> " + 196 item.getNickName() + "</html>"; 197 else 198 return null; 199 } 200 201 return null; 202 } 203 204 210 public void processMouseEvent( java.awt.event.MouseEvent e ) { 211 212 try { 213 if( e.isPopupTrigger() ) { 214 215 JContactListItem item = ( ( JContactListItem ) getSelectedValue() ); 216 217 218 if( item == null ) 219 return; 220 221 if( ( item.getPluginId() == -1 ) || ( item.getPluginId() == -2 ) ) 222 return; 223 224 JPlugin plugin = ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ); 225 int nPosX = e.getX(); 226 int nPosY = e.getY(); 227 228 item = ( JContactListItem ) JSharedObjects.getContactListManager().getContactList( plugin ).get( item.getUserId() ); 230 231 if( fireIsNewTabEnabled( item ) ) { 232 messageItem.setVisible( true ); 233 messageItem.setText( JSharedObjects.getLanguageManager().getStringEx( "NEW_TAB" ) ); 234 } 235 else { 236 if( fireIsNewMessageEnabled( item ) ) { 237 messageItem.setVisible( true ); 238 messageItem.setText( JSharedObjects.getLanguageManager().getStringEx( "NEW_MESSAGE" ) ); 239 } 240 else { 241 messageItem.setVisible( false ); 242 } 243 } 244 245 if( item.getContactFlag() == JContactListItem.CONTACT_NOT_IN_LIST ) 247 addContactToListItem.setVisible( true ); 248 else 249 addContactToListItem.setVisible( false ); 250 251 updateNameItem.setVisible( plugin.getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_UPDATE_SCREEN_NAME ) ); 253 254 blockUnblockItem.setVisible( plugin.getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_BLOCK_CONTACT ) ); 256 257 if( blockUnblockItem.isVisible() ) 258 259 blockUnblockItem.setEnabled( plugin.isConnected() ); 260 261 if( item.getBlocked() ) { 262 blockUnblockItem.setText( JSharedObjects.getLanguageManager().getStringEx( "UNBLOCK_CONTACT" ) ); 263 blockUnblockItem.setIcon( JSharedObjects.getResources().getIcon( JResources.UNBLOCK_16_ICON ) ); 264 } 265 else { 266 blockUnblockItem.setText( JSharedObjects.getLanguageManager().getStringEx( "BLOCK_CONTACT" ) ); 267 blockUnblockItem.setIcon( JSharedObjects.getResources().getIcon( JResources.BLOCK_16_ICON ) ); 268 } 269 270 if( nPosX > ( this.getPreferredScrollableViewportSize().width / 2 ) ) 271 nPosX = e.getX() - popMenu.getSize().width; 272 273 popMenu.show( this, nPosX, nPosY ); 274 } 275 else { 276 super.processMouseEvent( e ); 277 } 278 } catch( java.lang.Exception ex ) { 279 System.err.println( "JContactList.processMenuEvent() - " + ex ); 280 } 281 282 } 283 284 288 public void addMenuListener( JMenuListener menuListener ) { 289 290 this.menuListener.add( menuListener ); 291 } 292 293 297 public void removeMenuListener( JMenuListener menuListener ) { 298 299 this.menuListener.remove( menuListener ); 300 } 301 302 305 public void destroy() { 306 307 menuListener.clear(); 308 } 309 310 314 public boolean addItem( JContactListItem item ) { 315 316 if( listModel.contains( item ) ) { 317 320 JContactListItem tmpItem = getItem( item.getPluginId(), item.getUserId() ); 321 tmpItem.copy( item ); 322 return false; 323 } 324 325 listModel.addElement( item ); 326 327 return true; 328 } 329 330 336 public JContactListItem getItem( int nPluginId, java.lang.String strItemId ) { 337 338 JContactListItem item = new JContactListItem( nPluginId, strItemId, "" ); 339 int nItemIndex = listModel.indexOf( item ); 340 341 342 if( nItemIndex != -1 ) 343 return ( JContactListItem ) listModel.get( nItemIndex ); 344 else 345 return null; 346 } 347 348 352 public boolean removeItem( JContactListItem item ) { 353 354 if( !listModel.contains( item ) ) 355 return false; 356 357 listModel.removeElement( item ); 358 359 return true; 360 } 361 362 367 public boolean removePluginItems( int nPluginId ) { 368 369 int nCountItems = listModel.size(); 370 HashMap<JContactListItem, JContactListItem> hItems = new HashMap<JContactListItem, JContactListItem>(); 371 Iterator<JContactListItem> itItems; 372 JContactListItem item; 373 374 375 for( int nCount = 0; nCount < nCountItems; nCount++ ) { 376 377 item = ( JContactListItem ) listModel.get( nCount ); 378 379 if( item.getPluginId() == nPluginId ) 380 hItems.put( item, item ); 381 } 382 383 itItems = hItems.values().iterator(); 384 385 while( itItems.hasNext() ) { 386 item = itItems.next(); 387 listModel.removeElement( item ); 388 } 389 390 hItems.clear(); 391 392 return true; 393 } 394 395 402 public boolean setAllItemsStatus( int nPluginId, int nNewStatus ) { 403 404 JContactListItem item; 405 int nCountItems = listModel.getSize(); 406 boolean bChanged = false; 407 408 409 for( int nCount = 0; nCount < nCountItems; nCount++ ) { 410 411 item = ( JContactListItem ) listModel.get( nCount ); 412 413 if( item.getPluginId() == nPluginId ) { 414 item.setStatus( nNewStatus ); 415 bChanged = true; 416 } 417 } 418 419 if( bChanged ) { 420 listModel.sort(); 421 listModel.update(); 422 } 423 424 return true; 425 } 426 427 430 public boolean removeSelectedItem() { 431 432 int nSelectedIndex = getSelectedIndex(); 433 434 435 if( nSelectedIndex == -1 ) 436 return false; 437 438 remove( nSelectedIndex ); 439 440 return true; 441 } 442 443 448 public boolean setItemStatus( JContactListItem item, int nNewStatus ) { 449 450 int nItemPos = listModel.indexOf( item ); 451 int nStatus; 452 453 454 if( nItemPos == -1 ) 455 return false; 456 457 JContactListItem itemTmp = ( JContactListItem ) listModel.get( nItemPos ); 458 459 nStatus = itemTmp.getStatus(); 460 itemTmp.setStatus( nNewStatus ); 461 462 if( nStatus != nNewStatus ) 463 if( ( ( nStatus > JPlugin.STATUS_OFFLINE ) && 464 ( nNewStatus == JPlugin.STATUS_OFFLINE ) ) || 465 ( ( nStatus == JPlugin.STATUS_OFFLINE ) && 466 ( nNewStatus > JPlugin.STATUS_OFFLINE ) ) || 467 ( ( nStatus >= JPlugin.STATUS_URL ) ) || 468 ( ( nNewStatus >= JPlugin.STATUS_URL ) ) ) { 469 listModel.sort(); 470 } 471 else 472 listModel.update(); 473 else 474 listModel.update(); 475 476 return true; 477 } 478 479 483 public void restoreItemStatus( JContactListItem item ) { 484 485 int nItemPos = listModel.indexOf( item ); 486 487 488 if( nItemPos == -1 ) 489 return; 490 491 JContactListItem item2 = ( JContactListItem ) listModel.get( nItemPos ); 492 493 item2.restoreRealStatus(); 494 listModel.sort(); 495 } 496 497 503 public void mouseClicked( java.awt.event.MouseEvent mouseEvent ) { 504 505 try { 506 if( mouseEvent.getClickCount() == 2 ) { 507 508 JContactListItem item = ( JContactListItem ) getSelectedValue(); 509 510 if( item == null ) 511 return; 512 513 if( ( item.getPluginId() != -1 ) && ( item.getPluginId() != -2 ) ) 514 fireOnMessage( item ); 515 } 516 } catch( java.lang.Exception ex ) { 517 System.err.println( "JContactList.mouseClicked() - " + ex ); 518 } 519 } 520 521 527 public void mouseEntered( java.awt.event.MouseEvent mouseEvent ) { 528 529 } 530 531 537 public void mouseExited( java.awt.event.MouseEvent mouseEvent ) { 538 539 } 540 541 547 public void mousePressed( java.awt.event.MouseEvent mouseEvent ) { 548 549 } 550 551 557 public void mouseReleased( java.awt.event.MouseEvent mouseEvent ) { 558 559 } 560 561 566 public void actionPerformed( java.awt.event.ActionEvent actionEvent ) { 567 568 try { 569 JContactListItem item = ( JContactListItem ) getSelectedValue(); 570 JMenuItem menuItem = ( JMenuItem ) actionEvent.getSource(); 571 572 System.err.println( "JContactList.actionPerformed() - Triggered for item[" + item.getUserId() + "]" ); 573 574 if( menuItem.getName().compareTo( "message" ) == 0 ) { 576 fireOnMessage( item ); 577 return; 578 } 579 580 if( menuItem.getName().compareTo( "contact_details" ) == 0 ) { 582 JMOSContactDetails contactDetailDlg = new JMOSContactDetails( item ); 583 584 contactDetailDlg.setVisible( true ); 585 return; 586 } 587 588 if( menuItem.getName().compareTo( "remove_contact" ) == 0 ) { 590 591 int nRet = JOptionPane.showConfirmDialog( null, JSharedObjects.getLanguageManager().getStringEx( "REMOVE_CONTACT_WARNING" ) + " " + item.getNickName() + JSharedObjects.getLanguageManager().getStringEx( "QUESTION_SYMBOL" ), JSharedObjects.getLanguageManager().getStringEx( "REMOVE_CONTACT" ), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ); 592 593 if( nRet == JOptionPane.YES_OPTION ) 595 ( ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ) ).fireOnRemoveUserFromContactList( item.getUserId() ); 596 597 return; 598 } 599 600 if( menuItem.getName().compareTo( "update_contact_name" ) == 0 ) { 602 ( ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ) ).fireOnBuddyDetails( item.getUserId() ); 603 return; 604 } 605 606 if( menuItem.getName().compareTo( "rename_contact" ) == 0 ) { 608 609 java.lang.Object objNickName = JOptionPane.showInputDialog( null, JSharedObjects.getLanguageManager().getStringEx( "NICK_NAME" ), JSharedObjects.getLanguageManager().getStringEx( "RENAME_CONTACT" ), JOptionPane.PLAIN_MESSAGE, null, null, item.getNickName() ); 610 611 if( ( objNickName == null ? "" : objNickName.toString() ).compareTo( "" ) != 0 ) { 612 JSharedObjects.getContactListManager().updateScreenName( ( ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ) ), item.getUserId(), objNickName.toString() ); 613 item.setNickName( objNickName.toString() ); 614 listModel.update(); 615 } 616 617 return; 618 } 619 620 if( menuItem.getName().compareTo( "add_contact" ) == 0 ) { 622 ( ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ) ).fireOnAddUserToContactList( item.getUserId() ); 623 return; 624 } 625 626 if( menuItem.getName().compareTo( "block_unblock_contact" ) == 0 ) { 628 629 JContactListItem[] aContactList = new JContactListItem[1]; 630 JPlugin plugin = JSharedObjects.getPluginEngine().get( item.getPluginId() ); 631 632 aContactList[0] = ( JContactListItem ) JSharedObjects.getContactListManager().getContactList( plugin ).get( item.getUserId() ); 633 634 if( aContactList[0] != null ) { 635 aContactList[0].setBlocked( !aContactList[0].getBlocked() ); 636 plugin.fireOnChangePrivacy( aContactList ); 637 } 638 639 return; 640 } 641 } catch( java.lang.Exception ex ) { 642 System.err.println( "JContactist.actionPerformed() - " + ex ); 643 } 644 } 645 646 650 private void fireOnMessage( JContactListItem item ) { 651 652 ArrayList<JMenuListener> clone; 653 654 synchronized( this ) { 655 clone = ( ArrayList<JMenuListener> ) menuListener.clone(); 656 } 657 658 if( clone.size() > 0 ) { 659 for( int nCount = 0; nCount < clone.size(); nCount++ ) 660 ( ( JMenuListener ) clone.get( nCount ) ).onMessage( item ); 661 } 662 } 663 664 668 private void fireOnNewTab( JContactListItem item ) { 669 670 ArrayList<JMenuListener> clone; 671 672 synchronized( this ) { 673 clone = ( ArrayList<JMenuListener> ) menuListener.clone(); 674 } 675 676 if( clone.size() > 0 ) { 677 for( int nCount = 0; nCount < clone.size(); nCount++ ) 678 ( ( JMenuListener ) clone.get( nCount ) ).onNewTab( item ); 679 } 680 } 681 682 686 private boolean fireIsNewTabEnabled( JContactListItem item ) { 687 688 ArrayList<JMenuListener> clone; 689 boolean bRet = true; 690 691 synchronized( this ) { 692 clone = ( ArrayList<JMenuListener> ) menuListener.clone(); 693 } 694 695 if( clone.size() > 0 ) { 696 for( int nCount = 0; nCount < clone.size(); nCount++ ) 697 bRet = ( bRet && ( ( JMenuListener ) clone.get( nCount ) ).isNewTabEnabled( item ) ); 698 } 699 700 return bRet; 701 } 702 703 707 private boolean fireIsNewMessageEnabled( JContactListItem item ) { 708 709 ArrayList<JMenuListener> clone; 710 boolean bRet = true; 711 712 synchronized( this ) { 713 clone = ( ArrayList<JMenuListener> ) menuListener.clone(); 714 } 715 716 if( clone.size() > 0 ) { 717 for( int nCount = 0; nCount < clone.size(); nCount++ ) 718 bRet = ( bRet && ( ( JMenuListener ) clone.get( nCount ) ).isNewMessageEnabled( item ) ); 719 } 720 721 return bRet; 722 } 723 724 731 class JListCellRenderer extends javax.swing.JLabel implements javax.swing.ListCellRenderer { 732 733 boolean bSystemLabel = false; 734 735 736 739 public JListCellRenderer() { 740 741 setOpaque( true ); 742 } 743 744 751 public Component getListCellRendererComponent( javax.swing.JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { 752 753 try { 754 JContactListItem item = ( JContactListItem ) value; 755 ImageIcon icon = JSharedObjects.getResources().getIcon( item.getStatus() ); 756 Icon thisIcon = getIcon(); 757 Dimension dmItemSize = new Dimension( list.getParent().getSize().width, icon.getIconHeight() ); 758 boolean bEnabledItem = !item.getBlocked(); 759 760 761 setText( item.getNickName() ); 762 763 if( !list.isEnabled() ) 764 bEnabledItem = false; 765 766 setEnabled( bEnabledItem ); 767 768 if( ( thisIcon != null ? !thisIcon.equals( icon ) : true ) ) 769 setIcon( icon ); 770 771 if( isSelected && item.getPluginId() != -1 && item.getPluginId() != -2 ) { 772 setHorizontalAlignment( JLabel.LEADING ); 773 setBackground( list.getSelectionBackground() ); 774 setForeground( list.getSelectionForeground() ); 775 setBorder( new EtchedBorder() ); 776 bSystemLabel = false; 777 } 778 else { 779 if( item.getPluginId() == -1 || item.getPluginId() == -2 ) { 780 setHorizontalAlignment( JLabel.CENTER ); 781 bSystemLabel = true; 782 } 783 else { 784 setHorizontalAlignment( JLabel.LEADING ); 785 bSystemLabel = false; 786 } 787 788 setBackground( list.getBackground() ); 789 setForeground( list.getForeground() ); 790 setPreferredSize( dmItemSize ); 791 setBorder( null ); 792 } 793 } catch( java.lang.Exception e ) { 794 System.err.println( "JContactList.getListCellRendererComponent() - " + e ); 795 } 796 797 setFont( list.getFont() ); 798 799 return this; 800 } 801 802 810 public void paint( java.awt.Graphics g ) { 811 812 if( bSystemLabel ) { 813 java.awt.Dimension dmSize = getSize(); 814 int nCenterX = 10, 816 nCenterY = ( dmSize.height / 2 ) + ( g.getFontMetrics().getHeight() / 2 ); 817 818 g.draw3DRect( 0, ( int )( dmSize.getHeight() / 2 ), nCenterX - 2, 1, true ); 819 g.draw3DRect( ( int )( nCenterX + g.getFontMetrics().stringWidth( getText() ) ), ( int )( dmSize.getHeight() / 2 ), ( int ) ( dmSize.getWidth() - nCenterX - g.getFontMetrics().stringWidth( getText() ) - 2 ), 1, true ); 820 g.setFont( getFont() ); 821 g.setColor( this.getForeground() ); 822 g.drawString( getText(), nCenterX, nCenterY - 2 ); 823 } 824 else 825 super.paint( g ); 826 } 827 828 } } 830 831 | Popular Tags |