1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
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();
}
}
|