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: KeyTreeNode.java 2056 2010-12-12 04:34:41Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.passage;
23  
24  import java.util.Enumeration;
25  
26  import javax.swing.tree.TreeNode;
27  
28  import org.crosswire.common.util.EmptyIterator;
29  import org.crosswire.common.util.IteratorEnumeration;
30  import org.crosswire.jsword.passage.Key;
31  
32  /**
33   * An implementation of TreeNode that reads from Keys and KeyLists.
34   * 
35   * @see gnu.gpl.License for license details.<br>
36   *      The copyright to this program is held by it's authors.
37   * @author Joe Walker [joe at eireneh dot com]
38   */
39  public class KeyTreeNode implements TreeNode {
40      /**
41       * Simple ctor
42       */
43      public KeyTreeNode(Key key, TreeNode parent) {
44          this.key = key;
45          this.parent = parent;
46      }
47  
48      /* (non-Javadoc)
49       * @see javax.swing.tree.TreeNode#getChildCount()
50       */
51      public int getChildCount() {
52          return key == null ? 0 : key.getChildCount();
53      }
54  
55      /* (non-Javadoc)
56       * @see javax.swing.tree.TreeNode#getAllowsChildren()
57       */
58      public boolean getAllowsChildren() {
59          return key != null && key.canHaveChildren();
60      }
61  
62      /* (non-Javadoc)
63       * @see javax.swing.tree.TreeNode#isLeaf()
64       */
65      public boolean isLeaf() {
66          return key == null || key.isEmpty();
67      }
68  
69      /* (non-Javadoc)
70       * @see javax.swing.tree.TreeNode#children()
71       */
72      public Enumeration<Key> children() {
73          if (key != null) {
74              return new IteratorEnumeration<Key>(key.iterator());
75          }
76          return new IteratorEnumeration<Key>(new EmptyIterator<Key>());
77      }
78  
79      /* (non-Javadoc)
80       * @see javax.swing.tree.TreeNode#getParent()
81       */
82      public TreeNode getParent() {
83          return parent;
84      }
85  
86      /* (non-Javadoc)
87       * @see javax.swing.tree.TreeNode#getChildAt(int)
88       */
89      public TreeNode getChildAt(int index) {
90          Key child = key.get(index);
91          return new KeyTreeNode(child, this);
92      }
93  
94      /* (non-Javadoc)
95       * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
96       */
97      public int getIndex(TreeNode node) {
98          if (key != null && node instanceof KeyTreeNode) {
99              KeyTreeNode keynode = (KeyTreeNode) node;
100             Key that = keynode.getKey();
101 
102             return key.indexOf(that);
103         }
104         return -1;
105     }
106 
107     /**
108      * Accessor for the key
109      */
110     public Key getKey() {
111         return key;
112     }
113 
114     private Key key;
115     private TreeNode parent;
116 }
117