1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 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 General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.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: BooksComboBoxModel.java 2056 2010-12-12 04:34:41Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book;
23  
24  import java.io.IOException;
25  import java.io.ObjectInputStream;
26  import java.util.Comparator;
27  
28  import javax.swing.ComboBoxModel;
29  
30  import org.crosswire.jsword.book.Book;
31  import org.crosswire.jsword.book.BookFilter;
32  import org.crosswire.jsword.book.Defaults;
33  
34  /**
35   * The BibleModels class implements a number of swing DataModels and gives
36   * access to the list of current Bibles.
37   * 
38   * @see gnu.gpl.License for license details.<br>
39   *      The copyright to this program is held by it's authors.
40   * @author Joe Walker [joe at eireneh dot com]
41   */
42  public class BooksComboBoxModel extends BooksListModel implements ComboBoxModel {
43      /**
44       * Basic Constructor
45       */
46      public BooksComboBoxModel() {
47          this(null, null);
48      }
49  
50      /**
51       * Basic Constructor
52       */
53      public BooksComboBoxModel(BookFilter filter) {
54          this(filter, null);
55      }
56  
57      /**
58       * Basic Constructor
59       */
60      public BooksComboBoxModel(BookFilter filter, Comparator<Book> comparator) {
61          super(filter, comparator);
62  
63          postCacheData();
64  
65          if (getSize() > 0) {
66              // The default is to have the first selected
67              current = (Book) getElementAt(0);
68  
69              // If one of our favorite Books is in the list we ought to start
70              // with that selected. We put the Bible last so it will override
71              // the others if this is a broad list of books
72              tryInitialSelection(Defaults.getCommentary());
73              tryInitialSelection(Defaults.getDictionary());
74              tryInitialSelection(Defaults.getCurrentBible());
75          }
76      }
77  
78      /**
79       * @param book
80       */
81      private void tryInitialSelection(Book book) {
82          if (book != null) {
83              int i = getIndexOf(book);
84              if (i != -1) {
85                  current = book;
86              }
87          }
88      }
89  
90      /* (non-Javadoc)
91       * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
92       */
93      public void setSelectedItem(Object selected) {
94          this.current = (Book) selected;
95          Defaults.setCurrentBook(current);
96          fireContentsChanged(this, -1, -1);
97      }
98  
99      /* (non-Javadoc)
100      * @see javax.swing.ComboBoxModel#getSelectedItem()
101      */
102     public Object getSelectedItem() {
103         return current;
104     }
105 
106     /**
107      * Get the selected Bible
108      * 
109      * @return A Bible
110      */
111     public Book getSelectedBook() {
112         return current;
113     }
114 
115     /* (non-Javadoc)
116      * @see org.crosswire.bibledesktop.book.BooksListModel#cacheData()
117      */
118     protected final synchronized void postCacheData() {
119         // Find the previously selected item
120         boolean found = false;
121         int size = getSize();
122         for (int i = 0; i < size; i++) {
123             if (getElementAt(i) == current) {
124                 found = true;
125                 break;
126             }
127         }
128 
129         // If it was not found then either set to first element or null
130         if (!found) {
131             if (getSize() > 0) {
132                 current = (Book) getElementAt(0);
133             } else {
134                 current = null;
135             }
136         }
137     }
138 
139     /**
140      * Serialization support.
141      * 
142      * @param is
143      * @throws IOException
144      * @throws ClassNotFoundException
145      */
146     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
147         current = null;
148         is.defaultReadObject();
149     }
150 
151     /**
152      * The currently selected version
153      */
154     protected transient Book current;
155 
156     /**
157      * Serialization ID
158      */
159     private static final long serialVersionUID = 3906362740397388593L;
160 }
161