package org.dyndns.fishyu.messenger;
//ClientGUI

import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

public class ClientGUI extends Applet implements ItemListener
{
   Icon myPhotoIcon;   
         
   Icon yourPhotoIcon;  
         
   Icon fishIcon;
   Icon oceanIcon;
   Icon riverIcon;
   Icon gotakIcon;
   Icon chessIcon;
   Icon sandIcon;
   Icon sexIcon;
   Icon sweetIcon;
   Icon flowerIcon;
   Icon skyIcon;
   
    private String S;
	private int record=0;  
	JLabel lblMusic=new JLabel("請選擇背景音樂");  //JLabel
	Choice MusicMenu=new Choice(); 
	
	JLabel PhotoChoice=new JLabel("請選擇您的圖片");  //JLabel
	Choice PhotoMenu=new Choice(); 
	
	
	AudioClip voiceGet,song;    
    URL urlWelcome,urlReceive,urlLeave;
	URL urlOne,urlTwo,urlThree,urlFour,urlFive,urlSix,urlSeven,urlEight; //定義歌曲來源

   
	JFrame f = new JFrame( "未登入-Messenger" );
	
	
      DisconnectListener disconnectListener = new DisconnectListener();
      
	IdleClass idleClass = new IdleClass();
	
	TimerClass timerClass = new TimerClass();
	javax.swing.Timer timer = new javax.swing.Timer( 1000, timerClass );
	
    JPanel ChannelPane = new JPanel();
	JPanel PrivatePane = new JPanel();
	
	//---------------
	//set Photos
	
	JLabel yourPhotoLabel = new JLabel();
	JLabel yourPhoto = new JLabel();
	
	JLabel myPhotoLabel = new JLabel( "我的圖片" );
	JLabel myPhoto = new JLabel();
	
	JLabel timeLabel = new JLabel();
	JLabel idleLabel = new JLabel();
	//------------------------------------
	// for timer
	int SecCount = 0;
	int idleMinute = 0;
	
	//-------------------------------------------------------
	   // userName to add to outgoing messages
   private JMenu connectMenu;
   private JMenuItem connectMenuItem;
   private JMenuItem disconnectMenuItem;
   
   private String userName;
   
   // MessageManager for communicating with server
   private MessageManager messageManager;
   
   // MessageListener for receiving incoming messages
   private MessageListener messageListener;
   
   private int CurrentChannelStatus = 0; // default, about Channel
   private int CurrentPrivateStatus = 0; //100, no specified pri user, about Private 
   private final String s = " ";
   
   private final int FISH = 1;
   private final int OCEAN = 2;
   private final int RIVER = 3;
   private final int GOTAK = 4;
   private final int CHESS = 5;
   private final int SAND = 6;
   private final int SEX = 7;
   private final int SWEET = 8;
   private final int FLOWER = 9;
   private final int SKY = 10;
   
   private int photoStatus = GOTAK;
   
   //private StringTokenizer tokenizer;
   
	//-------------------------------------------------------
	
	FriendListener friendListener = new FriendListener();
	JoinListener joinListener = new JoinListener();
	//Channel part
	//------------------------------------------------------
	
	JMenuItem joinMenuItem = new JMenuItem( "加入頻道" );
	JMenuItem leaveMenuItem = new JMenuItem( "離開頻道" );
	
	JButton ChannelSendButton = new JButton( "Send" ); 
	JLabel ChannelUserlistLabel = new JLabel( "頻道成員" );
	JLabel ChannelsLable = new JLabel( "頻道表" );
	JLabel ChannelMessageLabel = new JLabel( "訊息內容" ); 
	JLabel ChannelSystemInfoLabel = new JLabel( "系統提示訊息" ); 
	
	JTextArea ChannelMessageArea = new JTextArea();
	JScrollPane ChannelMessageAreaPane = new JScrollPane( ChannelMessageArea );
	
	JTextArea ChannelSystemInfoArea = new JTextArea();
	JScrollPane ChannelSystemInfoAreaPane = new JScrollPane( ChannelSystemInfoArea );
	
	JTextField ChannelUserlistField[] = new JTextField[ 21 ];
	// go to 
	JPanel ChannelUserlistPanel = new JPanel();
	JScrollPane ChannelUserlistAreaPane = new JScrollPane( ChannelUserlistPanel );
	
	JPanel channelsPanel=new JPanel();
	JScrollPane ChannelsPanelPane = new JScrollPane( channelsPanel );
	
	JTextArea ChannelInputArea = new JTextArea();
	JScrollPane ChannelInputAreaPane = new JScrollPane( ChannelInputArea );
	
	private JTextField channels[] = new JTextField[ 21 ];
	
	private Channel channel[] = new Channel[ 21 ];
	//JScrollPane contentPane = new JScrollPane(a);
	
	//----------------------------------------------
	//private part
	//----------------------------------------------
	
	JMenuItem friendMenuItem = new JMenuItem( "設為好友" );
	JMenuItem badMenuItem = new JMenuItem( "設為壞人" );
	
	JButton PrivateSendButton = new JButton( "Send" ); 
	JLabel PrivateUserlistLabel = new JLabel( "線上名單" );
	JLabel PrivateMessageLabel = new JLabel( "訊息內容" ); 
	JLabel PrivateSystemInfoLabel = new JLabel( "系統提示訊息" ); 
	
	JTextArea PrivateMessageArea = new JTextArea();
	JScrollPane PrivateMessageAreaPane = new JScrollPane( PrivateMessageArea );
	
	JTextArea PrivateSystemInfoArea = new JTextArea();
	JScrollPane PrivateSystemInfoAreaPane = new JScrollPane( PrivateSystemInfoArea );
	
	JTextField PrivateUserlistField[] = new JTextField[ 21 ];
	// go to 
	JPanel PrivateUserlistPanel = new JPanel();
	JScrollPane PrivateUserlistAreaPane = new JScrollPane( PrivateUserlistPanel );
	
	JTextArea PrivateInputArea = new JTextArea();
	JScrollPane PrivateInputAreaPane = new JScrollPane( PrivateInputArea );
	
    private Private priUser[] = new Private[ 21 ];

//--------------
	
	JTabbedPane tabs = new JTabbedPane( JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT );
	
	
//-----------------------------------------------------------------------------------------
// this is for NOT GUI variables
//-----------------------------------------------------------------------------------------

   
   private ChannelsHandler channelsHandler = new ChannelsHandler();
   private PrivateUserlistHandler privateUserlistHandler = new PrivateUserlistHandler();
//-----------------------------------------------------------------------------------------
   
