dslinux/user/perl/jpl/JPL_Rolo JPL_Rolo.jpl Makefile.PL README cardfile

cayenne dslinux_cayenne at user.in-berlin.de
Mon Dec 4 18:00:01 CET 2006


Update of /cvsroot/dslinux/dslinux/user/perl/jpl/JPL_Rolo
In directory antilope:/tmp/cvs-serv17422/jpl/JPL_Rolo

Added Files:
	JPL_Rolo.jpl Makefile.PL README cardfile 
Log Message:
Adding fresh perl source to HEAD to branch from

--- NEW FILE: cardfile ---
name,address,city,state,zip,id
Brian Jepson,50 Hudson Street,Providence,RI,02909,100
Elvis Presley,50 Hudson Street,Providence,RI,02909,101
AS220,115 Empire Street,Providence,RI,02909,600
Mr. Jones,100 Loudermilk Drive,Springfield,??,,602
George Maciunas,Canal Street,New York,NY,????,603
Emmett Williams,Broome Street,New York,NY,?????,605

--- NEW FILE: README ---
Welcome to the Sprite sample application for Larry Wall's JPL. This
application showcases a merging of Java and Perl in which Java is employed
to generate a user interface, and Perl is used for data access.
Specifically, Perl is used with Shishir Gundavaram's Sprite module to offer
permanent storage through SQL manipulation of text files. This application
is a Rolodex(tm)-style address file, offering the ability to add, edit or
delete names and addresses. You may also navigate through the address list.

To use this example, you will need to install the Sprite module from CPAN.

To install the sample, you must first have JPL installed and working.
Please ensure that you have set environment variables as directed in the
JPL README and that the JPL Sample program works. Once this has been
accomplished, you can build the files in this directory with the following
commmands:

    perl Makefile.PL
    make
    make install

You can run this by typing:

    java JPL_Rolo

The application should appear with some sample data, and you can mess
around with it and put all your friends in the address book. Far out!


--- NEW FILE: Makefile.PL ---
#!/usr/bin/perl

$JPL_HOME = $ENV{JPL_HOME}
    or die "You have not run setvars to set your environment variables.\n" .
           "See the JPL README file for more information.\n";

use Config;

eval `$JPL_HOME/setvars -perl`;

chop($WHAT = `pwd`);
$WHAT =~ s#.*/##;

if ($^O eq 'linux') {
    $flags = "-Dbool=char";	# avoid builtin bool altogether
    $libs = "-lc -lm -ldl";
}
else {
    $flags = "";
    $libs = "-lc -lm -ldl";
}
chop($cwd = `pwd`);
($jpldir = $cwd) =~ s#/[^/]*$##;

open(MAKEFILE, ">Makefile");

print MAKEFILE <<"SUBS";
CC = $Config{cc}
WHAT = $WHAT
PERL = perl$]
ARCHNAME = $Config{archname}
JAVA_HOME = $ENV{JAVA_HOME}
JPL_HOME = $ENV{JPL_HOME}
PERLARCHDIR = $Config{archlib}
FLAGS = $Config{cccdlflags} $Config{ccdlflags} $Config{lddlflags} $flags
INCL  = -I\$(PERLARCHDIR)/CORE \\
	-I\$(JAVA_HOME)/include \\
	-I\$(JAVA_HOME)/include/$^O \\
	-I\$(JAVA_HOME)/include/genunix
LIBS = $libs

SUBS

print MAKEFILE <<'NOSUBS';
.SUFFIXES: .jpl .class

.jpl.class:
	$(PERL) -MJPL::Compile -e "file('$*.jpl')"

all: $(WHAT).class lib$(WHAT).so

debug: $(WHAT)_g.class lib$(WHAT)_g.so

lib$(WHAT).so: $(WHAT).c $(JPL_HOME)/lib/$(ARCHNAME)/libPerlInterpreter.so
	$(CC) $(FLAGS) $(INCL) $(WHAT).c \
	$(PERLARCHDIR)/CORE/libperl.so \
	$(JPL_HOME)/lib/$(ARCHNAME)/libPerlInterpreter.so \
	$(LIBS) \
	-o lib$(WHAT).so

lib$(WHAT)_g.so: $(WHAT).c $(JPL_HOME)/lib/$(ARCHNAME)/libPerlInterpreter_g.so
	$(CC) -g $(FLAGS) $(INCL) $(WHAT).c \
	$(PERLARCHDIR)/CORE/libperl.so \
	$(JPL_HOME)/lib/$(ARCHNAME)/libPerlInterpreter_g.so \
	$(LIBS) \
	-o lib$(WHAT)_g.so

