2 import java.awt.event.*;
6 public class JPL_Rolo extends Frame {
8 // The primary key of the row that is current onscreen.
12 // TextField objects for each column.
14 TextField fld_name, fld_address, fld_city, fld_state, fld_zip, fld_id;
20 // a layout manager for the Frame
22 GridBagLayout gb = new GridBagLayout();
26 Button next, previous, quit, save, newrow, edit, cancel, delete;
28 // A Panel for the action buttons.
33 * Construct a new instance of JPL_Rolo.
35 public JPL_Rolo(String[] argv) {
37 addWindowListener(new WinEventHandler() );
40 public void CreateForm() {
42 // set the layout for the frame
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.
52 // Add a text field for the name.
54 AddToFrame(new Label("Name:"), 0, i);
55 fld_name = new TextField(20);
56 fld_name.setEditable(false);
57 AddToFrame(fld_name, 1, i++);
61 AddToFrame(new Label("Address:"), 0, i);
62 fld_address = new TextField(35);
63 fld_address.setEditable(false);
64 AddToFrame(fld_address, 1, i++);
66 // The City. I'm not going to increment i, so the
67 // next field will show up on the same line.
69 AddToFrame(new Label("City:"), 0, i);
70 fld_city = new TextField(20);
71 fld_city.setEditable(false);
72 AddToFrame(fld_city, 1, i);
76 AddToFrame(new Label("State:"), 2, i);
77 fld_state = new TextField(2);
78 fld_state.setEditable(false);
79 AddToFrame(fld_state, 3, i++);
83 AddToFrame(new Label("Zip:"), 0, i);
84 fld_zip = new TextField(11);
85 fld_zip.setEditable(false);
86 AddToFrame(fld_zip, 1, i++);
88 // The id - this is always read-only.
90 AddToFrame(new Label("Id:"), 0, i);
91 fld_id = new TextField(4);
92 fld_id.setEditable(false);
93 AddToFrame(fld_id, 1, i++);
95 // create the button panel and give it a FlowLayout
97 actionbuttons = new Panel();
98 actionbuttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
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...
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);
114 // instantiate and add each of the buttons
116 previous = new Button("Previous");
117 actionbuttons.add(previous);
118 previous.addActionListener( new PrevRowHandler() );
120 next = new Button("Next");
121 actionbuttons.add(next);
122 next.addActionListener( new NextRowHandler() );
124 quit = new Button("Quit");
125 actionbuttons.add(quit);
126 quit.addActionListener( new QuitHandler() );
128 newrow = new Button("New");
129 actionbuttons.add(newrow);
130 newrow.addActionListener( new NewRowHandler() );
132 edit = new Button("Edit");
133 actionbuttons.add(edit);
134 edit.addActionListener( new EditRowHandler() );
136 delete = new Button("Delete");
137 actionbuttons.add(delete);
138 delete.addActionListener( new DeleteRowHandler() );
140 // save and cancel are disabled until the user
141 // is adding or editing.
143 save = new Button("Save");
144 actionbuttons.add(save);
145 save.setEnabled(false);
146 save.addActionListener( new SaveHandler() );
148 cancel = new Button("Cancel");
149 actionbuttons.add(cancel);
150 cancel.setEnabled(false);
151 cancel.addActionListener( new CancelHandler() );
153 // Invoke getRow() to display the first row in the table.
160 * Return the id of the current row.
162 public int getCurrentRowVal() {
166 public void setCols(String name, String address, String city, String state, String zip, String id) {
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);
176 current_row = Integer.parseInt(id);
181 public void setCurrentRow(int r) {
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(); }
193 * This eventhandler will move to the previous row.
195 class PrevRowHandler implements ActionListener {
196 public void actionPerformed( ActionEvent e) {
202 * This eventhandler will move to the next row.
204 class NextRowHandler implements ActionListener {
205 public void actionPerformed( ActionEvent e) {
211 * This eventhandler will terminate the application.
213 class QuitHandler implements ActionListener {
214 public void actionPerformed( ActionEvent e) {
220 * This eventhandler will display a blank record and put
221 * this application in new record mode.
223 class NewRowHandler implements ActionListener {
224 public void actionPerformed( ActionEvent e) {
232 * This eventhandler will put the application in edit
233 * mode (for the current row).
235 class EditRowHandler implements ActionListener {
236 public void actionPerformed( ActionEvent e) {
237 edit_status = "edit";
242 * This eventhandler will delete the current row.
244 class DeleteRowHandler implements ActionListener {
245 public void actionPerformed( ActionEvent e) {
251 * This eventhandler will save (update or insert) the
254 class SaveHandler implements ActionListener {
255 public void actionPerformed( ActionEvent e) {
257 if (edit_status.equals("new")) {
260 if (edit_status.equals("edit")) {
264 // set the edit_status to "browse", and call setBrowse()
266 edit_status = "browse";
272 * This eventhandler cancels any pending edit.
274 class CancelHandler implements ActionListener {
275 public void actionPerformed( ActionEvent e) {
276 // if it was new, make sure that they can't edit the
279 if (edit_status.equals("new")) {
280 fld_id.setEditable(false);
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()
287 edit_status = "browse";
293 // This is the event handler to deal with cases where
294 // the user closes the window with a window control.
296 class WinEventHandler extends WindowAdapter {
297 public void windowClosing(WindowEvent e) {
305 protected void clearForm () {
306 fld_name.setText("");
307 fld_address.setText("");
308 fld_city.setText("");
309 fld_state.setText("");
316 * A convenience method to wrap the living hell
317 * that is GridBagConstraints()
319 protected void AddToFrame (Component item, int x, int y) {
321 // some sane layout defaults.
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;
329 // set the grid coordinates
331 c.gridx = x; c.gridy = y;
333 // set the constraints, and add the item to the layout
336 ((GridBagLayout)this.getLayout()).setConstraints(item, c);
343 * prepare the form for editing/adding
345 protected void setEdit () {
347 // disable all these buttons
349 next.setEnabled(false);
350 previous.setEnabled(false);
351 newrow.setEnabled(false);
352 edit.setEnabled(false);
353 delete.setEnabled(false);
355 // set everything except the id to be editable
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);
363 // enable these two buttons
365 save.setEnabled(true);
366 cancel.setEnabled(true);
372 * prepare the form for viewing
375 protected void setBrowse() {
377 // enable all these buttons
379 next.setEnabled(true);
380 previous.setEnabled(true);
381 newrow.setEnabled(true);
382 edit.setEnabled(true);
383 delete.setEnabled(true);
385 // disable the fields
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);
394 // disable these two buttons
396 save.setEnabled(false);
397 cancel.setEnabled(false);
400 perl void delRow() {{
402 my $id = $self->getId____s();
404 $sql = qq[delete from cardfile ] .
405 qq[where (id = $id)];
408 my $rdb = new Sprite();
409 my @data = $rdb->sql($sql);
410 $rdb->close("cardfile");
411 my $status = shift @data;
413 print STDERR "Bummer - couldn't execute query!\n";
416 $self->setCurrentRow__I(0);
421 perl void updateRow() {{
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();
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)];
439 my $rdb = new Sprite();
440 my @data = $rdb->sql($sql);
441 $rdb->close("cardfile");
442 my $status = shift @data;
444 print STDERR "Bummer - couldn't execute query!\n";
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.
462 perl void getRow(int direction) {{
465 my $rdb = new Sprite();
467 my $nextid = $self->getCurrentRowVal____I() + $direction;
469 if ($direction == -1) {
474 my $sql = "select name, address, city, " .
475 "state, zip, id from cardfile " .
476 "where id $op $nextid";
478 my @data = $rdb->sql($sql);
479 $rdb->close("cardfile");
481 my $status = shift @data;
483 print STDERR "Bummer - couldn't execute query!\n";
487 my $numrows = scalar(@data);
490 print STDERR "End of file reached.\n";
495 if ($direction == -1) {
500 my($name, $address, $city, $state, $zip, $id) = @{$data[$index]};
501 $self->setCols__ssssss($name, $address, $city, $state, $zip, $id);
505 perl void saveIt() {{
508 my $rdb = new Sprite();
510 my @data = $rdb->sql("select id, name from cardfile");
512 my $status = shift @data;
514 print STDERR "Bummer - couldn't execute query!\n";
519 foreach $record (@data) {
520 my ($id, $name) = split (/\0/, $record, 2);
524 my $newid = $ids[$#ids] + 1;
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();
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");
536 $status = shift @data;
538 print STDERR "Bummer - couldn't execute insert!\n";
542 $self->setCurrentRow__I($newid);
546 public static void main(String[] args) {
548 // make a new JPL_Rolo, pack() it and show() it.
549 JPL_Rolo cardfile = new JPL_Rolo(args);