-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryUI.java
More file actions
executable file
·796 lines (690 loc) · 24 KB
/
Copy pathLibraryUI.java
File metadata and controls
executable file
·796 lines (690 loc) · 24 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
* LibraryUI.java former Comp302.java
*
* Created on March 6, 2003, 2:52 PM with netbeans
*
* Updated on March 25 2005 by Jerome Dolman
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.Document;
import javax.swing.text.BadLocationException;
import static java.awt.event.KeyEvent.VK_E;
import static java.awt.event.KeyEvent.VK_T;
import static javax.swing.BoxLayout.X_AXIS;
import static javax.swing.JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.QUESTION_MESSAGE;
import static javax.swing.JOptionPane.showInputDialog;
import static javax.swing.JOptionPane.showMessageDialog;
import static javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
import static javax.swing.KeyStroke.getKeyStroke;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
import static java.util.Calendar.DAY_OF_MONTH;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
public class LibraryUI extends JFrame {
// Actions
private Action exitAction;
private Action clearTextAction;
private Action borrowAction;
private Action returnAction;
// The main output area
private JTextArea outputArea;
// Return fields
private JTextField retISBN;
private JTextField retCustID;
// Borrow fields
private JTextField borISBN;
private JTextField borCustID;
private JComboBox borDay;
private JComboBox borMonth;
private JComboBox borYear;
// Buttons and tabbed pane - keep them for focus order
private JButton returnButton;
private JButton borrowButton;
private JTabbedPane tabbedPane;
// The data model
private LibraryModel model;
// A parent for modal dialogs
private JFrame dialogParent = this;
/**
* Create a new LibraryUI object - showing an authentication dialog
* then bringing up the main window.
*/
public LibraryUI() {
super("JDBC Library");
// Uncomment the following if you'd rather not see everything in bold
//UIManager.put("swing.boldMetal", Boolean.FALSE);
// Initialise everything
initActions();
initUI();
initFocusTraversalPolicy();
setSize(600, 600);
// Show Authentication dialog
AuthDialog ad = new AuthDialog(this, "Authentication");
ad.setVisible(true);
String userName = ad.getUserName();
String password = ad.getDatabasePassword();
// Create data model
model = new LibraryModel(this, userName, password);
// Center window on screen
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();
setLocation(center.x - getSize().width/2,
center.y - getSize().height/2);
// Show ourselves
setVisible(true);
}
private void initActions() {
exitAction = new ExitAction();
clearTextAction = new ClearTextAction();
borrowAction = new BorrowAction();
returnAction = new ReturnAction();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
doExit();
}
});
}
private void initUI() {
// Create tabbed pane with commands in it
tabbedPane = new JTabbedPane();
getContentPane().add(tabbedPane, BorderLayout.NORTH);
tabbedPane.addTab("Book", null, createBookPane(),
"View book information");
tabbedPane.addTab("Author", null, createAuthorPane(),
"View author information");
tabbedPane.addTab("Customer", null, createCustomerPane(),
"View customer information");
tabbedPane.addTab("Borrow Book", null, createBorrowPane(),
"Borrow books for a customer");
tabbedPane.addTab("Return Book", null, createReturnPane(),
"Return books for a customer");
// Create output area with scrollpane
outputArea = new JTextArea();
outputArea.setEditable(false);
outputArea.setFocusable(false);
outputArea.setTabSize(2);
JScrollPane sp = new JScrollPane(outputArea);
sp.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(sp, BorderLayout.CENTER);
// Create menus
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem clearTextMenuItem = new JMenuItem(clearTextAction);
JMenuItem exitMenuItem = new JMenuItem(exitAction);
fileMenu.add(clearTextMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// Pack it all
pack();
}
// By default the GridBagLayout stuffs up tab ordering for the
// borrow book and return book panes, so I need this to ensure it's all
// the right way round.
private void initFocusTraversalPolicy() {
Container nearestRoot =
(isFocusCycleRoot()) ? this : getFocusCycleRootAncestor();
final FocusTraversalPolicy defaultPolicy =
nearestRoot.getFocusTraversalPolicy();
MapFocusTraversalPolicy mine =
new MapFocusTraversalPolicy(defaultPolicy, tabbedPane);
mine.putAfter(retISBN, retCustID);
mine.putAfter(retCustID, returnButton);
mine.putAfter(returnButton, tabbedPane);
mine.putAfter(borISBN, borCustID);
mine.putAfter(borCustID, borDay);
mine.putAfter(borDay, borMonth);
mine.putAfter(borMonth, borYear);
mine.putAfter(borYear, borrowButton);
mine.putAfter(borrowButton, tabbedPane);
mine.putBefore(retCustID, retISBN);
mine.putBefore(returnButton, retCustID);
mine.putBefore(borCustID, borISBN);
mine.putBefore(borDay, borCustID);
mine.putBefore(borMonth, borDay);
mine.putBefore(borYear, borMonth);
mine.putBefore(borrowButton, borYear);
mine.putTabBefore("Borrow Book", borrowButton);
mine.putTabBefore("Return Book", returnButton);
nearestRoot.setFocusTraversalPolicy(mine);
}
private Container createBookPane() {
// Create buttons
JButton bookLookup = new JButton(new BookLookupAction());
JButton showCat = new JButton(new ShowCatalogueAction());
JButton showLoanedBook = new JButton(new ShowLoanedBooksAction());
JButton deleteBook = new JButton(new DeleteBookAction());
// Create panel
Box pane = new Box(X_AXIS);
pane.add(Box.createHorizontalGlue());
pane.add(bookLookup);
pane.add(Box.createHorizontalStrut(5));
pane.add(showCat);
pane.add(Box.createHorizontalStrut(5));
pane.add(showLoanedBook);
pane.add(Box.createHorizontalStrut(5));
pane.add(deleteBook);
pane.add(Box.createHorizontalGlue());
return pane;
}
private Container createAuthorPane() {
// Create buttons
JButton showAuthor = new JButton(new ShowAuthorAction());
JButton showAllAuth = new JButton(new ShowAllAuthorsAction());
JButton deleteAuthor = new JButton(new DeleteAuthorAction());
// Create panel
Box pane = new Box(X_AXIS);
pane.add(Box.createHorizontalGlue());
pane.add(showAuthor);
pane.add(Box.createHorizontalStrut(5));
pane.add(showAllAuth);
pane.add(Box.createHorizontalStrut(5));
pane.add(deleteAuthor);
pane.add(Box.createHorizontalGlue());
return pane;
}
private Container createCustomerPane() {
// Create buttons
JButton showCus = new JButton(new ShowCustomerAction());
JButton showAllCus = new JButton(new ShowAllCustomersAction());
JButton deleteCus = new JButton(new DeleteCustomerAction());
// Create panel
Box pane = new Box(X_AXIS);
pane.add(Box.createHorizontalGlue());
pane.add(showCus);
pane.add(Box.createHorizontalStrut(5));
pane.add(showAllCus);
pane.add(Box.createHorizontalStrut(5));
pane.add(deleteCus);
pane.add(Box.createHorizontalGlue());
return pane;
}
private Container createBorrowPane() {
// Initialise date combo boxes
borDay = new JComboBox();
borMonth = new JComboBox();
borYear = new JComboBox();
String[] days = new String[31];
for (int i = 0; i < 31; i++) days[i] = String.valueOf(i+1);
String[] months = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
String[] years = { "2005", "2006", "2007", "2008", "2009", "2010", "2011","2012","2013","2014","2015"};
borDay.setModel(new DefaultComboBoxModel(days));
borMonth.setModel(new DefaultComboBoxModel(months));
borYear.setModel(new DefaultComboBoxModel(years));
Calendar today = Calendar.getInstance();
borDay.setSelectedIndex(today.get(DAY_OF_MONTH)-1);
borMonth.setSelectedIndex(today.get(MONTH));
borYear.setSelectedIndex(today.get(YEAR) - 2005);
// Create borrow button
borrowButton = new JButton(borrowAction);
// Create text fields
borISBN = new JTextField(15);
borCustID = new JTextField(15);
// Create panel and layout
JPanel pane = new JPanel();
pane.setOpaque(false);
GridBagLayout gb = new GridBagLayout();
pane.setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(1, 5, 1, 5);
// Fill panel
c.anchor = GridBagConstraints.EAST;
addToGridBag(gb, c, pane, new JLabel("ISBN:"), 0, 0, 1, 1);
addToGridBag(gb, c, pane, new JLabel("Customer ID:"), 0, 1, 1, 1);
addToGridBag(gb, c, pane, new JLabel("Due Date:"), 0, 2, 1, 1);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(gb, c, pane, borISBN, 1, 0, 3, 1);
addToGridBag(gb, c, pane, borCustID, 1, 1, 3, 1);
c.fill = GridBagConstraints.NONE;
addToGridBag(gb, c, pane, borDay, 1, 2, 1, 1);
addToGridBag(gb, c, pane, borMonth, 2, 2, 1, 1);
addToGridBag(gb, c, pane, borYear, 3, 2, 1, 1);
addToGridBag(gb, c, pane, borrowButton, 4, 0, 1, 3);
// Set up VK_ENTER triggering the borrow button in this panel
InputMap input = pane.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
input.put(getKeyStroke("ENTER"), "borrowAction");
pane.getActionMap().put("borrowAction", borrowAction);
return pane;
}
private Container createReturnPane() {
// Create return button
returnButton = new JButton(returnAction);
// Create text fields
retISBN = new JTextField(15);
retCustID = new JTextField(15);
// Create panel and layout
JPanel pane = new JPanel();
pane.setOpaque(false);
GridBagLayout gb = new GridBagLayout();
pane.setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(1, 5, 1, 5);
// Fill panel
c.anchor = GridBagConstraints.EAST;
addToGridBag(gb, c, pane, new JLabel("ISBN:"), 0, 0, 1, 1);
addToGridBag(gb, c, pane, new JLabel("Customer ID:"), 0, 1, 1, 1);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(gb, c, pane, retISBN, 1, 0, 3, 1);
addToGridBag(gb, c, pane, retCustID, 1, 1, 3, 1);
c.fill = GridBagConstraints.NONE;
addToGridBag(gb, c, pane, returnButton, 4, 0, 1, 3);
// Set up VK_ENTER triggering the return button in this panel
InputMap input = pane.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
input.put(getKeyStroke("ENTER"), "returnAction");
pane.getActionMap().put("returnAction", returnAction);
return pane;
}
private void addToGridBag(GridBagLayout gb, GridBagConstraints c,
Container cont, JComponent item,
int x, int y, int w, int h) {
c.gridx = x;
c.gridy = y;
c.gridwidth = w;
c.gridheight = h;
gb.setConstraints(item, c);
cont.add(item);
}
private class ExitAction extends AbstractAction {
public ExitAction() {
super("Exit");
putValue(MNEMONIC_KEY, VK_E);
putValue(ACCELERATOR_KEY, getKeyStroke("ctrl Q"));
}
public void actionPerformed(ActionEvent evt) {
doExit();
}
}
private class ClearTextAction extends AbstractAction {
public ClearTextAction() {
super("Clear Text");
putValue(MNEMONIC_KEY, VK_T);
putValue(ACCELERATOR_KEY, getKeyStroke("ctrl T"));
}
public void actionPerformed(ActionEvent evt) {
Document document = outputArea.getDocument();
try {
document.remove(0, document.getLength());
} catch(BadLocationException ble) {
}
}
}
/**
* An Action that catches any exception thrown in the doAction method.
*/
private abstract class CatchAction extends AbstractAction {
public CatchAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
try {
doAction();
} catch(Exception ex) { showExceptionDialog(ex); }
}
/** Subclasses implement this for their behaviour */
protected abstract void doAction();
}
private class ReturnAction extends CatchAction {
public ReturnAction() {
super("Return");
}
public void doAction() {
try {
int isbn = Integer.parseInt(retISBN.getText());
int cusID = Integer.parseInt(retCustID.getText());
appendOutput(model.returnBook(isbn, cusID));
} catch ( NumberFormatException nfe ) {
showMessageDialog(dialogParent, "The values entered for ISBN or customer ID do not have number format. Please try again.",
"Format Error", ERROR_MESSAGE);
}
}
}
private class BorrowAction extends CatchAction {
public BorrowAction() {
super("Borrow");
}
public void doAction() {
try {
int isbn = Integer.parseInt(borISBN.getText());
int cusID = Integer.parseInt(borCustID.getText());
int day = Integer.parseInt((String)borDay.getSelectedItem());
int year = Integer.parseInt((String)borYear.getSelectedItem());
int month = borMonth.getSelectedIndex();
appendOutput(model.borrowBook(isbn, cusID, day, month, year));
} catch ( NumberFormatException nfe ) {
showMessageDialog(dialogParent,
"The values entered for ISBN or customer ID do not have a numeric format. Please try again.",
"Format Error", ERROR_MESSAGE);
}
}
}
// Convenience method for the LookupAction constructor
private static boolean isVowel(char c) {
switch(Character.toLowerCase(c)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
return true;
default:
return false;
}
}
/**
* A base class for lookup-based actions: prompt for a number with
* various strings in the right places and call the doLookup method.
*/
private abstract class LookupAction extends CatchAction {
String title, itemDesc, a;
public LookupAction(String name, String itemDesc) {
this(name, itemDesc, isVowel(itemDesc.charAt(0)) ? "an" : "a");
}
public LookupAction(String name, String itemDesc, String a) {
super(name);
title = name;
this.itemDesc = itemDesc;
this.a = a;
}
protected void doAction() {
try {
Object in = showInputDialog(dialogParent,
"Enter " + a + " " + itemDesc,
title,
QUESTION_MESSAGE,
null, null, null);
if (in == null)
return;
int item = Integer.parseInt((String)in);
doLookup(item);
} catch ( NumberFormatException nfe ) {
String message =
"The " + itemDesc + " entered does not have a numeric " +
"format. Please try again.";
showMessageDialog(dialogParent, message,
"Format Error", ERROR_MESSAGE);
}
}
/** Subclasses implement this for their behaviour */
protected abstract void doLookup(int id);
}
private class ShowCustomerAction extends LookupAction {
public ShowCustomerAction() {
super("Show Customer", "customer ID");
}
protected void doLookup(int customerID) {
appendOutput(model.showCustomer(customerID));
}
}
private class ShowAuthorAction extends LookupAction {
public ShowAuthorAction() {
super("Show Author", "author ID");
}
protected void doLookup(int authorID) {
appendOutput(model.showAuthor(authorID));
}
}
private class BookLookupAction extends LookupAction {
public BookLookupAction() {
super("Book Lookup", "ISBN");
}
protected void doLookup(int isbn) {
appendOutput(model.bookLookup(isbn));
}
}
private class DeleteCustomerAction extends LookupAction {
public DeleteCustomerAction() {
super("Delete Customer", "customer ID");
}
protected void doLookup(int customerID) {
appendOutput(model.deleteCus(customerID));
}
}
private class DeleteAuthorAction extends LookupAction {
public DeleteAuthorAction() {
super("Delete Author", "author ID");
}
protected void doLookup(int authorID) {
appendOutput(model.deleteAuthor(authorID));
}
}
private class DeleteBookAction extends LookupAction {
public DeleteBookAction() {
super("Delete Book", "ISBN");
}
protected void doLookup(int isbn) {
appendOutput(model.deleteBook(isbn));
}
}
private class ShowAllCustomersAction extends CatchAction {
public ShowAllCustomersAction() {
super("Show All Customers");
}
protected void doAction() {
appendOutput(model.showAllCustomers());
}
}
private class ShowAllAuthorsAction extends CatchAction {
public ShowAllAuthorsAction() {
super("Show All Authors");
}
protected void doAction() {
appendOutput(model.showAllAuthors());
}
}
private class ShowCatalogueAction extends CatchAction {
public ShowCatalogueAction() {
super("Show Catalogue");
}
protected void doAction() {
appendOutput(model.showCatalogue());
}
}
private class ShowLoanedBooksAction extends CatchAction {
public ShowLoanedBooksAction() {
super("Show Loaned Books");
}
protected void doAction() {
appendOutput(model.showLoanedBooks());
}
}
private void appendOutput(String str) {
if (str != null && !str.equals(""))
outputArea.append(str + "\n\n");
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
private void showExceptionDialog(Exception e) {
showMessageDialog(this,
e.toString(),
"Error performing action",
ERROR_MESSAGE);
}
/** Exit the Application */
private void doExit() {
model.closeDBConnection();
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Build the UI on the Swing thread
EventQueue.invokeLater(new Runnable() {
public void run() {
new LibraryUI();
}
});
}
}
class AuthDialog extends JDialog {
private boolean okButtonClicked = false;
String introText = "Please enter your username and database password";
private JPanel dialogPanel = new JPanel();
private JPanel labelPanel = new JPanel();
private JPanel inputPanel = new JPanel();
private JTextField usernameTf = new JTextField(20);
private JPasswordField passwdTf = new JPasswordField(20);
public AuthDialog() {
this(null, "Authentication", false);
}
public AuthDialog(JFrame parent) {
this(parent, "Authentication", true);
}
public AuthDialog(JFrame parent, String title) {
this(parent, title, true);
}
public AuthDialog(final JFrame parent, String title, boolean modal) {
super(parent, title, modal);
// Set up close behaviour
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (!okButtonClicked)
System.exit(0);
}
});
// Set up OK button behaviour
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (getUserName().length() == 0) {
showMessageDialog(AuthDialog.this,
"Please enter a username",
"Format Error",
ERROR_MESSAGE);
return;
}
if (getDatabasePassword().length() == 0) {
showMessageDialog(AuthDialog.this,
"Please enter a password",
"Format Error",
ERROR_MESSAGE);
return;
}
okButtonClicked = true;
setVisible(false);
}
});
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Set up dialog contents
labelPanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,5));
inputPanel.setBorder(BorderFactory.createEmptyBorder(20,5,5,20));
labelPanel.setLayout(new GridLayout(2, 1));
labelPanel.add(new JLabel("User Name: "));
labelPanel.add(new JLabel("Password:"));
inputPanel.setLayout(new GridLayout(2, 1));
inputPanel.add(usernameTf);
inputPanel.add(passwdTf);
Box buttonPane = new Box(X_AXIS);
buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(okButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(cancelButton);
buttonPane.add(Box.createHorizontalGlue());
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
JLabel introLabel = new JLabel(introText);
introLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
getContentPane().add(introLabel, BorderLayout.NORTH);
getContentPane().add(labelPanel, BorderLayout.WEST);
getContentPane().add(inputPanel, BorderLayout.CENTER);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
// Ensure the enter key triggers the OK button
getRootPane().setDefaultButton(okButton);
// And that the escape key exits
InputMap inputMap =
getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = getRootPane().getActionMap();
inputMap.put(getKeyStroke("ESCAPE"), "exitAction");
actionMap.put("exitAction", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Pack it all
pack();
// Center on the screen
setLocationRelativeTo(null);
}
public String getUserName() {
return usernameTf.getText();
}
public String getDatabasePassword() {
return new String(passwdTf.getPassword());
}
}
/**
* A custom focus traversal policy that allows components to be set
* and knows about tabbed panes.
*/
class MapFocusTraversalPolicy extends FocusTraversalPolicy {
private FocusTraversalPolicy defaultPolicy;
private JTabbedPane tabbedPane;
private Map<Component,Component> before =
new HashMap<Component,Component>();
private Map<String,Component> tabBefore =
new HashMap<String,Component>();
private Map<Component,Component> after =
new HashMap<Component,Component>();
public MapFocusTraversalPolicy(FocusTraversalPolicy def,
JTabbedPane tab) {
defaultPolicy = def;
tabbedPane = tab;
}
public void putAfter(Component a, Component b) {
after.put(a, b);
}
public void putBefore(Component a, Component b) {
before.put(a, b);
}
public void putTabBefore(String a, Component b) {
tabBefore.put(a, b);
}
public Component getComponentAfter(Container cont,
Component comp) {
Component next = after.get(comp);
if (next != null)
return next;
return defaultPolicy.getComponentAfter(cont, comp);
}
public Component getComponentBefore(Container cont,
Component comp) {
if (comp == tabbedPane) {
String tabTitle =
tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
Component prev = tabBefore.get(tabTitle);
if (prev != null)
return prev;
}
Component prev = before.get(comp);
if (prev != null)
return prev;
return defaultPolicy.getComponentBefore(cont, comp);
}
public Component getDefaultComponent(Container root) {
return defaultPolicy.getDefaultComponent(root);
}
public Component getLastComponent(Container root) {
return defaultPolicy.getLastComponent(root);
}
public Component getFirstComponent(Container root) {
return defaultPolicy.getFirstComponent(root);
}
}