test:

install: all
	cp $(WHAT).class	$(JPL_HOME)/lib
	cp lib$(WHAT).so	$(JPL_HOME)/lib/$(ARCHNAME)
	cp $(WHAT).pl		$(JPL_HOME)/perl

clean:
	rm -f $(WHAT).c $(WHAT).h \
	$(WHAT)*.class $(WHAT)*.pl lib$(WHAT).so $(WHAT)*.java

distclean: clean
	rm -f Makefile

NOSUBS

close MAKEFILE;

--- NEW FILE: JPL_Rolo.jpl ---
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;

public class JPL_Rolo extends Frame {

    // The primary key of the row that is current onscreen.
    //
    int current_row = 0;

    // TextField objects for each column.
    //
    TextField fld_name, fld_address, fld_city, fld_state, fld_zip, fld_id;

    // Add or Edit mode.
    //
    String edit_status; 

    // a layout manager for the Frame
    //
    GridBagLayout gb = new GridBagLayout();

    // Action buttons.
    //
    Button next, previous, quit, save, newrow, edit, cancel, delete;

    // A Panel for the action buttons.
    //
    Panel actionbuttons;

    /**
     * Construct a new instance of JPL_Rolo.
     */
    public JPL_Rolo(String[] argv) {
        CreateForm();
        addWindowListener(new WinEventHandler() );
    }

    public void CreateForm() {

        // set the layout for the frame
        //
        this.setLayout(gb);

        // this is the offset within the GridBagLayout. If
        // I want the next object on a different line, I 
        // postincrement. If not, I don't.
        //
        int i = 0;

        // Add a text field for the name.
        // 
        AddToFrame(new Label("Name:"), 0, i);
        fld_name = new TextField(20);
        fld_name.setEditable(false);
        AddToFrame(fld_name, 1, i++);

        // The address.
        //
        AddToFrame(new Label("Address:"), 0, i);
        fld_address = new TextField(35);
        fld_address.setEditable(false);
        AddToFrame(fld_address, 1, i++);

        // The City. I'm not going to increment i, so the
        // next field will show up on the same line.
        //
        AddToFrame(new Label("City:"), 0, i);
        fld_city = new TextField(20);
        fld_city.setEditable(false);
        AddToFrame(fld_city, 1, i);

        // The State.
        //
        AddToFrame(new Label("State:"), 2, i);
        fld_state = new TextField(2);
        fld_state.setEditable(false);
        AddToFrame(fld_state, 3, i++);

        // The Zip Code.
        //
        AddToFrame(new Label("Zip:"), 0, i);
        fld_zip = new TextField(11);
        fld_zip.setEditable(false);
        AddToFrame(fld_zip, 1, i++);

        // The id - this is always read-only.
        //
        AddToFrame(new Label("Id:"), 0, i);
        fld_id = new TextField(4);
        fld_id.setEditable(false);
        AddToFrame(fld_id, 1, i++);

        // create the button panel and give it a FlowLayout
        //
        actionbuttons = new Panel();
        actionbuttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    
        // Add the button panel to the Frame. The AddToFrame
        // method isn't really set up to handle this sort of
        // panel, so we will go through the tedious process
        // of managing the GridBagConstraints...
        //
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = 3; c.gridheight = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 0.0; c.weighty = 0.0;   
        c.gridx = 0; c.gridy = i;
        ((GridBagLayout)this.getLayout()).setConstraints(actionbuttons, c);   
        this.add(actionbuttons);

        // instantiate and add each of the buttons
        //
        previous = new Button("Previous");
        actionbuttons.add(previous);
        previous.addActionListener( new PrevRowHandler() );

        next = new Button("Next");
        actionbuttons.add(next);
        next.addActionListener( new NextRowHandler() );

        quit = new Button("Quit");
        actionbuttons.add(quit);
        quit.addActionListener( new QuitHandler() );

        newrow = new Button("New");
        actionbuttons.add(newrow);
        newrow.addActionListener( new NewRowHandler() );

        edit = new Button("Edit");
        actionbuttons.add(edit);
        edit.addActionListener( new EditRowHandler() );

        delete = new Button("Delete");
        actionbuttons.add(delete);
        delete.addActionListener( new DeleteRowHandler() );

        // save and cancel are disabled until the user
        // is adding or editing.
        //
        save = new Button("Save");
        actionbuttons.add(save);
        save.setEnabled(false);
        save.addActionListener( new SaveHandler() );

        cancel = new Button("Cancel");
        actionbuttons.add(cancel);
        cancel.setEnabled(false);
        cancel.addActionListener( new CancelHandler() );

        // Invoke getRow() to display the first row in the table.
        //
        getRow(0);

    }

