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
|
/*
* Book Catalog - Catalog your book collection.
*
* Copyright (C) 2009 Joshua Walters
* URL: http://joshwalters.com
*
* This file is part of Book Catalog.
*
* Book Catalog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Book Catalog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Book Catalog. If not, see <http://www.gnu.org/licenses/>.
*/
package com.joshwalters.bookcatalog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.joshwalters.bookcatalog.bookdatabase.BookAlreadyInDatabase;
import com.joshwalters.bookcatalog.bookdatabase.BookDatabase;
import com.joshwalters.bookcatalog.bookdatabase.EmptyDatabase;
import com.joshwalters.bookcatalog.isbnlookup.ISBNLookup;
import com.joshwalters.bookcatalog.isbnlookup.InvalidSearchTerms;
import com.joshwalters.bookcatalog.isbnlookup.NoBookFound;
import com.joshwalters.bookcatalog.sound.PlayWavSoundFile;
import com.joshwalters.bookcatalog.table.BookCatalogTableModel;
/**
* Adds a book to the book database using an ISBN number.
*
* @author Josh Walters
*/
class AddBook implements ActionListener {
private ISBNLookup bookLookup = null;
private BookDatabase bookDatabase = null;
private JTable bookCatalogTable = null;
/**
* Get the different objects needed for the class to function.
*
* @param bookDatabase
* @param bookLookup
* @param bookCatalogTable
*/
public AddBook(BookDatabase bookDatabase, ISBNLookup bookLookup,
JTable bookCatalogTable) {
this.bookDatabase = bookDatabase;
this.bookLookup = bookLookup;
this.bookCatalogTable = bookCatalogTable;
}
/**
* Add the book to the database using its ISBN number.
*/
public void actionPerformed(ActionEvent arg) {
try {
String barcode = null;
JTextField addBookField = (JTextField) arg.getSource();
barcode = addBookField.getText();
addBookField.setText("");
// Search for the book using its barcode.
bookLookup.searchBooks(barcode);
// Insert the book into the database.
bookDatabase.insertBook(bookLookup.getTitle(), bookLookup
.getAuthor(), bookLookup.getDate(), bookLookup
.getDescription(), bookLookup.getISBN(), bookLookup
.getPrice(), bookLookup.getPublisher(), bookLookup
.getSubject());
// Play the beep sound to alert the user that the book was added to
// the database.
new PlayWavSoundFile("data/sounds/beep.wav").start();
try {
// Get the table model of the table.
BookCatalogTableModel bookCatalogTableModel = (BookCatalogTableModel) bookCatalogTable
.getModel();
// Update the data in the table with the new book.
bookCatalogTableModel.setData(bookDatabase
.getDatabaseForTable());
// Refresh the table to draw the new info.
bookCatalogTableModel.fireTableDataChanged();
} catch (EmptyDatabase e) {
// The database was empty, print an error message.
System.err.println(e.getLocalizedMessage());
}
// The following code selects the newly added row.
int row = bookCatalogTable.getRowCount() - 1;
int col = 4;
// Search for the row, and then select it.
for (int i = 0; i <= row; i++) {
String value = (String) bookCatalogTable.getValueAt(i, col);
if (value.equalsIgnoreCase(bookLookup.getISBN())) {
bookCatalogTable.setRowSelectionInterval(i, i);
}
}
} catch (SQLException e) {
// There was an SQL error of some sort.
System.err.println(e.getLocalizedMessage());
} catch (NoBookFound e) {
// The book data was not found online, play the error sound.
new PlayWavSoundFile("data/sounds/error.wav").start();
} catch (BookAlreadyInDatabase e) {
// The book was already in the error database, play the error sound.
new PlayWavSoundFile("data/sounds/beep.wav").start();
} catch (InvalidSearchTerms e) {
// Invalid search terms, play the error sound.
new PlayWavSoundFile("data/sounds/error.wav").start();
}
}
}
|