	public ClientGUI( MessageManager manager ) 
	{
		 
		
		
		MusicMenu.add("請選擇歌曲");           //加歌曲
		MusicMenu.add("卡農");
		MusicMenu.add("鱒魚");
		MusicMenu.add("土耳其進行曲");
		MusicMenu.add("阿萊城姑娘");
		MusicMenu.add("仙劍奇俠傳");
		MusicMenu.add("命中註定");
		MusicMenu.add("新不了情");
		MusicMenu.add("還是會寂寞");
		MusicMenu.add("關掉");
		MusicMenu.addItemListener(this);
		timer.start();
		try{
			    	urlWelcome = new URL(this.getClass().getResource( "sound/music/welcome.wav").toString());
			    	voiceGet=Applet.newAudioClip(urlWelcome);  //指定歌曲給撥放源
			    	voiceGet.play();   //撥一次
			    	}catch(Exception e1){}
		Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
		int width = 675;
		int height = 460;
		int x=(screen.width-width)/2;
		int y=(screen.height-height)/2;
		f.setBounds(x,y,width,height);
		f.getContentPane().setLayout(null);
		
		ChannelPane.setLayout( null );
		PrivatePane.setLayout( null );
		
		
	    f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				disconnectListener.disconnect();
				System.exit(0);
			}
		});
		
		
//-------------------------------------------------
//set menus		
	        connectMenu = new JMenu( "Connect" );
      connectMenu.setMnemonic( 'S' );
      JMenuBar menuBar = new JMenuBar();
      menuBar.add( connectMenu );
      f.setJMenuBar( menuBar );
      

          
   
         
         ActionListener connectListener = new ConnectListener();
         
               Icon disconnectIcon = new ImageIcon( 
         getClass().getResource( "images/Disconnect.gif" ) );
         
             Icon connectIcon = new ImageIcon( 
         getClass().getResource( "images/Connect.gif" ) );
      
      connectMenuItem = new JMenuItem( "Connect", connectIcon );
      connectMenuItem.setMnemonic( 'C' );
      connectMenuItem.addActionListener( connectListener ); 
      

      
      disconnectMenuItem = new JMenuItem( "Disconnect", disconnectIcon );      
      disconnectMenuItem.setMnemonic( 'D' );
      
      
      disconnectMenuItem.setEnabled( false );
      disconnectMenuItem.addActionListener( disconnectListener );
      
      connectMenu.add( connectMenuItem );
      connectMenu.add( disconnectMenuItem );
      
      
      f.setJMenuBar( menuBar );
//-------------------------------------------------------------------------------
// set Photos
      myPhoto.setIcon( myPhotoIcon );
      yourPhoto.setIcon( yourPhotoIcon );
//--------------------------------------------------------------------------------
// for sockets

      messageManager = manager;
      messageListener = new MyMessageListener();
      
//------------------------------------------------------------------------------------------------
// for setting Channel, Private
// channel part
        ChannelMessageArea.setEditable( false );
        ChannelMessageArea.setWrapStyleWord( true );
        ChannelMessageArea.setLineWrap( true );
        ChannelMessageArea.setBorder(BorderFactory.createLineBorder(Color.black));
        
        ChannelSystemInfoArea.setEditable( false );
        ChannelSystemInfoArea.setWrapStyleWord( true );
        ChannelSystemInfoArea.setLineWrap( true );
        ChannelSystemInfoArea.setBorder(BorderFactory.createLineBorder(Color.black));
        
        ChannelInputArea.setEditable( true );
        ChannelInputArea.setWrapStyleWord( true );
        ChannelInputArea.setLineWrap( true );
        ChannelInputArea.setBorder(BorderFactory.createLineBorder(Color.black));
        
// private part
        PrivateMessageArea.setEditable( false );
        PrivateMessageArea.setWrapStyleWord( true );
        PrivateMessageArea.setLineWrap( true );
        PrivateMessageArea.setBorder(BorderFactory.createLineBorder(Color.black));
        
        PrivateSystemInfoArea.setEditable( false );
        PrivateSystemInfoArea.setWrapStyleWord( true );
        PrivateSystemInfoArea.setLineWrap( true );
        PrivateSystemInfoArea.setBorder(BorderFactory.createLineBorder(Color.black));
        
        PrivateInputArea.setEditable( true );
        PrivateInputArea.setWrapStyleWord( true );
        PrivateInputArea.setLineWrap( true );
        PrivateInputArea.setBorder(BorderFactory.createLineBorder(Color.black));

//end setting Channel, Private		
        
//------------------------------------------------------------------------------------------------	
//add components 
//channel part
      
      for ( int jjjj = 0; jjjj < channel.length; jjjj++ )
      {
          channel[ jjjj ] = new Channel();
      }

      for ( int j = 0; j < ChannelUserlistField.length; j++ ) // only 20, for more than 20 users we dynamically add 
      {
          ChannelUserlistField[ j ] = new JTextField();
          ChannelUserlistField[ j ].setBorder(BorderFactory.createLineBorder(Color.gray));
          ChannelUserlistField[ j ].setBackground( Color.white );
          ChannelUserlistField[ j ].setEditable( false );
          ChannelUserlistPanel.add( ChannelUserlistField[ j ] );
          //channels[ j ].setToolTipText( "按Join加入此頻道" );
          
      }


	  channels[ 0 ] = new JTextField( "公眾頻道" );
      channels[ 0 ].setBorder(BorderFactory.createLineBorder(Color.gray));
      channels[ 0 ].setBackground( Color.blue );
      channels[ 0 ].setForeground( Color.white );
      channels[ 0 ].setEditable( false );
      channels[ 0 ].setToolTipText( "按右鍵離開此頻道" );
      channelsPanel.add(channels[ 0 ]);
      channels[ 0 ].addMouseListener( channelsHandler );
      
      for ( int jj = 1; jj < channels.length; jj++ )
      {
          channels[ jj ] = new JTextField( "頻道" + jj );
          channels[ jj ].setBorder(BorderFactory.createLineBorder(Color.gray));
          channels[ jj ].setBackground( Color.white );
          channels[ jj ].setEditable( false );
          channels[ jj ].setToolTipText( "按右鍵加入此頻道" );
          channelsPanel.add(channels[ jj ]);
          channels[ jj ].addMouseListener( channelsHandler );
      }
      
      createChannelPopupMenu();
      
//private part

      for ( int jjjjj = 0; jjjjj < priUser.length; jjjjj++ )
      {
          priUser[ jjjjj ] = new Private();
      }

      for ( int j = 0; j < PrivateUserlistField.length; j++ ) // only 20, for more than 20 users we dynamically add 
      {
          PrivateUserlistField[ j ] = new JTextField( s );
          PrivateUserlistField[ j ].setBorder(BorderFactory.createLineBorder(Color.gray));
          PrivateUserlistField[ j ].setBackground( Color.white );
          PrivateUserlistField[ j ].setEditable( false );
          PrivateUserlistField[ j ].setToolTipText( "按一下看此人的訊息" );
          PrivateUserlistPanel.add( PrivateUserlistField[ j ]);
          PrivateUserlistField[ j ].addMouseListener( privateUserlistHandler );
      }
      
      createPrivatePopupMenu();
      
