Source code: com/eireneh/bible/passage/swing/PassageTreeNode.java
1
2 package com.eireneh.bible.passage.swing;
3
4 import java.util.Enumeration;
5 import javax.swing.*;
6 import javax.swing.tree.*;
7 import javax.swing.event.*;
8
9 import com.eireneh.bible.passage.*;
10
11 /**
12 * A PassageTreeNode extends TreeNode to Model a Passage.
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 * @version D5.I0.T0
34 */
35 public class PassageTreeNode implements TreeNode, PassageListener
36 {
37 /**
38 *
39 */
40 public PassageTreeNode(Passage ref, JTree tree)
41 {
42 this.ref = ref;
43 this.tree = tree;
44 ref.addPassageListener(this);
45 }
46
47 /**
48 * Returns the child <code>TreeNode</code> at index i
49 */
50 public TreeNode getChildAt(int index)
51 {
52 return new VerseRangeTreeNode(ref.getVerseRangeAt(index));
53 }
54
55 /**
56 * Returns the number of children <code>TreeNode</code>s the receiver
57 * contains.
58 */
59 public int getChildCount()
60 {
61 return ref.countRanges();
62 }
63
64 /**
65 * Returns the parent <code>TreeNode</code> of the receiver.
66 */
67 public TreeNode getParent()
68 {
69 return this;
70 }
71
72 /**
73 * Returns the index of <code>node</code> in the receivers children.
74 * If the receiver does not contain <code>node</code>, -1 will be
75 * returned.
76 */
77 public int getIndex(TreeNode node)
78 {
79 int count = 0;
80 Enumeration en = ref.rangeElements();
81
82 while (en.hasMoreElements())
83 {
84 if (en.nextElement() == node)
85 return count;
86
87 count++;
88 }
89
90 return -1;
91 }
92
93 /**
94 * Returns true if the receiver allows children.
95 */
96 public boolean getAllowsChildren()
97 {
98 return true;
99 }
100
101 /**
102 * Returns true if the receiver is a leaf.
103 */
104 public boolean isLeaf()
105 {
106 return false;
107 }
108
109 /**
110 * Sent after stuff has been added to the Passage.
111 * More info about what and where can be had from the Event
112 * @param ev a PassageEvent encapuslating the event information
113 */
114 public void versesAdded(PassageEvent ev)
115 {
116 }
117
118 /**
119 * Sent after stuff has been removed from the Passage.
120 * More info about what and where can be had from the Event
121 * @param ev a PassageEvent encapuslating the event information
122 */
123 public void versesRemoved(PassageEvent ev)
124 {
125 }
126
127 /**
128 * Sent after verses have been symultaneously added and removed from the Passage.
129 * More info about what and where can be had from the Event
130 * @param ev a PassageEvent encapuslating the event information
131 */
132 public void versesChanged(PassageEvent ev)
133 {
134 DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
135 model.nodeStructureChanged(this);
136 }
137
138 /**
139 * Returns the children of the reciever as an Enumeration.
140 */
141 public Enumeration children()
142 {
143 return ref.rangeElements();
144 }
145
146 /**
147 * Returns the children of the reciever as an Enumeration.
148 */
149 public String toString()
150 {
151 return ref.getOverview();
152 }
153
154 /** The Passage to be displayed */
155 protected Passage ref = null;
156
157 /** The Passage to be displayed */
158 protected JTree tree = null;
159 }
160