1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: AbstractBookList.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.jsword.book.basic;
23  
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.crosswire.common.util.CollectionUtil;
28  import org.crosswire.common.util.EventListenerList;
29  import org.crosswire.jsword.book.Book;
30  import org.crosswire.jsword.book.BookFilter;
31  import org.crosswire.jsword.book.BookFilterIterator;
32  import org.crosswire.jsword.book.BookList;
33  import org.crosswire.jsword.book.BooksEvent;
34  import org.crosswire.jsword.book.BooksListener;
35  
36  /**
37   * A basic implementation of BookList. The methods in this abstract class are
38   * duplicates of those in Books, so bugs fixed in one should be fixed in the
39   * other too.
40   * 
41   * @see gnu.lgpl.License for license details.<br>
42   *      The copyright to this program is held by it's authors.
43   * @author Joe Walker [joe at eireneh dot com]
44   */
45  public abstract class AbstractBookList implements BookList {
46      /*
47       * (non-Javadoc)
48       * 
49       * @see
50       * org.crosswire.jsword.book.BookList#getBooks(org.crosswire.jsword.book
51       * .BookFilter)
52       */
53      public List<Book> getBooks(BookFilter filter) {
54          List<Book> temp = CollectionUtil.createList(new BookFilterIterator(getBooks(), filter));
55          return Collections.unmodifiableList(temp);
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see
62       * org.crosswire.jsword.book.BookList#addBooksListener(org.crosswire.jsword
63       * .book.BooksListener)
64       */
65      public synchronized void addBooksListener(BooksListener li) {
66          listeners.add(BooksListener.class, li);
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see
73       * org.crosswire.jsword.book.BookList#removeBooksListener(org.crosswire.
74       * jsword.book.BooksListener)
75       */
76      public synchronized void removeBooksListener(BooksListener li) {
77          listeners.remove(BooksListener.class, li);
78      }
79  
80      /**
81       * Kick of an event sequence
82       * 
83       * @param source
84       *            The event source
85       * @param book
86       *            The changed Book
87       * @param added
88       *            Is it added?
89       */
90      protected static synchronized void fireBooksChanged(Object source, Book book, boolean added) {
91          // Guaranteed to return a non-null array
92          Object[] contents = listeners.getListenerList();
93  
94          // Process the listeners last to first, notifying
95          // those that are interested in this event
96          BooksEvent ev = null;
97          for (int i = contents.length - 2; i >= 0; i -= 2) {
98              if (contents[i] == BooksListener.class) {
99                  if (ev == null) {
100                     ev = new BooksEvent(source, book, added);
101                 }
102 
103                 if (added) {
104                     ((BooksListener) contents[i + 1]).bookAdded(ev);
105                 } else {
106                     ((BooksListener) contents[i + 1]).bookRemoved(ev);
107                 }
108             }
109         }
110     }
111 
112     /**
113      * The list of listeners
114      */
115     private static EventListenerList listeners = new EventListenerList();
116 }
117