Source code: com/eireneh/bible/passage/swing/BookTreeNode.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 * PassageTableModel.
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 BookTreeNode extends BibleTreeNode
35 {
36 /**
37 * This constructor is for when we are really a BookTreeNode
38 */
39 protected BookTreeNode(TreeNode parent, int book) throws NoSuchVerseException
40 {
41 this.parent = parent;
42 this.book = book;
43
44 kids = new ChapterTreeNode[Books.chaptersInBook(book)];
45 }
46
47 /**
48 * This constructor is for when we are really a BookTreeNode
49 */
50 public void setPassage(Passage ref, boolean filter)
51 {
52 this.ref = ref;
53
54 if (filter)
55 {
56 try
57 {
58 kids = new ChapterTreeNode[ref.chaptersInPassage(book)];
59
60 int current_Passage = 0;
61 int Passage_count = 0;
62
63 Enumeration en = ref.verseElements();
64 while (en.hasMoreElements())
65 {
66 Verse verse = (Verse) en.nextElement();
67
68 if ((book == 0 || verse.getBook() == book)
69 && current_Passage != verse.getChapter())
70 {
71 current_Passage = verse.getChapter();
72
73 ChapterTreeNode node = new ChapterTreeNode(this, book, current_Passage);
74 node.setPassage(ref, true);
75 kids[Passage_count++] = node;
76 }
77 }
78 }
79 catch (NoSuchVerseException ex)
80 {
81 throw new LogicError(ex);
82 }
83 }
84 }
85
86 /**
87 * Returns the child <code>TreeNode</code> at index i
88 */
89 public TreeNode getChildAt(int i)
90 {
91 try
92 {
93 if (kids[i] != null) return kids[i];
94
95 ChapterTreeNode node = new ChapterTreeNode(this, book, i+1);
96 node.setPassage(ref, false);
97 kids[i] = node;
98
99 return kids[i];
100 }
101 catch (NoSuchVerseException ex)
102 {
103 throw new LogicError(ex);
104 }
105 }
106
107 /**
108 * Returns the parent <code>TreeNode</code> of the receiver.
109 */
110 public TreeNode getParent()
111 {
112 return parent;
113 }
114
115 /**
116 * Returns the index of <code>node</code> in the receivers children.
117 * If the receiver does not contain <code>node</code>, -1 will be
118 * returned.
119 */
120 public int getIndex(TreeNode node)
121 {
122 if (!(node instanceof ChapterTreeNode))
123 return -1;
124
125 ChapterTreeNode chap = (ChapterTreeNode) node;
126 return chap.getChapter();
127 }
128
129 /**
130 * How we appear in the Tree
131 */
132 public String toString()
133 {
134 try
135 {
136 String book_name = Books.getLongBookName(book);
137 if (ref == null) return book_name;
138
139 int chapters = ref.chaptersInPassage(book);
140 if (chapters == 0) return book_name;
141
142 return book_name + " (" + chapters + ")";
143 }
144 catch (NoSuchVerseException ex)
145 {
146 throw new LogicError(ex);
147 }
148 }
149
150 /**
151 * The current book number (Genesis=1)
152 */
153 public int getBook()
154 {
155 return book;
156 }
157
158 /** The Book that this node referrs to */
159 protected int book = 0;
160
161 /** The base of this tree */
162 protected TreeNode parent = null;
163 }
164