summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/DeleteBook.java
blob: 7f0bffcf09fb35c90ddfe91f258836920ef982f1 (plain)
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
/*
 *      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.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.joshwalters.bookcatalog.bookdatabase.BookDatabase;

/**
 * Deletes a book from the database.
 * 
 * @author Josh Walters
 */
public class DeleteBook implements ActionListener {

	private BookDatabase bookDatabase;
	private JTextField titleField;
	private JTextField authorField;
	private JTextField dateField;
	private JTextArea descriptionArea;
	private JTextField ISBNField;
	private JTextField priceField;
	private JTextField publisherField;
	private JTextField subjectField;
	private JTextArea notesArea;
	private SearchDatabase searchDatabase;

	/**
	 * Get the different objects needed for the class to function.
	 * 
	 * @param bookDatabase
	 * @param titleField
	 * @param authorField
	 * @param dateField
	 * @param descriptionArea
	 * @param ISBNField
	 * @param priceField
	 * @param publisherField
	 * @param subjectField
	 * @param notesArea
	 * @param searchDatabase
	 */
	public DeleteBook(BookDatabase bookDatabase, JTextField titleField,
			JTextField authorField, JTextField dateField,
			JTextArea descriptionArea, JTextField ISBNField,
			JTextField priceField, JTextField publisherField,
			JTextField subjectField, JTextArea notesArea,
			SearchDatabase searchDatabase) {
		this.bookDatabase = bookDatabase;
		this.titleField = titleField;
		this.authorField = authorField;
		this.dateField = dateField;
		this.descriptionArea = descriptionArea;
		this.ISBNField = ISBNField;
		this.priceField = priceField;
		this.publisherField = publisherField;
		this.subjectField = subjectField;
		this.notesArea = notesArea;
		this.searchDatabase = searchDatabase;
	}

	/**
	 * The user clicked on the "Delete Book" button. Remove the book from the
	 * database.
	 */
	public void actionPerformed(ActionEvent arg) {
		try {
			// Check if the user really wants to delete the book.
			int result = JOptionPane.showConfirmDialog(null,
					"Do you really want to delete \"" + titleField.getText()
							+ "\" from your catalog?", "Warning",
					JOptionPane.YES_NO_OPTION);
			if (result == JOptionPane.YES_OPTION) {
				// Delete the book using its ISBN number.
				bookDatabase.deleteBook(ISBNField.getText());
				// Update the table.
				searchDatabase.updateTableWithSearchString();
				// Clear all the text fields/areas.
				titleField.setText("");
				authorField.setText("");
				dateField.setText("");
				descriptionArea.setText("");
				ISBNField.setText("");
				priceField.setText("");
				publisherField.setText("");
				subjectField.setText("");
				notesArea.setText("");
			}
		} catch (SQLException e) {
			// There was an SQL exception.
			System.err.println(e.getLocalizedMessage());
		}
	}
}