home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / jpl / JPL_Rolo / JPL_Rolo.jpl next >
Text File  |  1999-09-14  |  15KB  |  558 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.lang.*;
  4. import java.util.*;
  5.  
  6. public class JPL_Rolo extends Frame {
  7.  
  8.     // The primary key of the row that is current onscreen.
  9.     //
  10.     int current_row = 0;
  11.  
  12.     // TextField objects for each column.
  13.     //
  14.     TextField fld_name, fld_address, fld_city, fld_state, fld_zip, fld_id;
  15.  
  16.     // Add or Edit mode.
  17.     //
  18.     String edit_status; 
  19.  
  20.     // a layout manager for the Frame
  21.     //
  22.     GridBagLayout gb = new GridBagLayout();
  23.  
  24.     // Action buttons.
  25.     //
  26.     Button next, previous, quit, save, newrow, edit, cancel, delete;
  27.  
  28.     // A Panel for the action buttons.
  29.     //
  30.     Panel actionbuttons;
  31.  
  32.     /**
  33.      * Construct a new instance of JPL_Rolo.
  34.      */
  35.     public JPL_Rolo(String[] argv) {
  36.         CreateForm();
  37.         addWindowListener(new WinEventHandler() );
  38.     }
  39.  
  40.     public void CreateForm() {
  41.  
  42.         // set the layout for the frame
  43.         //
  44.         this.setLayout(gb);
  45.  
  46.         // this is the offset within the GridBagLayout. If
  47.         // I want the next object on a different line, I 
  48.         // postincrement. If not, I don't.
  49.         //
  50.         int i = 0;
  51.  
  52.         // Add a text field for the name.
  53.         // 
  54.         AddToFrame(new Label("Name:"), 0, i);
  55.         fld_name = new TextField(20);
  56.         fld_name.setEditable(false);
  57.         AddToFrame(fld_name, 1, i++);
  58.  
  59.         // The address.
  60.         //
  61.         AddToFrame(new Label("Address:"), 0, i);
  62.         fld_address = new TextField(35);
  63.         fld_address.setEditable(false);
  64.         AddToFrame(fld_address, 1, i++);
  65.  
  66.         // The City. I'm not going to increment i, so the
  67.         // next field will show up on the same line.
  68.         //
  69.         AddToFrame(new Label("City:"), 0, i);
  70.         fld_city = new TextField(20);
  71.         fld_city.setEditable(false);
  72.         AddToFrame(fld_city, 1, i);
  73.  
  74.         // The State.
  75.         //
  76.         AddToFrame(new Label("State:"), 2, i);
  77.         fld_state = new TextField(2);
  78.         fld_state.setEditable(false);
  79.         AddToFrame(fld_state, 3, i++);
  80.  
  81.         // The Zip Code.
  82.         //
  83.         AddToFrame(new Label("Zip:"), 0, i);
  84.         fld_zip = new TextField(11);
  85.         fld_zip.setEditable(false);
  86.         AddToFrame(fld_zip, 1, i++);
  87.  
  88.         // The id - this is always read-only.
  89.         //
  90.         AddToFrame(new Label("Id:"), 0, i);
  91.         fld_id = new TextField(4);
  92.         fld_id.setEditable(false);
  93.         AddToFrame(fld_id, 1, i++);
  94.  
  95.         // create the button panel and give it a FlowLayout
  96.         //
  97.         actionbuttons = new Panel();
  98.         actionbuttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  99.     
  100.         // Add the button panel to the Frame. The AddToFrame
  101.         // method isn't really set up to handle this sort of
  102.         // panel, so we will go through the tedious process
  103.         // of managing the GridBagConstraints...
  104.         //
  105.         GridBagConstraints c = new GridBagConstraints();
  106.         c.gridwidth = 3; c.gridheight = 1;
  107.         c.fill = GridBagConstraints.NONE;
  108.         c.anchor = GridBagConstraints.CENTER;
  109.         c.weightx = 0.0; c.weighty = 0.0;   
  110.         c.gridx = 0; c.gridy = i;
  111.         ((GridBagLayout)this.getLayout()).setConstraints(actionbuttons, c);   
  112.         this.add(actionbuttons);
  113.  
  114.         // instantiate and add each of the buttons
  115.         //
  116.         previous = new Button("Previous");
  117.         actionbuttons.add(previous);
  118.         previous.addActionListener( new PrevRowHandler() );
  119.  
  120.         next = new Button("Next");
  121.         actionbuttons.add(next);
  122.         next.addActionListener( new NextRowHandler() );
  123.  
  124.         quit = new Button("Quit");
  125.         actionbuttons.add(quit);
  126.         quit.addActionListener( new QuitHandler() );
  127.  
  128.         newrow = new Button("New");
  129.         actionbuttons.add(newrow);
  130.         newrow.addActionListener( new NewRowHandler() );
  131.  
  132.         edit = new Button("Edit");
  133.         actionbuttons.add(edit);
  134.         edit.addActionListener( new EditRowHandler() );
  135.  
  136.         delete = new Button("Delete");
  137.         actionbuttons.add(delete);
  138.         delete.addActionListener( new DeleteRowHandler() );
  139.  
  140.         // save and cancel are disabled until the user
  141.         // is adding or editing.
  142.         //
  143.         save = new Button("Save");
  144.         actionbuttons.add(save);
  145.         save.setEnabled(false);
  146.         save.addActionListener( new SaveHandler() );
  147.  
  148.         cancel = new Button("Cancel");
  149.         actionbuttons.add(cancel);
  150.         cancel.setEnabled(false);
  151.         cancel.addActionListener( new CancelHandler() );
  152.  
  153.         // Invoke getRow() to display the first row in the table.
  154.         //
  155.         getRow(0);
  156.  
  157.     }
  158.  
  159.     /**
  160.      * Return the id of the current row.
  161.      */
  162.     public int getCurrentRowVal() {
  163.         return current_row;
  164.     }
  165.  
  166.     public void setCols(String name, String address, String city, String state, String zip, String id) {
  167.  
  168.         clearForm();
  169.  
  170.         fld_name.setText(name);
  171.         fld_address.setText(address);
  172.         fld_city.setText(city);
  173.         fld_state.setText(state);
  174.         fld_zip.setText(zip);
  175.         fld_id.setText(id);
  176.         current_row = Integer.parseInt(id);
  177.           
  178.     }
  179.  
  180.  
  181.     public void setCurrentRow(int r) {
  182.         current_row = r;
  183.     }
  184.  
  185.     public String getName()    { return fld_name.getText(); }
  186.     public String getAddress() { return fld_address.getText(); }
  187.     public String getCity()    { return fld_city.getText(); }
  188.     public String getState()   { return fld_state.getText(); }
  189.     public String getZip()     { return fld_zip.getText(); }
  190.     public String getId()      { return fld_id.getText(); }
  191.  
  192.     /**
  193.      * This eventhandler will move to the previous row.
  194.      */
  195.     class PrevRowHandler implements ActionListener {
  196.         public void actionPerformed( ActionEvent e) {
  197.             getRow(-1);
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * This eventhandler will move to the next row.
  203.      */
  204.     class NextRowHandler implements ActionListener {
  205.         public void actionPerformed( ActionEvent e) {
  206.             getRow(1);
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * This eventhandler will terminate the application.
  212.      */
  213.     class QuitHandler implements ActionListener {
  214.         public void actionPerformed( ActionEvent e) {
  215.             System.exit(0);
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * This eventhandler will display a blank record and put
  221.      * this application in new record mode.
  222.      */
  223.     class NewRowHandler implements ActionListener {
  224.         public void actionPerformed( ActionEvent e) {
  225.                 clearForm();
  226.                 edit_status = "new";
  227.                 setEdit();
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * This eventhandler will put the application in edit
  233.      * mode (for the current row).
  234.      */
  235.     class EditRowHandler implements ActionListener {
  236.         public void actionPerformed( ActionEvent e) {
  237.             edit_status = "edit";
  238.             setEdit();
  239.         }
  240.     }
  241.     /**
  242.      * This eventhandler will delete the current row.
  243.      */
  244.     class DeleteRowHandler implements ActionListener {
  245.         public void actionPerformed( ActionEvent e) {
  246.             delRow();
  247.         }
  248.     }
  249.  
  250.     /**
  251.      * This eventhandler will save (update or insert) the
  252.      * current record.
  253.      */
  254.     class SaveHandler implements ActionListener {
  255.         public void actionPerformed( ActionEvent e) {
  256.  
  257.             if (edit_status.equals("new")) {
  258.                 saveIt();
  259.             }
  260.             if (edit_status.equals("edit")) {
  261.                 updateRow();
  262.             }
  263.  
  264.             // set the edit_status to "browse", and call setBrowse()
  265.             //
  266.             edit_status = "browse";
  267.             setBrowse();
  268.         }
  269.     }
  270.  
  271.     /**
  272.      * This eventhandler cancels any pending edit.
  273.      */
  274.     class CancelHandler implements ActionListener {
  275.         public void actionPerformed( ActionEvent e) {
  276.             // if it was new, make sure that they can't edit the
  277.             // id field...
  278.  
  279.             if (edit_status.equals("new")) {
  280.                 fld_id.setEditable(false);
  281.             } 
  282.  
  283.             // return the edit_status to browse, call getRow()
  284.             // to retrieve the row they were looking at
  285.             // before editing or adding, and call setBrowse()
  286.             //
  287.             edit_status = "browse";
  288.             getRow(0);
  289.             setBrowse();
  290.         }
  291.     }
  292.  
  293.     // This is the event handler to deal with cases where
  294.     // the user closes the window with a window control.
  295.     //  
  296.     class WinEventHandler extends WindowAdapter {
  297.         public void windowClosing(WindowEvent e) {
  298.             System.exit(0);
  299.         }
  300.     }       
  301.  
  302.     /**
  303.      * clearForm()
  304.      */
  305.     protected void clearForm () {
  306.         fld_name.setText("");
  307.         fld_address.setText("");
  308.         fld_city.setText("");
  309.         fld_state.setText("");
  310.         fld_zip.setText("");
  311.         fld_id.setText("");
  312.     }
  313.  
  314.     /**
  315.      * AddToFrame()
  316.      * A convenience method to wrap the living hell
  317.      * that is GridBagConstraints()
  318.      */
  319.     protected void AddToFrame (Component item, int x, int y) {
  320.  
  321.         // some sane layout defaults.
  322.         //
  323.         GridBagConstraints c = new GridBagConstraints();
  324.         c.gridwidth = 1; c.gridheight = 1;
  325.         c.fill = GridBagConstraints.NONE;
  326.         c.anchor = GridBagConstraints.NORTHWEST;
  327.         c.weightx = 0.0; c.weighty = 0.0;        
  328.  
  329.         // set the grid coordinates
  330.         //
  331.         c.gridx = x; c.gridy = y;
  332.  
  333.         // set the constraints, and add the item to the layout
  334.         //
  335.  
  336.         ((GridBagLayout)this.getLayout()).setConstraints(item, c);    
  337.         this.add(item);
  338.     }
  339.  
  340.     /**
  341.      * setEdit()
  342.      *
  343.      * prepare the form for editing/adding
  344.      */
  345.     protected void setEdit () {
  346.     
  347.         // disable all these buttons
  348.         //
  349.         next.setEnabled(false);
  350.         previous.setEnabled(false);
  351.         newrow.setEnabled(false);
  352.         edit.setEnabled(false);
  353.         delete.setEnabled(false);
  354.  
  355.         // set everything except the id to be editable
  356.         //
  357.         fld_name.setEditable(true);
  358.         fld_address.setEditable(true);
  359.         fld_city.setEditable(true);
  360.         fld_state.setEditable(true);
  361.         fld_zip.setEditable(true);
  362.  
  363.         // enable these two buttons
  364.         //
  365.         save.setEnabled(true);
  366.         cancel.setEnabled(true);
  367.     }
  368.  
  369.     /**
  370.      * setBrowse()
  371.      *
  372.      * prepare the form for viewing
  373.      *
  374.      */
  375.     protected void setBrowse() {
  376.  
  377.         // enable all these buttons
  378.         //
  379.         next.setEnabled(true);
  380.         previous.setEnabled(true);
  381.         newrow.setEnabled(true);
  382.         edit.setEnabled(true);
  383.         delete.setEnabled(true);
  384.  
  385.         // disable the fields
  386.         //
  387.         fld_name.setEditable(false);
  388.         fld_address.setEditable(false);
  389.         fld_city.setEditable(false);
  390.         fld_state.setEditable(false);
  391.         fld_zip.setEditable(false);
  392.         fld_id.setEditable(false);
  393.    
  394.         // disable these two buttons
  395.         //
  396.         save.setEnabled(false);
  397.         cancel.setEnabled(false);
  398.     }
  399.  
  400.     perl void delRow() {{
  401.  
  402.         my $id      = $self->getId____s();
  403.  
  404.         $sql = qq[delete from cardfile ] .
  405.                qq[where (id = $id)];
  406.         
  407.         use Sprite;
  408.         my $rdb = new Sprite();
  409.         my @data = $rdb->sql($sql);
  410.         $rdb->close("cardfile");
  411.         my $status = shift @data;
  412.         if (!$status) {
  413.             print STDERR "Bummer - couldn't execute query!\n";
  414.             die;
  415.         }
  416.         $self->setCurrentRow__I(0);
  417.         $self->getRow__I(0);
  418.  
  419.     }}
  420.  
  421.     perl void updateRow() {{
  422.  
  423.         my $name    = $self->getName____s();
  424.         my $address = $self->getAddress____s();
  425.         my $city    = $self->getCity____s();
  426.         my $state   = $self->getState____s();
  427.         my $zip     = $self->getZip____s();
  428.         my $id      = $self->getId____s();
  429.  
  430.         $sql = qq[update cardfile ] .
  431.                qq[set name    = ('$name'), ] .
  432.                qq[set address = ('$address'), ] .
  433.                qq[set city    = ('$city'), ] .
  434.                qq[set state   = ('$state'), ] .
  435.                qq[set zip     = ('$zip') ] .
  436.                qq[where (id = $id)];
  437.         
  438.         use Sprite;
  439.         my $rdb = new Sprite();
  440.         my @data = $rdb->sql($sql);
  441.         $rdb->close("cardfile");
  442.         my $status = shift @data;
  443.         if (!$status) {
  444.             print STDERR "Bummer - couldn't execute query!\n";
  445.             die;
  446.         }
  447.  
  448.     }}
  449.  
  450.  
  451.     /**
  452.      * getRow()
  453.      *
  454.      * This method is used to either fetch this current row,
  455.      * in which case it is given an argument of zero, or it
  456.      * can be used to move relative to the current row, in
  457.      * which case it must be given an argument of 1 or -1.
  458.      *
  459.      */
  460.  
  461.  
  462.     perl void getRow(int direction) {{
  463.  
  464.         use Sprite;
  465.         my $rdb = new Sprite();
  466.  
  467.         my $nextid = $self->getCurrentRowVal____I() + $direction;
  468.         my $op;
  469.         if ($direction == -1) {
  470.             $op = "<=";
  471.         } else {
  472.             $op = ">=";
  473.         }
  474.         my $sql = "select name, address, city, "  .
  475.                   "state, zip, id from cardfile " .
  476.                   "where id $op $nextid";
  477.     
  478.         my @data = $rdb->sql($sql);
  479.         $rdb->close("cardfile");
  480.  
  481.         my $status = shift @data;
  482.         if (!$status) {
  483.             print STDERR "Bummer - couldn't execute query!\n";
  484.             die;
  485.         }
  486.  
  487.         my $numrows = scalar(@data);
  488.  
  489.         if (!$numrows) {
  490.             print STDERR "End of file reached.\n";
  491.             return;
  492.         }
  493.  
  494.         my $index;
  495.         if ($direction == -1) {
  496.             $index = $#data;
  497.         } else {
  498.             $index = 0;
  499.         }
  500.         my($name, $address, $city, $state, $zip, $id) = @{$data[$index]};
  501.         $self->setCols__ssssss($name, $address, $city, $state, $zip, $id);
  502.  
  503.     }}
  504.  
  505.     perl void saveIt() {{
  506.  
  507.         use Sprite;
  508.         my $rdb = new Sprite();
  509.  
  510.         my @data = $rdb->sql("select id, name from cardfile");
  511.  
  512.         my $status = shift @data;
  513.         if (!$status) {
  514.             print STDERR "Bummer - couldn't execute query!\n";
  515.             die;
  516.         }
  517.  
  518.         my @ids;
  519.         foreach $record (@data) {
  520.             my ($id, $name) = split (/\0/, $record, 2);
  521.             push @ids, $id;
  522.         }
  523.         @ids = sort @ids;
  524.         my $newid = $ids[$#ids] + 1;
  525.  
  526.         my $name    = $self->getName____s();
  527.         my $address = $self->getAddress____s();
  528.         my $city    = $self->getCity____s();
  529.         my $state   = $self->getState____s();
  530.         my $zip     = $self->getZip____s();
  531.  
  532.         my $sql = "insert into cardfile (name, address, city, state, zip, id) values ('$name', '$address', '$city', '$state', '$zip', $newid)";
  533.         @data = $rdb->sql($sql);
  534.         $rdb->close("cardfile");
  535.  
  536.         $status = shift @data;
  537.         if (!$status) {
  538.             print STDERR "Bummer - couldn't execute insert!\n";
  539.             die;
  540.         }
  541.  
  542.         $self->setCurrentRow__I($newid);
  543.  
  544.     }}
  545.  
  546.     public static void main(String[] args) {
  547.  
  548.         // make a new JPL_Rolo, pack() it and show() it.
  549.         JPL_Rolo cardfile = new JPL_Rolo(args);
  550.         cardfile.pack();
  551.         cardfile.show();
  552.  
  553.     }
  554.  
  555. }
  556.  
  557.  
  558.