Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/book/swing/BiblesListModel.java


1   
2   package com.eireneh.bible.book.swing;
3   
4   import java.io.*;
5   import java.awt.*;
6   
7   import javax.swing.*;
8   import javax.swing.event.*;
9   import javax.swing.border.*;
10  
11  import com.eireneh.util.*;
12  import com.eireneh.bible.book.*;
13  
14  /**
15   * BiblesListModel creates a Swing ListModel from the available Bibles.
16   * I would normally implement BiblesListener in an inner class however
17   * doing that would stop me calling fireInterval*() in AbstractListModel
18   * because that is a protected method and the inner class is neither
19   * in the same package or a sub class.
20   *
21   * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
22   * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
23   * Distribution Licence:<br />
24   * Project B is free software; you can redistribute it
25   * and/or modify it under the terms of the GNU General Public License,
26   * version 2 as published by the Free Software Foundation.<br />
27   * This program is distributed in the hope that it will be useful,
28   * but WITHOUT ANY WARRANTY; without even the implied warranty of
29   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30   * General Public License for more details.<br />
31   * The License is available on the internet
32   * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
33   * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34   * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
35   * The copyright to this program is held by it's authors.
36   * </font></td></tr></table>
37   * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
38   * @see docs.Licence
39   * @author Joe Walker
40   * @version D0.I0.T0
41   */
42  public class BiblesListModel extends AbstractListModel implements BiblesListener
43  {
44      /**
45       * Basic constructor
46       */
47      public BiblesListModel()
48      {
49          cacheData();
50      }
51  
52      /**
53       * Setup the data-stores of the current Bibles and drivers
54       */
55      private void cacheData()
56      {
57          try
58          {
59              bibles = Bibles.getBibleNames();
60              drivers = new String[bibles.length];
61  
62              for (int i=0; i<drivers.length; i++)
63              {
64                  drivers[i] = BibleDriverManager.getDriverForBible(bibles[i]).getDriverName();
65              }
66          }
67          catch (BookException ex)
68          {
69              Reporter.informUser(this, ex);
70              bibles = new String[0];
71              drivers = new String[0];
72          }
73      }
74  
75      /**
76       * Returns the length of the list.
77       */
78      public int getSize()
79      {
80          return bibles.length;
81      }
82  
83      /**
84       * Returns the value at the specified index.
85       */
86      public Object getElementAt(int index)
87      {
88          if (index >= bibles.length)
89              return null;
90  
91          return bibles[index] + " (" + drivers[index] + ")";
92      }
93  
94      /**
95       * Returns the index-position of the specified object in the list.
96       * @param test the object to find
97       * @return an int representing the index position, where 0 is the first position
98       */
99      public int getIndexOf(Object test)
100     {
101         for (int i=0; i<bibles.length; i++)
102         {
103             if (test == bibles[i])
104                 return i;
105         }
106 
107         return -1;
108     }
109 
110     /**
111      * Given an item, work out the name of the Bible that it represents
112      * @param The item from the list
113      * @return A Bible name
114      */
115     public String getBibleName(Object test)
116     {
117         String item = test.toString();
118         int end = item.indexOf(" (");
119         return item.substring(0, end);
120     }
121 
122     /**
123      * Given an item, work out the name of the Driver that it represents
124      * @param The item from the list
125      * @return A Driver
126      */
127     public String getDriverName(Object test)
128     {
129         String item = test.toString();
130         int end = item.indexOf(" (");
131         return item.substring(end+2, item.length()-1);
132     }
133 
134     /**
135      * Add a listener to the list that's notified each time a change
136      * to the data model occurs.
137      * @param li the ListDataListener
138      */
139     public void addListDataListener(ListDataListener li)
140     {
141         if (listenerList.getListenerCount() == 0)
142             Bibles.addBiblesListener(this);
143 
144         super.addListDataListener(li);
145     }
146 
147     /**
148      * Remove a listener from the list that's notified each time a
149      * change to the data model occurs.
150      * @param li the ListDataListener
151      */
152     public void removeListDataListener(ListDataListener li)
153     {
154         super.removeListDataListener(li);
155 
156         if (listenerList.getListenerCount() == 0)
157             Bibles.removeBiblesListener(this);
158     }
159 
160     /**
161      * Called whenever a new Bible is added or a Bible is removed from
162      * the system.
163      * @param ev A description of the change
164      */
165     public void biblesChanged(BiblesEvent ev)
166     {
167         int old_size = getSize();
168 
169         cacheData();
170 
171         if (ev.isAddition())
172             fireIntervalAdded(ev.getSource(), 0, old_size);
173         else
174             fireIntervalRemoved(ev.getSource(), 0, old_size);
175     }
176 
177     /** The array of versions */
178     protected String[] bibles;
179 
180     /** The array of driver names */
181     protected String[] drivers;
182 }