summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/table/BookCatalogTableModel.java
blob: 8fe69ba100f6a00b3ada422b52de0662ff96a354 (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
/*
 *      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.table;

import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;

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

/**
 * The custom table model for the table that will display the book database.
 * 
 * @author Josh Walters
 */
public class BookCatalogTableModel extends AbstractTableModel {

	/** The serial ID */
	private static final long serialVersionUID = -3321121530039301264L;
	/** The column names. */
	private String[] columnNames = { "Title", "Author", "Date", "Description",
			"ISBN", "Price", "Publisher", "Subject", "Notes" };
	/** Stores the data that is displayed in the table */
	private String[][] data;
	/** The SQL database that stores the book collection records. */
	BookDatabase bookDatabase;

	/**
	 * Populates the table with the info in the book database.
	 * 
	 * @param bookDatabase
	 */
	public BookCatalogTableModel(BookDatabase bookDatabase) {
		this.bookDatabase = bookDatabase;
		try {
			data = bookDatabase.getDatabaseForTable();
		} catch (SQLException e) {
			// SQL error.
			System.err.println(e.getLocalizedMessage());
		} catch (EmptyDatabase e) {
			// Database was empty.
			System.err.println(e.getLocalizedMessage());
		}
	}

	/**
	 * Sets the data to be displayed.
	 * 
	 * @param data
	 */
	public void setData(String[][] data) {
		this.data = data;
	}

	/**
	 * Return the number of columns.
	 */
	public int getColumnCount() {
		return columnNames.length;
	}

	/**
	 * Get the number of rows.
	 */
	public int getRowCount() {
		return data.length;
	}

	/**
	 * Get the name of a column.
	 */
	public String getColumnName(int col) {
		return columnNames[col];
	}

	/**
	 * Get the value at a column.
	 */
	public String getValueAt(int row, int col) {
		return data[row][col];
	}
}