View Javadoc

1   package de.matthias_burbach.mosaique.swing;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.swing.tree.DefaultMutableTreeNode;
7   
8   /***
9    * Is a base class for tree nodes which need to initialize child nodes
10   * explicitly and lazily.
11   *
12   * @author Matthias Burbach
13   */
14  public abstract class BaseMutableTreeNode extends DefaultMutableTreeNode {
15      /***
16       * Whether children have already been initialized.
17       */
18      private boolean childrenAreInitialized = false;
19  
20      /***
21       * Initializes the children nodes of this node.
22       */
23      protected abstract void initChildren();
24  
25      /*(non-Javadoc)
26       * @see javax.swing.tree.TreeNode#getChildCount()
27       */
28      /***
29       * {@inheritDoc}
30       */
31      public int getChildCount() {
32          if (!childrenAreInitialized) {
33              childrenAreInitialized = true;
34              initChildren();
35          }
36          return super.getChildCount();
37      }
38  
39      /*(non-Javadoc)
40       * @see javax.swing.tree.TreeNode#isLeaf()
41       */
42      /***
43       * {@inheritDoc}
44       */
45      public boolean isLeaf() {
46          return false;
47      }
48  
49      /***
50       * Introduced to support usage of this class in the Eclipse context.
51       * @return The child notes of this node.
52       */
53      public Object[] getChildren() {
54          int childCount = getChildCount();
55          List result = new ArrayList();
56          for (int i = 0; i < childCount; i++) {
57              Object child = getChildAt(i);
58              result.add(child);
59          }
60          return result.toArray();
61      }
62  
63      /***
64       * @return Signals that the node has discovered an error related to this
65       *         node.
66       *         Can be used to display the error state, for example.
67       *         The default returned is <code>false</code>. Can be overridden.
68       */
69      public boolean isErroneous() {
70          return false;
71      }
72  
73      /***
74       * @return Whether children have already been initialized.
75       */
76      protected boolean childrenAreInitialized() {
77          return childrenAreInitialized;
78      }
79  
80      /***
81       * Initializes this object if it hasn't been yet.
82       */
83      protected void assertChildrenAreInitialized() {
84          if (!childrenAreInitialized) {
85              childrenAreInitialized = true;
86              initChildren();
87          }
88      }
89  }