/* * 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 . */ 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]; } }