-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookPanel.java
More file actions
422 lines (364 loc) · 11.2 KB
/
BookPanel.java
File metadata and controls
422 lines (364 loc) · 11.2 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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* The portion of the GUI used to interact with the books in the database
*
* @author Joshua
* @version 1.0
*/
public class BookPanel extends JPanel {
private static final long serialVersionUID = -6402970633467557805L;
private static JTextArea ta;
private JScrollPane sp;
private JPanel buttonPane;
private JPanel buff1, buff2, buff3;
private JPanel titlePane;
private JPanel buttons;
private JButton addBook;
private JButton delBook;
private JButton fndBook;
private JButton rfsBooks;
private JButton genReport;
private JButton savReport;
private JButton pntBooks;
private JButton exit;
private JLabel btnL;
private static JLabel title;
/**
* BookPanel Constructor
*/
public BookPanel()
{
buttons = new JPanel();
titlePane = new JPanel();
buttonPane = new JPanel();
buff1 = new JPanel();
buff2 = new JPanel();
buff3 = new JPanel();
setLayout(new BorderLayout());
ta = new JTextArea("Read more books! :)", 30, 50);
ta.setFont(new Font("Courier", Font.PLAIN, 14));
ta.setMargin(new Insets(20, 25, 20, 25));
ta.setEditable(false);
sp = new JScrollPane(ta);
sp.setPreferredSize(new Dimension(1000, 500));
sp.setMaximumSize(new Dimension(1000, 500));
title = new JLabel("Book Page Status: Welcome!");
title.setFont(new Font(getFont().getName(), Font.PLAIN, 36));
title.setAlignmentX(CENTER_ALIGNMENT);
titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.Y_AXIS));
titlePane.add(title);
titlePane.add(sp);
btnL = new JLabel("Actions");
btnL.setFont(new Font(getFont().getName(), Font.PLAIN, 24));
btnL.setAlignmentX(CENTER_ALIGNMENT);
addBook = new JButton("Add Book");
addBook.addActionListener(new AddListener());
addBook.setAlignmentX(CENTER_ALIGNMENT);
delBook = new JButton("Delete Book");
delBook.addActionListener(new DelListener());
delBook.setAlignmentX(CENTER_ALIGNMENT);
fndBook = new JButton("Find Book");
fndBook.addActionListener(new FndListener());
fndBook.setAlignmentX(CENTER_ALIGNMENT);
rfsBooks = new JButton("Refresh List");
rfsBooks.addActionListener(new RfsListener());
rfsBooks.setAlignmentX(CENTER_ALIGNMENT);
genReport = new JButton("Generate Report");
genReport.addActionListener(new GenListener());
genReport.setAlignmentX(CENTER_ALIGNMENT);
savReport = new JButton("Save");
savReport.addActionListener(new SavListener());
savReport.setAlignmentX(CENTER_ALIGNMENT);
pntBooks = new JButton("Print");
pntBooks.addActionListener(new PntListener());
pntBooks.setAlignmentX(CENTER_ALIGNMENT);
exit = new JButton("Quit");
exit.addActionListener(new ExtListener());
exit.setAlignmentX(CENTER_ALIGNMENT);
buttons.setLayout(new GridLayout(9, 1, 180, 10));
buttons.setMaximumSize(new Dimension(180, 300));
buttons.add(addBook);
buttons.add(delBook);
buttons.add(fndBook);
buttons.add(rfsBooks);
buttons.add(genReport);
buttons.add(savReport);
buttons.add(pntBooks);
buttons.add(exit);
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.Y_AXIS));
buttonPane.add(Box.createRigidArea(new Dimension(180, 50)));
buttonPane.add(btnL);
buttonPane.add(Box.createRigidArea(new Dimension(180, 25)));
buttonPane.add(buttons);
buttonPane.setPreferredSize(new Dimension(200, 600));
buff1.setPreferredSize(new Dimension(100, 100));
buff2.setPreferredSize(new Dimension(50, 50));
buff3.setPreferredSize(new Dimension(50, 50));
add(titlePane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.EAST);
add(buff1, BorderLayout.WEST);
add(buff2, BorderLayout.NORTH);
add(buff3, BorderLayout.SOUTH);
ta.setText(Data.getBookReport());
}
/**
* A private class that is triggered when the Add Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name;
String author;
String edition = null;
String er = "";
int trys = 0;
int confirm = JOptionPane.NO_OPTION;
// try a better validation if time allows
do
{
if(trys > 0)
er = "Try Again\n";
name = JOptionPane.showInputDialog(er + "Enter Book Name","Moby Dick");
trys++;
}while(!Utility.validateString(name) && name != null);
if(name != null)
{
er = "";
trys = 0;
do
{
if(trys > 0)
er = "Try Again\n";
author = JOptionPane.showInputDialog(er + "Enter Author Name", "J.R.R. Tolkien");
trys++;
}while(!Utility.validateString(author) && author != null);
if(author != null)
{
er = "";
trys = 0;
if(JOptionPane.showOptionDialog(null, "Does your book have an edition?", "Edition?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null) == 0)
{
do{
if(trys > 0)
er = "Try Again\n";
edition = JOptionPane.showInputDialog(er + "Enter Book Edition", "5,000th");
trys++;
if(edition == null)
confirm = JOptionPane.showConfirmDialog(null, "Does the book have an edition?", "Confirm Cancel", JOptionPane.YES_NO_OPTION);
}while(!Utility.validateString(edition) && confirm != JOptionPane.YES_OPTION);
}
confirm = JOptionPane.NO_OPTION;
confirm = JOptionPane.showConfirmDialog(null, "Add Book: " + name + " " + author + " " + edition);
if(confirm == JOptionPane.YES_OPTION){
if(Utility.validateString(edition)){
ta.setText(Data.add(new Book(0, name, author, edition, true)));
}
else{
ta.setText(Data.add(new Book(0, name, author, true)));
title.setText("Book Page Status: Add Complete");
}
}
}
}
}
}
/**
* A private class that is triggered when the Delete Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class DelListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String s = "";
String er ="";
int id = 0;
int trys = 0;
// try a better validation if time allows
do{
if(trys > 0)
er = "Try Again\n";
s = JOptionPane.showInputDialog( er + "Enter the Id of the book you wish deleted", "00");
trys++;
if(s != null)
try{
id = Integer.parseInt(s);
}catch(Exception ex){ id = 0; }
}while((!Utility.validateInt(id) && s != null) || id == 0 && Utility.validateString(s));
if(s != null){
if(Utility.validateBookDelete(id))
{
ta.setText(Data.delBook(id));
title.setText("Book Page Status: Deletion Complete");
}
else
{
title.setText("Book Page Status: Deletion Failed | Bad Id");
}
}
}
}
/**
* A private class that is triggered when the Find Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class FndListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String s = "";
int option;
int num;
option = JOptionPane.showOptionDialog(null, "What criteria do you wish to search by", "Search Criteria?",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"Author", "Title", "Id"}, null);
if(option == 2){
s = JOptionPane.showInputDialog("Enter book Id to search", "00");
try{
num = Integer.parseInt(s);
}catch(Exception ex){
num = -1;
}
if(num >= 0)
s = "" + num;
else
s = "";
}
else if(option == 1)
s = JOptionPane.showInputDialog("Enter book title to search", "Art of War");
else if(option == 0)
s = JOptionPane.showInputDialog("Enter author name to search", "George");
else
s = "";
if(Utility.validateString(s))
{
ta.setText(Data.findBooks(s, option));
title.setText("Book Page Status: Search Results for \"" + s + "\"");
}
else if(option != -1)
title.setText("Book Page Status: Search for \"" + s + "\" Failed");
}
}
/**
* A private class that is triggered when the Refresh Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class RfsListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
ButtonGroup bg = new ButtonGroup();
JRadioButton op1 = new JRadioButton("List all");
op1.setSelected(true);
JRadioButton op2 = new JRadioButton("List available");
JRadioButton op3 = new JRadioButton("List non-available");
bg.add(op1);
bg.add(op2);
bg.add(op3);
int option = 0;
// I need to find code to make a better layout
option = JOptionPane.showOptionDialog(null, "Sort by?", "Sort by?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, new Object[] {op1, op2, op3, "Id", "Title", "Author"}, null);
if(option > 0 && op1.isSelected())
ta.setText(Data.listBooks(option - 2, 1));
else if(option > 0 && op2.isSelected())
ta.setText(Data.listBooks(option - 2, 2));
else if(option > 0 && op3.isSelected())
ta.setText(Data.listBooks(option - 2, 3));
if(option < 0)
title.setText("Book Page Status: List Refreshed");
}
}
/**
* A private class that is triggered when the Generate Report Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class GenListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
ta.setText(Data.getBookReport());
title.setText("Book Page Status: Report Generated");
}
}
/**
* A private class that is triggered when the Save Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class SavListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(Data.toFile("book", ta.getText()))
title.setText("Book Page Status: File Saved");
else
title.setText("Book Page Status: File Save Failed");
}
}
/**
* A private class that is triggered when the Print Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class PntListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
ta.setFont(new Font("Courier", Font.PLAIN, 14));
try
{
if(ta.print())
title.setText("Book Page Status: Print Successful");
}
catch(PrinterException pe)
{
title.setText("Book Page Status: Print Failed");
}
}
}
/**
* A private class that is triggered when the Exit Button on the BookPanel is pressed.
*
* @author Joshua
* @version 1.0
*/
private class ExtListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
/**
* A static method used to refresh the BookPanel from the other panels in the GUI.
*/
public static void refresh() {
ta.setText(Data.getBookReport());
title.setText("Book Page Status: Welcome!");
}
}