    /**
     * Return the id of the current row.
     */
    public int getCurrentRowVal() {
        return current_row;
    }

    public void setCols(String name, String address, String city, String state, String zip, String id) {

        clearForm();

        fld_name.setText(name);
        fld_address.setText(address);
        fld_city.setText(city);
        fld_state.setText(state);
        fld_zip.setText(zip);
        fld_id.setText(id);
        current_row = Integer.parseInt(id);
          
    }


    public void setCurrentRow(int r) {
        current_row = r;
    }

    public String getName()    { return fld_name.getText(); }
    public String getAddress() { return fld_address.getText(); }
    public String getCity()    { return fld_city.getText(); }
    public String getState()   { return fld_state.getText(); }
    public String getZip()     { return fld_zip.getText(); }
    public String getId()      { return fld_id.getText(); }

    /**
     * This eventhandler will move to the previous row.
     */
    class PrevRowHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            getRow(-1);
        }
    }

    /**
     * This eventhandler will move to the next row.
     */
    class NextRowHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            getRow(1);
        }
    }

    /**
     * This eventhandler will terminate the application.
     */
    class QuitHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            System.exit(0);
        }
    }

    /**
     * This eventhandler will display a blank record and put
     * this application in new record mode.
     */
    class NewRowHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
                clearForm();
                edit_status = "new";
                setEdit();
        }
    }

    /**
     * This eventhandler will put the application in edit
     * mode (for the current row).
     */
    class EditRowHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            edit_status = "edit";
            setEdit();
        }
    }
    /**
     * This eventhandler will delete the current row.
     */
    class DeleteRowHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            delRow();
        }
    }

    /**
     * This eventhandler will save (update or insert) the
     * current record.
     */
    class SaveHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {

            if (edit_status.equals("new")) {
                saveIt();
            }
            if (edit_status.equals("edit")) {
                updateRow();
            }

            // set the edit_status to "browse", and call setBrowse()
            //
            edit_status = "browse";
            setBrowse();
        }
    }

    /**
     * This eventhandler cancels any pending edit.
     */
    class CancelHandler implements ActionListener {
        public void actionPerformed( ActionEvent e) {
            // if it was new, make sure that they can't edit the
            // id field...

            if (edit_status.equals("new")) {
                fld_id.setEditable(false);
            } 

            // return the edit_status to browse, call getRow()
            // to retrieve the row they were looking at
            // before editing or adding, and call setBrowse()
            //
            edit_status = "browse";
            getRow(0);
            setBrowse();
        }
    }

    // This is the event handler to deal with cases where
    // the user closes the window with a window control.
    //  
    class WinEventHandler extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }       

    /**
     * clearForm()
     */
    protected void clearForm () {
        fld_name.setText("");
        fld_address.setText("");
        fld_city.setText("");
        fld_state.setText("");
        fld_zip.setText("");
        fld_id.setText("");
    }

    /**
     * AddToFrame()
     * A convenience method to wrap the living hell
     * that is GridBagConstraints()
     */
    protected void AddToFrame (Component item, int x, int y) {

        // some sane layout defaults.
        //
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = 1; c.gridheight = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.weightx = 0.0; c.weighty = 0.0;        

        // set the grid coordinates
        //
        c.gridx = x; c.gridy = y;

        // set the constraints, and add the item to the layout
        //

        ((GridBagLayout)this.getLayout()).setConstraints(item, c);    
        this.add(item);
    }

    /**
     * setEdit()
     *
     * prepare the form for editing/adding
     */
    protected void setEdit () {
    
        // disable all these buttons
        //
        next.setEnabled(false);
        previous.setEnabled(false);
        newrow.setEnabled(false);
        edit.setEnabled(false);
        delete.setEnabled(false);

        // set everything except the id to be editable
        //
        fld_name.setEditable(true);
        fld_address.setEditable(true);
        fld_city.setEditable(true);
        fld_state.setEditable(true);
        fld_zip.setEditable(true);

        // enable these two buttons
        //
        save.setEnabled(true);
        cancel.setEnabled(true);
    }

    /**
     * setBrowse()
     *
     * prepare the form for viewing
     *
     */
    protected void setBrowse() {

        // enable all these buttons
        //
        next.setEnabled(true);
        previous.setEnabled(true);
        newrow.setEnabled(true);
        edit.setEnabled(true);
        delete.setEnabled(true);

        // disable the fields
        //
        fld_name.setEditable(false);
        fld_address.setEditable(false);
        fld_city.setEditable(false);
        fld_state.setEditable(false);
        fld_zip.setEditable(false);
        fld_id.setEditable(false);
   
        // disable these two buttons
        //
        save.setEnabled(false);
        cancel.setEnabled(false);
    }

    perl void delRow() {{

        my $id      = $self->getId____s();

        $sql = qq[delete from cardfile ] .
               qq[where (id = $id)];
        
        use Sprite;
        my $rdb = new Sprite();
        my @data = $rdb->sql($sql);
        $rdb->close("cardfile");
        my $status = shift @data;
        if (!$status) {
            print STDERR "Bummer - couldn't execute query!\n";
            die;
        }
        $self->setCurrentRow__I(0);
        $self->getRow__I(0);

    }}

    perl void updateRow() {{

        my $name    = $self->getName____s();
        my $address = $self->getAddress____s();
        my $city    = $self->getCity____s();
        my $state   = $self->getState____s();
        my $zip     = $self->getZip____s();
        my $id      = $self->getId____s();

        $sql = qq[update cardfile ] .
               qq[set name    = ('$name'), ] .
               qq[set address = ('$address'), ] .
               qq[set city    = ('$city'), ] .
               qq[set state   = ('$state'), ] .
               qq[set zip     = ('$zip') ] .
               qq[where (id = $id)];
        
        use Sprite;
        my $rdb = new Sprite();
        my @data = $rdb->sql($sql);
        $rdb->close("cardfile");
        my $status = shift @data;
        if (!$status) {
            print STDERR "Bummer - couldn't execute query!\n";
            die;
        }

    }}


    /**
     * getRow()
     *
     * This method is used to either fetch this current row,
     * in which case it is given an argument of zero, or it
     * can be used to move relative to the current row, in
     * which case it must be given an argument of 1 or -1.
     *
     */


    perl void getRow(int direction) {{

        use Sprite;
        my $rdb = new Sprite();

        my $nextid = $self->getCurrentRowVal____I() + $direction;
        my $op;
        if ($direction == -1) {
            $op = "<=";
        } else {
            $op = ">=";
        }
        my $sql = "select name, address, city, "  .
                  "state, zip, id from cardfile " .
                  "where id $op $nextid";
	
        my @data = $rdb->sql($sql);
        $rdb->close("cardfile");

        my $status = shift @data;
        if (!$status) {
            print STDERR "Bummer - couldn't execute query!\n";
            die;
        }

        my $numrows = scalar(@data);

        if (!$numrows) {
            print STDERR "End of file reached.\n";
            return;
        }

        my $index;
        if ($direction == -1) {
            $index = $#data;
        } else {
            $index = 0;
        }
        my($name, $address, $city, $state, $zip, $id) = @{$data[$index]};
        $self->setCols__ssssss($name, $address, $city, $state, $zip, $id);

    }}

    perl void saveIt() {{

        use Sprite;
        my $rdb = new Sprite();

        my @data = $rdb->sql("select id, name from cardfile");

        my $status = shift @data;
        if (!$status) {
            print STDERR "Bummer - couldn't execute query!\n";
            die;
        }

        my @ids;
        foreach $record (@data) {
            my ($id, $name) = split (/\0/, $record, 2);
            push @ids, $id;
        }
        @ids = sort @ids;
        my $newid = $ids[$#ids] + 1;

        my $name    = $self->getName____s();
        my $address = $self->getAddress____s();
        my $city    = $self->getCity____s();
        my $state   = $self->getState____s();
        my $zip     = $self->getZip____s();

        my $sql = "insert into cardfile (name, address, city, state, zip, id) values ('$name', '$address', '$city', '$state', '$zip', $newid)";
        @data = $rdb->sql($sql);
        $rdb->close("cardfile");

        $status = shift @data;
        if (!$status) {
            print STDERR "Bummer - couldn't execute insert!\n";
            die;
        }

        $self->setCurrentRow__I($newid);

    }}

    public static void main(String[] args) {

        // make a new JPL_Rolo, pack() it and show() it.
        JPL_Rolo cardfile = new JPL_Rolo(args);
        cardfile.pack();
        cardfile.show();

    }

}






More information about the dslinux-commit mailing list