// end adding components to channelsPanel
//--------------------------------------------------------------------------------------------------
//Layout
//channel part
        GridLayout channelsPanelLayout=new GridLayout(21,1);
        channelsPanel.setLayout( channelsPanelLayout );
        
        GridLayout ChannelUserlistPanelLayout = new GridLayout( 21, 1 );
        ChannelUserlistPanel.setLayout( ChannelUserlistPanelLayout );
        
//parivate part

        GridLayout PrivateUserlistPanelLayout = new GridLayout( 21, 1 );
        PrivateUserlistPanel.setLayout( PrivateUserlistPanelLayout );
        

//----------------------------------------------------------------
// setting channel GUI
        
        ChannelUserlistLabel.setBounds( 0, 0, 80, 20 );
        ChannelUserlistAreaPane.setBounds( 0, 20, 80, 200 );
        
        ChannelsLable.setBounds( 80, 0, 80, 20 );
		ChannelsPanelPane.setBounds( 80, 20, 80, 200 );
		
		ChannelMessageLabel.setBounds( 160, 0, 320, 20 ); 
        ChannelMessageAreaPane.setBounds( 160, 20, 320, 200 ); 
        
        //ChannelSystemInfoLabel.setBounds( 380, 0, 100, 20 );
        //ChannelSystemInfoAreaPane.setBounds( 380, 20, 100, 200 ); 
        
        ChannelSendButton.setBounds( 0, 220, 80, 80 ); 
        ChannelInputAreaPane.setBounds( 80, 220, 400, 80 ); 
	
	    ChannelPane.add( ChannelUserlistAreaPane );
		ChannelPane.add( ChannelsPanelPane );
        ChannelPane.add( ChannelMessageAreaPane );
        ChannelPane.add( ChannelSystemInfoAreaPane );
        ChannelPane.add( ChannelInputAreaPane );
        
        ChannelPane.add( ChannelUserlistLabel );
        ChannelPane.add( ChannelsLable );
        ChannelPane.add( ChannelMessageLabel );
        ChannelPane.add( ChannelSystemInfoLabel );
        
        ChannelPane.add( ChannelSendButton );
        
       
        
// setting private
        
        PrivateUserlistLabel.setBounds( 0, 0, 80, 20 );
        PrivateUserlistAreaPane.setBounds( 0, 20, 80, 200 );
        
		PrivateMessageLabel.setBounds( 80, 0, 400, 20 ); 
        PrivateMessageAreaPane.setBounds( 80, 20, 400, 200 ); 
        
        //PrivateSystemInfoLabel.setBounds( 380, 0, 100, 20 );
        //PrivateSystemInfoAreaPane.setBounds( 380, 20, 100, 200 ); 
        
        PrivateSendButton.setBounds( 0, 220, 80, 80 ); 
        PrivateInputAreaPane.setBounds( 80, 220, 400, 80 ); 
        
	    PrivatePane.add( PrivateUserlistAreaPane );
        PrivatePane.add( PrivateMessageAreaPane );
        PrivatePane.add( PrivateSystemInfoAreaPane );
        PrivatePane.add( PrivateInputAreaPane );
        
        PrivatePane.add( PrivateUserlistLabel );
        PrivatePane.add( PrivateMessageLabel );
        PrivatePane.add( PrivateSystemInfoLabel );
        
        PrivatePane.add( PrivateSendButton );
        
        
//-------------------------------------------------------------------
//add mouse listener
      PrivateSendButton.addActionListener(

         new ActionListener() {
            
            // send new message when user activates sendButton
            public void actionPerformed( ActionEvent event )
            {
               
               if( CurrentPrivateStatus != 100 ) // 100, default
               {
               	   if( !PrivateUserlistField[ CurrentPrivateStatus ].getText().equals( s ) 
               	     && !PrivateInputArea.getText().equals("") )
               	   
               	   {
	               		messageManager.sendPrivateMessage( userName + "`", PrivateUserlistField[ CurrentPrivateStatus ].getText() + "`",  
	                  "> " + PrivateInputArea.getText() + "`", "#PriSend#" ); //:PriSend code
	               
	               // clear inputArea
	               		PrivateInputArea.setText("");
	               	}
	           }
            }
         } 
      );
      
      ChannelSendButton.addActionListener(

         new ActionListener() {
            
            // send new message when user activates sendButton
            public void actionPerformed( ActionEvent event )
            {
            
            	
	            if( channel[ CurrentChannelStatus ].isOpen() 
	              && !ChannelInputArea.getText().equals( "" ) )
	            {
	               messageManager.sendChannelMessage( userName + "`", 
	                  "> " + ChannelInputArea.getText() + "`", CurrentChannelStatus + "#ChannelSend#" );
	               
	               // clear inputArea
	               ChannelInputArea.setText("");
	               ChannelSendButton.setEnabled( true );
	            }
	            
	            //else
	                //ChannelSendButton.setEnabled( false );
            }
         } 
      );


// set Tab
        tabs.addTab("私人訊息", PrivatePane ); 
        tabs.addTab("頻道訊息", ChannelPane );
        
        tabs.setBounds( 0, 0, 500, 330 );
        
        myPhotoLabel.setBounds( 520, 20, 130, 20 );
        myPhoto.setBounds( 520, 40, 130, 130 );
        yourPhotoLabel.setBounds( 520, 180, 130, 20 );
        yourPhoto.setBounds( 520, 200, 130, 130 );
        
        timeLabel.setBounds( 0, 330, 300, 20 );
		idleLabel.setBounds( 0, 350, 300, 20 );

		lblMusic.setBounds( 320, 330, 100,25);
		MusicMenu.setBounds(320, 355, 100,25);
		
		PhotoChoice.setBounds( 440, 330, 100,25);
		PhotoMenu.setBounds(440, 355, 100, 25);
        
        f.getContentPane().add( tabs );
        
		f.getContentPane().add( myPhotoLabel );
		f.getContentPane().add( myPhoto );
		f.getContentPane().add( yourPhotoLabel );
		f.getContentPane().add( yourPhoto );
		f.getContentPane().add( timeLabel );
		f.getContentPane().add( idleLabel );
		
		f.getContentPane().add(lblMusic);
	    f.getContentPane().add(MusicMenu);
		
		f.getContentPane().add(PhotoChoice);
		f.getContentPane().add(PhotoMenu);	
		
		f.addMouseListener( idleClass );
		f.addMouseMotionListener( idleClass );
		
		PrivateUserlistField[ 0 ].setBackground( Color.blue );
        PrivateUserlistField[ 0 ].setForeground( Color.white);

yourPhotoLabel.setText( PrivateUserlistField[ CurrentPrivateStatus ].getText()
	                                    + "的圖片" );
        setDefaultPhoto();
		
		f.show();
		
		
	} // end clientGUI constructor
	
