home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / MICROSOFT / RDOSAMPLE / SIMPLERDO.JAVA < prev    next >
Encoding:
Java Source  |  1996-12-06  |  11.5 KB  |  414 lines

  1. //******************************************************************************
  2. // simplerdo.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import simplerdoframe;
  8.  
  9. import msrdo20.*;
  10. import com.ms.com.*;
  11.  
  12. // SimpleForm is the data entry form for the sample
  13. class SimpleForm extends Panel
  14. {
  15.     // Access to the data
  16.     _rdoResultset m_resultset;
  17.  
  18.     // The fields in the form
  19.     TextField field1;
  20.     TextField field2;
  21.     TextField field3;
  22.     TextField field4;
  23.     TextField field5;
  24.     TextField field6;
  25.     Checkbox field7;
  26.  
  27.     // The names of the resultset columns
  28.     Variant name1;
  29.     Variant name2;
  30.     Variant name3;
  31.     Variant name4;
  32.     Variant name5;
  33.     Variant name6;
  34.     Variant name7;
  35.  
  36.     // Create the form
  37.     SimpleForm(_rdoResultset r)
  38.     {
  39.         // Set the recordset
  40.         m_resultset = r;
  41.  
  42.         // Create panels for the labels and fields
  43.         Panel labels = new Panel();
  44.         Panel fields = new Panel();
  45.  
  46.         // Set those panels as a grid, one column wide
  47.         labels.setLayout(new GridLayout(0, 1));
  48.         fields.setLayout(new GridLayout(0, 1));
  49.  
  50.         // Set the form as labels to the left, fields to the right
  51.         setLayout(new FlowLayout());
  52.         add(labels);
  53.         add(fields);
  54.  
  55.         // Set the first label and field
  56.         //    Create the label and add it into the form
  57.         labels.add(new Label("Full Name"));
  58.         //    Create the field and add it into the form
  59.         fields.add(field1 = new TextField(25));
  60.         //    Disable the field since the access is read-only
  61.         field1.disable();
  62.         //    Create the field name for use with getItem
  63.         name1 = new Variant();
  64.         name1.putString("Name");
  65.  
  66.         // Repeat for the other labels and fields
  67.         labels.add(new Label("E-Mail Name"));
  68.         fields.add(field2 = new TextField(10));
  69.         field2.disable();
  70.         name2 = new Variant();
  71.         name2.putString("Email");
  72.         
  73.         labels.add(new Label("Phone Extension"));
  74.         fields.add(field3 = new TextField(12));
  75.         field3.disable();
  76.         name3 = new Variant();
  77.         name3.putString("Phone");
  78.         
  79.         labels.add(new Label("Office Location"));
  80.         fields.add(field4 = new TextField(15));
  81.         field4.disable();
  82.         name4 = new Variant();
  83.         name4.putString("Location");
  84.         
  85.         labels.add(new Label("Manager"));
  86.         fields.add(field5 = new TextField(10));
  87.         field5.disable();
  88.         name5 = new Variant();
  89.         name5.putString("Manager");
  90.         
  91.         labels.add(new Label("Date of Hire"));
  92.         fields.add(field6 = new TextField(10));
  93.         field6.disable();
  94.         name6 = new Variant();
  95.         name6.putString("HireDate");
  96.         
  97.         labels.add(new Label("Certified"));
  98.         fields.add(field7 = new Checkbox());
  99.         field7.disable();
  100.         name7 = new Variant();
  101.         name7.putString("Certified");
  102.     
  103.     }
  104.  
  105.     // Show the data in the form
  106.     void showData()
  107.     {
  108.         // Get the columns in the resultset
  109.         rdoColumns columns = m_resultset.getrdoColumns();
  110.         _rdoColumn c;
  111.  
  112.         // Create a Variant to get the value of each column
  113.         Variant value;
  114.  
  115.         // Get the first column
  116.         c = columns.getItem(name1);
  117.         // Get its value
  118.         value = c.getValue();
  119.         // Set the field in the form
  120.         field1.setText(value.toString());
  121.     
  122.         // Repeat for the other columns
  123.         c = columns.getItem(name2);
  124.         value = c.getValue();
  125.         field2.setText(value.toString());
  126.  
  127.         c = columns.getItem(name3);
  128.         value = c.getValue();
  129.         field3.setText(value.toString());
  130.  
  131.         c = columns.getItem(name4);
  132.         value = c.getValue();
  133.         field4.setText(value.toString());
  134.  
  135.         c = columns.getItem(name5);
  136.         value = c.getValue();
  137.         field5.setText(value.toString());
  138.  
  139.         c = columns.getItem(name6);
  140.         value = c.getValue();
  141.         field6.setText(value.toString());
  142.  
  143.         c = columns.getItem(name7);
  144.         value = c.getValue();
  145.         field7.setState(value.getBoolean());
  146.     }
  147. }
  148.  
  149.  
  150. //==============================================================================
  151. // Main Class for applet simplerdo
  152. //
  153. //==============================================================================
  154. public class simplerdo extends Applet
  155. {
  156.     // RDO objects
  157.     //    The connection to the ODBC data source
  158.     public static _rdoConnection m_IConnection;
  159.     //  The resultset from the query
  160.     public static _rdoResultset m_IResultSet;
  161.  
  162.     // Toolbar
  163.     Panel m_toolbar;
  164.     Button m_first;
  165.     Button m_prev;
  166.     Button m_next;
  167.     Button m_last;
  168.  
  169.     // Data Entry Form
  170.     SimpleForm m_form;
  171.  
  172.     // STANDALONE APPLICATION SUPPORT:
  173.     //        m_fStandAlone will be set to true if applet is run standalone
  174.     //--------------------------------------------------------------------------
  175.     boolean m_fStandAlone = false;
  176.  
  177.     
  178.     
  179.     // STANDALONE APPLICATION SUPPORT
  180.     //     The main() method acts as the applet's entry point when it is run
  181.     // as a standalone application. It is ignored if the applet is run from
  182.     // within an HTML page.
  183.     //--------------------------------------------------------------------------
  184.     public static void main(String args[])
  185.     {
  186.         // Create Toplevel Window to contain applet simplerdo
  187.         //----------------------------------------------------------------------
  188.         simplerdoframe frame = new simplerdoframe("simplerdo");
  189.  
  190.         // Must show Frame before we size it so insets() will return valid values
  191.         //----------------------------------------------------------------------
  192.         frame.show();
  193.         frame.hide();
  194.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  195.                      frame.insets().top  + frame.insets().bottom + 240);
  196.  
  197.         // The following code starts the applet running within the frame window.
  198.         // It also calls GetParameters() to retrieve parameter values from the
  199.         // command line, and sets m_fStandAlone to true to prevent init() from
  200.         // trying to get them from the HTML page.
  201.         //----------------------------------------------------------------------
  202.         simplerdo applet_simplerdo = new simplerdo();
  203.  
  204.         frame.add("Center", applet_simplerdo);
  205.         applet_simplerdo.m_fStandAlone = true;
  206.         applet_simplerdo.init();
  207.         applet_simplerdo.start();
  208.         frame.show();
  209.     }
  210.  
  211.     // simplerdo Class Constructor
  212.     //--------------------------------------------------------------------------
  213.     public simplerdo()
  214.     {
  215.         // Create the toolbar and add it to the applet
  216.         m_toolbar = new Panel();
  217.         setLayout(new BorderLayout());
  218.         add("North", m_toolbar);
  219.         m_toolbar.add(m_first = new Button("First"));
  220.         m_toolbar.add(m_prev = new Button("Prev"));
  221.         m_toolbar.add(m_next = new Button("Next"));
  222.         m_toolbar.add(m_last = new Button("Last"));
  223.     }
  224.  
  225.     // APPLET INFO SUPPORT:
  226.     //        The getAppletInfo() method returns a string describing the applet's
  227.     // author, copyright date, or miscellaneous information.
  228.     //--------------------------------------------------------------------------
  229.     public String getAppletInfo()
  230.     {
  231.         return "Name: simplerdo\r\n" +
  232.                "Created with Microsoft Visual J++ Version 1.0";
  233.     }
  234.  
  235.     
  236.     // The init() method is called by the AWT when an applet is first loaded or
  237.     // reloaded.  Override this method to perform whatever initialization your
  238.     // applet needs, such as initializing data structures, loading images or
  239.     // fonts, creating frame windows, setting the layout manager, or adding UI
  240.     // components.
  241.     //--------------------------------------------------------------------------
  242.     public void init()
  243.     {
  244.         // If you use a ResourceWizard-generated "control creator" class to
  245.         // arrange controls in your applet, you may want to call its
  246.         // CreateControls() method from within this method. Remove the following
  247.         // call to resize() before adding the call to CreateControls();
  248.         // CreateControls() does its own resizing.
  249.         //----------------------------------------------------------------------
  250.         resize(320, 240);
  251.  
  252.     }
  253.  
  254.     // Place additional applet clean up code here.  destroy() is called when
  255.     // when you applet is terminating and being unloaded.
  256.     //-------------------------------------------------------------------------
  257.     public void destroy()
  258.     {
  259.     }
  260.  
  261.     // simplerdo Paint Handler
  262.     //--------------------------------------------------------------------------
  263.     public void paint(Graphics g)
  264.     {
  265.     }
  266.  
  267.     //        The start() method is called when the page containing the applet
  268.     // first appears on the screen. The AppletWizard's initial implementation
  269.     // of this method starts execution of the applet's thread.
  270.     //--------------------------------------------------------------------------
  271.     public void start()
  272.     {
  273.         // Setup Variants for optional parameters
  274.         Variant v1 = new Variant();
  275.         Variant v2 = new Variant();
  276.         Variant v3 = new Variant();
  277.         Variant v4 = new Variant();
  278.         Variant v5 = new Variant();
  279.         Variant v6 = new Variant();
  280.         Variant v7 = new Variant();
  281.  
  282.         // Set optional parameter values
  283.         v1.putInt(PromptConstants.rdDriverNoPrompt);
  284.         v2.putBoolean(false);
  285.         v3.putInt(0);
  286.  
  287.         // Open a connection to the data source
  288.         rdoConnection Connection = new rdoConnection();
  289.         m_IConnection = (_rdoConnection) Connection;
  290.  
  291.         m_IConnection.putConnect("DSN=Sample Database;UID=;PWD=;Database=Sample Database;");
  292.  
  293.         m_IConnection.EstablishConnection(v1, v2, v3);
  294.  
  295.         // Set optional parameters
  296.         v4.putInt(ResultsetTypeConstants.rdOpenKeyset);
  297.         v5.putInt(LockTypeConstants.rdConcurReadOnly);
  298.         v6.putInt(0);
  299.  
  300.         m_IResultSet = (_rdoResultset) m_IConnection.OpenResultset("SELECT * FROM Employees",v4,v5,v6);
  301.     
  302.         // Create the form, passing the resultset
  303.         m_form = new SimpleForm(m_IResultSet);
  304.         
  305.         // Add the form to the applet
  306.         add("Center", m_form);
  307.  
  308.         // Get the first row in the resultset
  309.         m_IResultSet.MoveFirst();
  310.  
  311.         // Display that row
  312.         m_form.showData();
  313.  
  314.     }
  315.     
  316.     //        The stop() method is called when the page containing the applet is
  317.     // no longer on the screen. The AppletWizard's initial implementation of
  318.     // this method stops execution of the applet's thread.
  319.     //--------------------------------------------------------------------------
  320.     public void stop()
  321.     {
  322.         // Close the resultset
  323.         m_IResultSet.Close();
  324.         m_IResultSet = null;
  325.  
  326.         // And remove the form
  327.         remove(m_form);
  328.         m_form = null;
  329.     }
  330.  
  331.     public boolean action(Event  evt, Object  what)
  332.     {
  333.         // Handle click on the First button
  334.         if (evt.target == m_first)
  335.         {
  336.             // Update the status bar, if available
  337.             if (! m_fStandAlone) showStatus ("Getting first record");
  338.  
  339.             // Move to the requested row
  340.             m_IResultSet.MoveFirst();
  341.  
  342.             // Show that row
  343.             m_form.showData();
  344.  
  345.             // Event handled
  346.             return true;
  347.         }
  348.  
  349.         // Handle click on the Prev button
  350.         if (evt.target == m_prev)
  351.         {
  352.             // Update the status bar, if available
  353.             if (! m_fStandAlone) showStatus ("Getting previous record");
  354.  
  355.             // Move to the requested row
  356.             m_IResultSet.MovePrevious();
  357.  
  358.             // Make sure we don't move before BOF
  359.             if (m_IResultSet.getBOF())
  360.                 m_IResultSet.MoveFirst();
  361.  
  362.             // Show that row
  363.             m_form.showData();
  364.  
  365.             // Event handled
  366.             return true;
  367.         }
  368.         
  369.         // Handle click on the Next button
  370.         if (evt.target == m_next)
  371.         {
  372.             // Update the status bar, if available
  373.             if (! m_fStandAlone) showStatus ("Getting next record");
  374.  
  375.             // Move to the requested row
  376.             m_IResultSet.MoveNext();
  377.  
  378.             // Make sure we don't move past EOF
  379.             if (m_IResultSet.getEOF())
  380.             {
  381.                 Variant v1 = new Variant();
  382.                 m_IResultSet.MoveLast(v1);
  383.             }
  384.     
  385.             // Show that row
  386.             m_form.showData();
  387.  
  388.             // Event handled
  389.             return true;
  390.         }
  391.         
  392.         // Handle click on the Last button
  393.         if (evt.target == m_last)
  394.         {
  395.             Variant v1 = new Variant();
  396.             // Update the status bar, if available
  397.             if (! m_fStandAlone) showStatus ("Getting last record");
  398.  
  399.             // Move to the requested row
  400.             m_IResultSet.MoveLast(v1);
  401.  
  402.             // Show that row
  403.             m_form.showData();
  404.  
  405.             // Event handled
  406.             return true;
  407.         }
  408.         
  409.         // Event not handled
  410.         return false;
  411.     }
  412.  
  413. }
  414.