home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / visualj / msdev / samples / microsoft / olecontrols / orgchart.java < prev    next >
Encoding:
Java Source  |  1996-08-27  |  8.7 KB  |  325 lines

  1. //******************************************************************************
  2. // orgchart.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7.  
  8. import msoutl32.*;
  9.     // import the Outline control
  10. import com.ms.com.Variant;
  11.  
  12. // mock database for the applet
  13. //    The mock database was chosen to minimize the number
  14. //    of technologies in the sample
  15. import employee;
  16. import sample_database;
  17.  
  18. // SimpleForm is the data display form for the applet
  19. class SimpleForm extends Panel
  20. {
  21.     // Access to the database
  22.     sample_database m_db;
  23.     employee m_current;
  24.  
  25.     // The fields on the form
  26.     TextField field1;
  27.     TextField field2;
  28.     TextField field3;
  29.     TextField field4;
  30.     TextField field5;
  31.     TextField field6;
  32.     Checkbox field7;
  33.     
  34.     // The OLE control to be filled
  35.     IOutlineCtrl m_tree;
  36.  
  37.     // Construct the form
  38.     SimpleForm()
  39.     {
  40.         // Create panels for labels and fields
  41.         Panel labels = new Panel();
  42.         Panel fields = new Panel();
  43.  
  44.         // Set those panels as single-column grids
  45.         labels.setLayout(new GridLayout(0, 1));
  46.         fields.setLayout(new GridLayout(0, 1));
  47.  
  48.         // Put the labels and fields panels in to the form
  49.         setLayout(new FlowLayout());
  50.         add(labels);
  51.         add(fields);
  52.  
  53.         // Add the individual labels and fields to the form
  54.         //    Add the label
  55.         labels.add(new Label("Email Name"));
  56.         //    Add the field
  57.         fields.add(field1 = new TextField(10));
  58.         //    Enable it for query
  59.         field1.enable();
  60.  
  61.         //    Add the label
  62.         labels.add(new Label("Full Name"));
  63.         //    Add the field
  64.         fields.add(field2 = new TextField(25));
  65.         //    Disable it, display only
  66.         field2.disable();
  67.  
  68.         labels.add(new Label("Office Location"));
  69.         fields.add(field3 = new TextField(15));
  70.         field3.disable();
  71.  
  72.         labels.add(new Label("Phone Extension"));
  73.         fields.add(field4 = new TextField(12));
  74.         field4.disable();
  75.  
  76.         labels.add(new Label("Manager"));
  77.         fields.add(field5 = new TextField(10));
  78.         field5.disable();
  79.  
  80.         labels.add(new Label("Date of Hire"));
  81.         fields.add(field6 = new TextField(10));
  82.         field6.disable();
  83.  
  84.         labels.add(new Label("Certified"));
  85.         fields.add(field7 = new Checkbox());
  86.         field7.disable();
  87.     }
  88.  
  89.     void setDB(sample_database db, IOutlineCtrl tree)
  90.     {
  91.         // Set the database and the outline control
  92.         m_db = db;
  93.         m_tree = tree;
  94.     }
  95.  
  96.     // Show the record for the specified email name
  97.     void showData(String email)
  98.     {
  99.         // Search the database for the name    
  100.         m_current = m_db.find(email);
  101.  
  102.         // if the name is not found empty the form
  103.         if(m_current == null)
  104.         {
  105.             field1.setText(email);
  106.             field2.setText("Not found");
  107.             field3.setText("");
  108.             field4.setText("");
  109.             field5.setText("");
  110.             field6.setText("");
  111.             field7.setState(false);
  112.         }
  113.         else // populate the form with data
  114.         {
  115.             field1.setText(m_current.email);
  116.             field2.setText(m_current.name);
  117.             field3.setText(m_current.location);
  118.             field4.setText(m_current.phone);
  119.             field5.setText(m_current.manager);
  120.             field6.setText(m_current.hireDate.toString());
  121.             field7.setState(m_current.certified);
  122.         }
  123.  
  124.         // Fill the outline control with direct reports
  125.         fillTree();
  126.         
  127.     }
  128.  
  129.     // Show the record for the email name on the form
  130.     void showData()
  131.     {
  132.         showData(field1.getText());
  133.     }
  134.  
  135.     // Fill the outline control with direct reports
  136.     public void fillTree()
  137.     {
  138.         // Create a variable for the root of the outline control
  139.         short root = 0;
  140.  
  141.         // If there are entries in the outline control, delete them
  142.         if(m_tree.getListCount() > 0)
  143.             m_tree.RemoveItem(root);
  144.         
  145.         // If the email name is not found, return
  146.         if(m_current == null)
  147.             return;
  148.  
  149.         // Create a Variant for an optional parameter
  150.         Variant noParam = new Variant();
  151.         // Set the Variant for no parameter
  152.         noParam.noParam();
  153.  
  154.         // Add the employee as the root of the outline
  155.         m_tree.AddItem(m_current.email, noParam);
  156.  
  157.         // Set the root as the current item
  158.         //    Subsequent AddItem will be subentries of the current item
  159.         m_tree.putListIndex(root);
  160.  
  161.         // Find all the direct reports in the database
  162.         String reports[] = m_db.findReports(m_current.email);
  163.  
  164.         // If there are no direct reports, return
  165.         if(reports.length == 0)
  166.             return;
  167.  
  168.         // Add each direct report to the outline as a subentry
  169.         for(int i = 0; i < reports.length; i ++)
  170.         {
  171.             m_tree.AddItem(reports[i], noParam);
  172.         }
  173.  
  174.     }
  175.  
  176.  
  177. }
  178.  
  179. //==============================================================================
  180. // Main Class for applet orgchart
  181. //
  182. //==============================================================================
  183. public class orgchart extends Applet
  184. {
  185.     // The data form
  186.     SimpleForm m_form;
  187.  
  188.     // The toolbar
  189.     Panel m_tools;
  190.     Button m_email;
  191.     Button m_mgr;
  192.     Button m_sel;
  193.  
  194.     // The Outline control
  195.     IOutlineCtrl m_outline;
  196.  
  197.     // The mock database
  198.     sample_database m_db = new sample_database();
  199.  
  200.     // orgchart Class Constructor
  201.     //--------------------------------------------------------------------------
  202.     public orgchart()
  203.     {
  204.         // Create the toolbar and add it to the applet
  205.         setLayout(new BorderLayout());
  206.         add("North", m_tools = new Panel());
  207.         m_tools.add(m_email = new Button("Find by Email"));
  208.         m_tools.add(m_mgr = new Button("Look Up Manager"));
  209.         m_tools.add(m_sel = new Button("Find Selection"));
  210.  
  211.         // Create the form and add it to the applet
  212.         add("Center", m_form = new SimpleForm());
  213.     }
  214.  
  215.     // APPLET INFO SUPPORT:
  216.     //        The getAppletInfo() method returns a string describing the applet's
  217.     // author, copyright date, or miscellaneous information.
  218.     //--------------------------------------------------------------------------
  219.     public String getAppletInfo()
  220.     {
  221.         return "Name: orgchart\r\n" +
  222.                "Author: Dan Jinguji\r\n" +
  223.                "Created with Microsoft Visual J++ Version 1.0";
  224.     }
  225.  
  226.  
  227.     // The init() method is called by the AWT when an applet is first loaded or
  228.     // reloaded.  Override this method to perform whatever initialization your
  229.     // applet needs, such as initializing data structures, loading images or
  230.     // fonts, creating frame windows, setting the layout manager, or adding UI
  231.     // components.
  232.     //--------------------------------------------------------------------------
  233.     public void init()
  234.     {
  235.         // If you use a ResourceWizard-generated "control creator" class to
  236.         // arrange controls in your applet, you may want to call its
  237.         // CreateControls() method from within this method. Remove the following
  238.         // call to resize() before adding the call to CreateControls();
  239.         // CreateControls() does its own resizing.
  240.         //----------------------------------------------------------------------
  241.         resize(320, 200);
  242.     }
  243.  
  244.     // Place additional applet clean up code here.  destroy() is called when
  245.     // when you applet is terminating and being unloaded.
  246.     //-------------------------------------------------------------------------
  247.     public void destroy()
  248.     {
  249.     }
  250.  
  251.     // orgchart Paint Handler
  252.     //--------------------------------------------------------------------------
  253.     public void paint(Graphics g)
  254.     {
  255.     }
  256.  
  257.     //        The start() method is called when the page containing the applet
  258.     // first appears on the screen. The AppletWizard's initial implementation
  259.     // of this method starts execution of the applet's thread.
  260.     //--------------------------------------------------------------------------
  261.     public void start()
  262.     {
  263.     }
  264.     
  265.     //        The stop() method is called when the page containing the applet is
  266.     // no longer on the screen. The AppletWizard's initial implementation of
  267.     // this method stops execution of the applet's thread.
  268.     //--------------------------------------------------------------------------
  269.     public void stop()
  270.     {
  271.     }
  272.  
  273.     // Accept the Outline control passed from Window_OnLoad
  274.     public void setControl(Object outlineCtl)
  275.     {
  276.         // Cast the control
  277.         m_outline = (IOutlineCtrl) outlineCtl;
  278.  
  279.         // Set control style
  280.         m_outline.putStyle(StyleConstants.outPlusMinusText);
  281.  
  282.         // Set the database and control in the form
  283.         m_form.setDB(m_db, m_outline);
  284.  
  285.         // Display a record
  286.         m_form.showData("chrisb");
  287.     }
  288.  
  289.     public boolean action(Event evt, Object what)
  290.     {
  291.         // Handle the Find By Email button
  292.         if(evt.target == m_email)
  293.         {
  294.             // Find the value in the email TextField
  295.             m_form.showData();
  296.  
  297.             // Event handled
  298.             return true;
  299.         }
  300.         
  301.         // Handle the Look Up Manager button
  302.         if(evt.target == m_mgr)
  303.         {
  304.             // Find the value in the manager TextField
  305.             m_form.showData(m_form.field5.getText());
  306.  
  307.             // Event handled
  308.             return true;
  309.         }
  310.         
  311.         // Handle the Find Selection button
  312.         if(evt.target == m_sel)
  313.         {
  314.             // Find the value of the selection in the outline
  315.             m_form.showData(m_outline.getText());
  316.  
  317.             // Event handled
  318.             return true;
  319.         }
  320.  
  321.         // Event not handled
  322.         return false;
  323.     }
  324. }
  325.