package server;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

// -1:terminate  0:聊天內容   1:聊天室成員  2:悄悄話　3:建新room  4:換room
// 5:所有clients  6:傳送檔案　I:同名使用者

public class Server extends JFrame
{
	private JTextField enter;
	private JComboBox target1;	// 選擇要傳廣播、單人還是聊天室
	private JComboBox target2;	// 選擇對象
	private JTextArea display;
	private JList memList;		// 顯示現有clients
	private JTextArea memData;	// 顯示現有clients詳細資料
	private JButton memKick;	// 踢掉使用者
	private JList roomList;		// 顯示現有聊天室
	private JTextArea roomData;	// 顯示聊天室成員
//	private JButton roomClose;	
	private JPanel left, left1, left2, top, top1, bottom;
	
	private MultiClient clients[];
	private String username[];
	private boolean isActive[];	// 判斷client是否為active
	private Chatroom rooms[];
	private String roomName[];
	private boolean isOpen[];	// 判斷聊天室是否關閉
	private String message = "";
	private int counter = 0;
	private int roomNum = 1;	// 一定有預設聊天室
	
	private Handler h;
	
	public Server()
	{
		super("Server");
		
		clients = new MultiClient[ 10 ];	// 同時能有的clients數上限
		username = new String[ clients.length ];
		isActive = new boolean[ clients.length ];
		rooms = new Chatroom[ 10 ];    // 同時能有的聊天室上限
		roomName = new String[ rooms.length ];
		isOpen = new boolean[ rooms.length ];
		h = new Handler();
				
		Container c = getContentPane();

		top = new JPanel();
		top1 = new JPanel();
		top1.setLayout( new GridLayout( 1, 2 ));
		enter = new JTextField( 40 );
		enter.setEnabled(true);
		enter.addActionListener( h );
		target1 = new JComboBox();
		target1.addItem( "廣播　" );
		target1.addItem( "單人　" );
		target1.addItem( "聊天室" );
		target1.addItemListener( h );
		target2 = new JComboBox();
		target2.addItem( "廣播　　　" );
		target2.setEnabled( false );
		target2.addItemListener( h );
		top1.add( target1 );
		top1.add( target2 );
		top.add( top1 );
		top.add( enter );
		c.add( top, BorderLayout.NORTH );
		
		display = new JTextArea( "Waiting for connection\n" );
		display.setEditable( false );
		
		left = new JPanel();
		left.setLayout( new GridLayout( 2, 2 ));
		left1 = new JPanel();
		left1.setLayout( new BorderLayout() );
		left2 = new JPanel();
		left2.setLayout( new BorderLayout() );
		
		memList = new JList();
		memList.addListSelectionListener( h );
		memData = new JTextArea();
		memData.setEditable( false );
		memKick = new JButton( "踢掉" );
		memKick.addActionListener( h );
		left1.add( new JScrollPane( memData ), BorderLayout.CENTER );
		left1.add( memKick, BorderLayout.SOUTH );
		left.add( new JScrollPane( memList ) );
		left.add( left1 );
		
		roomList = new JList();
		roomList.addListSelectionListener( h );
		roomData = new JTextArea();
		roomData.setEditable( false );
//		roomClose = new JButton( "關閉" );
//		roomClose.addActionListener( h );
		left2.add( new JScrollPane( roomData ), BorderLayout.CENTER );
//		left2.add( roomClose, BorderLayout.SOUTH );
		left.add( new JScrollPane( roomList ) );
		left.add( left2 );
		
		bottom = new JPanel();
		bottom.setLayout( new GridLayout( 1, 2 ));
		bottom.add( left );
		bottom.add( new JScrollPane( display ) );
		c.add( bottom, BorderLayout.CENTER );
		
		setSize( 650, 400 );
		setVisible( true );
		
		addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				for( int i = 0; i < clients.length; i++ )
					if( isActive[i] )
					{
						try{
							clients[i].output.writeObject( "-1" );
							clients[i].output.flush();
						}
						catch( IOException cnfex ){
							display.append("\nError writing object");
						}
					}
				System.exit(0);
			}
		});
	}
	
	public void runServer()
	{
		ServerSocket server;
		Socket temp;    // clients數滿時，通知無法登入訊息用
		rooms[ 0 ] = new Chatroom( this, "預設聊天室" );
		isOpen[ 0 ] = true;
		roomName[ 0 ] = "預設聊天室";
		String[] tmpR = { "預設聊天室" };
		roomList.setListData( tmpR );
		
		try{
			//Serversocket( port, queue length )
			server = new ServerSocket( 5000 );
			
			for( int i = 0; i < clients.length; i++ )
			{
				clients[i] = new MultiClient( server.accept(), this, i );
				clients[i].start();
				counter++;
				isActive[i] = true;
				message = username[i] + "登入\n";
				sendtoAll( message );
				sendList();
				rooms[ 0 ].add( i );
				
				String memL[] = new String[ counter ];
				for( int j = 0, k = 0; k < counter; j++ )
					if( isActive[j] )
						memL[ k++ ] = username[j];
				memList.setListData( memL );
				
				if( target1.getSelectedIndex() == 1 )
					target2.addItem( username[i] );
			}
				
			while( true )
			{
				temp = server.accept();
				if( counter >= clients.length )
				{
					ObjectOutputStream tmpOut = new ObjectOutputStream(
						temp.getOutputStream() );
					tmpOut.flush();
					ObjectInputStream tmpIn = new ObjectInputStream(
						temp.getInputStream() );
					
					tmpIn.readObject();
					tmpOut.writeObject( "0" + "SERVER>>> Client數已到上限: "
							+ counter + "，請稍後再試。\n" );
					tmpOut.writeObject( "-1" );
					tmpOut.flush();
					tmpOut.close();
					tmpIn.close();
					temp.close();	
					
					display.append( "Client數已到上限: " + counter );
					display.setCaretPosition( display.getText().length() );
				}
				else
				{
					int i = 0;
					while( isActive[i] )
						i++;
					
					clients[i] = new MultiClient( temp, this, i );
					clients[i].start();
					counter++;
					isActive[i] = true;
					message = username[i] + "登入\n";
					sendtoAll( message );
					sendList();
					rooms[ 0 ].add(i);
					
					String memL[] = new String[ counter ];
					for( int j = 0, k = 0; k < counter; j++ )
						if( isActive[j] )
							memL[ k++ ] = username[j];
					memList.setListData( memL );
					
					if( target1.getSelectedIndex() == 1 )
						target2.addItem( username[i] );
				}
			}				
		}
		catch( EOFException eof ){
			System.out.println( "Client terminated connection" );
		}
		catch( IOException io ){
			io.printStackTrace();
		}
		catch( ClassNotFoundException cnfex ){
			display.append( "\nUnknown object type received" );
		}
	}
	
	private void sendtoRoom( String s, int room, int num )
	{
		message = s;
		int m[] = rooms[ room ].members;
			
		for( int i = 0; m[i] != -1; i++ )
			if( m[i] != num && isActive[ m[i] ] )
			{
				try{
					clients[ m[i] ].output.writeObject( "0" + message );
					clients[ m[i] ].output.flush();
				}
				catch( IOException cnfex ){
					display.append( "\nError writing object" );
				}
			}
		
		display.append( "\n" + message );
		display.setCaretPosition( display.getText().length() );
	}
		
	private void sendtoAll( String s )
	{
		message = "\nSERVER>>> " + s;
		for( int i = 0; i < clients.length; i++ )
			if( isActive[i] )
			{
				try{
					clients[i].output.writeObject( "0" + message );
					clients[i].output.flush();
				}
				catch( IOException cnfex ){
					display.append("\nError writing object");
				}
			}
		
		display.append( "\n" + message );
		display.setCaretPosition( display.getText().length() );
	}
	
	public void sendtoOne( int num, String s )
	{
		String sendto = s.substring( 0, s.indexOf('\n') );
		message = s.substring( s.indexOf('\n') + 1 );
		
		try{
			int i = 0;
			while( (i < username.length ) && !sendto.equals( username[i]) )
				i++;
			if( i == username.length )
			{
				clients[ num ].output.writeObject( "0" + "對方不在線上！" );
				return;
			}
			if( isActive[i] )
			{
					clients[i].output.writeObject( "2" + message );
					clients[i].output.flush();
			}
		}
		catch( IOException cnfex ){
			display.append( "\nError writing object" );
		}
	}
	
	public void showText( String s )
	{
		message = s;
		display.append( "\n" + message );
		display.setCaretPosition( display.getText().length() );		
	}
	
	public void close( int num )
	{
		if( target1.getSelectedIndex() == 1 )
			target2.removeItem( username[ num ] );
		isActive[ num ] = false;
		username[ num ] = "";
		counter--;
		rooms[ clients[ num ].room ].delete( num );
		sendList();
		
//		memList.setText(temp);
		String memL[] = new String[ counter ];
		for( int j = 0, k = 0; k < counter; j++ )
			if( isActive[j] )
				memL[ k++ ] = username[j];
		memList.setListData( memL );
	}
	
	public void sendList()    // 送全部名單的
	{
		String list = "5";
		for( int i = 0; i < clients.length; i++ )
			if( isActive[i] )
				list += "\n" + username[ i ];
		
		for( int i = 0; i < clients.length; i++ )
			if( isActive[ i ] )
			{
				try{
					clients[ i ].output.writeObject( list );
					clients[ i ].output.flush();
				}
				catch( IOException cnfex ){
					display.append( "\nError writing object" );
				}
			}
	}
	
	public void sendList( int m[] )	  // 送所在聊天室名單的
	{
		String list = "1";
		for( int i = 0; m[i] != -1; i++ )
			list += username[ m[i] ] + "\n";
		
		for( int i = 0; m[i] != -1; i++ )
			if( isActive[ m[i] ] )
			{
				try{
					clients[ m[i] ].output.writeObject( list );
					clients[ m[i] ].output.flush();
				}
				catch( IOException cnfex ){
					display.append( "\nError writing object" );
				}
			}		
	}
	
	public void createRoom( int num, String name )
	{
		try{
			if( roomNum < rooms.length )
			{
				int k = 0;
				while( isOpen[ k ] )
					k++;
				rooms[ k ] = new Chatroom( this, name );
				isOpen[ k ] = true;
				roomName[ k ] = name;
				roomNum++;
				
				for( int i = 0; i < clients.length; i++ )
					if( isActive[i] )
					{
						try{
							clients[i].output.writeObject( "31" + name );
							clients[i].output.writeObject(
								"0" + "\n新聊天室建立：" + name );
							clients[i].output.flush();
						}
						catch( IOException cnfex ){
							display.append( "\nError writing object" );
						}
					}
					
//				String tmpR[] = new String[ roomNum ];
//				for( int j = 0; j < rooms.length; j++ )
//					if( isOpen[j] )
//						tmpR[j] = rooms[j].name;
//				roomList.setListData( tmpR );
				roomList.setListData( roomName );
				
				if( target1.getSelectedIndex() == 2 )
					target2.addItem( name );
			}
			else
			{
				clients[ num ].output.writeObject( "30" + "聊天室數目已滿！" );
				clients[ num ].output.flush();
			}
		}
		catch( IOException cnfex ){
			display.append( "\nError writing object" );
		}
	}
	
	public void changeRoom( int num, int oldRm, String newRm )
	{
		try{
			int i = 0;
			while( i < rooms.length && !(roomName[i].equals( newRm )) )
				i++;
			if( i == rooms.length )
			{
				clients[ num ].output.writeObject( "40" + "該聊天室已關閉！" );
				return;
			}
			if( rooms[i].counter < rooms[i].members.length )
			{
				rooms[ oldRm ].delete( num );
				rooms[i].add( num );
				clients[ num ].room = i;
				clients[ num ].output.writeObject( "41" + "已換至 " + newRm );
				if( username[ num ].equals( (String)memList.getSelectedValue() ) )
					memData.setText(
						"ID:" + username[ num ] +"\nIP:" + clients[ num ].IP 
						+ "\nChatroom:"	+ newRm );
			}
			else
				clients[ num ].output.writeObject( "40" + "人數已滿，無法切換！" );
		}
		catch( IOException cnfex ){
			display.append( "\nError writing object" );
		}
	}
	
