import java.awt.*; import java.net.*; import java.io.*; import java.applet.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import com.sun.CORBA.iiop.*; import ClientApp.*; public class Client extends Applet { private DisplayPanel dp; public static org.omg.CORBA.ORB theOrb_; public static ClientApp.ClientQuery ref_; private boolean isClose = true; public void init() { try { // create and initialize the ORB theOrb_ = org.omg.CORBA.ORB.init(this, null); // get the root naming context org.omg.CORBA.Object objRef = theOrb_.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // resolve the Object Reference in Naming NameComponent nc = new NameComponent("ClientApp", ""); NameComponent path1[] = {nc}; ref_ = ClientApp.ClientQueryHelper.narrow(ncRef.resolve(path1)); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Client: Exception-> " + ex) ; } } public Client() { 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 { ref_.search(a); getData(ref_.getName(), ref_.getAge(), ref_.getOcc()); } } public void open() { if (isClose) { isClose = false; dp.showStatus(0); ref_.open(); } else { dp.showStatus(2); } dp.repaint(); } public void close() { if (isClose) { dp.showStatus(3); } else { isClose = true; dp.showStatus(1); ref_.close(); } dp.repaint(); } } class HeaderPanel extends Panel { private Label header; private Client par; public HeaderPanel(Client c) { par = c; setFont(new Font("Helvitica", Font.BOLD, 24)); add(header = new Label("Database Connectivity Test")); } } class ControlPanel extends Panel { private Client parent; private TextField name; private Label lbl; public ControlPanel(Client 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;} }