//-------------------------------------------------------------------
// inner class 	ChannesHandler

	private class ChannelsHandler implements MouseListener {
      public void mouseClicked( MouseEvent event )
      {}
      
      public void mouseReleased( MouseEvent event )
      {}
      
      public void mouseEntered( MouseEvent event )
      {}
      
      public void mouseExited( MouseEvent event )
      {}
      // process textfield events
      public void mousePressed( MouseEvent event )
      {
      	  JTextField source = (JTextField) event.getSource();
      	  for ( int i = 0; i < channels.length; i++ )
      	  {
      	  	  if( channels[ i ].getBackground() == Color.blue )
      	      {
      	      	  channels[ i ].setBackground( Color.white );
                  channels[ i ].setForeground( Color.black);
                  break;
              }
          }
          
          for ( int j = 0; j < channels.length; j++ )
          {
          	  if ( source.getText().equals( channels[ j ].getText() ) )
          	  {
          	      CurrentChannelStatus = j;
          	      break;
          	  }
          }
          	      

      	  channels[ CurrentChannelStatus ].setBackground( Color.blue );
          channels[ CurrentChannelStatus ].setForeground( Color.white);
              
      
          if( channel[ CurrentChannelStatus ].isOpen() )
	      {
	      	  ChannelSendButton.setEnabled( true );
	          joinMenuItem.setEnabled( false );
	      	  leaveMenuItem.setEnabled( true );
	      }
	      else
	      {
	      	  ChannelSendButton.setEnabled( false );
	       	  joinMenuItem.setEnabled( true );
	          leaveMenuItem.setEnabled( false );
	      }
	      
	      
          if( !channel[ CurrentChannelStatus ].isRead() )
	      {
	     	  channel[ CurrentChannelStatus ].setRead( true );
	      }
	
	  	  ChannelMessageArea.setText( channel[ CurrentChannelStatus ].getContents() );
          for( int i = 0; i < ChannelUserlistField.length; i++ )
          {
	          ChannelUserlistField[ i ].setText( channel[ CurrentChannelStatus ].getUsers( i ) );
	          //ChannelUserlistField[ i ].setBackground( Color.red );
	          //channels[ CurrentChannelStatus ].setForeground( Color.white);
	      }
	   }
	      	      
	} // end class ChannesHandler 
	