//	public void closeRoom( int Rm )
//	{
//		if( target1.getSelectedIndex() == 2 )
//			target2.removeItem( roomName[ Rm ]);
//		int num;
//		for( int i = 0; i < rooms[ Rm ].counter; i++ )
//		{
//			num = rooms[ Rm ].members[i];
//			rooms[ 0 ].add( num );
//			clients[ num ].room = 0;
//			rooms[ Rm ].members[i] = -1;
//		}
//		roomName[ Rm ] = "";
//		isOpen[ Rm ] = false;
//		roomNum--;
//		roomList.setListData( roomName );
//	}
	
	public void transFile( int num, String s )
	{
		try{
			String temp = s.substring( 0, s.indexOf('\n') );
			int sendto = 0;
			while( ( sendto < username.length ) && !temp.equals( username[ sendto ]) )
				sendto++;
//			if( sendto == username.length )
//			{
//				clients[num].output.writeObject( "0" + "對方不在線上！" );
//				return;
//			}
			String filename = s.substring( s.indexOf('\n') + 1 );
//			int sendto = 1;
			
			ServerSocket skt = new ServerSocket( 4500 );
			Socket recSkt = skt.accept();
			clients[ sendto ].output.writeObject( "6" + filename );
			Socket sendSkt = skt.accept();
			
			showText( username[ num ] + " 傳送 " + filename + " 給 " + username[ sendto ]);
			TransFile trans = new TransFile( recSkt, sendSkt, filename, this );
			trans.start();
			skt.close();
		}
		catch( IOException e ){ 
            System.out.println( e.toString() ); 
        }
	}
	
	public void transMedia( int num, String s, boolean active )
	{
		showText( s );

		try{
			if( active )
			{
				String name = s.substring( 0, s.indexOf('\n') );
				String port = s.substring( s.indexOf('\n') + 1 );
				int sendto = 0;
				while( ( sendto < username.length ) && !name.equals(username[sendto]) )
					sendto++;
				if( sendto == username.length )
				{
					clients[ num ].output.writeObject( "70" );
					return;
				}
				else
				{
					clients[ sendto ].output.writeObject(
						"72"+ username[ num ] +"\n"+ clients[ num ].IP +
						"\n" + port );
				}
			}
			else
			{
				clients[ num ].output.writeObject( "7" + s );
			}
		}
		catch( IOException e ){
			System.out.println( e.toString() );
		}
	}
	
	public void transAudio( int num, String s, boolean active )
	{
		showText( s );

		try{
			if( active )
			{
				String name = s.substring( 0, s.indexOf('\n') );
				String port = s.substring( s.indexOf('\n') + 1 );
				int sendto = 0;
				while( ( sendto < username.length ) && !name.equals(username[sendto]) )
					sendto++;
				if( sendto == username.length )
				{
					clients[ num ].output.writeObject( "80" );
					return;
				}
				else
				{
					clients[ sendto ].output.writeObject(
						"82"+ username[ num ] +"\n"+ clients[ num ].IP +
						"\n" + port );
				}
			}
			else
			{
				clients[ num ].output.writeObject( "8" + s );
			}
		}
		catch( IOException e ){
			System.out.println( e.toString() );
		}
	}
	
	public void transMovie( int num, String s, boolean active )
	{
		showText( s );

		try{
			if( active )
			{
				String name = s.substring( 0, s.indexOf('\n') );
				String port = s.substring( s.indexOf('\n') + 1 );
				int sendto = 0;
				while( ( sendto < username.length ) && !name.equals(username[sendto]) )
					sendto++;
				if( sendto == username.length )
				{
					clients[ num ].output.writeObject( "90" );
					return;
				}
				else
				{
					clients[ sendto ].output.writeObject(
						"92"+ username[ num ] +"\n"+ clients[ num ].IP +
						"\n" + port );
				}
			}
			else
			{
				clients[ num ].output.writeObject( "9" + s );
			}
		}
		catch( IOException e ){
			System.out.println( e.toString() );
		}
	}
	
	public static void main( String args[] )
	{
		Server app = new Server();
		app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		app.runServer();
	}

