Source code: com/eireneh/bible/passage/swing/BibleTreeNode.java
1
2 package com.eireneh.bible.passage.swing;
3
4 import java.util.Enumeration;
5 import javax.swing.tree.*;
6 import javax.swing.event.*;
7
8 import com.eireneh.util.*;
9 import com.eireneh.bible.passage.*;
10
11 /**
12 * BibleTreeNode.
13 *
14 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
15 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
16 * Distribution Licence:<br />
17 * Project B is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General Public License,
19 * version 2 as published by the Free Software Foundation.<br />
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.<br />
24 * The License is available on the internet
25 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
26 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
28 * The copyright to this program is held by it's authors.
29 * </font></td></tr></table>
30 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
31 * @see docs.Licence
32 * @author Joe Walker
33 */
34 public class BibleTreeNode implements TreeNode
35 {
36 /**
37 *
38 */
39 public BibleTreeNode()
40 {
41 kids = new BookTreeNode[Books.booksInBible()];
42 }
43
44 /**
45 *
46 */
47 public void setPassage(Passage ref, boolean filter)
48 {
49 this.ref = ref;
50
51 try
52 {
53 if (filter)
54 {
55 kids = new BookTreeNode[ref.booksInPassage()];
56
57 int current_book = 0;
58 int book_count = 0;
59
60 Enumeration en = ref.verseElements();
61 while (en.hasMoreElements())
62 {
63 Verse verse = (Verse) en.nextElement();
64 if (current_book != verse.getBook())
65 {
66 current_book = verse.getBook();
67 BookTreeNode node = new BookTreeNode(this, current_book);
68 node.setPassage(ref, true);
69 kids[book_count++] = node;
70 }
71 }
72 }
73 }
74 catch (NoSuchVerseException ex)
75 {
76 throw new LogicError(ex);
77 }
78 }
79
80 /**
81 * Returns the child <code>TreeNode</code> at index i
82 */
83 public TreeNode getChildAt(int i)
84 {
85 try
86 {
87 if (kids[i] != null) return kids[i];
88
89 BookTreeNode node = new BookTreeNode(this, i+1);
90 node.setPassage(ref, false);
91 kids[i] = node;
92
93 return kids[i];
94 }
95 catch (NoSuchVerseException ex)
96 {
97 throw new LogicError(ex);
98 }
99 }
100
101 /**
102 * Returns the number of children <code>TreeNode</code>s the receiver
103 * contains.
104 */
105 public int getChildCount()
106 {
107 return kids.length;
108 }
109
110 /**
111 * Returns the parent <code>TreeNode</code> of the receiver.
112 */
113 public TreeNode getParent()
114 {
115 return this;
116 }
117
118 /**
119 * Returns the index of <code>node</code> in the receivers children.
120 * If the receiver does not contain <code>node</code>, -1 will be
121 * returned.
122 */
123 public int getIndex(TreeNode node)
124 {
125 if (!(node instanceof BookTreeNode))
126 return -1;
127
128 BookTreeNode book = (BookTreeNode) node;
129 return book.getBook();
130 }
131
132 /**
133 * Returns true if the receiver allows children.
134 */
135 public boolean getAllowsChildren()
136 {
137 return true;
138 }
139
140 /**
141 * Returns true if the receiver is a leaf.
142 */
143 public boolean isLeaf()
144 {
145 return false;
146 }
147
148 /**
149 * Returns the children of the reciever as an Enumeration.
150 */
151 public Enumeration children()
152 {
153 return new NodeEnumeration();
154 }
155
156 /**
157 * Returns the children of the reciever as an Enumeration.
158 */
159 public String toString()
160 {
161 if (ref == null) return "The Whole Bible";
162
163 return "Search ("+ref.getOverview()+")";
164 }
165
166 /**
167 * The Enumerate over an array
168 */
169 public class NodeEnumeration implements Enumeration
170 {
171 public boolean hasMoreElements()
172 {
173 return (index < kids.length);
174 }
175
176 public Object nextElement()
177 {
178 return kids[index++];
179 }
180
181 int index = 0;
182 }
183
184 /** If we are only displaying some of the verses */
185 protected Passage ref = null;
186
187 /** The ChapterTreeNodes that we have created */
188 protected TreeNode[] kids = null;
189 }
190