001 /* PAVLOV -- Multiple Choice Study System 002 * Copyright (C) 2000 - 2004 T.J. Willis 003 * 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License 006 * as published by the Free Software Foundation; either version 2 007 * of the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. 017 * 018 * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/library/Library.java,v 1.6 2004/07/01 05:50:20 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.library; 021 import java.io.*; 022 import java.util.*; 023 import javax.swing.tree.DefaultMutableTreeNode; 024 import org.apache.log4j.*; 025 026 //import net.sourceforge.pavlov.event.BookAndChapterEvent; 027 028 /** 029 * Describes a Library, which is a collection of Books. 030 * 031 * @author <a href="mailto:tj_willis@users.sourceforge.net"></a> 032 * @version 1.0 033 * @see Book 034 * @see Chapter 035 * @see Question 036 * @has 1 Has 1..n net.sourceforge.pavlov.library.AbstractBook 037 * @has 1 Has 1 java.io.File 038 * @has 1 Has 1 net.sourceforge.pavlov.library.LibraryLoader 039 */ 040 public final class Library implements AbstractLibrary { 041 /** Collection of Book objects. */ 042 private Hashtable<String, AbstractBook> books; 043 private File directory; 044 045 /** Creates an empty library.*/ 046 public Library() { 047 books = new Hashtable<String,AbstractBook>(); 048 } 049 050 /** 051 * Describe <code>setDirectory</code> method here. 052 * 053 * @param dir a <code>File</code> value 054 */ 055 public void setDirectory(File dir) 056 { 057 directory = dir; 058 } 059 060 /** 061 * Describe <code>getDirectory</code> method here. 062 * 063 * @return a <code>File</code> value 064 */ 065 public File getDirectory() 066 { 067 return directory; 068 } 069 070 /** 071 * Returns all books as a Collection. 072 * @return a <code>Collection</code> value 073 * @deprecated Use getBooks() 074 */ 075 @Deprecated public Collection<AbstractBook> getBookCollection() { 076 return getBooks(); 077 } 078 079 /** 080 * Returns all books as a read-only Collection. 081 * @return a <code>Collection</code> value 082 */ 083 public Collection<AbstractBook> getBooksReadOnly(){ 084 return Collections.unmodifiableCollection(getBooks()); 085 } 086 087 /** 088 * Returns all books as a Collection. 089 * @return a <code>Collection</code> value 090 */ 091 public Collection<AbstractBook> getBooks() { 092 final boolean mode = true; 093 if(mode){ 094 return new TreeSet<AbstractBook>(books.values()); 095 } else { 096 return books.values(); 097 } 098 } 099 100 /** 101 * Adds the book to the library , keyed on Book.getName(). 102 * @param c a <code>Book</code> value 103 */ 104 public void addBook(final Book c) { 105 assert books!=null: "Books is Null"; 106 assert c!=null: "Adding null book"; 107 books.put(c.getName(), c); 108 } 109 110 111 /** 112 * Adds the book to the library , keyed on Book.getName(). 113 * @param c an <code>AbstractBook</code> value 114 */ 115 public void addBook(final AbstractBook c) { 116 assert books!=null: "Books is Null"; 117 assert c!=null: "Adding null book"; 118 books.put(c.getName(), c); 119 } 120 121 122 /** 123 * Asks each book to save itself in its cannonical filename in my directory. 124 * 125 */ 126 public void save() 127 throws IOException 128 { 129 Collection<AbstractBook> cv = books.values(); 130 for(AbstractBook da : cv) { 131 da.save(directory); 132 } 133 } 134 135 136 /** 137 * Saves the given book in its cannonical filename in my directory. 138 * 139 * @param b a <code>Book</code> value 140 */ 141 public void saveBook(final Book b) 142 throws IOException 143 { 144 b.save(directory); 145 } 146 147 /** 148 * Displays books and chapters as a tree. 149 * @param top a <code>DefaultMutableTreeNode</code> value 150 */ 151 public void populateTree(DefaultMutableTreeNode top) { 152 DefaultMutableTreeNode bookNode = null; 153 154 Collection<AbstractBook> cv = books.values(); 155 //String x; 156 for( AbstractBook book : cv) { 157 bookNode = new DefaultMutableTreeNode(book); 158 top.add(bookNode); 159 book.populateTree(bookNode); 160 } 161 } 162 163 /** 164 * Returns the named Chapter from the named book. Null if either doesn't exist. 165 * @param bkName a <code>String</code> value 166 * @param cpName a <code>String</code> value 167 * @return a <code>Chapter</code> value 168 */ 169 public Chapter getChapter(final String bkName, final String cpName) { 170 AbstractBook b = (AbstractBook) books.get(bkName); 171 if (b == null) return null; 172 return b.getChapter(cpName); 173 } 174 175 /** 176 * Returns the named book. 177 * 178 * @param bkName a <code>String</code> value 179 * @return a <code>Book</code> value 180 */ 181 public AbstractBook getBook(final String bkName) 182 { 183 return books.get(bkName); 184 } 185 186 /** 187 * Removes the named book from this library. 188 * 189 * @param bkName a <code>String</code> value 190 */ 191 public void deleteBook(final String bkName) 192 { 193 if(bkName==null) return; 194 Object o = books.remove(bkName); 195 } 196 197 } 198