/*************************************************************/
	
	class MultiClient extends Thread
	{
		private Socket connection;
		private ObjectInputStream input;
		private ObjectOutputStream output;
		private Server control;
		String message = "";
		private int number;
		public int room = 0;
		private String IP;
		
		public MultiClient( Socket s, Server t, int num )
		{
			control = t;
			connection = s;
			number = num;
			
			try{
				input = new ObjectInputStream( connection.getInputStream() );
				output = new ObjectOutputStream( connection.getOutputStream() );
				String temp = ( String )input.readObject();
				for( int i = 0; i < username.length; i++ )
					if( temp.equals( username[i] ) && isActive[i] )
					{
						output.writeObject("I");
						temp = ( String )input.readObject();
						i = -1;
					}
				username[ num ] = temp;
				output.writeObject( "\n已成功登入預設聊天室，若欲換聊天室請按右側選單" );
				for( int i = 0; i < control.roomNum; i++ )
				{
					output.writeObject( "31" + control.rooms[i].name );
				}
			}
			catch( IOException e ){
				e.printStackTrace();
				System.exit(1);
			}
			catch( ClassNotFoundException cnfex ){
				display.append( "\nUnknown username" );
			}
			
			IP = connection.getInetAddress().getHostAddress();
			control.showText( "Connection " + number + " received from: " + IP );
		}
		
		public void run()
		{
			try{
				char type;
				do
				{
					message = ( String )input.readObject();
					
					type = message.charAt(0);
					message = message.substring(1);
					if( type == '-' )
					{
						output.writeObject( "-1" );
						break;
					}
					switch( type )
					{
						case '0':
							control.sendtoRoom( message, room, number );
							break;
						case '2':
							control.sendtoOne( number, message );
							break;
						case '3':
							control.createRoom( number, message );
							break;
						case '4':
							control.changeRoom( number, room, message );
							break;
						case '6':
							control.transFile( number, message );
							break;
						case '7':
							if( message.charAt(0) == '2' )
								control.transMedia( number,
									message.substring(1), true );
							else
							{
								int sendto = 0;
//								System.out.println(message);
								while( ( sendto < username.length ) &&
										!message.substring(1).equals(username[sendto]) )
									sendto++;
								String s = message.charAt(0) + IP;
								transMedia( sendto, s, false );
							}
							break;
						case '8':
							if( message.charAt(0) == '2' )
								control.transAudio( number,
									message.substring(1), true );
							else
							{
								int sendto = 0;
//								System.out.println(message);
								while( ( sendto < username.length ) &&
										!message.substring(1).equals(username[sendto]) )
									sendto++;
								String s = message.charAt(0) + IP;
								transAudio( sendto, s, false );
							}
							break;
						case '9':
							if( message.charAt(0) == '2' )
								control.transMovie( number,
									message.substring(1), true );
							else
							{
								int sendto = 0;
//								System.out.println(message);
								while( ( sendto < username.length ) &&
										!message.substring(1).equals(username[sendto]) )
									sendto++;
								String s = message.charAt(0) + IP;
								transMovie( sendto, s, false );
							}
							break;
						default:
							break;
					}
				} while( true );
			
				control.showText( "Connection " + number + " terminated" );
			
				output.close();
				input.close();
				connection.close();
				control.close( number );
			}
			catch( IOException io ){
				io.printStackTrace();
			}
			catch( ClassNotFoundException cnfex ){
				message = "\nUnknown object type received";
			}
		}

		
//			String input = s.substring( s.indexOf('\n') + 1 );
//			try{
//				File reFile = new File( s.substring( 0, s.indexOf('\n') ) );
//				FileWriter fw = new FileWriter( reFile );
//				showText( reFile.getName() );
//				showText(  "me : "+input );
//				fw.write( input );
//				showText(  "me : "+input );
//				fw.close();
//			}
//			catch( IOException e )
//			{
//				e.printStackTrace();
//			}
		
//		public void reset( Socket s )
//		{
//			connection = s;
//			room = 0;
//			
//			try{
//				input = new ObjectInputStream( connection.getInputStream() );
//				output = new ObjectOutputStream( connection.getOutputStream() );
//				username[ number ] = (String)input.readObject();
//				output.writeObject("\n已成功登入預設聊天室，若欲換聊天室請按右側選單");
//				for( int i = 0; i < control.roomNum; i++ )
//				{
//					output.writeObject( "31" + control.rooms[i].name );
//				}
//			}
//			catch( ClassNotFoundException cnfex ){
//				display.append("\nUnknown object type received");
//			}
//			catch( IOException e ){
//				e.printStackTrace();
//				System.exit(1);
//			}
//			
//			IP = connection.getInetAddress().getHostName();
//			control.showText( "Connection " + number + " reset from: " + IP );			
//		}
	}
	
