home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-27 | 15.8 KB | 521 lines |
- //******************************************************************************
- // simpledao.java: Applet
- //
- //******************************************************************************
- import java.applet.*;
- import java.awt.*;
- import simpledaoframe;
-
- // Creates the DAO DBEngine use the license
- import dao_dbengine;
-
- import dao3032.*;
- import com.ms.com.Variant;
-
- // SimpleForm is the data entry form for the sample
- class SimpleForm extends Panel
- {
- // Access to the data
- Recordset m_recordset;
-
- // The fields in the form
- TextField field1;
- TextField field2;
- TextField field3;
- TextField field4;
- TextField field5;
- TextField field6;
- Checkbox field7;
-
- // The names of the recordset fields
- Variant name1;
- Variant name2;
- Variant name3;
- Variant name4;
- Variant name5;
- Variant name6;
- Variant name7;
-
- // Create the form
- SimpleForm(Recordset r)
- {
- // Set the recordset
- m_recordset = r;
-
- // Create panels for the labels and fields
- Panel labels = new Panel();
- Panel fields = new Panel();
-
- // Set those panels as a grid, one column wide
- labels.setLayout(new GridLayout(0, 1));
- fields.setLayout(new GridLayout(0, 1));
-
- // Set the form as labels to the left, fields to the right
- setLayout(new FlowLayout());
- add(labels);
- add(fields);
-
- // Set the first label and field
- // Create the label and add it into the form
- labels.add(new Label("Full Name"));
- // Create the field and add it into the form
- fields.add(field1 = new TextField(25));
- // Disable the field since the access is read-only
- field1.disable();
- // Create the field name for use with getItem
- name1 = new Variant();
- name1.putString("Name");
-
- // Repeat for the other labels and fields
- labels.add(new Label("E-Mail Name"));
- fields.add(field2 = new TextField(10));
- field2.disable();
- name2 = new Variant();
- name2.putString("Email");
-
- labels.add(new Label("Phone Extension"));
- fields.add(field3 = new TextField(12));
- field3.disable();
- name3 = new Variant();
- name3.putString("Phone");
-
- labels.add(new Label("Office Location"));
- fields.add(field4 = new TextField(15));
- field4.disable();
- name4 = new Variant();
- name4.putString("Location");
-
- labels.add(new Label("Manager"));
- fields.add(field5 = new TextField(10));
- field5.disable();
- name5 = new Variant();
- name5.putString("Manager");
-
- labels.add(new Label("Date of Hire"));
- fields.add(field6 = new TextField(10));
- field6.disable();
- name6 = new Variant();
- name6.putString("HireDate");
-
- labels.add(new Label("Certified"));
- fields.add(field7 = new Checkbox());
- field7.disable();
- name7 = new Variant();
- name7.putString("Certified");
- }
-
- // Show the data in the form
- void showData()
- {
- // Get the fields in the recordset
- Fields fields = m_recordset.getFields();
- _Field f;
-
- // Create a Variant to get the value of each field
- Variant value;
-
- // Get the first field
- f = fields.getItem(name1);
- // Get its value
- value = f.getValue();
- // Set the field in the form
- field1.setText(value.toString());
-
- // Repeat for the other fields
- f = fields.getItem(name2);
- value = f.getValue();
- field2.setText(value.toString());
-
- f = fields.getItem(name3);
- value = f.getValue();
- field3.setText(value.toString());
-
- f = fields.getItem(name4);
- value = f.getValue();
- field4.setText(value.toString());
-
- f = fields.getItem(name5);
- value = f.getValue();
- field5.setText(value.toString());
-
- f = fields.getItem(name6);
- value = f.getValue();
- field6.setText(value.toString());
-
- f = fields.getItem(name7);
- value = f.getValue();
- field7.setState(value.getBoolean());
- }
- }
-
- //==============================================================================
- // Main Class for applet simpledao
- //
- //==============================================================================
- public class simpledao extends Applet
- {
- // Toolbar
- Panel m_toolbar;
- Button m_first;
- Button m_prev;
- Button m_next;
- Button m_last;
-
- // Data Entry Form
- SimpleForm m_form;
- Recordset recordset;
- String filename;
-
- // STANDALONE APPLICATION SUPPORT:
- // m_fStandAlone will be set to true if applet is run standalone
- //--------------------------------------------------------------------------
- boolean m_fStandAlone = false;
-
- // PARAMETER SUPPORT:
- // Parameters allow an HTML author to pass information to the applet;
- // the HTML author specifies them using the <PARAM> tag within the <APPLET>
- // tag. The following variables are used to store the values of the
- // parameters.
- //--------------------------------------------------------------------------
-
- // Members for applet parameters
- // <type> <MemberVar> = <Default Value>
- //--------------------------------------------------------------------------
- private String m_Database = "Sample Database.mdb";
- private String m_Recordset = "Employees";
-
- // Parameter names. To change a name of a parameter, you need only make
- // a single change. Simply modify the value of the parameter string below.
- //--------------------------------------------------------------------------
- private final String PARAM_Database = "Database";
- private final String PARAM_Recordset = "Recordset";
-
- // STANDALONE APPLICATION SUPPORT
- // The GetParameter() method is a replacement for the getParameter() method
- // defined by Applet. This method returns the value of the specified parameter;
- // unlike the original getParameter() method, this method works when the applet
- // is run as a standalone application, as well as when run within an HTML page.
- // This method is called by GetParameters().
- //---------------------------------------------------------------------------
- String GetParameter(String strName, String args[])
- {
- if (args == null)
- {
- // Running within an HTML page, so call original getParameter().
- //-------------------------------------------------------------------
- return getParameter(strName);
- }
-
- // Running as standalone application, so parameter values are obtained from
- // the command line. The user specifies them as follows:
- //
- // JView simpledao param1=<val> param2=<"val with spaces"> ...
- //-----------------------------------------------------------------------
- int i;
- String strArg = strName + "=";
- String strValue = null;
-
- for (i = 0; i < args.length; i++)
- {
- if (strArg.equalsIgnoreCase(args[i].substring(0, strArg.length())))
- {
- // Found matching parameter on command line, so extract its value.
- // If in double quotes, remove the quotes.
- //---------------------------------------------------------------
- strValue= args[i].substring(strArg.length());
- if (strValue.startsWith("\""))
- {
- strValue = strValue.substring(1);
- if (strValue.endsWith("\""))
- strValue = strValue.substring(0, strValue.length() - 1);
- }
- }
- }
-
- return strValue;
- }
-
- // STANDALONE APPLICATION SUPPORT
- // The GetParameters() method retrieves the values of each of the applet's
- // parameters and stores them in variables. This method works both when the
- // applet is run as a standalone application and when it's run within an HTML
- // page. When the applet is run as a standalone application, this method is
- // called by the main() method, which passes it the command-line arguments.
- // When the applet is run within an HTML page, this method is called by the
- // init() method with args == null.
- //---------------------------------------------------------------------------
- void GetParameters(String args[])
- {
- // Query values of all Parameters
- //--------------------------------------------------------------
- String param;
-
- // Database: Parameter description
- //--------------------------------------------------------------
- param = GetParameter(PARAM_Database, args);
- if (param != null)
- m_Database = param;
-
- // Recordset: Parameter description
- //--------------------------------------------------------------
- param = GetParameter(PARAM_Recordset, args);
- if (param != null)
- m_Recordset = param;
-
- }
-
- // STANDALONE APPLICATION SUPPORT
- // The main() method acts as the applet's entry point when it is run
- // as a standalone application. It is ignored if the applet is run from
- // within an HTML page.
- //--------------------------------------------------------------------------
- public static void main(String args[])
- {
- // Create Toplevel Window to contain applet simpledao
- //----------------------------------------------------------------------
- simpledaoframe frame = new simpledaoframe("simpledao");
-
- // Must show Frame before we size it so insets() will return valid values
- //----------------------------------------------------------------------
- frame.show();
- frame.hide();
- frame.resize(frame.insets().left + frame.insets().right + 320,
- frame.insets().top + frame.insets().bottom + 240);
-
- // The following code starts the applet running within the frame window.
- // It also calls GetParameters() to retrieve parameter values from the
- // command line, and sets m_fStandAlone to true to prevent init() from
- // trying to get them from the HTML page.
- //----------------------------------------------------------------------
- simpledao applet_simpledao = new simpledao();
-
- frame.add("Center", applet_simpledao);
- applet_simpledao.m_fStandAlone = true;
- applet_simpledao.GetParameters(args);
- applet_simpledao.init();
- applet_simpledao.start();
- frame.show();
- }
-
- // simpledao Class Constructor
- //--------------------------------------------------------------------------
- public simpledao()
- {
- // Create the toolbar and add it to the applet
- m_toolbar = new Panel();
- add("North", m_toolbar);
- m_toolbar.add(m_first = new Button("First"));
- m_toolbar.add(m_prev = new Button("Prev"));
- m_toolbar.add(m_next = new Button("Next"));
- m_toolbar.add(m_last = new Button("Last"));
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: simpledao\r\n" +
- "Author: Dan Jinguji\r\n" +
- "Created with Microsoft Visual J++ Version 1.0";
- }
-
- // PARAMETER SUPPORT
- // The getParameterInfo() method returns an array of strings describing
- // the parameters understood by this applet.
- //
- // simpledao Parameter Information:
- // { "Name", "Type", "Description" },
- //--------------------------------------------------------------------------
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_Database, "String", "Name of the Database" },
- { PARAM_Recordset, "String", "Name of the Recordset" },
- };
- return info;
- }
-
- // 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 (!m_fStandAlone)
- GetParameters(null);
- // 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);
-
- // Set the name of the database (parameter)
- if (m_fStandAlone)
- // use the value on the command line for the MDB filename
- filename = m_Database;
- else
- try {
- // otherwise generate it relative to the applet
- java.net.URL fn;
- fn = new java.net.URL(getDocumentBase(), m_Database);
- // strip away the "file:/" from the URL
- filename = fn.getFile().substring(1);
- } catch(Exception e) {
- showStatus("Error: " + e.getMessage());
- }
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- }
-
- // simpledao 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()
- {
- // Create the database engine using the license key
- _DBEngine idbengine = dao_dbengine.create();
- Database database;
-
- // Create Variants for optional parameters
- Variant v1 = new Variant();
- Variant v2 = new Variant();
- Variant v3 = new Variant();
-
- v1.putBoolean(false);
- v2.putBoolean(false);
- v3.putString("");
-
- // Open the database for non-exclusive access
- database = idbengine.OpenDatabase(filename, v1, v2, v3);
-
- // Create Variants for optional parameters
- Variant v4 = new Variant();
- Variant v5 = new Variant();
-
- v4.putShort(Constants.dbOpenDynaset);
- v5.putShort(Constants.dbReadOnly);
-
- // Create the recordset, a read-only dynaset
- recordset = database.OpenRecordset(m_Recordset, v4, v5);
-
- // Create the form, passing it the recordset
- m_form = new SimpleForm(recordset);
-
- // Add it to the applet
- add("Center", m_form);
-
- // Get the first record in the recordset
- recordset.MoveFirst();
-
- // Display that record
- m_form.showData();
-
- }
-
- // 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()
- {
- // Close the recordset
- recordset.Close();
- recordset = null;
-
- // Remove the form
- remove(m_form);
- m_form = null;
- }
-
-
- public boolean action(Event evt, Object what)
- {
- // Handle click on the First button
- if (evt.target == m_first)
- {
- // Update the status bar, if available
- if (! m_fStandAlone) showStatus ("Getting first record");
-
- // Move to the requested record
- recordset.MoveFirst();
-
- // Show that record
- m_form.showData();
-
- // Event handled
- return true;
- }
-
- // Handle click on the Prev button
- if (evt.target == m_prev)
- {
- // Update the status bar, if available
- if (! m_fStandAlone) showStatus ("Getting previous record");
-
- // Move to the requested record
- recordset.MovePrevious();
-
- // Show that record
- m_form.showData();
-
- // Event handled
- return true;
- }
-
- // Handle click on the Next button
- if (evt.target == m_next)
- {
- // Update the status bar, if available
- if (! m_fStandAlone) showStatus ("Getting next record");
-
- // Move to the requested record
- recordset.MoveNext();
-
- // Show that record
- m_form.showData();
-
- // Event handled
- return true;
- }
-
- // Handle click on the Last button
- if (evt.target == m_last)
- {
- // Update the status bar, if available
- if (! m_fStandAlone) showStatus ("Getting last record");
-
- // Move to the requested record
- recordset.MoveLast();
-
- // Show that record
- m_form.showData();
-
- // Event handled
- return true;
- }
-
- // Event not handled
- return false;
- }
-
- }
-