home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-27 | 8.7 KB | 325 lines |
- //******************************************************************************
- // orgchart.java: Applet
- //
- //******************************************************************************
- import java.applet.*;
- import java.awt.*;
-
- import msoutl32.*;
- // import the Outline control
- import com.ms.com.Variant;
-
- // mock database for the applet
- // The mock database was chosen to minimize the number
- // of technologies in the sample
- import employee;
- import sample_database;
-
- // SimpleForm is the data display form for the applet
- class SimpleForm extends Panel
- {
- // Access to the database
- sample_database m_db;
- employee m_current;
-
- // The fields on the form
- TextField field1;
- TextField field2;
- TextField field3;
- TextField field4;
- TextField field5;
- TextField field6;
- Checkbox field7;
-
- // The OLE control to be filled
- IOutlineCtrl m_tree;
-
- // Construct the form
- SimpleForm()
- {
- // Create panels for labels and fields
- Panel labels = new Panel();
- Panel fields = new Panel();
-
- // Set those panels as single-column grids
- labels.setLayout(new GridLayout(0, 1));
- fields.setLayout(new GridLayout(0, 1));
-
- // Put the labels and fields panels in to the form
- setLayout(new FlowLayout());
- add(labels);
- add(fields);
-
- // Add the individual labels and fields to the form
- // Add the label
- labels.add(new Label("Email Name"));
- // Add the field
- fields.add(field1 = new TextField(10));
- // Enable it for query
- field1.enable();
-
- // Add the label
- labels.add(new Label("Full Name"));
- // Add the field
- fields.add(field2 = new TextField(25));
- // Disable it, display only
- field2.disable();
-
- labels.add(new Label("Office Location"));
- fields.add(field3 = new TextField(15));
- field3.disable();
-
- labels.add(new Label("Phone Extension"));
- fields.add(field4 = new TextField(12));
- field4.disable();
-
- labels.add(new Label("Manager"));
- fields.add(field5 = new TextField(10));
- field5.disable();
-
- labels.add(new Label("Date of Hire"));
- fields.add(field6 = new TextField(10));
- field6.disable();
-
- labels.add(new Label("Certified"));
- fields.add(field7 = new Checkbox());
- field7.disable();
- }
-
- void setDB(sample_database db, IOutlineCtrl tree)
- {
- // Set the database and the outline control
- m_db = db;
- m_tree = tree;
- }
-
- // Show the record for the specified email name
- void showData(String email)
- {
- // Search the database for the name
- m_current = m_db.find(email);
-
- // if the name is not found empty the form
- if(m_current == null)
- {
- field1.setText(email);
- field2.setText("Not found");
- field3.setText("");
- field4.setText("");
- field5.setText("");
- field6.setText("");
- field7.setState(false);
- }
- else // populate the form with data
- {
- field1.setText(m_current.email);
- field2.setText(m_current.name);
- field3.setText(m_current.location);
- field4.setText(m_current.phone);
- field5.setText(m_current.manager);
- field6.setText(m_current.hireDate.toString());
- field7.setState(m_current.certified);
- }
-
- // Fill the outline control with direct reports
- fillTree();
-
- }
-
- // Show the record for the email name on the form
- void showData()
- {
- showData(field1.getText());
- }
-
- // Fill the outline control with direct reports
- public void fillTree()
- {
- // Create a variable for the root of the outline control
- short root = 0;
-
- // If there are entries in the outline control, delete them
- if(m_tree.getListCount() > 0)
- m_tree.RemoveItem(root);
-
- // If the email name is not found, return
- if(m_current == null)
- return;
-
- // Create a Variant for an optional parameter
- Variant noParam = new Variant();
- // Set the Variant for no parameter
- noParam.noParam();
-
- // Add the employee as the root of the outline
- m_tree.AddItem(m_current.email, noParam);
-
- // Set the root as the current item
- // Subsequent AddItem will be subentries of the current item
- m_tree.putListIndex(root);
-
- // Find all the direct reports in the database
- String reports[] = m_db.findReports(m_current.email);
-
- // If there are no direct reports, return
- if(reports.length == 0)
- return;
-
- // Add each direct report to the outline as a subentry
- for(int i = 0; i < reports.length; i ++)
- {
- m_tree.AddItem(reports[i], noParam);
- }
-
- }
-
-
- }
-
- //==============================================================================
- // Main Class for applet orgchart
- //
- //==============================================================================
- public class orgchart extends Applet
- {
- // The data form
- SimpleForm m_form;
-
- // The toolbar
- Panel m_tools;
- Button m_email;
- Button m_mgr;
- Button m_sel;
-
- // The Outline control
- IOutlineCtrl m_outline;
-
- // The mock database
- sample_database m_db = new sample_database();
-
- // orgchart Class Constructor
- //--------------------------------------------------------------------------
- public orgchart()
- {
- // Create the toolbar and add it to the applet
- setLayout(new BorderLayout());
- add("North", m_tools = new Panel());
- m_tools.add(m_email = new Button("Find by Email"));
- m_tools.add(m_mgr = new Button("Look Up Manager"));
- m_tools.add(m_sel = new Button("Find Selection"));
-
- // Create the form and add it to the applet
- add("Center", m_form = new SimpleForm());
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: orgchart\r\n" +
- "Author: Dan Jinguji\r\n" +
- "Created with Microsoft Visual J++ Version 1.0";
- }
-
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- // If you use a ResourceWizard-generated "control creator" class to
- // arrange controls in your applet, you may want to call its
- // CreateControls() method from within this method. Remove the following
- // call to resize() before adding the call to CreateControls();
- // CreateControls() does its own resizing.
- //----------------------------------------------------------------------
- resize(320, 200);
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- }
-
- // orgchart Paint Handler
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- }
-
- // Accept the Outline control passed from Window_OnLoad
- public void setControl(Object outlineCtl)
- {
- // Cast the control
- m_outline = (IOutlineCtrl) outlineCtl;
-
- // Set control style
- m_outline.putStyle(StyleConstants.outPlusMinusText);
-
- // Set the database and control in the form
- m_form.setDB(m_db, m_outline);
-
- // Display a record
- m_form.showData("chrisb");
- }
-
- public boolean action(Event evt, Object what)
- {
- // Handle the Find By Email button
- if(evt.target == m_email)
- {
- // Find the value in the email TextField
- m_form.showData();
-
- // Event handled
- return true;
- }
-
- // Handle the Look Up Manager button
- if(evt.target == m_mgr)
- {
- // Find the value in the manager TextField
- m_form.showData(m_form.field5.getText());
-
- // Event handled
- return true;
- }
-
- // Handle the Find Selection button
- if(evt.target == m_sel)
- {
- // Find the value of the selection in the outline
- m_form.showData(m_outline.getText());
-
- // Event handled
- return true;
- }
-
- // Event not handled
- return false;
- }
- }
-