/*************************************************************/

	class Chatroom
	{
		private Server control;
		private int members[];
		private int counter = 0;
//		private int number;
		private String name;
		
		public Chatroom( Server s, String n )
		{
			control = s;
			name = n;
			members = new int[ 10 ];
			for( int i = 0; i < members.length; i++ )
				members[i] = -1;
		}
		
		public void add( int num )
		{
			members[ counter++ ] = num;
			control.sendList( members );
			
			if( name.equals( (String)control.roomList.getSelectedValue() ) )
			{
				String tmp = "";
				for( int j = 0; j < counter; j++ )
					tmp += "\n" + control.username[ members[j] ];
				roomData.setText( "Room:" + name +"\nMembers:" + tmp);
			}
		}
		
		public void delete( int num )
		{
			if( counter > 1 )
			{
				int i = 0;
				while( members[i] != num )
					i++;			
				members[i] = members[ --counter ];
				members[ counter ] = -1;
			}
			else
			{
				members[0] = -1;
				counter = 0;
			}
			control.sendList( members );
			
			if( name.equals( (String)control.roomList.getSelectedValue() ) )
			{
				String tmp = "";
				for( int j = 0; j < counter; j++ )
					tmp += "\n" + control.username[ members[j] ];
				roomData.setText( "Room:" + name +"\nMembers:" + tmp);
			}
		}
	}