//--------------------------------------------------------------------------
// inner class 	ChannelUserlistHandler

      private class PrivateUserlistHandler implements MouseListener {
      public void mouseClicked( MouseEvent event )
      {}
      
      public void mouseReleased( MouseEvent event )
      {}
      
      public void mouseEntered( MouseEvent event )
      {}
      
      public void mouseExited( MouseEvent event )
      {}
      // process textfield events
      public void mousePressed( MouseEvent event )
      {
      	
      	  JTextField source = (JTextField) event.getSource();
      	  String tmp = source.getText();
          for ( int i = 0; i < PrivateUserlistField.length; i++ )
      	  {
      	  	  if( PrivateUserlistField[ i ].getBackground() == Color.blue )
      	  	  {
      	          PrivateUserlistField[ i ].setBackground( Color.white );
              	  PrivateUserlistField[ i ].setForeground( Color.black);
              	  break;
              }
          }
          
          for ( int j = 0; j < PrivateUserlistField.length; j++ )
      	  {
      	  	  if ( tmp.equals( PrivateUserlistField[ j ].getText() ) )
      	      {
      	      	  CurrentPrivateStatus = j;
      	      	  break;
      	      }
      	  }
      	  
      	  PrivateUserlistField[ CurrentPrivateStatus ].setBackground( Color.blue );
          PrivateUserlistField[ CurrentPrivateStatus ].setForeground( Color.white);
          
          
          
          
          
          int target = 0;
          for( ; target < PrivateUserlistField.length; target++ )
          {
              if( priUser[ target ].getName().equals( tmp ) )
              {
              	  if( !priUser[ target ].isOpen() )
              	  {
              	  	  PrivateSendButton.setEnabled( false );
              	  	  badMenuItem.setEnabled( false );
              	  	  friendMenuItem.setEnabled( true );
              	  }
              	  else
              	  {
              	  	  PrivateSendButton.setEnabled( true );
              	  	  badMenuItem.setEnabled( true );
              	  	  friendMenuItem.setEnabled( false );
              	  }
              	  	
              	      
		          if( !priUser[ target ].isRead() )
		          {
		              priUser[ target ].setRead( true );
		          }
		          break;
		      }
		  }
		  
		  yourPhotoLabel.setText( PrivateUserlistField[ CurrentPrivateStatus ].getText()
		                          + "的圖片" );
		  setPhoto();
          
          PrivateSendButton.setEnabled( true );
          
      	  PrivateMessageArea.setText( priUser[ target ].getContents() );
      	  
      }
    } // end clas
    
    public void createPrivatePopupMenu() 
    {
        

        //Create the popup menu.
        JPopupMenu popup = new JPopupMenu();
        
        friendMenuItem.addActionListener( friendListener );
        popup.add( friendMenuItem );
       
        badMenuItem.addActionListener( friendListener );
        popup.add( badMenuItem );

        //Add listener to the text area so the popup menu can come up.
        MouseListener popupListener = new PrivatePopupListener( popup );
        
        for( int i = 0; i < PrivateUserlistField.length; i++ )
            PrivateUserlistField[ i ].addMouseListener( popupListener );
    } // end method createPopupMenu
    
    public void createChannelPopupMenu() 
    {
        

        //Create the popup menu.
        JPopupMenu popup = new JPopupMenu();
        
        joinMenuItem.addActionListener( joinListener );
        popup.add( joinMenuItem );
       
        leaveMenuItem.addActionListener( joinListener );
        popup.add( leaveMenuItem );

        //Add listener to the text area so the popup menu can come up.
        MouseListener popupListener = new ChannelPopupListener( popup );
        
        for( int i = 0; i < channels.length; i++ )
            channels[ i ].addMouseListener( popupListener );
    } // end method createPopupMenu
    
    private class JoinListener implements ActionListener
    {
    	
        public void actionPerformed( ActionEvent e )
            {
            	 JMenuItem source = (JMenuItem)(e.getSource());
            	 
            	 if( !channel[ CurrentChannelStatus ].isOpen() && source.getText().equals( "加入頻道" ) )
            	 {
            	 	channel[ CurrentChannelStatus ].setOpen( true );
            	 	ChannelSendButton.setEnabled( true );
            	 	leaveMenuItem.setEnabled( true );
            	 	joinMenuItem.setEnabled( false );
            	 	channels[ CurrentChannelStatus ].setToolTipText( "按右鍵離開此頻道" );
            	 		 messageManager.sendChannelMessage( userName + "`",
	                  " 加入了頻道." + "`", CurrentChannelStatus + "#ChannelSendConnectMode#" );	                  
	             }
	             
	             else if( channel[ CurrentChannelStatus ].isOpen() && source.getText().equals( "離開頻道" ) )
            	 {
            	 	ChannelSendButton.setEnabled( false );
            	 	leaveMenuItem.setEnabled( false );
            	 	joinMenuItem.setEnabled( true );
            	 	channels[ CurrentChannelStatus ].setToolTipText( "按右鍵加入此頻道"  );
            	 	    messageManager.sendChannelMessage( userName + "`",
	                  " 離開了頻道." + "`", CurrentChannelStatus + "#ChannelSendDisconnectMode#" );	                  
	             }
            }
	     
	            
    }
    
    
    private class FriendListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e) 
    	{
    		int target = 0;
    		String tmp = PrivateUserlistField[ CurrentPrivateStatus ].getText();
	               
    		for( int i = 0; i < PrivateUserlistField.length; i++ )
	        {   
	            if ( priUser[ i ].getName().equals( tmp ) )
	            {
	                   	   target = i;
	                   	   break;
	             }
	         }
	            
    	
    		badMenuItem.setEnabled( false );
    		friendMenuItem.setEnabled( false );
        
        JMenuItem source = (JMenuItem)(e.getSource());
        
        
        if( CurrentPrivateStatus != 100 && !priUser[ target ].isOpen() 
                                        && source == friendMenuItem) // 100, default
               {
	               
	                       priUser[ target ].setOpen( true );
	                       badMenuItem.setEnabled( true );
	            }
	            
	     else if( CurrentPrivateStatus != 100 && priUser[ target ].isOpen() 
                                        && source == badMenuItem) // 100, default
               {
	               
	                       priUser[ target ].setOpen( false );
	                       friendMenuItem.setEnabled( true );
	            }
	   }
	            
    }
    
    private class PrivatePopupListener extends MouseAdapter { 
        JPopupMenu popup;

        PrivatePopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }

        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(),
                           e.getX(), e.getY());
            }
        }
    }
    
    private class ChannelPopupListener extends MouseAdapter { 
        JPopupMenu popup;

        ChannelPopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }

        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(),
                           e.getX(), e.getY());
            }
        }
    } // end class ChannelPopupListener
    
    private class ConnectListener implements ActionListener {
      
      // connect to server and enable/disable GUI components
      public void actionPerformed( ActionEvent event )
      {
      	 
         // connect to server and route messages to messageListener
         messageManager.connect( messageListener ); 

         // prompt for userName
         userName = JOptionPane.showInputDialog( 
            null, "Enter user name:" );
         
         // clear messageArea
         ChannelMessageArea.setText( "" );
         PrivateMessageArea.setText( "" );
         
         for( int i = 0; i < channel.length; i++ )
             channel[ i ].clearContents();
             
         for( int j = 0; j < channel.length; j++ )
             priUser[ j ].clearContents();
             
                      
         
         //messageManager.sendPhotoMessage( userName + "`", photoStatus,  "`#SET_PHOTO#" );
         
         
         //messageManager.sendMessage( 1, userName ); // add user list
       
         // update GUI components
         connectMenuItem.setEnabled( false );
         //connectMenuItem.setEnabled( false );
         disconnectMenuItem.setEnabled( true );
         //disconnectMenuItem.setEnabled( true );
         ChannelSendButton.setEnabled( true );
         PrivateSendButton.setEnabled( false );
         ChannelInputArea.setEditable( true );
         PrivateInputArea.setEditable( true );
         ChannelInputArea.requestFocus();  
         PrivateInputArea.requestFocus(); 
         //statusBar.setText( "Connected: " + userName ); 
         //PriStatusBar.setText( "Connected: " + userName );   
         
         f.setTitle( userName + "-Messenger" );      
         
         channel[ CurrentChannelStatus ].setOpen( true );
                  
                  
         
         messageManager.sendChannelMessage( userName + "`", 
         " 加入了頻道." + "`", CurrentChannelStatus + "#ChannelSendConnectMode#|*?+CaSe_1"  );//+  photoStatus + "#DEFAULT_SET_PHOTO#" );
         

         
                 
      }   
      
   } // end ConnectListener inner class
   
   // DisconnectListener listens for user requests to disconnect
   // from DeitelMessengerServer
   private class DisconnectListener implements ActionListener {
      
      // disconnect from server and enable/disable GUI components
      public void actionPerformed( ActionEvent event )
      {
      	   disconnect();
      }
      
      public void disconnect()
      {
      	try{
			    	urlLeave = new URL(this.getClass().getResource( "sound/music/leave.wav").toString());
			    	voiceGet=Applet.newAudioClip(urlLeave);
			    	voiceGet.play();
			    	}catch(Exception e3){}
			    	
      	 for( int i = 0; i < channel.length; i++ )
         {
         	 channel[ i ].setEmpty(); // set channel empty
         	 
             if ( channel[ i ].isOpen() )	
             {
             	   
                 messageManager.sendChannelMessage( userName + "`", 
                  " 離開了頻道." + "`", i + "#ChannelSendDisconnectMode#" );
             }
             
         }
         
         for( int i = 0; i < 65534; i++ )
         {}
              
              messageManager.sendChannelMessage( userName
              , "`", "|*?+CaSe_2" );
        
         messageManager.disconnect( messageListener );

         // update GUI componets
         ChannelSendButton.setEnabled( false );
         //disconnectButton.setEnabled( false );
         disconnectMenuItem.setEnabled( false );
         ChannelInputArea.setEditable( false );
         PrivateInputArea.setEditable( false );
         
         //connectButton.setEnabled( true );         
         connectMenuItem.setEnabled( true );
         f.setTitle( "Messenger-未登入" ); 
      }
      
   } // end DisconnectListener inner class
   
   // MyMessageListener listens for new messages from MessageManager and 
   // displays messages in messageArea using MessageDisplayer.
   private class MyMessageListener implements MessageListener {
   	
   	public void photoMessageReceived( String message )
   	{
   		        //messageManager.sendPhotoMessage( userName + "`", photoStatus,  "`#SET_PHOTO#" );
                StringTokenizer toz = new StringTokenizer( message, "`" );
                String targetName = "";
                int type = 0;
                while( true )
                {
                	String tmp;
                    targetName = toz.nextToken();
                    if( targetName.equals("#SET_PHOTO#" ) )
                        break;
                    else if( targetName.equals( s ) )
                        tmp = toz.nextToken();
                    else
                    {
                    	String t = toz.nextToken();
                        type = Integer.parseInt( t );
                        if( targetName.equals( userName ) )
                        {
                            savePhoto( targetName, type );
                            setMyIcon( type ); 
                        }
                        else
                            savePhoto( targetName, type );
                    }
                }
                
                setPhoto();
   	}
   	
   	public void killMessageReceived( String message )
   	  {
   	  	   if( message.indexOf( "#never#" ) >= 0 )
   	  	   {
   	  	   	   StringTokenizer toz = new StringTokenizer( message, "`" );
   	  	   	   if( userName.equals( toz.nextToken() ) )
   	  	   	       disconnectListener.disconnect();
   	  	   }
   	  }
       
      public void UserlistMessageReceived( String message )  // Pri, users[]
      {
      
         StringTokenizer tokenizer = new StringTokenizer( message, "`" );
          
              for ( int i = 0; i < 21; i++ )
              {
              	  String tmp = tokenizer.nextToken();
              	  
                      
                  PrivateUserlistField[ i ].setText( tmp );
                  
                  for ( int j = 0; j < 21; j++ )
                      if( priUser[ j ].getName().equals( s ) )
                      {
                          priUser[ j ].setName( tmp );
                          break;
                      }
                          
              }
              
          
              
      } 
      
      public void ChannelUserlistReceived( String message )  // Channel, users[]
      {
      	
      	  if( message.indexOf( "#ChannelSendConnectMode#" ) >= 0 )
      	  {
	          int code = message.indexOf( "#ChannelSendConnectMode#"  );
	          
	          String tmp = message.substring( ( code - 1 ), code );
	            int NO = Integer.parseInt(tmp);
	          
	          StringTokenizer toz = new StringTokenizer( message, "`" );
	          
	          
	          // add all users to channel[ NO ]
	          if( channel[ NO ].isOpen() )
	          {
	          	  channel[ NO ].appendContents( toz.nextToken() );
	          	  String targetName = toz.nextToken(); // no use, only for nextToken()
	          	  
	          	  for( int i = 0; i < ChannelUserlistField.length; i++ ) //  IDs.length, users Max NO. in channel 
	                  channel[ NO ].addUser( toz.nextToken() ); 
	          	  
	          	  if( ! ( channels[ NO ].getBackground() == Color.blue ) )
			             {
			             	 channels[ NO ].setBackground( Color.red );
			                 channels[ NO ].setForeground( Color.white);
			                 
			                 channel[ NO ].setRead( false );
			             }
			             
			             else
			             {
			             	ChannelMessageArea.setText( channel[ NO ].getContents() );
			             	 
			                 for( int j = 0; j < ChannelUserlistField.length; j++ ) // set IDs text
		      	                 ChannelUserlistField[ j ].setText( channel[ NO ].getUsers( j ) );
		      	                 
			             	 ChannelMessageArea.setCaretPosition( ChannelMessageArea.getText().length() );
			             }
		       }
		   }
		   
		   else if ( message.indexOf( "#ChannelSendDisconnectMode#" ) >= 0 )
		   {
	          int code = message.indexOf( "#ChannelSendDisconnectMode#" );
	          
	          String tmp = message.substring( ( code - 1 ), code );
	            int NO = Integer.parseInt(tmp);
	                  
	          
	          //int NO = Integer.parseInt( tmp.toString() );
	          StringTokenizer toz = new StringTokenizer( message, "`" );
	          
	          
	          
	          
	          // add all users to channel[ NO ]
	          if( channel[ NO ].isOpen() )
	          {
	              
	          	  channel[ NO ].appendContents( toz.nextToken() );
	          	  String targetName = toz.nextToken();
	              channel[ NO ].delUser( targetName ); 
	                  
	              for( int i = 0; i < ChannelUserlistField.length; i++ ) //  IDs.length, users Max NO. in channel 
	                  channel[ NO ].addUser( toz.nextToken() );
	              
	              if( ! ( channels[ NO ].getBackground() == Color.blue ) )
			      {
			          channels[ NO ].setBackground( Color.red );
			          channels[ NO ].setForeground( Color.white);
			          channel[ NO ].setRead( false );
			      }
			      else
			      {
			      	
			          ChannelMessageArea.setText( channel[ NO ].getContents() );
			             	 
			          for( int j = 0; j < ChannelUserlistField.length; j++ ) // set IDs text
		      	          ChannelUserlistField[ j ].setText( channel[ NO ].getUsers( j ) );
		      	                 
			          ChannelMessageArea.setCaretPosition( ChannelMessageArea.getText().length() );
			       }
	              
	               if( userName.equals( targetName ) )  // if  myself leave the channel
		      	   {
		      	       channel[ NO ].setOpen( false );  
		      	   }
	          }  
		      	  
		   } // end else if ( message.indexOf( "#ChannelSendDisconnectMode#" ) >= 0 )
		   	
      } // end ChannelUserlistReceived

      // when received, display new messages in messageArea
      public void PriMessageReceived( String from, String to, String message ) 
      {
         // append message using MessageDisplayer and
         // invokeLater, ensuring thread-safe access messageArea
         SwingUtilities.invokeLater( 
            new PriMessageDisplayer( from, to, message ) );
      } 
      
      public void ChannelMessageReceived( String from, String message, char channel_NO ) 
      {
         // append message using MessageDisplayer and
         // invokeLater, ensuring thread-safe access messageArea
         
         SwingUtilities.invokeLater( 
            new MessageDisplayer( from, channel_NO + "" , message ) );
      }
   }  
   
   // MessageDisplayer displays a new messaage by appending the message to 
   // the messageArea JTextArea. This Runnable object should be executed 
   // only on the Event thread, because it modifies a live Swing component
   private class MessageDisplayer implements Runnable {
      private String fromUser;
      private String messageBody;
      private int NO;
      
      // MessageDisplayer constructor
      
      public MessageDisplayer( String from, String channel_NO, String body )
      {
         fromUser = from;
         messageBody = body;
         NO = Integer.parseInt( channel_NO );
      }
      
      // display new message in messageArea
      public void run() 
      {
		  if ( channel[ NO ].isOpen() )
		  {
		      if ( messageBody.indexOf( "Disconnect" ) >= 0 )
			      channel[ NO ].delUser( fromUser );
			  else
			      channel[ NO ].addUser( fromUser );
			          	 
			  channel[ NO ].appendContents( fromUser + messageBody );
			      
			  if( ! ( channels[ NO ].getBackground() == Color.blue ) )
			  {
			      channels[ NO ].setBackground( Color.red );
			      channels[ NO ].setForeground( Color.white);
			                 
			      channel[ NO ].setRead( false );
			   }
			   else
			   {
			      ChannelMessageArea.setText( channel[ NO ].getContents() );
			      ChannelMessageArea.setCaretPosition( ChannelMessageArea.getText().length() );
			   }
			         
		    }                
      }      
      
   } // end MessageDisplayer inner class
   
   
      private class PriMessageDisplayer implements Runnable {
      private String fromUser;
      private String toUser;
      private String messageBody;
      private String fromUserName;

      
      // MessageDisplayer constructor
      public PriMessageDisplayer( String from, String to, String body )
      {
         fromUser = from;
         toUser = to;
         messageBody = body;
         fromUserName = from;
      }

      // display new message in messageArea
      public void run() 
      {
      	
      	 if ( userName.equals( fromUser ) )
      	 {
      	 	 String tmp = toUser;
      	 	 toUser = fromUser;
      	 	 fromUser = tmp;
      	 }
      	 	 
      	 // Pri
        
         	 int target = 0;
         	 //= findPriUsersOrder( fromUser )
         	 
         	 
         	 if( userName.equals( toUser ) )
         	 {
         	 	 boolean NextStep = true;
		         for( int i = 0; i < priUser.length; i++ )
		         {  // to find the specified id to talk with
		         	if ( priUser[ i ].getName().equals( fromUser ) )
		         	{
		         		target = i;
		         		NextStep = false;
		         		break;
		         	}
		         } 
		            // if failed, open a new one with code `.....`
		         if ( NextStep )
		         {
			         for( int j = 0; j < priUser.length; j++ )
			         {
			         	if ( priUser[ j ].getName().equals( s ) ) // s == " "
			         	{
			         		priUser[ j ].setName( fromUser );
			         		target = j;
			         		break;
			         	}
			         }
			     }
			     
			     
		         
		         if( priUser[ target ].isOpen() ) // if friend
		         {		
		             try{
			    	     urlReceive = new URL(this.getClass().getResource( "sound/music/message.wav").toString());
			    	 	 voiceGet=Applet.newAudioClip(urlReceive);  //指定歌曲給撥放源
			    	 	 voiceGet.play();  //撥一次
			    	 }catch(Exception e2){}	 
					 priUser[ target ].appendContents( fromUserName + messageBody );
			         //PriMessageArea.setText( priUsers[ target ].getContents() );
			         
			         int i = 0;
			         for ( ; i < PrivateUserlistField.length; i++ )
			             if ( PrivateUserlistField[ i ].getText().equals( fromUser ) )
			                 break;
			            
			    
			             	 if( ! ( PrivateUserlistField[ i ].getBackground() == Color.blue ) )
			             	 {
			             	 	 PrivateUserlistField[ i ].setBackground( Color.red );
			                     PrivateUserlistField[ i ].setForeground( Color.white);
			                 	 priUser[ target ].setRead( false );
			                 }
			                 else
			                 {
			                 	//PrivateUserlistField[ i ].setText( fromUser );
			                 	PrivateMessageArea.setText( priUser[ target ].getContents() );
			                 	priUser[ target ].setRead( true );
			                 }
			     } // end if( priUser[ target ].isOpen() ) 
			     
		         // move caret to end of messageArea to ensure new 
		         // message is visible on screen
		         //PriMessageArea.setCaretPosition( PriMessageArea.getText().length() );  
		     } // end if( userName.equals( toUser ) )
	     
	                      
      }      
      
   } // end MessageDisplayer inner class
   
   private class TimerClass implements ActionListener
   {
   	   public void actionPerformed( ActionEvent e )
   	   {
   	   	   timeLabel.setText( "現在時刻：" + new java.util.Date().toString());//顯示現在時間
   	   	   SecCount++;
   	   	   if( SecCount % 60==1)   //計算已進入多久了
		    {
		        idleLabel.setText( "您己經發呆了"+idleMinute+"分鐘" );	
		        idleMinute++;
			}
   	   }
    }
    
   private class IdleClass implements MouseListener, MouseMotionListener
   {
        public void mouseDragged(MouseEvent e) 
        {
        	resetIdle();
        }
        	
          
        public void mouseMoved(MouseEvent e) 
        {
        	resetIdle();
        } 
        
        public void mouseClicked( MouseEvent event )
        {
        	resetIdle();
        }
      
        public void mouseReleased( MouseEvent event )
        {
        	resetIdle();
        }
      
        public void mouseEntered( MouseEvent event )
        {
        	resetIdle();
        }
      
        public void mouseExited( MouseEvent event )
        {
        	resetIdle();
        }
      // process textfield events
        public void mousePressed( MouseEvent event )
        {
        	resetIdle();
        }
        
        public void resetIdle()
        {
        	idleMinute = 0;
        	idleLabel.setText( "您己經發呆了"+idleMinute+"分鐘" );
        }
    }
    
    public void itemStateChanged(ItemEvent e)
	{
		 S = e.getItem().toString();  //s=歌名
        
        	if(S=="卡農")
        	{
        		if(record!=0)   //如果之前撥放源已有歌在play，關掉它
        		{song.stop();}
        		try{
			    	urlOne = new URL(this.getClass().getResource( "sound/music/cl6_18.mid").toString());
			    
			    	song=Applet.newAudioClip(urlOne);   //指定歌曲給撥放源
			    	song.loop(); //循環撥
			    	record=1;
			    	}catch(Exception e4){}
        	}
      	    if(S=="鱒魚")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlTwo = new URL(this.getClass().getResource( "sound/music/ac3cl_18.mid").toString());
			    	song=Applet.newAudioClip(urlTwo);
			    	song.loop();
			    	record=2;
			    	}catch(Exception e5){}
        	}
        	if(S=="土耳其進行曲")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlThree = new URL(this.getClass().getResource( "sound/music/ac3cl_1.mid").toString());
			    	song=Applet.newAudioClip(urlThree);
			    	song.loop();
			    	record=3;
			    	}catch(Exception e6){}
        	}
        	if(S=="阿萊城姑娘")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlFour = new URL(this.getClass().getResource( "sound/music/cl5_19.mid").toString());
			    	song=Applet.newAudioClip(urlFour);
			    	song.loop();
			    	record=4;
			    	}catch(Exception e7){}
        	}
        	if(S=="仙劍奇俠傳")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlFive = new URL(this.getClass().getResource( "sound/music/ct11_17.mid").toString());
			    	song=Applet.newAudioClip(urlFive);
			    	song.loop();
			    	record=5;
			    	}catch(Exception e8){}
        	}
        	if(S=="命中註定")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlSix = new URL(this.getClass().getResource( "sound/music/ch45_6.mid").toString());
			    	song=Applet.newAudioClip(urlSix);
			    	song.loop();
			    	record=6;
			    	}catch(Exception e9){}
        	}
        	if(S=="新不了情")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlSeven = new URL(this.getClass().getResource( "sound/music/ch46_18.mid").toString());
			    	song=Applet.newAudioClip(urlSeven);
			    	song.loop();
			    	record=7;
			    	}catch(Exception e10){}
        	}
        	if(S=="還是會寂寞")
        	{
        		if(record!=0)
        		{song.stop();}
        		try{
			    	urlEight = new URL(this.getClass().getResource( "sound/music/ch32_7.mid").toString());
			    	song=Applet.newAudioClip(urlEight);
			    	song.loop();
			    	record=8;
			    	}catch(Exception e11){}
        	}
        	if(S=="關掉")
        	{
        		song.stop();   //關掉，reset record
        	    record=0;
        	}
        	
        	if(S=="小魚兒")
        	{
        		myPhoto.setIcon( fishIcon );
                messageManager.sendPhotoMessage( userName + "`", FISH, "#SET_PHOTO#" ); // FISH: 1
            }
            
            if(S=="海洋")
        	{
        		myPhoto.setIcon( oceanIcon );
                messageManager.sendPhotoMessage( userName + "`", OCEAN, "#SET_PHOTO#" ); // OCEAN : 2
            }
            
            if(S=="河流")
        	{
        		myPhoto.setIcon( riverIcon );
                messageManager.sendPhotoMessage( userName + "`", RIVER, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="圍棋")
        	{
        		myPhoto.setIcon( gotakIcon );
                messageManager.sendPhotoMessage( userName + "`", GOTAK, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="西洋棋")
        	{
        		myPhoto.setIcon( chessIcon );
                messageManager.sendPhotoMessage( userName + "`", CHESS, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="沙灘")
        	{
        		myPhoto.setIcon( sandIcon );
                messageManager.sendPhotoMessage( userName + "`", SAND, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="美女")
        	{
        		myPhoto.setIcon( sexIcon );
                messageManager.sendPhotoMessage( userName + "`", SEX, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="小甜甜")
        	{
        		myPhoto.setIcon( sweetIcon );
                messageManager.sendPhotoMessage( userName + "`", SWEET, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="花兒")
        	{
        		myPhoto.setIcon( flowerIcon );
                messageManager.sendPhotoMessage( userName + "`", FLOWER, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
            if(S=="天空")
        	{
        		myPhoto.setIcon( skyIcon );
                messageManager.sendPhotoMessage( userName + "`", SKY, "#SET_PHOTO#" ); //  RIVER: 3
            }
            
	  
	}
	
	public void savePhoto( String targetName, int type )
	{
		for( int i = 0; i < PrivateUserlistField.length; i++ )
		{
			if(  priUser[ i ].getName().equals( targetName ) )
			{
				priUser[ i ].setPhotoStatus( type );
			    break;
			}       
        } 
    }
	
	public void setPhoto()
	{
		yourPhotoLabel.setText( PrivateUserlistField[ CurrentPrivateStatus ].getText() 
		                        + "的圖片" );
		String targetName = PrivateUserlistField[ CurrentPrivateStatus ].getText();
		for( int i = 0; i < priUser.length; i++ )
		{
			
			if( priUser[ i ].getName().equals( targetName ) )
			{
				int type = priUser[ i ].getPhotoStatus();
                	if( type == FISH )
                	{
                		yourPhoto.setIcon( fishIcon );
                	}
                	
                	else if ( type == OCEAN )
                	{
                		yourPhoto.setIcon( oceanIcon );
                	}
                	
                	else if ( type == RIVER )
                	{
                		yourPhoto.setIcon( riverIcon );
                	}
                	else if ( type == GOTAK )
                	{
                		yourPhoto.setIcon( gotakIcon );
                	}
                	else if ( type == CHESS )
                	{
                		yourPhoto.setIcon( chessIcon );
                	}
                	
                	else if ( type == SAND )
                	{
                		yourPhoto.setIcon( sandIcon );
                	}
                	
                	else if ( type == SEX )
                	{
                		yourPhoto.setIcon( sexIcon );
                	}
                	
                	else if ( type == SWEET )
                	{
                		yourPhoto.setIcon( sweetIcon );
                	}
                	
                	else if ( type == FLOWER )
                	{
                		yourPhoto.setIcon( flowerIcon );
                	}
                	
                	else if ( type == SKY )
                	{
                		yourPhoto.setIcon( skyIcon );
                	}
                	
                	break;
             }
             
        }
                
	}
	
	public void setDefaultPhoto()
	{
	    PhotoMenu.add("請選擇圖片");
		PhotoMenu.add("小魚兒");
		PhotoMenu.add("海洋");
		PhotoMenu.add("河流");
		PhotoMenu.add("圍棋");
		PhotoMenu.add("西洋棋");
		PhotoMenu.add("沙灘");
		PhotoMenu.add("美女");
		PhotoMenu.add("小甜甜");
		PhotoMenu.add("花兒");
		PhotoMenu.add("天空");
		
		
		PhotoMenu.addItemListener(this);
		myPhotoIcon = new ImageIcon( 
                                    getClass().getResource( "images/gotak.gif" ) );   
         
          
         
        fishIcon = new ImageIcon( 
                                     getClass().getResource( "images/fish.gif" ) );
        oceanIcon = new ImageIcon( 
                                     getClass().getResource( "images/ocean.gif" ) );
        riverIcon = new ImageIcon( 
                                     getClass().getResource( "images/river.gif" ) ) ; 
        gotakIcon = new ImageIcon( 
                                     getClass().getResource( "images/gotak.gif" ) ) ; 
        chessIcon = new ImageIcon( 
                                     getClass().getResource( "images/chess.gif" ) ) ; 
        sandIcon = new ImageIcon( 
                                     getClass().getResource( "images/sand.gif" ) ) ; 
        sexIcon = new ImageIcon( 
                                     getClass().getResource( "images/sex.gif" ) ) ; 
        sweetIcon = new ImageIcon( 
                                     getClass().getResource( "images/sweet.gif" ) ) ; 
        flowerIcon = new ImageIcon( 
                                     getClass().getResource( "images/flower.gif" ) ) ; 
        skyIcon = new ImageIcon( 
                                     getClass().getResource( "images/sky.gif" ) ) ;
                                      
                                     
        myPhoto.setIcon( gotakIcon );
        yourPhoto.setIcon( gotakIcon );
        setPhoto();                            
     }
     
     public void setMyIcon( int type )
	{
		            if( type == FISH )
                	{
                		photoStatus = FISH;
                		myPhoto.setIcon( fishIcon );
                	}
                	
                	else if ( type == OCEAN )
                	{
                		photoStatus = OCEAN;
                		myPhoto.setIcon( oceanIcon );
                	}
                	
                	else if ( type == RIVER )
                	{
                		photoStatus = RIVER;
                		myPhoto.setIcon( riverIcon );
                	}
                	else if ( type == GOTAK )
                	{
                		photoStatus = GOTAK;
                		myPhoto.setIcon( gotakIcon );
                	}
                	else if ( type == CHESS )
                	{
                		photoStatus = CHESS;
                		myPhoto.setIcon( chessIcon );
                	}
                	
                	else if ( type == SAND )
                	{
                		photoStatus = SAND;
                		myPhoto.setIcon( sandIcon );
                	}
                	
                	else if ( type == SEX )
                	{
                		photoStatus = SEX;
                		myPhoto.setIcon( sexIcon );
                	}
                	else if ( type == SWEET )
                	{
                		photoStatus = SWEET;
                		myPhoto.setIcon( sweetIcon );
                	}
                	else if ( type == FLOWER )
                	{
                		photoStatus = FLOWER;
                		myPhoto.setIcon( flowerIcon );
                	}
                	else if ( type == SKY )
                	{
                		photoStatus = SKY;
                		myPhoto.setIcon( skyIcon );
                	}
                	
	}


}