summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/ApplyChanges.java
blob: 445d89c524c86b05ce9e67a59e7ace9c0da434c7 (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
/*
 *      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.JTextArea;
import javax.swing.JTextField;

import com.joshwalters.bookcatalog.bookdatabase.BookDatabase;
import com.joshwalters.bookcatalog.bookdatabase.EmptyDatabase;
import com.joshwalters.bookcatalog.table.BookCatalogTableModel;

/**
 * Updates a book's entry in the database.
 * 
 * @author Josh Walters
 */
class ApplyChanges implements ActionListener {

	private JTable bookCatalogTable;
	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;

	/**
	 * Get the different objects needed for the class to function.
	 * 
	 * @param bookCatalogTable
	 * @param bookDatabase
	 * @param titleField
	 * @param authorField
	 * @param dateField
	 * @param descriptionArea
	 * @param ISBNField
	 * @param priceField
	 * @param publisherField
	 * @param subjectField
	 * @param notesArea
	 */
	ApplyChanges(JTable bookCatalogTable, BookDatabase bookDatabase,
			JTextField titleField, JTextField authorField,
			JTextField dateField, JTextArea descriptionArea,
			JTextField ISBNField, JTextField priceField,
			JTextField publisherField, JTextField subjectField,
			JTextArea notesArea) {
		this.bookCatalogTable = bookCatalogTable;
		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;
	}

	/**
	 * The user hit the "Apply Changes" button, update the book entry.
	 */
	public void actionPerformed(ActionEvent arg) {
		try {
			// Update the book entry.
			bookDatabase.updateBook(titleField.getText(),
					authorField.getText(), dateField.getText(), descriptionArea
							.getText(), ISBNField.getText(), priceField
							.getText(), publisherField.getText(), subjectField
							.getText(), notesArea.getText());
			// Get the table model.
			BookCatalogTableModel bookCatalogTableModel = (BookCatalogTableModel) bookCatalogTable
					.getModel();
			// Update the table's data.
			bookCatalogTableModel.setData(bookDatabase.getDatabaseForTable());
			// Refresh the table.
			bookCatalogTableModel.fireTableDataChanged();
		} catch (SQLException e) {
			// There was an SQL error.
			System.err.println(e.getLocalizedMessage());
		} catch (EmptyDatabase e) {
			// The database was empty.
			System.err.println(e.getLocalizedMessage());
		}
	}
}