/*************************************************************/
	
	class Handler implements ActionListener, ListSelectionListener, ItemListener
	{
		private int type = 0;	//0:廣播 1:單人 2:聊天室
		private int sendto = 0;
//		private boolean isEnable = false;
		
		public void actionPerformed( ActionEvent e )
		{
			try{
				if( e.getSource() == enter )
				{
					String s = "";
					switch( type )
					{
						case 0:
							sendtoAll( "(廣播)" + e.getActionCommand() );
							break;
						case 1:
							s = ( String ) target2.getSelectedItem();
							sendto = 0;
							while( !s.equals( username[ sendto ] ) )
								sendto++;
							clients[sendto].output.writeObject(
									"0\nSERVER>>> " + e.getActionCommand());
							break;
						case 2:
							s = ( String ) target2.getSelectedItem();
							sendto = 0;
							while( !s.equals( roomName[ sendto ] ) )
								sendto++;
							sendtoRoom(
								"SERVER>>> "+ e.getActionCommand(), sendto, -1 );
							break;
						default:
							break;
					}
					enter.setText("");
				}
			
				if( e.getSource() == memKick && !memList.isSelectionEmpty() )
				{
					String s = ( String ) memList.getSelectedValue();
					int num = memList.getSelectedIndex();
					while( username[ num ] != s )
						num++;
					clients[ num ].output.writeObject( "-1"  );
				}
			}
			catch( EOFException eof ){
				System.out.println( "Client terminated connection" );
			}
			catch( IOException io ){
				io.printStackTrace();
			}
		}
		
		public void itemStateChanged( ItemEvent e )
		{
			if( e.getSource() == target1 )
			{
				type = target1.getSelectedIndex();
//				isEnable = false;
				target2.removeAllItems();
				
				switch( type )
				{
					case 0:
						target2.addItem( "廣播　　　" );
						if( counter > 0 )
							enter.setEnabled( true );
						else
							enter.setEnabled( false );
						target2.setEnabled( false );
						break;
					case 1:
						if( counter == 0 )
						{
							target2.addItem( "　　　　　" );
							enter.setEnabled( false );
							target2.setEnabled( false );
							break;
						}
						for( int i = 0; i < username.length; i++ )
							if( isActive[i] )
								target2.addItem( username[i] );
						enter.setEnabled( true );
						target2.setEnabled( true );
						break;
					case 2:
						for( int i = 0; i < rooms.length; i++ )
							if( isOpen[i])
								target2.addItem( roomName[i] );
						if( counter > 0 )
							enter.setEnabled( true );
						else
							enter.setEnabled( false );
						target2.setEnabled( true );
						break;
					default:
						break;
				}
//				isEnable = true;
			}
		}
		
		public void valueChanged( ListSelectionEvent e )
		{
			if( e.getSource() == memList )
			{
				if( memList.isSelectionEmpty() )
				{
					memData.setText("");
					return;
				}
				String s = ( String ) memList.getSelectedValue();
				int num = 0;
				while( username[ num ] != s )
					num++;
				memData.setText(
					"ID:" + s +"\nIP:" + clients[num].IP + "\nChatroom:"
					+ rooms[ clients[ num ].room ].name );
			}
			else if( e.getSource() == roomList )
			{
				if( roomList.isSelectionEmpty() )
				{
					roomData.setText("");
					return;
				}
				String s = ( String ) roomList.getSelectedValue(), tmp = "";
				int num = roomList.getSelectedIndex();
				while( rooms[ num ].name != s )
					num++;
				for( int i = 0; i < rooms[ num ].counter; i++ )
					tmp += "\n" + username[ rooms[ num ].members[i] ];
				roomData.setText(
					"Room:" + rooms[ num ].name + "\nMembers:" + tmp );
			}
		}
	}

}
