home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************************************
- * *
- * *
- * *
- * Class name: Employee *
- * Purpose: This application accesses the employee table and demonstrates basic database *
- * navigation and functionality (e.g., Add Save, First, Next). *
- * *
- * Imports: java.awt.* *
- * symjava.applet.* *
- * dbFunction *
- * *
- * Methods include: public class Employee *
- * public void init() *
- * public boolean handleEvent(Event) *
- * public void start() *
- * public boolean keyDown(Event, int) *
- * void tab(Event) *
- * int tabindex(Component, boolean) *
- * public void resetControls(TextField[], Checkbox[], Button[], String) *
- * public void selChangeDeptNames(Event) *
- * *
- * Additional documentation for the database table can be seen in Employee.txt. To read this *
- * information, just open the file Employee.txt in the Cafe' editor. *
- * *
- * Additional Notes: *
- * THIS SOFTWARE HAS BEEN COMPILED AND EXECUTED SUCCESSFULLY IN SPECIFIC SYMANTEC *
- * ENVIRONMENTS, AND IS BEING PROVIDED ONLY AS SAMPLE CODE. *
- * *
- * SYMANTEC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, *
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF *
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SYMANTEC SHALL NOT *
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING *
- * THIS SOFTWARE OR ITS DERIVATIVES. *
- * *
- * Please see the Sample Code Configuration item in the Read Me file for additional *
- * information about the sample database used in this applet. *
- * *
- * Copyright (c) 1996 Symantec. All Rights Reserved. *
- * *
- ***************************************************************************************************/
-
- import java.awt.*;
- import java.applet.*;
- import dbFunctions;
-
- public class Employee extends Applet {
- String sstatusBarMessage = new String();
-
- // Global variables...
- TextField[] gtfList;
- Button[] gbList;
- TextField gtfStatusBar;
- List[] glList;
- Checkbox[] gcbList;
- String gsempId = new String();
-
- // Screen components...
- Label Applet_Title;
- Label label_emp_fname;
- Label label_emp_lname;
- Label label_birth_date;
- TextField tf_emp_fname;
- TextField tf_emp_lname;
- TextField tf_street;
- Label label_street;
- Label label_city;
- TextField tf_city;
- Label label_state;
- Label label_zip;
- TextField tf_zip;
- Label label_emp_id;
- TextField tf_emp_id;
- TextField tf_birth_date;
- Label label_phone;
- TextField tf_phone;
- TextField tf_ss_number;
- Label label_ss_number;
- Label label_status;
- Label label_sex;
- TextField tf_sex;
- Label label_salary;
- TextField tf_salary;
- Label label_start_date;
- Label label_termination_date;
- Label label_insurance;
- Checkbox cb_health;
- Checkbox cb_life;
- Checkbox cb_dc;
- Label label_dept_name;
- List l_dept_names;
- Label label_manager;
- List l_manager_names;
- TextField tf_start_date;
- TextField tf_termination_date;
- Button button_first;
- Button button_next;
- Button button_last;
- Button button_search;
- Button button_update;
- TextField tf_state;
- Button button_new_insert;
- Button button_save;
- Button button_all;
- Button button_Quit;
- TextField tf_status;
- TextField Status_Bar;
-
- // Supporting class instantiation
- dbFunctions db;
-
- public void init() {
-
- super.init();
-
- setLayout(null);
- resize(819,484);
-
- Applet_Title=new Label("Employee Information", Label.CENTER);
- Applet_Title.setFont(new Font("Dialog",Font.BOLD,18));
- add(Applet_Title);
- Applet_Title.reshape(298,8,252,30);
-
- label_emp_id=new Label("Employee #:");
- add(label_emp_id);
- label_emp_id.reshape(37,49,84,15);
- tf_emp_id=new TextField(7);
- add(tf_emp_id);
- tf_emp_id.reshape(130,45,61,23);
-
- label_emp_lname=new Label("Last Name:");
- label_emp_lname.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_emp_lname);
- label_emp_lname.reshape(42,83,88,15);
- tf_emp_lname=new TextField(22);
- add(tf_emp_lname);
- tf_emp_lname.reshape(130,79,182,22);
-
- label_emp_fname=new Label("First Name:");
- label_emp_fname.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_emp_fname);
- label_emp_fname.reshape(327,83,77,15);
- tf_emp_fname=new TextField(22);
- add(tf_emp_fname);
- tf_emp_fname.reshape(417,79,182,22);
-
- label_street=new Label("Street Address:");
- label_street.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_street);
- label_street.reshape(18,116,105,15);
- tf_street=new TextField(25);
- add(tf_street);
- tf_street.reshape(130,113,210,22);
-
- label_city=new Label("City:");
- label_city.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_city);
- label_city.reshape(364,116,39,15);
- tf_city=new TextField(20);
- add(tf_city);
- tf_city.reshape(417,113,168,22);
-
- label_state=new Label("State:");
- label_state.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_state);
- label_state.reshape(599,116,49,15);
- tf_state=new TextField(4);
- add(tf_state);
- tf_state.reshape(651,113,40,20);
-
- label_zip=new Label("Zip:");
- label_zip.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_zip);
- label_zip.reshape(695,116,35,15);
- tf_zip=new TextField(9);
- add(tf_zip);
- tf_zip.reshape(733,113,79,22);
-
- label_phone=new Label("Phone #:");
- label_phone.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_phone);
- label_phone.reshape(53,150,70,15);
- tf_phone=new TextField(13);
- add(tf_phone);
- tf_phone.reshape(130,146,112,23);
-
- label_ss_number=new Label("SSN#:");
- label_ss_number.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_ss_number);
- label_ss_number.reshape(354,150,52,15);
- tf_ss_number=new TextField(13);
- add(tf_ss_number);
- tf_ss_number.reshape(417,146,113,23);
-
- label_status=new Label("Status:");
- label_status.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_status);
- label_status.reshape(67,188,56,15);
- tf_status=new TextField(3);
- add(tf_status);
- tf_status.reshape(130,191,30,23);
-
- label_sex=new Label("Sex:");
- label_sex.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_sex);
- label_sex.reshape(273,188,37,15);
- tf_sex=new TextField(3);
- add(tf_sex);
- tf_sex.reshape(315,191,30,23);
-
- label_birth_date=new Label("Date of Birth:");
- label_birth_date.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_birth_date);
- label_birth_date.reshape(410,191,91,15);
- label_birth_date.hide();
- tf_birth_date=new TextField(9);
- add(tf_birth_date);
- tf_birth_date.reshape(511,191,81,23);
- tf_birth_date.hide();
-
- label_start_date=new Label("Start Date:");
- label_start_date.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_start_date);
- label_start_date.reshape(49,229,81,15);
- tf_start_date=new TextField(9);
- add(tf_start_date);
- tf_start_date.reshape(130,229,77,22);
-
- label_termination_date=new Label("Termination Date:");
- label_termination_date.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_termination_date);
- label_termination_date.reshape(242,233,115,15);
- tf_termination_date=new TextField(9);
- add(tf_termination_date);
- tf_termination_date.reshape(364,233,77,22);
-
- label_salary=new Label("Salary:");
- label_salary.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_salary);
- label_salary.reshape(480,233,54,15);
- tf_salary=new TextField(9);
- add(tf_salary);
- tf_salary.reshape(539,233,77,23);
-
- label_insurance=new Label("Insurances:");
- add(label_insurance);
- label_insurance.reshape(639,236,80,15);
- cb_health=new Checkbox("Heath");
- cb_health.setFont(new Font("Dialog",Font.PLAIN,10));
- add(cb_health);
- cb_health.reshape(728,236,84,19);
- cb_life=new Checkbox("Life");
- cb_life.setFont(new Font("Dialog",Font.PLAIN,10));
- add(cb_life);
- cb_life.reshape(728,259,84,18);
- cb_dc=new Checkbox("Day Care");
- cb_dc.setFont(new Font("Dialog",Font.PLAIN,10));
- add(cb_dc);
- cb_dc.reshape(728,281,84,19);
-
- label_dept_name=new Label("Department:");
- label_dept_name.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_dept_name);
- label_dept_name.reshape(106,271,74,15);
- l_dept_names=new List();
- add(l_dept_names);
- l_dept_names.resize(l_dept_names.preferredSize(5));
- l_dept_names.move(190,271);
-
- label_manager=new Label("Manager:");
- label_manager.setFont(new Font("Dialog",Font.PLAIN,10));
- add(label_manager);
- label_manager.reshape(466,271,64,15);
- l_manager_names=new List();
- add(l_manager_names);
- l_manager_names.resize(l_manager_names.preferredSize(2));
- l_manager_names.move(538,271);
-
- button_first=new Button("<<");
- add(button_first);
- button_first.reshape(187,362,88,22);
- button_first.disable();
- button_next=new Button(">");
- add(button_next);
- button_next.reshape(292,362,88,22);
- button_last=new Button(">>");
- add(button_last);
- button_last.reshape(397,362,88,22);
- button_all=new Button("All");
- add(button_all);
- button_all.reshape(502,362,88,22);
- button_search=new Button("Search");
- add(button_search);
- button_search.reshape(607,362,88,22);
- button_new_insert=new Button("New");
- add(button_new_insert);
- button_new_insert.reshape(292,398,88,22);
- button_update=new Button("Update");
- add(button_update);
- button_update.reshape(397,398,88,22);
- button_Quit=new Button("Quit");
- add(button_Quit);
- button_Quit.reshape(502,398,88,22);
-
- Status_Bar=new TextField(96);
- add(Status_Bar);
- Status_Bar.reshape(12,439,795,26);
- Status_Bar.setText("Connecting to database engine....One moment please....");
-
-
- // Additional form properties....
- setBackground(Color.lightGray);
- tf_emp_id.setEditable(false);
- cb_health.setBackground(Color.lightGray);
- cb_life.setBackground(Color.lightGray);
- cb_dc.setBackground(Color.lightGray);
- Status_Bar.setEditable(false);
- Status_Bar.setBackground(Color.green);
- TextField[] tfList = {tf_emp_id, tf_emp_lname, tf_emp_fname, tf_street, tf_city, tf_state, tf_zip,
- tf_phone, tf_ss_number, tf_status, tf_sex, tf_salary, tf_start_date,
- tf_termination_date, tf_birth_date};
- Checkbox[] cbList = {cb_health, cb_life, cb_dc};
- Button[] bList = {button_first, button_next, button_last, button_all, button_search, button_new_insert, button_update,
- button_Quit};
- TextField tfStatusBar = Status_Bar;
- List[] lList = {l_dept_names, l_manager_names};
-
- gtfList = tfList;
- glList = lList;
- gbList = bList;
- gtfStatusBar = tfStatusBar;
- gcbList = cbList;
-
- show();
- db = new dbFunctions();
- // Fill in screen with data...
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying first record!");
- }
-
- public boolean handleEvent(Event event) {
- if (event.id == Event.LIST_SELECT && event.target == l_dept_names) {
- selChangeDeptNames(event);
- }
- else if (event.target == button_next) {
- if (!button_first.isEnabled())
- button_first.enable();
- if (!db.nextRecord(db.grsemployee))
- System.out.println("Failure navigating to next record!");
- else {
- db.gicurrentRecord++;
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying next record!");
- if (db.gicurrentRecord == db.girecordCount) {
- button_next.disable();
- button_last.disable();
- Status_Bar.setBackground(Color.red);
- }
- else if ((db.girecordCount - db.gicurrentRecord) == 1)
- Status_Bar.setBackground(Color.yellow);
- }
- }
- else if (event.target == button_last) {
- button_first.enable();
- button_last.disable();
- button_next.disable();
- if (!db.goLastRecord(db.gssearchClause, db.gsand))
- System.out.println("Failure navigating to last record!");
- else {
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying next record!");
- Status_Bar.setBackground(Color.red);
- }
- }
- else if (event.target == button_first) {
- if (!button_next.isEnabled())
- button_next.enable();
- button_first.disable();
- button_last.enable();
- if (!db.goFirstRecord(db.gssearchClause, db.gswhere, db.gsand))
- System.out.println("Failed retrieving first record on button_first ");
- else {
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying first record!");
- if ((db.girecordCount - db.gicurrentRecord) == 1)
- Status_Bar.setBackground(Color.yellow);
- else
- Status_Bar.setBackground(Color.green);
- }
- }
- else if (event.target == button_all) {
- db.gssearchClause = "";
- db.gswhere = "";
- db.gsand = "";
- button_last.enable();
- button_first.disable();
- if (!db.goFirstRecord(db.gssearchClause, db.gswhere, db.gsand))
- System.out.println("Failed retrieving first record on button_all");
- else {
- if (!button_next.isEnabled())
- button_next.enable();
- if (!button_first.isEnabled())
- button_next.enable();
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying first record!");
- if (db.gicurrentRecord == db.girecordCount) {
- button_next.disable();
- Status_Bar.setBackground(Color.red);
- }
- else if ((db.girecordCount - db.gicurrentRecord) == 1)
- Status_Bar.setBackground(Color.yellow);
- else
- Status_Bar.setBackground(Color.green);
- }
- }
- else if (event.target == button_search) {
- if (!db.gbexecuteSearch)
- db.gotoRecord(gtfList, gcbList, glList, gbList);
- else {
- db.gotoRecord(gtfList, gcbList, glList, gbList);
- if (db.girecordCount == 0) {
- Status_Bar.setText("Search retrieved 0 records!");
- Status_Bar.setBackground(Color.red);
- tf_emp_lname.requestFocus();
- }
- if (!db.populateScreen(gtfList, gcbList, glList, gtfStatusBar))
- System.out.println("Failure displaying search record!");
- if (db.gicurrentRecord == db.girecordCount) {
- button_next.disable();
- button_last.disable();
- Status_Bar.setBackground(Color.red);
- }
- else if ((db.girecordCount - db.gicurrentRecord) == 1)
- Status_Bar.setBackground(Color.yellow);
- else
- Status_Bar.setBackground(Color.green);
- button_first.disable();
- }
- }
- else if (event.target == button_update) {
- db.gbInsert = false;
- int irowsUpdated = db.updateRecord(gtfList, gcbList, glList);
- Status_Bar.setText(irowsUpdated + " record(s) modified!");
- }
- else if (event.target == button_new_insert) {
- if (button_new_insert.getLabel().equals("New")) {
- gsempId = db.getNextID();
- if (!gsempId.equals(null)) {
- resetControls(gtfList, gcbList, gbList, gsempId);
- Status_Bar.setBackground(Color.blue);
- Status_Bar.setText("New record");
- }
- else
- System.out.println("Error retrieving new ID!");
-
- }
- else {
- int irowsInserted = db.insertNewValues(gtfList, gcbList, glList, gbList);
- if (irowsInserted == 0) {
- Status_Bar.setBackground(Color.red);
- Status_Bar.setText("Record not inserted! Review entered data.");
- }
- else {
- button_last.disable();
- button_next.disable();
- Status_Bar.setBackground(Color.red);
- Status_Bar.setText("Record " + db.gicurrentRecord + " of " + db.girecordCount);
- }
- }
- }
- return super.handleEvent(event);
- }
-
- // Methods for adding tab control....
- public void start() {
- tf_emp_id.requestFocus();
- }
-
- public boolean keyDown(Event evt, int key) {
- if ((key==9) || (key==10)) {
- tab(evt);
- return true;
- }
- else return super.keyDown(evt, key);
- }
-
- void tab(Event evt) {
- Component temp = (Component)evt.target;
- boolean shiftpressed=evt.shiftDown();
-
- int index = tabindex(temp, shiftpressed);
- Component newtab = temp.getParent().getComponent(index);
- newtab.requestFocus();
- if (newtab instanceof TextField)
- ((TextField)newtab).selectAll();
- }
-
- int tabindex(Component current, boolean shiftpressed) { //Returns the tab index of the next component
- Component[] temp;
- temp = current.getParent().getComponents();
- int increment;
- boolean foundit=false;
-
- if (!(shiftpressed))
- {
- increment=1;
- for (int i=0;i<(temp.length)-1;i++)
- {
- if (current == temp[i]) foundit=true;
- if (foundit)
- {
- Component next=temp[i+increment];
- if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
- return (i+increment);
- else if (next instanceof Checkbox)
- {
- Checkbox tempcheckbox=(Checkbox)next;
- if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
- }
- }
- }
- }
- else
- {
- increment=-1;
- for (int i=(temp.length)-1;i>=1;i--) {
- if (current == temp[i]) foundit=true;
- if (foundit)
- {
- Component next=temp[i+increment];
- if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
- return (i+increment);
- else if (next instanceof Checkbox)
- {
- Checkbox tempcheckbox=(Checkbox)next;
- if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
- }
- }
- }
- }
- if (shiftpressed) {return (temp.length)-1;} else return 0; //Base case or if we've hit the final component
- }
- // End of tab control methods....
-
- public void resetControls(TextField[] tfList, Checkbox[] cbList, Button[] bList, String sempId) {
- int imaxLoops = tfList.length, iloopCnt = 0;
- for (iloopCnt = 1; iloopCnt < imaxLoops; iloopCnt++)
- tfList[iloopCnt].setText("");
- iloopCnt = 0;
- imaxLoops = cbList.length;
- for (iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++)
- cbList[iloopCnt].setState(false);
- imaxLoops = bList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++) {
- if (iloopCnt != 5) //The new_insert button...
- bList[iloopCnt].disable();
- else
- bList[iloopCnt].setLabel("Insert");
- }
- tfList[0].setText(sempId);
- tfList[1].requestFocus();
- }
-
- public void selChangeDeptNames(Event ev) {
- String dept = "dept";
- int dept_id = db.idLookup(l_dept_names.getSelectedItem(), dept);
- glList[1].clear();
- db.populateMgrList(dept_id, glList[1]);
- }
- }
-