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


public class Gui implements WindowListener{
private JFrame fr;
private JMenuBar bar;
private JMenu fileMenu;
private JMenuItem connectItem,disconnectItem,exitItem ;
private JPanel p2;

private JList list;
private JTextArea ta;

private Vector listItem = new Vector();

String message="";
private String logText="";

//*********************************************

private static ServerSocket serverSocket;
private static int port = 5555;

private Socket clientSocket;
private Gui gui;


/****************************************************/






public Gui () {
bar = new JMenuBar();
fileMenu = new JMenu("File");

connectItem = new JMenuItem("Connect");
connectItem.setMnemonic('C');
connectItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
openConnection();
fr.repaint();
}
});



disconnectItem = new JMenuItem("Disconnect");
disconnectItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event2)
{
enableConnect();
ta.append("\nTerminating Server Connection");
disconnectServer();
}
});
disconnectItem.setMnemonic('D');



exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});

fileMenu.add(connectItem);
fileMenu.add(disconnectItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);

list = new JList(listItem);
list.setFixedCellWidth(100);
list.setFixedCellHeight(16);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(10);


ta= new JTextArea(10,30);
ta.setEditable(false);

p2 = new JPanel();
p2.add(new JScrollPane(list) );
p2.add(new JScrollPane(ta) );

bar.add(fileMenu);
launchFrame();
}

private void launchFrame()
{
fr = new JFrame();
fr.addWindowListener(this);
fr.setJMenuBar(bar);
fr.getContentPane().add(p2);

fr.pack();
fr.setVisible(true);
openConnection();
}

public static void main(String args[])
{
Gui gui = new Gui();
}


public void showMessage(String str)
{
message = "\n"+" "+str;
ta.append(message);
}

public void showServerStatusMessage(String str)
{
message = "\n"+" "+str;
ta.append(message);
}

private void enableConnect()
{
connectItem.setEnabled(true);
disconnectItem.setEnabled(false);
}

private void disableConnect()
{
connectItem.setEnabled(false);
disconnectItem.setEnabled(true);
}


//***************
public void openConnection()
{
disableConnect();
ta.append("\nConnecting Server...");
connectServer();
//Cannot Enable this function
getClientConnection();/**************/
}

public void connectServer()
{
try {
serverSocket = new ServerSocket(port);
InetAddress addr = serverSocket.getInetAddress().getLocalHost() ;
String message="\n\t***Server Information***"+
"\n\tHostName : "+addr.getHostName()+
"\n\tIP Address : "+addr.getHostAddress()+
"\n\tPort :"+serverSocket.getLocalPort()+"\n";
System.out.println(message);
showServerStatusMessage(message);
}catch (IOException e){
System.err.println("Error Creating Server Thread.!\nPlease try again.\n"+e);
showMessage("Error Creating Server Thread.!\nPlease try again.\n"+e);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}


public void getClientConnection () {
do {
try {
System.out.println("Waiting for client");
//Problem Started here
clientSocket = serverSocket.accept();/**********/

}catch (IOException e){
System.err.println("ClientSocket Disconnected.");
break;
}
catch(Exception ex){
ex.printStackTrace();
}
}while(!serverSocket.isClosed());
}


public void disconnectServer()
{
try{

serverSocket.close();
}catch(IOException e)
{
showMessage("Failed to disconnect server.");
}
catch(Exception ex)

{
System.out.println("Error");
}
}
//***************************************************************************


public void windowClosing(WindowEvent e) {
fr.dispose();
System.out.println("Server Connection Closed");
disconnectServer();
System.exit(0);
}

public void windowClosed(WindowEvent e) {

}

public void windowOpened(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowActivated(WindowEvent e) {

}

public void windowDeactivated(WindowEvent e) {

}

}