home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / EmployeeDataManager.java < prev    next >
Text File  |  1998-02-26  |  12KB  |  407 lines

  1. /*
  2.     This simple extension of the java.awt.Frame class
  3.     contains all the elements necessary to act as the
  4.     main window of an application.
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.io.*;
  9.  
  10.  
  11. import symantec.itools.awt.util.edit.SocialSecurityNumber;
  12. public class EmployeeDataManager extends Frame
  13. {
  14.     final static String employeeFileName = "employee.dat";
  15.     EmployeeFileAccessor efa;
  16.  
  17.     void updateEmployeeDisplay(EmployeeList elist)
  18.     {
  19.         Employee emp = elist.first();
  20.         String textdata = "";
  21.         for (;;)
  22.         {
  23.             if (emp == null) break;
  24.             System.out.println("Appending " + emp.toString() + " to textarea");
  25.             textdata += emp.toString();
  26.             emp = elist.next();
  27.         }
  28.         ta.setText(textdata);
  29.     }
  30.  
  31.     void miAbout_Action(java.awt.event.ActionEvent event)
  32.     {
  33.         //{{CONNECTION
  34.         // Action from About Create and show as modal
  35.         (new AboutDialog(this, true)).show();
  36.         //}}
  37.     }
  38.  
  39.     void miExit_Action(java.awt.event.ActionEvent event)
  40.     {
  41.         //{{CONNECTION
  42.         // Action from Exit Create and show as modal
  43.         (new QuitDialog(this, true)).show();
  44.         //}}
  45.     }
  46.  
  47.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  48.     {
  49.         hide();         // hide the Frame
  50.         dispose();      // free the system resources
  51.         System.exit(0); // close the application
  52.     }
  53.  
  54.     public EmployeeDataManager()
  55.     {
  56.         // This code is automatically generated by Visual Cafe when you add
  57.         // components to the visual environment. It instantiates and initializes
  58.         // the components. To modify the code, only use code syntax that matches
  59.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  60.         // parse your Java file into its visual environment.
  61.  
  62.         //{{INIT_CONTROLS
  63.         setLayout(new BorderLayout(0,0));
  64.         setVisible(false);
  65.         setSize(751,309);
  66.         setFont(new Font("Dialog", Font.PLAIN, 12));
  67.         setForeground(new Color(0));
  68.         setBackground(new Color(12632256));
  69.         addPanel = new java.awt.Panel();
  70.         GridBagLayout gridBagLayout;
  71.         gridBagLayout = new GridBagLayout();
  72.         addPanel.setLayout(gridBagLayout);
  73.         addPanel.setBounds(0,0,751,0);
  74.         add("North", addPanel);
  75.         label1 = new java.awt.Label("Name:",Label.RIGHT);
  76.         label1.setBounds(39,0,51,23);
  77.         GridBagConstraints gbc;
  78.         gbc = new GridBagConstraints();
  79.         gbc.gridx = 0;
  80.         gbc.gridy = 0;
  81.         gbc.weightx = 1.0;
  82.         gbc.weighty = 1.0;
  83.         gbc.anchor = GridBagConstraints.EAST;
  84.         gbc.fill = GridBagConstraints.NONE;
  85.         gbc.insets = new Insets(0,0,0,0);
  86.         ((GridBagLayout)addPanel.getLayout()).setConstraints(label1, gbc);
  87.         addPanel.add(label1);
  88.         nfield = new java.awt.TextField();
  89.         nfield.setBounds(90,0,112,23);
  90.         gbc = new GridBagConstraints();
  91.         gbc.gridx = 1;
  92.         gbc.gridy = 0;
  93.         gbc.weightx = 2.0;
  94.         gbc.weighty = 1.0;
  95.         gbc.anchor = GridBagConstraints.WEST;
  96.         gbc.fill = GridBagConstraints.HORIZONTAL;
  97.         gbc.insets = new Insets(0,0,0,0);
  98.         ((GridBagLayout)addPanel.getLayout()).setConstraints(nfield, gbc);
  99.         addPanel.add(nfield);
  100.         label2 = new java.awt.Label("Section#:",Label.RIGHT);
  101.         label2.setBounds(231,0,65,23);
  102.         gbc = new GridBagConstraints();
  103.         gbc.gridx = 2;
  104.         gbc.gridy = 0;
  105.         gbc.weightx = 1.0;
  106.         gbc.weighty = 1.0;
  107.         gbc.anchor = GridBagConstraints.EAST;
  108.         gbc.fill = GridBagConstraints.NONE;
  109.         gbc.insets = new Insets(0,0,0,0);
  110.         ((GridBagLayout)addPanel.getLayout()).setConstraints(label2, gbc);
  111.         addPanel.add(label2);
  112.         afield = new java.awt.TextField();
  113.         afield.setBounds(296,0,68,23);
  114.         gbc = new GridBagConstraints();
  115.         gbc.gridx = 3;
  116.         gbc.gridy = 0;
  117.         gbc.weightx = 1.0;
  118.         gbc.weighty = 1.0;
  119.         gbc.anchor = GridBagConstraints.WEST;
  120.         gbc.fill = GridBagConstraints.HORIZONTAL;
  121.         gbc.insets = new Insets(0,0,0,0);
  122.         ((GridBagLayout)addPanel.getLayout()).setConstraints(afield, gbc);
  123.         addPanel.add(afield);
  124.         label3 = new java.awt.Label(" SSN:",Label.RIGHT);
  125.         label3.setBounds(398,0,45,23);
  126.         gbc = new GridBagConstraints();
  127.         gbc.gridx = 4;
  128.         gbc.gridy = 0;
  129.         gbc.weightx = 1.0;
  130.         gbc.weighty = 1.0;
  131.         gbc.anchor = GridBagConstraints.EAST;
  132.         gbc.fill = GridBagConstraints.NONE;
  133.         gbc.insets = new Insets(0,0,0,0);
  134.         ((GridBagLayout)addPanel.getLayout()).setConstraints(label3, gbc);
  135.         addPanel.add(label3);
  136.         sfield = new symantec.itools.awt.util.edit.SocialSecurityNumber();
  137.         sfield.setColumns(30);
  138.         sfield.setBounds(443,2,233,19);
  139.         sfield.setFont(new Font("Dialog", Font.PLAIN, 12));
  140.         sfield.setBackground(new Color(16777215));
  141.         gbc = new GridBagConstraints();
  142.         gbc.gridx = 5;
  143.         gbc.gridy = 0;
  144.         gbc.weightx = 2.0;
  145.         gbc.weighty = 1.0;
  146.         gbc.anchor = GridBagConstraints.WEST;
  147.         gbc.fill = GridBagConstraints.HORIZONTAL;
  148.         gbc.insets = new Insets(0,0,0,0);
  149.         gbc.ipadx = 30;
  150.         ((GridBagLayout)addPanel.getLayout()).setConstraints(sfield, gbc);
  151.         addPanel.add(sfield);
  152.         sfield.setCursor(new Cursor(Cursor.TEXT_CURSOR));
  153.         addbutton = new java.awt.Button();
  154.         addbutton.setLabel("Add");
  155.         addbutton.setBounds(695,0,35,23);
  156.         gbc = new GridBagConstraints();
  157.         gbc.gridx = 6;
  158.         gbc.gridy = 0;
  159.         gbc.weightx = 1.0;
  160.         gbc.weighty = 1.0;
  161.         gbc.fill = GridBagConstraints.NONE;
  162.         gbc.insets = new Insets(0,0,0,0);
  163.         ((GridBagLayout)addPanel.getLayout()).setConstraints(addbutton, gbc);
  164.         addPanel.add(addbutton);
  165.         sortPanel = new java.awt.Panel();
  166.         gridBagLayout = new GridBagLayout();
  167.         sortPanel.setLayout(gridBagLayout);
  168.         sortPanel.setBounds(0,23,751,286);
  169.         add("Center", sortPanel);
  170.         choice = new java.awt.Choice();
  171.         choice.addItem("Name");
  172.         choice.addItem("Section");
  173.         choice.addItem("SSN");
  174.         try {
  175.             choice.select(0);
  176.         }
  177.         catch (IllegalArgumentException e) { }
  178.         choice.setBounds(10,30,129,21);
  179.         choice.setBackground(new Color(16777215));
  180.         gbc = new GridBagConstraints();
  181.         gbc.gridx = 0;
  182.         gbc.gridy = 0;
  183.         gbc.weightx = 1.0;
  184.         gbc.weighty = 1.0;
  185.         gbc.anchor = GridBagConstraints.NORTH;
  186.         gbc.fill = GridBagConstraints.HORIZONTAL;
  187.         gbc.insets = new Insets(30,10,0,0);
  188.         ((GridBagLayout)sortPanel.getLayout()).setConstraints(choice, gbc);
  189.         sortPanel.add(choice);
  190.         sortbutton = new java.awt.Button();
  191.         sortbutton.setLabel("Sort");
  192.         sortbutton.setBounds(169,30,36,23);
  193.         sortbutton.setFont(new Font("Dialog", Font.PLAIN, 12));
  194.         gbc = new GridBagConstraints();
  195.         gbc.gridx = 1;
  196.         gbc.gridy = 0;
  197.         gbc.weightx = 1.0;
  198.         gbc.weighty = 1.0;
  199.         gbc.anchor = GridBagConstraints.NORTH;
  200.         gbc.fill = GridBagConstraints.NONE;
  201.         gbc.insets = new Insets(30,0,0,0);
  202.         ((GridBagLayout)sortPanel.getLayout()).setConstraints(sortbutton, gbc);
  203.         sortPanel.add(sortbutton);
  204.         ta = new java.awt.TextArea(5,0);
  205.         ta.setBounds(235,30,500,216);
  206.         gbc = new GridBagConstraints();
  207.         gbc.gridx = 2;
  208.         gbc.gridy = 0;
  209.         gbc.weightx = 1.0;
  210.         gbc.weighty = 1.0;
  211.         gbc.anchor = GridBagConstraints.NORTH;
  212.         gbc.fill = GridBagConstraints.BOTH;
  213.         gbc.insets = new Insets(30,0,40,15);
  214.         ((GridBagLayout)sortPanel.getLayout()).setConstraints(ta, gbc);
  215.         sortPanel.add(ta);
  216.         setTitle("Employee Data Manager");
  217.         //}}
  218.  
  219.         //{{INIT_MENUS
  220.         mainMenuBar = new java.awt.MenuBar();
  221.         menu1 = new java.awt.Menu("File");
  222.         miExit = new java.awt.MenuItem("Exit");
  223.         menu1.add(miExit);
  224.         mainMenuBar.add(menu1);
  225.         menu3 = new java.awt.Menu("Help");
  226.         mainMenuBar.setHelpMenu(menu3);
  227.         miAbout = new java.awt.MenuItem("About..");
  228.         menu3.add(miAbout);
  229.         mainMenuBar.add(menu3);
  230.         setMenuBar(mainMenuBar);
  231.         //$$ mainMenuBar.move(4,277);
  232.         //}}
  233.  
  234.         //{{REGISTER_LISTENERS
  235.         Window lWindow = new Window();
  236.         addWindowListener(lWindow);
  237.         Action lAction = new Action();
  238.         miAbout.addActionListener(lAction);
  239.         miExit.addActionListener(lAction);
  240.         addbutton.addActionListener(lAction);
  241.         sortbutton.addActionListener(lAction);
  242.         //}}
  243.  
  244.         System.out.println("Opening file");
  245.         try
  246.         {
  247.             efa = new EmployeeFileAccessor(employeeFileName);
  248.             System.out.println("File opened");
  249.         }
  250.         catch (IOException e)
  251.         {
  252.             System.out.println("Unable to open file " + employeeFileName);
  253.             return;
  254.         }
  255.  
  256.         // Fetch all employees from file, and display them in the text area
  257.         EmployeeList elist = efa.buildEmployeeList();
  258.         updateEmployeeDisplay(elist);
  259.  
  260.     }
  261.  
  262.     public EmployeeDataManager(String title)
  263.     {
  264.         this();
  265.         setTitle(title);
  266.     }
  267.  
  268.     public synchronized void show()
  269.     {
  270.         move(50, 50);
  271.         super.show();
  272.     }
  273.  
  274.     static public void main(String args[])
  275.     {
  276.         (new EmployeeDataManager()).show();
  277.     }
  278.  
  279.     public void addNotify()
  280.     {
  281.         super.addNotify();
  282.  
  283.         if (fComponentsAdjusted)
  284.             return;
  285.  
  286.         // Adjust components according to the insets
  287.         setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
  288.         Component components[] = getComponents();
  289.         for (int i = 0; i < components.length; i++)
  290.         {
  291.             Point p = components[i].getLocation();
  292.             p.translate(insets().left, insets().top);
  293.             components[i].setLocation(p);
  294.         }
  295.         fComponentsAdjusted = true;
  296.     }
  297.  
  298.     boolean fComponentsAdjusted = false;
  299.  
  300.     //{{DECLARE_CONTROLS
  301.     java.awt.Panel addPanel;
  302.     java.awt.Label label1;
  303.     java.awt.TextField nfield;
  304.     java.awt.Label label2;
  305.     java.awt.TextField afield;
  306.     java.awt.Label label3;
  307.     symantec.itools.awt.util.edit.SocialSecurityNumber sfield;
  308.     java.awt.Button addbutton;
  309.     java.awt.Panel sortPanel;
  310.     java.awt.Choice choice;
  311.     java.awt.Button sortbutton;
  312.     java.awt.TextArea ta;
  313.     //}}
  314.  
  315.     //{{DECLARE_MENUS
  316.     java.awt.MenuBar mainMenuBar;
  317.     java.awt.Menu menu1;
  318.     java.awt.MenuItem miExit;
  319.     java.awt.Menu menu3;
  320.     java.awt.MenuItem miAbout;
  321.     //}}
  322.  
  323.     class Window extends java.awt.event.WindowAdapter
  324.     {
  325.         public void windowClosing(java.awt.event.WindowEvent event)
  326.         {
  327.             Object object = event.getSource();
  328.             if (object == EmployeeDataManager.this)
  329.                 Frame1_WindowClosing(event);
  330.         }
  331.     }
  332.  
  333.     class Action implements java.awt.event.ActionListener
  334.     {
  335.         public void actionPerformed(java.awt.event.ActionEvent event)
  336.         {
  337.             Object object = event.getSource();
  338.             if (object == miAbout)
  339.                 miAbout_Action(event);
  340.             else if (object == miExit)
  341.                 miExit_Action(event);
  342.             else if (object == addbutton)
  343.                 addbutton_Action(event);
  344.             else if (object == sortbutton)
  345.                 sortbutton_Action(event);
  346.         }
  347.     }
  348.  
  349.     void addbutton_Action(java.awt.event.ActionEvent event)
  350.     {
  351.         Employee emp;
  352.  
  353.         // The ADD button was pressed; add new employee
  354.         System.out.println("Add an employee");
  355.  
  356.         // Create (and validate) an employee object
  357.         try    {
  358.             // get the SS Number String and add dashes if needed
  359.             String ssn = sfield.getText();
  360.             if (ssn.charAt(3) != '-')
  361.             {
  362.                 ssn = ssn.substring(0,3) + '-' + ssn.substring(3);
  363.             }
  364.             if (ssn.charAt(6) != '-')
  365.             {
  366.                 ssn = ssn.substring(0,6) + '-' + ssn.substring(6);            
  367.             }            
  368.             
  369.             emp = new Employee (nfield.getText(), afield.getText(), ssn);
  370.         } catch (Exception ex1)    {
  371.             System.out.println("Invalid employee data");
  372.             System.out.println(ex1.getMessage());
  373.             //showStatus("Invalid employee data; please re-enter");
  374.             return;
  375.         }
  376.  
  377.         // Add the employee to the file
  378.         try
  379.         {
  380.             efa.addEmployee(emp);
  381.         }
  382.         catch (IOException ex2)
  383.         {
  384.             System.out.println("Unable to add employee to file");
  385.             return;
  386.         }
  387.  
  388.         System.out.println("Employee added");
  389.  
  390.         // Redisplay the employee data
  391.         EmployeeList elist = efa.buildEmployeeList();
  392.         updateEmployeeDisplay(elist);
  393.     }
  394.  
  395.     void sortbutton_Action(java.awt.event.ActionEvent event)
  396.     {
  397.         // The SORT button was pressed; perform sort
  398.         EmployeeList elist = efa.buildEmployeeList();
  399.         elist.sort(choice.getSelectedIndex());
  400.         System.out.println("Sort on field " + choice.getSelectedIndex());
  401.  
  402.         // Redisplay the employee data
  403.         updateEmployeeDisplay(elist); 
  404.     }
  405. }
  406.  
  407.