package DBC;

import java.io.*;
import java.awt.*;
import java.rmi.*;
import java.applet.*;

public class DBCApplet extends Applet {
	
    private DisplayPanel dp;	
    public DBC obj;
    private boolean isClose = true;

    public void init() {
        String url = getParameter("url");
        // System.out.println("URL" + url);
        try {
            obj = (DBC) Naming.lookup(url);
        }
        catch (Exception ex) {
            System.out.println("DBCApplet: Exception-> " + ex) ;
            ex.printStackTrace();
        }
    }

    public DBCApplet() {
  	setBackground(Color.pink);
	add("North",  new HeaderPanel(this));
	add("Center", dp = new DisplayPanel());
	add("South",  new ControlPanel(this));
    }

    public void showData(String name, String age, String occ) {
	PersonalData pd = new PersonalData(name, age, occ);
	dp.show(pd);
	dp.repaint();
    }

    public void getData(String a, String b, String c) {
	if (a.equals("")) {
	    dp.showerr(1);
	    dp.repaint();
	}
	else showData(a,b,c);
    }
	
    public void search(String a) {
        if (isClose) {
            dp.showerr(0);
            dp.repaint();
        }
        else {
            try {
                obj.search(a);
	        getData(obj.getName(), obj.getAge(), obj.getOcc());
            } catch (Exception ex) {
                System.out.println("DBCApplet: Exception-> " + ex) ;
                ex.printStackTrace();
            }
        }
    }

    public void open() {
        if (isClose) {
            isClose = false;
            dp.showStatus(0);
            try {
                obj.open();
            } catch (Exception ex) {
                System.out.println("DBCApplet: Exception-> " + ex) ;
                ex.printStackTrace();
            }
        }
        else {
            dp.showStatus(2);
        }
        dp.repaint();
    }
 
    public void close() {
        if (isClose) {
            dp.showStatus(3);
        }
        else {
            isClose = true;
            dp.showStatus(1);
            try {
                obj.close();
            } catch (Exception ex) {
                System.out.println("DBCApplet: Exception-> " + ex) ;
                ex.printStackTrace();
            }
        }
        dp.repaint();
    }
}

class HeaderPanel extends Panel {

    private Label header;
    private DBCApplet par;

    public HeaderPanel(DBCApplet c) {
	par = c;
	setFont(new Font("Helvitica", Font.BOLD, 24));
	add(header = new Label("Database Connectivity Test"));
    }
}

class ControlPanel extends Panel {

    private DBCApplet parent;
    private TextField name;
    private Label lbl;

    public ControlPanel(DBCApplet clt) {
	parent = clt;
	setFont(new Font("Times-Roman", Font.BOLD, 14));
	add(lbl = new Label("Enter name to search"));
	add(name = new TextField(20));
        add(new Button("Open DB"));
        add(new Button("Close DB"));
	add(new Button("Search"));
	add(new Button("Clear"));
    }

    public boolean action(Event e, java.lang.Object what) {
        if (what.equals("Open DB")) {
            try {
                parent.open();
            }
            catch (Exception ex) {return false;}
            return true;
        }
        if (what.equals("Close DB")) {
            try {
                parent.close();
            }
            catch (Exception ex) {return false;}
            return true;
        }
        if (what.equals("Search")) {
	    try {
	        parent.search(name.getText());
	    }
	    catch (Exception ex) {return false;}
	    return true;
	}
	if (what.equals("Clear")) {
	    name.setText("");
	    return true;
	}
	return false;
    }
}

class DisplayPanel extends Canvas {

    private PersonalData pdata;
    private boolean nodata = false;
    private boolean noConnection = false;
    private boolean connected = false;
    private boolean closed = false;
    private boolean alreadyOpen = false;
    private boolean alreadyClose = false;

    String header = "Search Information";
    String nodatagot = "Sorry! There is no such person by that name.";
    String noConnectionMsg1 = "Sorry! No connection to the database server.";
    String noConnectionMsg2 = "Please connect to the database by clicking [Open DB]";
    String DBCloseMsg1 = "Please close the connection by clicking [Close DB]";
    String DBCloseMsg2 = "whenever you are done with the search.";
    String connectedMsg = "Connection established.";
    String closedMsg = "Connection lost.";
    String alreadyOpenMsg = "Connection has been established already!";
    String alreadyCloseMsg = "Connection has not been established yet!";

    public DisplayPanel() {
	pdata=null;
	resize(600,400);
    }

    public void show(PersonalData per) {
	pdata = per;
        nodata = false;
    }
		
    public void showerr(int op) {
        if (op == 1) {
            pdata = null;
	    nodata = true;
        }
        if (op == 0) {
            pdata = null;
            noConnection = true;
        }
    }

    public void showStatus(int op) {
        if (op == 0) {
            connected = true;
            pdata = null;
        }
        if (op == 1) {
            closed = true;
            pdata = null;
        }
        if (op == 2) {
            alreadyOpen = true;
            pdata = null;
        }
        if (op == 3) {
            alreadyClose = true;
            pdata = null;
        }
    }

    public void paint(Graphics g) {
	setBackground(Color.orange);
	g.setFont( new Font("Times-Roman",Font.BOLD,22));
	g.setColor(new java.awt.Color(255,0,0));
	g.drawString(header,150,25);
	if(pdata!=null) {
	    g.setFont( new Font("Times-Roman",Font.BOLD,18));
	    g.setColor(new java.awt.Color(0,0,0));
	    g.drawString("Name       : " + pdata.getName(), 50, 60);
	    g.drawString("Age        : " + String.valueOf(pdata.getAge()), 50, 80);
	    g.drawString("Occupation : " + pdata.getOcc(), 50, 100);

            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(255,0,0));
            g.drawString(DBCloseMsg1, 50, 140);
            g.drawString(DBCloseMsg2, 50, 160);
	}
        if (connected) {
            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(0,0,255));
            g.drawString(connectedMsg, 50, 60);
            connected = false;
        }
        if (noConnection) {
            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(255,0,0));
            g.drawString(noConnectionMsg1, 50, 60);
            g.drawString(noConnectionMsg2, 50, 80);
            noConnection = false;
        }
        if (closed) {
            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(0,0,255));
            g.drawString(closedMsg, 50, 60);
            closed = false;
        }
	if (nodata) {
	    g.setFont( new Font("Times-Roman",Font.BOLD,18));
	    g.setColor(new java.awt.Color(0,0,0));
	    g.drawString(nodatagot, 50, 60);
            nodata = false;

            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(255,0,0));
            g.drawString(DBCloseMsg1, 50, 140);
            g.drawString(DBCloseMsg2, 50, 160);
	}
        if (alreadyOpen) {
            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(255,0,0));
            g.drawString(alreadyOpenMsg, 50, 60);
            alreadyOpen = false;
        }
        if (alreadyClose) {
            g.setFont( new Font("Times-Roman",Font.BOLD,18));
            g.setColor(new java.awt.Color(255,0,0));
            g.drawString(alreadyCloseMsg, 50, 60);
            alreadyClose = false;
        }
    }		       		
}

class PersonalData {

    private String Name;
    private String Age;
    private String Occ;

    public PersonalData(String name, String age, String occ) {
	Name = name;
	Age = age;
	Occ = occ;
    }

    public String getName() { return Name;}
    public String getAge()	{ return Age;}
    public String getOcc()	{ return Occ;}
}